2022年2月15日 星期二

[fft, python] How to do the FFT transform and show the result

How to do the FFT transform and show the result

井民全, Jing, mqjing@gmail.com


Code

import matplotlib.pyplot as plt

import numpy as np

from numpy import where

from numpy.fft import fft



N = 1600         # Define the total number of data points 

fs = 800.0        # Define the sampling rate

dt = 1.0 / fs      # Define the sampling interval (sec)

t = np.linspace(0.0, N*dt, N) # start=0, stop = N*T, total sample = N

s = np.sin(50.0 * 2.0*np.pi*t) + 0.5*np.sin(80.0 * 2.0*np.pi*t)



sf = fft(s) # sf = fft(s-s.mean())


# Show the signal

fig, (ax0, ax1) = plt.subplots(2, 1)

ax0.set_title('Signal')

ax0.plot(t, s)


fNQ = fs/ 2.0                       # Determine Nyquist frequency

faxis = np.linspace(0.0, fNQ, N//2) # Construct frequency axis


ax1.set_title('FFT')


ax1.plot(faxis, np.abs(sf[:N//2]))

# ax1.plot(faxis, 2.0/N * np.abs(sf[:N//2])) // amplitude normalization


Result