Skip to content

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

Closed

Description

@numpy-gitbot

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.

Activity

  1. charris commented on Feb 19, 2014

    @charris
    Member

    Looks to be a useful enhancement.

  2. jaimefrio commented on Sep 5, 2014

    @jaimefrio
    Member

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

  3. njsmith commented on Sep 5, 2014

    @njsmith
    Member

    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.)

  4. Nodd commented on Sep 5, 2014

    @Nodd
    Contributor

    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.

  5. charris commented on Sep 5, 2014

    @charris
    Member

    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.

  6. Nodd commented on Sep 26, 2014

    @Nodd
    Contributor

    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.

  7. charris commented on Sep 26, 2014

    @charris
    Member

    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.

  8. vasole commented on Jan 11, 2019

    @vasole

    @mattip

    Was this already closed by #5992?

  9. mattip commented on Jan 11, 2019

    @mattip
    Member

    Yes, thanks

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions