You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a norm keyword arg to fft functions (Trac #1545) #2142
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.
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.)
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.
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.
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.
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.
Activity
charris commentedon Feb 19, 2014
Looks to be a useful enhancement.
jaimefrio commentedon Sep 5, 2014
If it only has two possible values, I find boolean flags more convenient than string arguments.
njsmith commentedon 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 commentedon 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 commentedon Sep 5, 2014
I actually like to multiply the forward fft by
dt, or whatever the sample spacing is, and the inverse bydf, or whatever the frequency sample spacing is. Normal defaults aredt = 1anddf = 1/n. This gives the spectrum nice units and everything has the nice symmetry of the integral transforms . The norm keyword corresponds to the choicedt =1/sqrt(n)`, which is a bit weird for spacing, but there you go.Nodd commentedon Sep 26, 2014
1/sqrt(n) is not about spacing but about enegry conservation. Try this:
You have to divide by sqrt(n) to get the same energy.
Now add some 0-padding:
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 commentedon 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 commentedon Jan 11, 2019
@mattip
Was this already closed by #5992?
mattip commentedon Jan 11, 2019
Yes, thanks