Study material for signal processing in Python.
numpy, matplotlib scipy fftpack
N = 2**20 # data number
dt = 0.0001 # data step [s]
f1 = 5 # frequency[Hz]
A1 = 1 # Amplitude
p1 = 90*pi/180 # phase [rad]
#Waveform formation
t = np.arange(0, N*dt, dt) # time
freq = np.linspace(0, 1.0/dt, N) # frequency step
y = A1*np.sin(2*np.pi*f1*t + p1)
#Discrete Fourier transform&Standardization
yf = fft(y)/(N/2)
# y :numpy array
# N :Number of samplings
Whole code
fft.py
import numpy as np
from scipy.fftpack import fft
import matplotlib.pyplot as plt
#Pi
pi = np.pi
# parameters
N = 2**20 # data number
dt = 0.0001 # data step [s]
f1 = 5 # frequency[Hz]
A1 = 1 # Amplitude
p1 = 90*pi/180 # phase
#Waveform formation
t = np.arange(0, N*dt, dt) # time
freq = np.linspace(0, 1.0/dt, N) # frequency step
y = A1*np.sin(2*np.pi*f1*t + p1)
#Fourier transform
yf = fft(y)/(N/2) #Discrete Fourier transform&Standardization
#plot
plt.figure(2)
plt.subplot(211)
plt.plot(t, y)
plt.xlim(0, 1)
plt.xlabel("time")
plt.ylabel("amplitude")
plt.subplot(212)
plt.plot(freq, np.abs(yf))
plt.xlim(0, 10)
plt.xlabel("frequency")
plt.ylabel("amplitude")
plt.tight_layout()
plt.savefig("01")
plt.show()
Top: Time domain graph: Confirm that the phase is 90 degrees out of phase. Bottom: Frequency domain graph: 5Hz frequency confirmed.
I've used matlab for signal processing so far, but python's scipy is easier.
Recommended Posts