Fast Fourier Transform (FFT) of a sum of two sine signals#

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

from scipy.fftpack import fft
# Number of samplepoints
N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = fft(y)

halfN = np.int(N/2)

xf = np.linspace(0.0, 1.0/(2.0*T), halfN)
import matplotlib.pyplot as plt
plt.plot(xf, 2.0/N * np.abs(yf[0:halfN]))
plt.grid()
plt.show()
/tmp/ipykernel_182311/3724980803.py:14: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  halfN = np.int(N/2)
../_images/4de9e78aceb566c00080f7f8172507ba4978b7668589ccc0a42c38a7e19ae271.png
plt.figure()
plt.plot(x,y)
plt.plot(x,0.7*np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x),'r')
[<matplotlib.lines.Line2D at 0x7f2cfe5cec10>]
../_images/683d7d57322886d3d4fe85b92d15041de102022487cc07dc1189e065286bb726.png
from scipy.signal.windows import hann
w = hann(N)
ywf = fft(y*w)
plt.figure()
plt.plot(xf[1:halfN], 2.0/N * np.abs(yf[1:halfN]), '-b')
plt.plot(xf[1:halfN], np.sqrt(8/3)*2.0/N * np.abs(ywf[1:halfN]), '-r')
plt.legend(['FFT', 'FFT w. window'])
plt.grid()
plt.show()
../_images/d0d13f391afaca738b879b36f504cccf5b61213965bfe4bdb112971d8e91bc45.png