# FFT of a gaussian signal in Python

I've been trying to get the FFT of a gaussian in Python. When I use the following parameters, the FFT goes hand in hand with the theoretical FT of the gaussian, but if I increase $$\sigma$$ they rapidly start to differ even if I have change the $$t$$ range or the length $$N$$ of the FFT. What is going on? How can I make my code calculate the correct FFT of a gaussian for any $$\sigma$$?. The following code produces the following plots (as it is it plots the FFT for $$\sigma = 0.1$$ and then $$\sigma$$ could be modified):

import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft, ifft
from scipy.fftpack import fftshift, ifftshift

fs=80 #sampling frequency
t = np.arange(-0.5, 0.5, 1/fs) # time domain
sigma=0.1 # $$\sigma$$ of the gaussian
variance=sigma**2

x=1/(np.sqrt(2*np.pi*variance))*(np.exp(-t**2/(2*variance))) # gaussian input signal

L=len(x)
NFFT = 1024 # length of FFT
f = (fs/NFFT)*np.arange(-NFFT/2, NFFT/2) # frequency domain
X = fftshift(fft(x,NFFT)) # FFT of the gaussian signal
Xtheory = np.exp(-0.5*(2*np.pi*sigma*f)**2) # theoretical FFT of the gaussian signal

fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize = (5,8))

ax1.plot(t,x, color = "black", linewidth = 1, linestyle = "--");
ax1.set_title('Gaussian Pulse $$\sigma$$={}'.format(sigma))
ax1.set_xlabel('Time(s)')
ax1.set_ylabel('Amplitude')

ax2.plot(f, abs(X)/L, color = "black", linewidth = 1, linestyle = "--", label =             "Scipy")
ax2.plot(f, Xtheory, color = "blue",linewidth = 5, alpha = 0.25, label = "Theory")
ax2.set_title('Magnitude of FFT');
ax2.set_xlabel('Frequency (Hz)')
ax2.set_ylabel('|X(f)|');
ax2.set_xlim(-10,10)
ax2.legend()

plt.show


As I mentioned, if I change $$\sigma = 0.1$$ to any other, in this case $$\sigma = 1$$, the FFT differ from the theoretical one. I am VERY new into signal processing. Thanks in advance for any help.

There are two issues:

• The time axis is not long enough to capture a sufficient length of the Gaussian.

• The FFT is not properly scaled.

For the first item mentioned regarding the time axis, the result is the product of the Gaussian with a rectangular pulse, so the result in frequency is the convolution of the desired Gaussian frequency response with a Sinc function (as the FT of a rectangular pulse). With the truncation as given with $$\sigma =1$$ the resulting distortion is significant enough to view in the scale of the plot given.

For the second item mentioned regarding the FFT, to match the theoretical response given, the total scaling needed comes out the $$10\sigma/L$$. Further as $$\sigma$$ increases the range of the function in frequency decreases, so the frequency axis can be reduced for higher $$\sigma$$ values.

I have resolved both of these items in the modified code below where both time and frequency are dynamically set based on the target $$\sigma$$ and the FFT is scaled to match the theoretical FFT result as given:

import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft, ifft
from scipy.fftpack import fftshift, ifftshift

sigma= .1 #       $$\sigma$$ of the gaussian

fs=80/(10*sigma)  #sampling frequency
tmax = 5*sigma
t = np.arange(-tmax, tmax, 1/fs) # time domain
variance=sigma**2
x=1/(np.sqrt(2*np.pi*variance))*(np.exp(-t**2/(2*variance))) # gaussian input signal
L=len(x)
NFFT = 1024 # length of FFT
f = (fs/NFFT)*np.arange(-NFFT/2, NFFT/2) # frequency domain
X = 10 * sigma / L * fftshift(fft(x,NFFT)) # FFT of the gaussian signal
Xtheory = np.exp(-0.5*(2*np.pi*sigma*f)**2) # theoretical FFT of the gaussian signal

• Thank you for your answer. I understand the first item. For the second one, how did you get the 'proper scaling'? if I change the 10 you put in # sampling frequency nothing happens but if I change the 10 you put in # FFT of the gaussian signal I go back to my original problem, why is that so?
– JIVP
May 16 at 23:08