Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fourier transform of a Gaussian is not a Gaussian, but thats wrong! - Python

Tags:

python

numpy

fft

I am trying to utilize Numpy's fft function, however when I give the function a simple gausian function the fft of that gausian function is not a gausian, its close but its halved so that each half is at either end of the x axis.

The Gaussian function I'm calculating is y = exp(-x^2)

Here is my code:

from cmath import *
from numpy import multiply
from numpy.fft import fft
from pylab import plot, show

""" Basically the standard range() function but with float support """
def frange (min_value, max_value, step):
    value = float(min_value)
    array = []
    while value < float(max_value):
        array.append(value)
        value += float(step)
    return array


N = 256.0 # number of steps
y = []
x = frange(-5, 5, 10/N)

# fill array y with values of the Gaussian function   
cache = -multiply(x, x)
for i in cache: y.append(exp(i))

Y = fft(y)

# plot the fft of the gausian function
plot(x, abs(Y))
show()

The result is not quite right, cause the FFT of a Gaussian function should be a Gaussian function itself...

like image 787
chutsu Avatar asked Mar 22 '11 21:03

chutsu


People also ask

What is the Fourier transform of a Gaussian function?

The Fourier transform of a Gaussian function of x is a Gaussian function of k. The standard deviation of is inversely proportional to the standard deviation of . If the function is an even function, its Fourier transform can be a Fourier cosine transform: (11.39)

Is there a Gaussian function in Python?

gauss() gauss() is an inbuilt method of the random module. It is used to return a random floating point number with gaussian distribution. Example 2: We can generate the number multiple times and plot a graph to observe the gaussian distribution.

What does Numpy FFT Fftfreq do?

fft. fftfreq. Return the Discrete Fourier Transform sample frequencies.


1 Answers

np.fft.fft returns a result in so-called "standard order": (from the docs)

If A = fft(a, n), then A[0] contains the zero-frequency term (the mean of the signal), which is always purely real for real inputs. Then A[1:n/2] contains the positive-frequency terms, and A[n/2+1:] contains the negative-frequency terms, in order of decreasingly negative frequency.

The function np.fft.fftshift rearranges the result into the order most humans expect (and which is good for plotting):

The routine np.fft.fftshift(A) shifts transforms and their frequencies to put the zero-frequency components in the middle...

So using np.fft.fftshift:

import matplotlib.pyplot as plt
import numpy as np

N = 128
x = np.arange(-5, 5, 10./(2 * N))
y = np.exp(-x * x)
y_fft = np.fft.fftshift(np.abs(np.fft.fft(y))) / np.sqrt(len(y))
plt.plot(x,y)
plt.plot(x,y_fft)
plt.show()

enter image description here

like image 141
unutbu Avatar answered Nov 07 '22 19:11

unutbu