8000 ENH: add optional argument `dtype` to numpy.fft.fftfreq (#9126) by cval26 · Pull Request #16496 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: add optional argument dtype to numpy.fft.fftfreq (#9126) #16496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions numpy/fft/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

"""
from numpy.compat import integer_types
from numpy.core import integer, empty, arange, asarray, roll
from numpy.core import (integer, empty, arange, asarray, roll, reciprocal,
float32, float64)
from numpy.core.overrides import array_function_dispatch, set_module

# Created by Pearu Peterson, September 2002
Expand Down Expand Up @@ -122,7 +123,7 @@ def ifftshift(x, axes=None):


@set_module('numpy.fft')
def fftfreq(n, d=1.0):
def fftfreq(n, d=1.0, dtype=float64):
"""
Return the Discrete Fourier Transform sample frequencies.

Expand All @@ -140,7 +141,10 @@ def fftfreq(n, d=1.0):
n : int
Window length.
d : scalar, optional
Sample spacing (inverse of the sampling rate). Defaults to 1.
Sample spacing (inverse of the sampling rate). Defaults to 1.0.
dtype : object, optional
Output data type; either float32 or float64
Defaults to float64.

Returns
-------
Expand All @@ -160,18 +164,22 @@ def fftfreq(n, d=1.0):
"""
if not isinstance(n, integer_types):
raise ValueError("n should be an integer")
val = 1.0 / (n * d)
if dtype != float32:
dtype = float64

val = reciprocal(n*d, dtype=dtype)
results = empty(n, int)
N = (n-1)//2 + 1
p1 = arange(0, N, dtype=int)
results[:N] = p1
p2 = arange(-(n//2), 0, dtype=int)
results[N:] = p2

return results FBC0 * val


@set_module('numpy.fft')
def rfftfreq(n, d=1.0):
def rfftfreq(n, d=1.0, dtype=float64):
"""
Return the Discrete Fourier Transform sample frequencies
(for usage with rfft, irfft).
Expand All @@ -193,7 +201,10 @@ def rfftfreq(n, d=1.0):
n : int
Window length.
d : scalar, optional
Sample spacing (inverse of the sampling rate). Defaults to 1.
Sample spacing (inverse of the sampling rate). Defaults to 1.0.
dtype : object, optional
Output data type; either float32 or float64
Defaults to float64.

Returns
-------
Expand All @@ -216,7 +227,11 @@ def rfftfreq(n, d=1.0):
"""
if not isinstance(n, integer_types):
raise ValueError("n should be an integer")
val = 1.0/(n*d)
if dtype != float32:
dtype = float64

val = reciprocal(n*d, dtype=dtype)
N = n//2 + 1
results = arange(0, N, dtype=int)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry one last thought. Would it make sense to cast this to dtype as well?

Suggested change
results = arange(0, N, dtype=int)
results = arange(0, N, dtype=dtype)

Copy link
Contributor Author
@cval26 cval26 Jun 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could but from what I understand this is always just an array of integers, which when multiplied with the dtype cast val will lead to the returned result being of type dtype anyway, right?


return results * val
0