forked from data-apis/array-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfft.py
701 lines (533 loc) · 34.2 KB
/
fft.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
__all__ = [
"fft",
"ifft",
"fftn",
"ifftn",
"rfft",
"irfft",
"rfftn",
"irfftn",
"hfft",
"ihfft",
"fftfreq",
"rfftfreq",
"fftshift",
"ifftshift",
]
from ._types import Tuple, Union, Sequence, array, Optional, Literal, dtype, device
def fft(
x: array,
/,
*,
n: Optional[int] = None,
axis: int = -1,
norm: Literal["backward", "ortho", "forward"] = "backward",
) -> array:
"""
Computes the one-dimensional discrete Fourier transform.
.. note::
Applying the one-dimensional inverse discrete Fourier transform to the output of this function must return the original (i.e., non-transformed) input array within numerical accuracy (i.e., ``ifft(fft(x)) == x``), provided that the transform and inverse transform are performed with the same arguments (number of elements, axis, and normalization mode).
Parameters
----------
x: array
input array. Should have a complex floating-point data type.
n: Optional[int]
number of elements over which to compute the transform along the axis (dimension) specified by ``axis``. Let ``M`` be the size of the input array along the axis specified by ``axis``. When ``n`` is ``None``, the function must set ``n`` equal to ``M``.
- If ``n`` is greater than ``M``, the axis specified by ``axis`` must be zero-padded to size ``n``.
- If ``n`` is less than ``M``, the axis specified by ``axis`` must be trimmed to size ``n``.
- If ``n`` equals ``M``, all elements along the axis specified by ``axis`` must be used when computing the transform.
Default: ``None``.
axis: int
axis (dimension) of the input array over which to compute the transform. A valid ``axis`` must be an integer on the interval ``[-N, N)``, where ``N`` is the rank (number of dimensions) of ``x``. If an ``axis`` is specified as a negative integer, the function must determine the axis along which to compute the transform by counting backward from the last dimension (where ``-1`` refers to the last dimension). Default: ``-1``.
norm: Literal['backward', 'ortho', 'forward']
normalization mode. Should be one of the following modes:
- ``'backward'``: no normalization.
- ``'ortho'``: normalize by ``1/sqrt(n)`` (i.e., make the FFT orthonormal).
- ``'forward'``: normalize by ``1/n``.
Default: ``'backward'``.
Returns
-------
out: array
an array transformed along the axis (dimension) specified by ``axis``. The returned array must have the same data type as ``x`` and must have the same shape as ``x``, except for the axis specified by ``axis`` which must have size ``n``.
Notes
-----
.. versionadded:: 2022.12
.. versionchanged:: 2023.12
Required the input array have a complex floating-point data type and required that the output array have the same data type as the input array.
"""
def ifft(
x: array,
/,
*,
n: Optional[int] = None,
axis: int = -1,
norm: Literal["backward", "ortho", "forward"] = "backward",
) -> array:
"""
Computes the one-dimensional inverse discrete Fourier transform.
.. note::
Applying the one-dimensional inverse discrete Fourier transform to the output of this function must return the original (i.e., non-transformed) input array within numerical accuracy (i.e., ``ifft(fft(x)) == x``), provided that the transform and inverse transform are performed with the same arguments (number of elements, axis, and normalization mode).
Parameters
----------
x: array
input array. Should have a complex floating-point data type.
n: Optional[int]
number of elements over which to compute the transform along the axis (dimension) specified by ``axis``. Let ``M`` be the size of the input array along the axis specified by ``axis``. When ``n`` is ``None``, the function must set ``n`` equal to ``M``.
- If ``n`` is greater than ``M``, the axis specified by ``axis`` must be zero-padded to size ``n``.
- If ``n`` is less than ``M``, the axis specified by ``axis`` must be trimmed to size ``n``.
- If ``n`` equals ``M``, all elements along the axis specified by ``axis`` must be used when computing the transform.
Default: ``None``.
axis: int
axis (dimension) of the input array over which to compute the transform. A valid ``axis`` must be an integer on the interval ``[-N, N)``, where ``N`` is the rank (number of dimensions) of ``x``. If an ``axis`` is specified as a negative integer, the function must determine the axis along which to compute the transform by counting backward from the last dimension (where ``-1`` refers to the last dimension). Default: ``-1``.
norm: Literal['backward', 'ortho', 'forward']
normalization mode. Should be one of the following modes:
- ``'backward'``: normalize by ``1/n``.
- ``'ortho'``: normalize by ``1/sqrt(n)`` (i.e., make the FFT orthonormal).
- ``'forward'``: no normalization.
Default: ``'backward'``.
Returns
-------
out: array
an array transformed along the axis (dimension) specified by ``axis``. The returned array must have the same data type as ``x`` and must have the same shape as ``x``, except for the axis specified by ``axis`` which must have size ``n``.
Notes
-----
.. versionadded:: 2022.12
.. versionchanged:: 2023.12
Required the input array have a complex floating-point data type and required that the output array have the same data type as the input array.
"""
def fftn(
x: array,
/,
*,
s: Optional[Sequence[int]] = None,
axes: Optional[Sequence[int]] = None,
norm: Literal["backward", "ortho", "forward"] = "backward",
) -> array:
"""
Computes the n-dimensional discrete Fourier transform.
.. note::
Applying the n-dimensional inverse discrete Fourier transform to the output of this function must return the original (i.e., non-transformed) input array within numerical accuracy (i.e., ``ifftn(fftn(x)) == x``), provided that the transform and inverse transform are performed with the same arguments (sizes, axes, and normalization mode).
Parameters
----------
x: array
input array. Should have a complex floating-point data type.
s: Optional[Sequence[int]]
number of elements over which to compute the transform along the axes (dimensions) specified by ``axes``. Let ``i`` be the index of the ``n``-th axis specified by ``axes`` (i.e., ``i = axes[n]``) and ``M[i]`` be the size of the input array along axis ``i``. When ``s`` is ``None``, the function must set ``s`` equal to a sequence of integers such that ``s[i]`` equals ``M[i]`` for all ``i``.
- If ``s[i]`` is greater than ``M[i]``, axis ``i`` must be zero-padded to size ``s[i]``.
- If ``s[i]`` is less than ``M[i]``, axis ``i`` must be trimmed to size ``s[i]``.
- If ``s[i]`` equals ``M[i]`` or ``-1``, all elements along axis ``i`` must be used when computing the transform.
If ``s`` is not ``None``, ``axes`` must not be ``None``. Default: ``None``.
axes: Optional[Sequence[int]]
axes (dimensions) over which to compute the transform. A valid axis in ``axes`` must be an integer on the interval ``[-N, N)``, where ``N`` is the rank (number of dimensions) of ``x``. If an axis is specified as a negative integer, the function must determine the axis along which to compute the transform by counting backward from the last dimension (where ``-1`` refers to the last dimension).
If ``s`` is provided, the corresponding ``axes`` to be transformed must also be provided. If ``axes`` is ``None``, the function must compute the transform over all axes. Default: ``None``.
If ``axes`` contains two or more entries which resolve to the same axis (i.e., resolved axes are not unique), the behavior is unspecified and thus implementation-defined.
norm: Literal['backward', 'ortho', 'forward']
normalization mode. Should be one of the following modes:
- ``'backward'``: no normalization.
- ``'ortho'``: normalize by ``1/sqrt(n)`` (i.e., make the FFT orthonormal).
- ``'forward'``: normalize by ``1/n``.
where ``n = prod(s)`` is the logical FFT size.
Default: ``'backward'``.
Returns
-------
out: array
an array transformed along the axes (dimensions) specified by ``axes``. The returned array must have the same data type as ``x`` and must have the same shape as ``x``, except for the axes specified by ``axes`` which must have size ``s[i]``.
Notes
-----
.. versionadded:: 2022.12
.. versionchanged:: 2023.12
Required the input array have a complex floating-point data type and required that the output array have the same data type as the input array.
"""
def ifftn(
x: array,
/,
*,
s: Optional[Sequence[int]] = None,
axes: Optional[Sequence[int]] = None,
norm: Literal["backward", "ortho", "forward"] = "backward",
) -> array:
"""
Computes the n-dimensional inverse discrete Fourier transform.
.. note::
Applying the n-dimensional inverse discrete Fourier transform to the output of this function must return the original (i.e., non-transformed) input array within numerical accuracy (i.e., ``ifftn(fftn(x)) == x``), provided that the transform and inverse transform are performed with the same arguments (sizes, axes, and normalization mode).
Parameters
----------
x: array
input array. Should have a complex floating-point data type.
s: Optional[Sequence[int]]
number of elements over which to compute the transform along the axes (dimensions) specified by ``axes``. Let ``i`` be the index of the ``n``-th axis specified by ``axes`` (i.e., ``i = axes[n]``) and ``M[i]`` be the size of the input array along axis ``i``. When ``s`` is ``None``, the function must set ``s`` equal to a sequence of integers such that ``s[i]`` equals ``M[i]`` for all ``i``.
- If ``s[i]`` is greater than ``M[i]``, axis ``i`` must be zero-padded to size ``s[i]``.
- If ``s[i]`` is less than ``M[i]``, axis ``i`` must be trimmed to size ``s[i]``.
- If ``s[i]`` equals ``M[i]`` or ``-1``, all elements along axis ``i`` must be used when computing the transform.
If ``s`` is not ``None``, ``axes`` must not be ``None``. Default: ``None``.
axes: Optional[Sequence[int]]
axes (dimensions) over which to compute the transform. A valid axis in ``axes`` must be an integer on the interval ``[-N, N)``, where ``N`` is the rank (number of dimensions) of ``x``. If an axis is specified as a negative integer, the function must determine the axis along which to compute the transform by counting backward from the last dimension (where ``-1`` refers to the last dimension).
If ``s`` is provided, the corresponding ``axes`` to be transformed must also be provided. If ``axes`` is ``None``, the function must compute the transform over all axes. Default: ``None``.
If ``axes`` contains two or more entries which resolve to the same axis (i.e., resolved axes are not unique), the behavior is unspecified and thus implementation-defined.
norm: Literal['backward', 'ortho', 'forward']
specify the normalization mode. Should be one of the following modes:
- ``'backward'``: normalize by ``1/n``.
- ``'ortho'``: normalize by ``1/sqrt(n)`` (i.e., make the FFT orthonormal).
- ``'forward'``: no normalization.
where ``n = prod(s)`` is the logical FFT size.
Default: ``'backward'``.
Returns
-------
out: array
an array transformed along the axes (dimensions) specified by ``axes``. The returned array must have the same data type as ``x`` and must have the same shape as ``x``, except for the axes specified by ``axes`` which must have size ``s[i]``.
Notes
-----
.. versionadded:: 2022.12
.. versionchanged:: 2023.12
Required the input array have a complex floating-point data type and required that the output array have the same data type as the input array.
"""
def rfft(
x: array,
/,
*,
n: Optional[int] = None,
axis: int = -1,
norm: Literal["backward", "ortho", "forward"] = "backward",
) -> array:
"""
Computes the one-dimensional discrete Fourier transform for real-valued input.
.. note::
Applying the one-dimensional inverse discrete Fourier transform for real-valued input to the output of this function must return the original (i.e., non-transformed) input array within numerical accuracy (i.e., ``irfft(rfft(x)) == x``), provided that the transform and inverse transform are performed with the same arguments (axis and normalization mode) and consistent values for the number of elements over which to compute the transforms.
Parameters
----------
x: array
input array. Must have a real-valued floating-point data type.
n: Optional[int]
number of elements over which to compute the transform along the axis (dimension) specified by ``axis``. Let ``M`` be the size of the input array along the axis specified by ``axis``. When ``n`` is ``None``, the function must set ``n`` equal to ``M``.
- If ``n`` is greater than ``M``, the axis specified by ``axis`` must be zero-padded to size ``n``.
- If ``n`` is less than ``M``, the axis specified by ``axis`` must be trimmed to size ``n``.
- If ``n`` equals ``M``, all elements along the axis specified by ``axis`` must be used when computing the transform.
Default: ``None``.
axis: int
axis (dimension) of the input array over which to compute the transform. A valid ``axis`` must be an integer on the interval ``[-N, N)``, where ``N`` is the rank (number of dimensions) of ``x``. If an ``axis`` is specified as a negative integer, the function must determine the axis along which to compute the transform by counting backward from the last dimension (where ``-1`` refers to the last dimension). Default: ``-1``.
norm: Literal['backward', 'ortho', 'forward']
normalization mode. Should be one of the following modes:
- ``'backward'``: no normalization.
- ``'ortho'``: normalize by ``1/sqrt(n)`` (i.e., make the FFT orthonormal).
- ``'forward'``: normalize by ``1/n``.
Default: ``'backward'``.
Returns
-------
out: array
an array transformed along the axis (dimension) specified by ``axis``. The returned array must have a complex floating-point data type whose precision matches the precision of ``x`` (e.g., if ``x`` is ``float64``, then the returned array must have a ``complex128`` data type). The returned array must have the same shape as ``x``, except for the axis specified by ``axis`` which must have size ``n//2 + 1``.
Notes
-----
.. versionadded:: 2022.12
"""
def irfft(
x: array,
/,
*,
n: Optional[int] = None,
axis: int = -1,
norm: Literal["backward", "ortho", "forward"] = "backward",
) -> array:
"""
Computes the one-dimensional inverse of ``rfft`` for complex-valued input.
.. note::
Applying the one-dimensional inverse discrete Fourier transform for real-valued input to the output of this function must return the original (i.e., non-transformed) input array within numerical accuracy (i.e., ``irfft(rfft(x)) == x``), provided that the transform and inverse transform are performed with the same arguments (axis and normalization mode) and consistent values for the number of elements over which to compute the transforms.
Parameters
----------
x: array
input array. Should have a complex floating-point data type.
n: Optional[int]
number of elements along the transformed axis (dimension) specified by ``axis`` in the **output array**. Let ``M`` be the size of the input array along the axis specified by ``axis``. When ``n`` is ``None``, the function must set ``n`` equal to ``2*(M-1)``.
- If ``n//2+1`` is greater than ``M``, the axis of the input array specified by ``axis`` must be zero-padded to size ``n//2+1``.
- If ``n//2+1`` is less than ``M``, the axis of the input array specified by ``axis`` must be trimmed to size ``n//2+1``.
- If ``n//2+1`` equals ``M``, all elements along the axis of the input array specified by ``axis`` must be used when computing the transform.
Default: ``None``.
axis: int
axis (dimension) of the input array over which to compute the transform. A valid ``axis`` must be an integer on the interval ``[-N, N)``, where ``N`` is the rank (number of dimensions) of ``x``. If an ``axis`` is specified as a negative integer, the function must determine the axis along which to compute the transform by counting backward from the last dimension (where ``-1`` refers to the last dimension). Default: ``-1``.
norm: Literal['backward', 'ortho', 'forward']
normalization mode. Should be one of the following modes:
- ``'backward'``: normalize by ``1/n``.
- ``'ortho'``: normalize by ``1/sqrt(n)`` (i.e., make the FFT orthonormal).
- ``'forward'``: no normalization.
Default: ``'backward'``.
Returns
-------
out: array
an array transformed along the axis (dimension) specified by ``axis``. The returned array must have a real-valued floating-point data type whose precision matches the precision of ``x`` (e.g., if ``x`` is ``complex128``, then the returned array must have a ``float64`` data type). The returned array must have the same shape as ``x``, except for the axis specified by ``axis`` which must have size ``n``.
Notes
-----
- In order to return an array having an odd number of elements along the transformed axis, the function must be provided an odd integer for ``n``.
.. versionadded:: 2022.12
.. versionchanged:: 2023.12
Required the output array have a real-valued floating-point data type having the same precision as the input array.
"""
def rfftn(
x: array,
/,
*,
s: Optional[Sequence[int]] = None,
axes: Optional[Sequence[int]] = None,
norm: Literal["backward", "ortho", "forward"] = "backward",
) -> array:
"""
Computes the n-dimensional discrete Fourier transform for real-valued input.
.. note::
Applying the n-dimensional inverse discrete Fourier transform for real-valued input to the output of this function must return the original (i.e., non-transformed) input array within numerical accuracy (i.e., ``irfftn(rfftn(x)) == x``), provided that the transform and inverse transform are performed with the same arguments (axes and normalization mode) and consistent sizes.
Parameters
----------
x: array
input array. Must have a real-valued floating-point data type.
s: Optional[Sequence[int]]
number of elements over which to compute the transform along axes (dimensions) specified by ``axes``. Let ``i`` be the index of the ``n``-th axis specified by ``axes`` (i.e., ``i = axes[n]``) and ``M[i]`` be the size of the input array along axis ``i``. When ``s`` is ``None``, the function must set ``s`` equal to a sequence of integers such that ``s[i]`` equals ``M[i]`` for all ``i``.
- If ``s[i]`` is greater than ``M[i]``, axis ``i`` must be zero-padded to size ``s[i]``.
- If ``s[i]`` is less than ``M[i]``, axis ``i`` must be trimmed to size ``s[i]``.
- If ``s[i]`` equals ``M[i]`` or ``-1``, all elements along axis ``i`` must be used when computing the transform.
If ``s`` is not ``None``, ``axes`` must not be ``None``. Default: ``None``.
axes: Optional[Sequence[int]]
axes (dimensions) over which to compute the transform. A valid axis in ``axes`` must be an integer on the interval ``[-N, N)``, where ``N`` is the rank (number of dimensions) of ``x``. If an axis is specified as a negative integer, the function must determine the axis along which to compute the transform by counting backward from the last dimension (where ``-1`` refers to the last dimension).
If ``s`` is provided, the corresponding ``axes`` to be transformed must also be provided. If ``axes`` is ``None``, the function must compute the transform over all axes. Default: ``None``.
If ``axes`` contains two or more entries which resolve to the same axis (i.e., resolved axes are not unique), the behavior is unspecified and thus implementation-defined.
norm: Literal['backward', 'ortho', 'forward']
normalization mode. Should be one of the following modes:
- ``'backward'``: no normalization.
- ``'ortho'``: normalize by ``1/sqrt(n)`` (i.e., make the FFT orthonormal).
- ``'forward'``: normalize by ``1/n``.
where ``n = prod(s)``, the logical FFT size.
Default: ``'backward'``.
Returns
-------
out: array
an array transformed along the axes (dimension) specified by ``axes``. The returned array must have a complex floating-point data type whose precision matches the precision of ``x`` (e.g., if ``x`` is ``float64``, then the returned array must have a ``complex128`` data type). The returned array must have the same shape as ``x``, except for the last transformed axis which must have size ``s[-1]//2 + 1`` and the remaining transformed axes which must have size ``s[i]``.
Notes
-----
.. versionadded:: 2022.12
"""
def irfftn(
x: array,
/,
*,
s: Optional[Sequence[int]] = None,
axes: Optional[Sequence[int]] = None,
norm: Literal["backward", "ortho", "forward"] = "backward",
) -> array:
"""
Computes the n-dimensional inverse of ``rfftn`` for complex-valued input.
.. note::
Applying the n-dimensional inverse discrete Fourier transform for real-valued input to the output of this function must return the original (i.e., non-transformed) input array within numerical accuracy (i.e., ``irfftn(rfftn(x)) == x``), provided that the transform and inverse transform are performed with the same arguments (axes and normalization mode) and consistent sizes.
Parameters
----------
x: array
input array. Should have a complex floating-point data type.
s: Optional[Sequence[int]]
number of elements along the transformed axes (dimensions) specified by ``axes`` in the **output array**. Let ``i`` be the index of the ``n``-th axis specified by ``axes`` (i.e., ``i = axes[n]``) and ``M[i]`` be the size of the input array along axis ``i``. When ``s`` is ``None``, the function must set ``s`` equal to a sequence of integers such that ``s[i]`` equals ``M[i]`` for all ``i``, except for the last transformed axis in which ``s[i]`` equals ``2*(M[i]-1)``. For each ``i``, let ``n`` equal ``s[i]``, except for the last transformed axis in which ``n`` equals ``s[i]//2+1``.
- If ``n`` is greater than ``M[i]``, axis ``i`` of the input array must be zero-padded to size ``n``.
- If ``n`` is less than ``M[i]``, axis ``i`` of the input array must be trimmed to size ``n``.
- If ``n`` equals ``M[i]`` or ``-1``, all elements along axis ``i`` of the input array must be used when computing the transform.
If ``s`` is not ``None``, ``axes`` must not be ``None``. Default: ``None``.
axes: Optional[Sequence[int]]
axes (dimensions) over which to compute the transform. A valid axis in ``axes`` must be an integer on the interval ``[-N, N)``, where ``N`` is the rank (number of dimensions) of ``x``. If an axis is specified as a negative integer, the function must determine the axis along which to compute the transform by counting backward from the last dimension (where ``-1`` refers to the last dimension).
If ``s`` is provided, the corresponding ``axes`` to be transformed must also be provided. If ``axes`` is ``None``, the function must compute the transform over all axes. Default: ``None``.
If ``axes`` contains two or more entries which resolve to the same axis (i.e., resolved axes are not unique), the behavior is unspecified and thus implementation-defined.
norm: Literal['backward', 'ortho', 'forward']
normalization mode. Should be one of the following modes:
- ``'backward'``: normalize by ``1/n``.
- ``'ortho'``: normalize by ``1/sqrt(n)`` (i.e., make the FFT orthonormal).
- ``'forward'``: no normalization.
where ``n = prod(s)`` is the logical FFT size.
Default: ``'backward'``.
Returns
-------
out: array
an array transformed along the axes (dimension) specified by ``axes``. The returned array must have a real-valued floating-point data type whose precision matches the precision of ``x`` (e.g., if ``x`` is ``complex128``, then the returned array must have a ``float64`` data type). The returned array must have the same shape as ``x``, except for the transformed axes which must have size ``s[i]``.
Notes
-----
- In order to return an array having an odd number of elements along the last transformed axis, the function must be provided an odd integer for ``s[-1]``.
.. versionadded:: 2022.12
.. versionchanged:: 2023.12
Required the output array have a real-valued floating-point data type having the same precision as the input array.
"""
def hfft(
x: array,
/,
*,
n: Optional[int] = None,
axis: int = -1,
norm: Literal["backward", "ortho", "forward"] = "backward",
) -> array:
"""
Computes the one-dimensional discrete Fourier transform of a signal with Hermitian symmetry.
Parameters
----------
x: array
input array. Should have a complex floating-point data type.
n: Optional[int]
number of elements along the transformed axis (dimension) specified by ``axis`` in the **output array**. Let ``M`` be the size of the input array along the axis specified by ``axis``. When ``n`` is ``None``, the function must set ``n`` equal to ``2*(M-1)``.
- If ``n//2+1`` is greater than ``M``, the axis of the input array specified by ``axis`` must be zero-padded to length ``n//2+1``.
- If ``n//2+1`` is less than ``M``, the axis of the input array specified by ``axis`` must be trimmed to size ``n//2+1``.
- If ``n//2+1`` equals ``M``, all elements along the axis of the input array specified by ``axis`` must be used when computing the transform.
Default: ``None``.
axis: int
axis (dimension) of the input array over which to compute the transform. A valid ``axis`` must be an integer on the interval ``[-N, N)``, where ``N`` is the rank (number of dimensions) of ``x``. If an ``axis`` is specified as a negative integer, the function must determine the axis along which to compute the transform by counting backward from the last dimension (where ``-1`` refers to the last dimension). Default: ``-1``.
norm: Literal['backward', 'ortho', 'forward']
normalization mode. Should be one of the following modes:
- ``'backward'``: no normalization.
- ``'ortho'``: normalize by ``1/sqrt(n)`` (i.e., make the FFT orthonormal).
- ``'forward'``: normalize by ``1/n``.
Default: ``'backward'``.
Returns
-------
out: array
an array transformed along the axis (dimension) specified by ``axis``. The returned array must have a real-valued floating-point data type whose precision matches the precision of ``x`` (e.g., if ``x`` is ``complex128``, then the returned array must have a ``float64`` data type). The returned array must have the same shape as ``x``, except for the axis specified by ``axis`` which must have size ``n``.
Notes
-----
.. versionadded:: 2022.12
.. versionchanged:: 2023.12
Required the input array to have a complex floating-point data type and required that the output array have a real-valued data type having the same precision as the input array.
"""
def ihfft(
x: array,
/,
*,
n: Optional[int] = None,
axis: int = -1,
norm: Literal["backward", "ortho", "forward"] = "backward",
) -> array:
"""
Computes the one-dimensional inverse discrete Fourier transform of a signal with Hermitian symmetry.
Parameters
----------
x: array
input array. Must have a real-valued floating-point data type.
n: Optional[int]
number of elements over which to compute the transform along the axis (dimension) specified by ``axis``. Let ``M`` be the size of the input array along the axis specified by ``axis``. When ``n`` is ``None``, the function must set ``n`` equal to ``M``.
- If ``n`` is greater than ``M``, the axis specified by ``axis`` must be zero-padded to size ``n``.
- If ``n`` is less than ``M``, the axis specified by ``axis`` must be trimmed to size ``n``.
- If ``n`` equals ``M``, all elements along the axis specified by ``axis`` must be used when computing the transform.
Default: ``None``.
axis: int
axis (dimension) of the input array over which to compute the transform. A valid ``axis`` must be an integer on the interval ``[-N, N)``, where ``N`` is the rank (number of dimensions) of ``x``. If an ``axis`` is specified as a negative integer, the function must determine the axis along which to compute the transform by counting backward from the last dimension (where ``-1`` refers to the last dimension). Default: ``-1``.
norm: Literal['backward', 'ortho', 'forward']
normalization mode. Should be one of the following modes:
- ``'backward'``: normalize by ``1/n``.
- ``'ortho'``: normalize by ``1/sqrt(n)`` (i.e., make the FFT orthonormal).
- ``'forward'``: no normalization.
Default: ``'backward'``.
Returns
-------
out: array
an array transformed along the axis (dimension) specified by ``axis``. The returned array must have a complex floating-point data type whose precision matches the precision of ``x`` (e.g., if ``x`` is ``float64``, then the returned array must have a ``complex128`` data type). The returned array must have the same shape as ``x``, except for the axis specified by ``axis`` which must have size ``n//2 + 1``.
Notes
-----
.. versionadded:: 2022.12
"""
def fftfreq(
n: int,
/,
*,
d: float = 1.0,
dtype: Optional[dtype] = None,
device: Optional[device] = None,
) -> array:
"""
Computes the discrete Fourier transform sample frequencies.
For a Fourier transform of length ``n`` and length unit of ``d``, the frequencies are described as:
.. code-block::
f = [0, 1, ..., n/2-1, -n/2, ..., -1] / (d*n) # if n is even
f = [0, 1, ..., (n-1)/2, -(n-1)/2, ..., -1] / (d*n) # if n is odd
Parameters
----------
n: int
window length.
d: float
sample spacing between individual samples of the Fourier transform input. Default: ``1.0``.
dtype: Optional[dtype]
output array data type. Must be a real-valued floating-point data type. If ``dtype`` is ``None``, the output array data type must be the default real-valued floating-point data type. Default: ``None``.
device: Optional[device]
device on which to place the created array. Default: ``None``.
Returns
-------
out: array
an array of shape ``(n,)`` containing the sample frequencies.
Notes
-----
.. versionadded:: 2022.12
.. versionchanged:: 2023.12
Required the output array have the default real-valued floating-point data type.
"""
def rfftfreq(
n: int,
/,
*,
d: float = 1.0,
dtype: Optional[dtype] = None,
device: Optional[device] = None,
) -> array:
"""
Computes the discrete Fourier transform sample frequencies (for ``rfft`` and ``irfft``).
For a Fourier transform of length ``n`` and length unit of ``d``, the frequencies are described as:
.. code-block::
f = [0, 1, ..., n/2-1, n/2] / (d*n) # if n is even
f = [0, 1, ..., (n-1)/2-1, (n-1)/2] / (d*n) # if n is odd
The Nyquist frequency component is considered to be positive.
Parameters
----------
n: int
window length.
d: float
sample spacing between individual samples of the Fourier transform input. Default: ``1.0``.
dtype: Optional[dtype]
output array data type. Must be a real-valued floating-point data type. If ``dtype`` is ``None``, the output array data type must be the default real-valued floating-point data type. Default: ``None``.
device: Optional[device]
device on which to place the created array. Default: ``None``.
Returns
-------
out: array
an array of shape ``(n//2+1,)`` containing the sample frequencies.
Notes
-----
.. versionadded:: 2022.12
.. versionchanged:: 2023.12
Required the output array have the default real-valued floating-point data type.
"""
def fftshift(x: array, /, *, axes: Optional[Union[int, Sequence[int]]] = None) -> array:
"""
Shifts the zero-frequency component to the center of the spectrum.
This function swaps half-spaces for all axes (dimensions) specified by ``axes``.
.. note::
``out[0]`` is the Nyquist component only if the length of the input is even.
Parameters
----------
x: array
input array. Should have a floating-point data type.
axes: Optional[Union[int, Sequence[int]]]
axes over which to shift. If ``None``, the function must shift all axes. Default: ``None``.
If ``axes`` contains two or more entries which resolve to the same axis (i.e., resolved axes are not unique), the behavior is unspecified and thus implementation-defined.
Returns
-------
out: array
the shifted array. The returned array must have the same data type and shape as ``x``.
Notes
-----
.. versionadded:: 2022.12
"""
def ifftshift(
x: array, /, *, axes: Optional[Union[int, Sequence[int]]] = None
) -> array:
"""
Inverse of ``fftshift``.
.. note::
Although identical for even-length ``x``, ``fftshift`` and ``ifftshift`` differ by one sample for odd-length ``x``.
Parameters
----------
x: array
input array. Should have a floating-point data type.
axes: Optional[Union[int, Sequence[int]]]
axes over which to perform the inverse shift. If ``None``, the function must shift all axes. Default: ``None``.
If ``axes`` contains two or more entries which resolve to the same axis (i.e., resolved axes are not unique), the behavior is unspecified and thus implementation-defined.
Returns
-------
out: array
the shifted array. The returned array must have the same data type and shape as ``x``.
Notes
-----
.. versionadded:: 2022.12
"""