Ghilas
Ghilas

Reputation: 1

Fourier transform with pyfftw module problem

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 ?

Upvotes: -1

Views: 122

Answers (1)

Kuroneko
Kuroneko

Reputation: 146

pyfftw.interfaces.numpy_fft functions behave like their equivalents from numpy. You probably need to shift the zero-frequency components to the middle.

fourier_transform = pyfftw.interfaces.numpy_fft.fftshift(y)

From the Numpy documentation:

The values in the result follow so-called “standard” order: If A = fft(a, n), then A[0] contains the zero-frequency term (the sum of the signal), which is always purely real for real inputs.
...
The routine np.fft.fftshift(A) shifts transforms and their frequencies to put the zero-frequency components in the middle

Plotting results with fftshift

Upvotes: 0

Related Questions