To test the pyfftw module, I tried to compute the fourier transfom of a gaussian function knowing that the result is an other gaussian function. But there is what I get : a complet no sense result.
import numpy as np
import pyfftw
import matplotlib.pyplot as plt
from math import *
c=3
A=3
# Define the function to be transformed
x = np.linspace(-50, 50, 100)
"the gaussian test"
y=np.exp(-(x*x)/(2*c*c))*A
"her theoretical fourier transform, aka an other guassian"
fr=np.exp(-(x*x*c*c)/2)*(A*sqrt(2*pi)*c)
# Compute the Fourier transform using pyfftw
fourier_transform = pyfftw.interfaces.numpy_fft.fft(y)
# Plot the result
plt.plot(x,y, color='b', label="function")
plt.plot(x,fourier_transform, color='r', label="fourier f")
plt.plot(x,fr, color='g', label="theoretical ft")
plt.xlabel('x or k')
plt.ylabel('f or ~f')
plt.title("fourier transform")
plt.grid(True)
plt.legend()
plt.show
The ploting result : enter image description here
I dont know if I have to specify more arguments for the pyfftw.interfaces function or what ?
pyfftw.interfaces.numpy_fftfunctions behave like their equivalents fromnumpy. You probably need to shift the zero-frequency components to the middle.From the Numpy documentation:
Plotting results with fftshift