Python_FFT
In Python, there are very mature FFT functions both in numpy and scipy. In this section, we will take a look of both packages and see how we can easily use .
This part of the code was written by my friend. I recommend his blog here:
A very strong friend, friends who are interested can take a look.
Above code:
-
import numpy as np
-
from scipy.fftpack import fft
-
def FFT(data,Fs):
-
n=len(data)
-
if n%2!=0:
-
n-=1
-
# data=data[range(0,n)] #由于要进行取半处理,所以将n与2取余
-
data.pop()
-
Y=fft(data) #快速傅里叶变换
-
Fre=np.linspace(0,n-1,n)*Fs/n #linspace(start,stop,number)
-
Fre=Fre[range(0,int(n/2))]
-
Amp=np.abs(Y[range(0,int(n/2))])
-
Amp=Amp/(n/2)
-
Amp[0]=Amp[0]/2
-
Amp[-1]=Amp[-1]/2
-
return Fre, Amp
There are not many code comments, so I will add some text to explain:
The first parameter is the sampling value of the waveform function of Fourier analysis, and the second parameter is the number of sampling points per unit time, which refers to how many points of data are sampled in one second.
For example:
I gave 100 points for two cycles, so one cycle is 50 points. Assuming one cycle is 10 seconds, then 10 seconds corresponds to 50 points, 1 second is 5 points, one cycle is 10 seconds, the fundamental frequency is 0.1, so if only harmonics are analyzed, it is 0.3, 0.5, 0.7, etc. In this way, the corresponding fundamental wave, third harmonic, and fifth harmonic can all be obtained.
Attach the example and drawing code:
-
func1=func[:76] #取两个周期的点
-
F,A=FFT(func1,38)
-
plt.bar(F,A,width=0.4)
-
x=range(0,15,1)
-
plt.xlim(0,15)
-
plt.xticks(x)
-
plt.xlabel(‘Harmonic order’)
-
plt.ylabel(‘Amplitude’)
The decomposed result looks like the one above.
Finally, a few more words. Since FFT processing requires the use of all the data of the waveform, if the original data contains abnormal data such as nan, FFT will not produce any results during processing. Pay attention to the cleaning of the original data.