? on scipy.fftpack

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Duffy

    ? on scipy.fftpack

    I've been debugging a simulation I wrote a while ago, and it seems that
    the problem is in the fft module itself. I'm trying a simple test by
    just feeding the function a basic real gaussian. Obviously, I should
    get back the same real gaussian, but what I get is not even close. Can
    anyone help me? Thanks in advance.

    <code>
    from scipy import *
    import pylab

    fft, ifft = fftpack.rfft, fftpack.irfft

    def main():
    sigma = 50.
    x = arange(-100, 100, 1, 'f')
    g = exp(-x**2 / (2 * sigma)) / sqrt(2 * pi)
    testFFT(x, g)

    def testFFT(x, f):
    pylab.plot(x, f)
    pylab.title('In itial gaussian')
    pylab.show()
    pylab.clf()

    f = fft(f)
    pylab.plot(x, f)
    pylab.title('Af ter first FFT')
    pylab.show()
    pylab.clf()

    f = fft(f)
    pylab.plot(x, f)
    pylab.title('Af ter first iFFt')
    pylab.show()
    pylab.clf()

    if __name__ == '__main__': main()
    </code>

  • Robert Kern

    #2
    Re: ? on scipy.fftpack

    Mike Duffy wrote:[color=blue]
    > I've been debugging a simulation I wrote a while ago, and it seems that
    > the problem is in the fft module itself. I'm trying a simple test by
    > just feeding the function a basic real gaussian. Obviously, I should
    > get back the same real gaussian, but what I get is not even close. Can
    > anyone help me? Thanks in advance.[/color]

    You will probably want to ask scipy questions on scipy-user. There aren't many
    scipy people here.



    I haven't run your code, yet, but one of the things you are running into is the
    FFT packing convention for FFTs on real functions. Please read the docstring:

    Type: function
    Base Class: <type 'function'>
    String Form: <function rfft at 0x204fbf0>
    Namespace: Interactive
    File:
    /Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/site-packages/scipy-0.5.0.1980-py2.4-mac
    osx-10.4-ppc.egg/scipy/fftpack/basic.py
    Definition: rfft(x, n=None, axis=-1, overwrite_x=0)
    Docstring:
    rfft(x, n=None, axis=-1, overwrite_x=0) -> y

    Return discrete Fourier transform of real sequence x.

    The returned real arrays contains
    [y(0),Re(y(1)),I m(y(1)),...,Re( y(n/2))] if n is even
    [y(0),Re(y(1)),I m(y(1)),...,Re( y(n/2)),Im(y(n/2))] if n is odd
    where
    y(j) = sum[k=0..n-1] x[k] * exp(-sqrt(-1)*j*k* 2*pi/n)
    j = 0..n-1
    Note that y(-j) = y(n-j).

    Optional input:
    n
    Defines the length of the Fourier transform. If n is not
    specified then n=x.shape[axis] is set. If n<x.shape[axis],
    x is truncated. If n>x.shape[axis], x is zero-padded.
    axis
    The transform is applied along the given axis of the input
    array (or the newly constructed array if n argument was used).
    overwrite_x
    If set to true, the contents of x can be destroyed.

    Notes:
    y == rfft(irfft(y)) within numerical accuracy.



    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco

    Comment

    • Mike Duffy

      #3
      Re: ? on scipy.fftpack


      Robert Kern wrote:[color=blue]
      > You will probably want to ask scipy questions on scipy-user. There aren't many
      > scipy people here.
      >
      > http://www.scipy.org/Mailing_Lists
      >
      > I haven't run your code, yet, but one of the things you are running into is the
      > FFT packing convention for FFTs on real functions. Please read the docstring:[/color]

      Ok, thanks a lot. I was unaware of that mailing list, I will certainly
      go there next. I have read the documentation, but I'm not sure what
      packing convention you are referring to.

      Comment

      • Mike Duffy

        #4
        Re: ? on scipy.fftpack

        Oops, sorry. I see what you mean. I was reading the docs for the
        regular (complex) fft function, since that was what I was initially
        having the bug with. I was just using the rfft to simplify things, but
        I guess that was ironic. Anyway, I appreciate your help and don't mean
        to burden you. Again, I will suubmit this to the scipy list.

        Comment

        Working...