Skip to content

Commit 720a250

Browse files
authored
FFT. add wrappers for shift, freq and other (#360)
1 parent 3faed19 commit 720a250

File tree

2 files changed

+178
-52
lines changed

2 files changed

+178
-52
lines changed

dpnp/fft/dpnp_iface_fft.py

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,21 @@
5151
__all__ = [
5252
"fft",
5353
"fft2",
54+
"fftfreq",
5455
"fftn",
56+
"fftshift",
57+
"hfft",
5558
"ifft",
5659
"ifft2",
5760
"ifftn",
61+
"ifftshift",
62+
"ihfft",
5863
"irfft",
5964
"irfft2",
6065
"irfftn",
6166
"rfft",
6267
"rfft2",
68+
"rfftfreq",
6369
"rfftn"
6470
]
6571

@@ -132,6 +138,21 @@ def fft2(x1, s=None, axes=(-2, -1), norm=None):
132138
return call_origin(numpy.fft.fft2, x1, s, axes, norm)
133139

134140

141+
def fftfreq(n=None, d=1.0):
142+
"""
143+
Compute the one-dimensional discrete Fourier Transform sample frequencies.
144+
145+
Limitations
146+
-----------
147+
Parameter ``d`` is unsupported.
148+
149+
For full documentation refer to :obj:`numpy.fft.fftfreq`.
150+
151+
"""
152+
153+
return call_origin(numpy.fft.fftfreq, n, d)
154+
155+
135156
def fftn(x1, s=None, axes=None, norm=None):
136157
"""
137158
Compute the N-dimensional FFT.
@@ -181,6 +202,77 @@ def fftn(x1, s=None, axes=None, norm=None):
181202
return call_origin(numpy.fft.fftn, x1, s, axes, norm)
182203

183204

205+
def fftshift(x1, axes=None):
206+
"""
207+
Shift the zero-frequency component to the center of the spectrum.
208+
209+
Limitations
210+
-----------
211+
Parameter ``axes`` is unsupported.
212+
Parameter ``x1`` supports ``dpnp.int32``, ``dpnp.int64``, ``dpnp.float32``, ``dpnp.float64`` and
213+
``dpnp.complex128`` datatypes only.
214+
215+
For full documentation refer to :obj:`numpy.fft.fftshift`.
216+
217+
"""
218+
219+
is_x1_dparray = isinstance(x1, dparray)
220+
221+
if (not use_origin_backend(x1) and is_x1_dparray and 0):
222+
if axis is None:
223+
axis_param = -1 # the most right dimension (default value)
224+
else:
225+
axis_param = axes
226+
227+
if x1.size < 1:
228+
pass # let fallback to handle exception
229+
else:
230+
return dpnp_fft(x1, input_boundarie, output_boundarie, axis_param, False)
231+
232+
return call_origin(numpy.fft.fftshift, x1, axes)
233+
234+
235+
def hfft(x1, n=None, axis=-1, norm=None):
236+
"""
237+
Compute the one-dimensional discrete Fourier Transform of a signal that has Hermitian symmetry.
238+
239+
Limitations
240+
-----------
241+
Parameter ``norm`` is unsupported.
242+
Parameter ``x1`` supports ``dpnp.int32``, ``dpnp.int64``, ``dpnp.float32``, ``dpnp.float64`` and
243+
``dpnp.complex128`` datatypes only.
244+
245+
For full documentation refer to :obj:`numpy.fft.hfft`.
246+
247+
"""
248+
249+
is_x1_dparray = isinstance(x1, dparray)
250+
251+
if (not use_origin_backend(x1) and is_x1_dparray and 0):
252+
if axis is None:
253+
axis_param = -1 # the most right dimension (default value)
254+
else:
255+
axis_param = axis
256+
257+
if n is None:
258+
input_boundarie = x1.shape[axis_param]
259+
else:
260+
input_boundarie = n
261+
262+
if x1.size < 1:
263+
pass # let fallback to handle exception
264+
elif input_boundarie < 1:
265+
pass # let fallback to handle exception
266+
elif norm is not None:
267+
pass
268+
else:
269+
output_boundarie = input_boundarie
270+
271+
return dpnp_fft(x1, input_boundarie, output_boundarie, axis_param, False)
272+
273+
return call_origin(numpy.fft.hfft, x1, n, axis, norm)
274+
275+
184276
def ifft(x1, n=None, axis=-1, norm=None):
185277
"""
186278
Compute the one-dimensional inverse discrete Fourier Transform.
@@ -249,6 +341,36 @@ def ifft2(x1, s=None, axes=(-2, -1), norm=None):
249341
return call_origin(numpy.fft.ifft2, x1, s, axes, norm)
250342

251343

344+
def ifftshift(x1, axes=None):
345+
"""
346+
Inverse shift the zero-frequency component to the center of the spectrum.
347+
348+
Limitations
349+
-----------
350+
Parameter ``axes`` is unsupported.
351+
Parameter ``x1`` supports ``dpnp.int32``, ``dpnp.int64``, ``dpnp.float32``, ``dpnp.float64`` and
352+
``dpnp.complex128`` datatypes only.
353+
354+
For full documentation refer to :obj:`numpy.fft.ifftshift`.
355+
356+
"""
357+
358+
is_x1_dparray = isinstance(x1, dparray)
359+
360+
if (not use_origin_backend(x1) and is_x1_dparray and 0):
361+
if axis is None:
362+
axis_param = -1 # the most right dimension (default value)
363+
else:
364+
axis_param = axes
365+
366+
if x1.size < 1:
367+
pass # let fallback to handle exception
368+
else:
369+
return dpnp_fft(x1, input_boundarie, output_boundarie, axis_param, False)
370+
371+
return call_origin(numpy.fft.ifftshift, x1, axes)
372+
373+
252374
def ifftn(x1, s=None, axes=None, norm=None):
253375
"""
254376
Compute the N-dimensional inverse discrete Fourier Transform.
@@ -298,6 +420,47 @@ def ifftn(x1, s=None, axes=None, norm=None):
298420
return call_origin(numpy.fft.ifftn, x1, s, axes, norm)
299421

300422

423+
def ihfft(x1, n=None, axis=-1, norm=None):
424+
"""
425+
Compute inverse one-dimensional discrete Fourier Transform of a signal that has Hermitian symmetry.
426+
427+
Limitations
428+
-----------
429+
Parameter ``norm`` is unsupported.
430+
Parameter ``x1`` supports ``dpnp.int32``, ``dpnp.int64``, ``dpnp.float32``, ``dpnp.float64`` and
431+
``dpnp.complex128`` datatypes only.
432+
433+
For full documentation refer to :obj:`numpy.fft.ihfft`.
434+
435+
"""
436+
437+
is_x1_dparray = isinstance(x1, dparray)
438+
439+
if (not use_origin_backend(x1) and is_x1_dparray and 0):
440+
if axis is None:
441+
axis_param = -1 # the most right dimension (default value)
442+
else:
443+
axis_param = axis
444+
445+
if n is None:
446+
input_boundarie = x1.shape[axis_param]
447+
else:
448+
input_boundarie = n
449+
450+
if x1.size < 1:
451+
pass # let fallback to handle exception
452+
elif input_boundarie < 1:
453+
pass # let fallback to handle exception
454+
elif norm is not None:
455+
pass
456+
else:
457+
output_boundarie = input_boundarie
458+
459+
return dpnp_fft(x1, input_boundarie, output_boundarie, axis_param, False)
460+
461+
return call_origin(numpy.fft.ihfft, x1, n, axis, norm)
462+
463+
301464
def irfft(x1, n=None, axis=-1, norm=None):
302465
"""
303466
Compute the one-dimensional inverse discrete Fourier Transform for real input..
@@ -487,6 +650,21 @@ def rfft2(x1, s=None, axes=(-2, -1), norm=None):
487650
return call_origin(numpy.fft.rfft2, x1, s, axes, norm)
488651

489652

653+
def rfftfreq(n=None, d=1.0):
654+
"""
655+
Compute the one-dimensional discrete Fourier Transform sample frequencies.
656+
657+
Limitations
658+
-----------
659+
Parameter ``d`` is unsupported.
660+
661+
For full documentation refer to :obj:`numpy.fft.rfftfreq`.
662+
663+
"""
664+
665+
return call_origin(numpy.fft.rfftfreq, n, d)
666+
667+
490668
def rfftn(x1, s=None, axes=None, norm=None):
491669
"""
492670
Compute the N-dimensional discrete Fourier Transform for real input.

tests/skipped_tests.tbl

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -357,58 +357,6 @@ tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy
357357
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid1
358358
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid2
359359
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid3
360-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftfreq_param_0_{d=1, n=1}::test_fftfreq
361-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftfreq_param_0_{d=1, n=1}::test_rfftfreq
362-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftfreq_param_1_{d=0.5, n=10}::test_fftfreq
363-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftfreq_param_1_{d=0.5, n=10}::test_rfftfreq
364-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftfreq_param_2_{d=2, n=100}::test_fftfreq
365-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftfreq_param_2_{d=2, n=100}::test_rfftfreq
366-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftshift_param_0_{axes=None, shape=(5,)}::test_fftshift
367-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftshift_param_0_{axes=None, shape=(5,)}::test_ifftshift
368-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftshift_param_1_{axes=0, shape=(5,)}::test_fftshift
369-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftshift_param_1_{axes=0, shape=(5,)}::test_ifftshift
370-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftshift_param_2_{axes=None, shape=(10,)}::test_fftshift
371-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftshift_param_2_{axes=None, shape=(10,)}::test_ifftshift
372-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftshift_param_3_{axes=0, shape=(10,)}::test_fftshift
373-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftshift_param_3_{axes=0, shape=(10,)}::test_ifftshift
374-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftshift_param_4_{axes=None, shape=(10, 10)}::test_fftshift
375-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftshift_param_4_{axes=None, shape=(10, 10)}::test_ifftshift
376-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftshift_param_5_{axes=0, shape=(10, 10)}::test_fftshift
377-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftshift_param_5_{axes=0, shape=(10, 10)}::test_ifftshift
378-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftshift_param_6_{axes=(0, 1), shape=(10, 10)}::test_fftshift
379-
tests/third_party/cupy/fft_tests/test_fft.py::TestFftshift_param_6_{axes=(0, 1), shape=(10, 10)}::test_ifftshift
380-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_0_{n=None, norm=None, shape=(10,)}::test_hfft
381-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_0_{n=None, norm=None, shape=(10,)}::test_ihfft
382-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_10_{n=10, norm='ortho', shape=(10,)}::test_hfft
383-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_10_{n=10, norm='ortho', shape=(10,)}::test_ihfft
384-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_11_{n=10, norm='ortho', shape=(10, 10)}::test_hfft
385-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_11_{n=10, norm='ortho', shape=(10, 10)}::test_ihfft
386-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_12_{n=15, norm=None, shape=(10,)}::test_hfft
387-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_12_{n=15, norm=None, shape=(10,)}::test_ihfft
388-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_13_{n=15, norm=None, shape=(10, 10)}::test_hfft
389-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_13_{n=15, norm=None, shape=(10, 10)}::test_ihfft
390-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_14_{n=15, norm='ortho', shape=(10,)}::test_hfft
391-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_14_{n=15, norm='ortho', shape=(10,)}::test_ihfft
392-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_15_{n=15, norm='ortho', shape=(10, 10)}::test_hfft
393-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_15_{n=15, norm='ortho', shape=(10, 10)}::test_ihfft
394-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_1_{n=None, norm=None, shape=(10, 10)}::test_hfft
395-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_1_{n=None, norm=None, shape=(10, 10)}::test_ihfft
396-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_2_{n=None, norm='ortho', shape=(10,)}::test_hfft
397-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_2_{n=None, norm='ortho', shape=(10,)}::test_ihfft
398-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_3_{n=None, norm='ortho', shape=(10, 10)}::test_hfft
399-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_3_{n=None, norm='ortho', shape=(10, 10)}::test_ihfft
400-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_4_{n=5, norm=None, shape=(10,)}::test_hfft
401-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_4_{n=5, norm=None, shape=(10,)}::test_ihfft
402-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_5_{n=5, norm=None, shape=(10, 10)}::test_hfft
403-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_5_{n=5, norm=None, shape=(10, 10)}::test_ihfft
404-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_6_{n=5, norm='ortho', shape=(10,)}::test_hfft
405-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_6_{n=5, norm='ortho', shape=(10,)}::test_ihfft
406-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_7_{n=5, norm='ortho', shape=(10, 10)}::test_hfft
407-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_7_{n=5, norm='ortho', shape=(10, 10)}::test_ihfft
408-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_8_{n=10, norm=None, shape=(10,)}::test_hfft
409-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_8_{n=10, norm=None, shape=(10,)}::test_ihfft
410-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_9_{n=10, norm=None, shape=(10, 10)}::test_hfft
411-
tests/third_party/cupy/fft_tests/test_fft.py::TestHfft_param_9_{n=10, norm=None, shape=(10, 10)}::test_ihfft
412360
tests/third_party/cupy/indexing_tests/test_generate.py::TestIndices::test_indices_list0
413361
tests/third_party/cupy/indexing_tests/test_generate.py::TestIndices::test_indices_list1
414362
tests/third_party/cupy/indexing_tests/test_generate.py::TestIndices::test_indices_list2

0 commit comments

Comments
 (0)