8000 Add a norm keyword arg to fft functions (Trac #1545) · Issue #2142 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Add a norm keyword arg to fft functions (Trac #1545) #2142

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
numpy-gitbot opened this issue Oct 19, 2012 · 9 comments
Closed

Add a norm keyword arg to fft functions (Trac #1545) #2142

numpy-gitbot opened this issue Oct 19, 2012 · 9 comments

Comments

@numpy-gitbot
Copy link

Original ticket http://projects.scipy.org/numpy/ticket/1545 on 2010-07-15 by trac user dgoldsmith, assigned to unknown.

Presently, the fft functions return the unscaled transform and the ifft functions return the (1/n)-scaled inverse transform, meaning that neither result is unitary. In contrast, the discrete cosine transform (DCT) functions in scipy.fftpack include a keyword argument (norm) which, when set to "ortho," normalize both the forward and inverse transforms (both functions have the keyword argument) by 1/sqrt(n), thus returning unitary results. There appears to be no reason why the fft functions couldn't also support this.

@charris
Copy link
Member
charris commented Feb 19, 2014

Looks to be a useful enhancement.

@jaimefrio
Copy link
Member

If it only has two possible values, I find boolean flags more convenient than string arguments.

@njsmith
Copy link
Member
njsmith commented Sep 5, 2014

scipy compatibility seems like a reasonably compelling reason though, plus string arguments are easier to extend later... (I've often switched boolean flags to be string arguments later when it became clear that there really were more than 2 possible options.)

@Nodd
Copy link
Contributor
Nodd commented Sep 5, 2014

A third possibility would be to use 1/n for fft and nothing for ifft. This would allow to speed up computation if you don't care for the amplitude calibration. It seems very uncommon though.

@charris
Copy link
Member
charris commented Sep 5, 2014

I actually like to multiply the forward fft by dt, or whatever the sample spacing is, and the inverse by df, or whatever the frequency sample spacing is. Normal defaults are dt = 1 and df = 1/n. This gives the spectrum nice units and everything has the nice symmetry of the integral transforms . The norm keyword corresponds to the choice dt =1/sqrt(n)`, which is a bit weird for spacing, but there you go.

@Nodd
Copy link
Contributor
Nodd commented Sep 26, 2014

1/sqrt(n) is not about spacing but about enegry conservation. Try this:

x = np.random.random(128)
np.sum(np.abs(x)**2)
X = np.fft.fft(x)
np.sum(np.abs(X)**2)
np.sum(np.abs(X / np.sqrt(128))**2)

You have to divide by sqrt(n) to get the same energy.
Now add some 0-padding:

XX = np.fft.fft(x, 256)
np.sum(np.abs(XX)**2)
np.sum(np.abs(XX / np.sqrt(256))**2)

Again, you need to divide by the number of points to get the same energy. If you don't normalize, the 0-padding changes the total energy, while you only added zeros to the signal.

@charris
Copy link
Member
charris commented Sep 26, 2014

This is why I like the dt, df scaling approach, as df changes when zeros are added to the signal. It also changes if you interpolate points using some other method. A class is needed to track such things, essentially the units. Such a class that could be built on top of the current functions but I'm not sure it should be part of numpy. In any case, including a scaling factor argument with some default values could be useful.

@vasole
Copy link
vasole commented Jan 11, 2019

@mattip

Was this already closed by #5992?

@mattip
Copy link
Member
mattip commented Jan 11, 2019

Yes, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants
0