以下內容引用鏈接:https://blog.csdn.net/baidu_37352210/article/details/79596633
(注意:通過如下內容可知,將序列信號進行傅里葉變換后,得到的頻譜圖上各k值(1~N/2)對應的振幅,觀察主要振幅,並得到其信號主要分量的周期N/k)
# 離散時間傅里葉變換的python實現
import numpy as np
import math
import pylab as pl
import scipy.signal as signal
import matplotlib.pyplot as plt
sampling_rate=1000
t1=np.arange(0, 10.0, 1.0/sampling_rate)
x1 =np.sin(15*np.pi*t1)
# 傅里葉變換
def fft1(xx):
# t=np.arange(0, s)
t=np.linspace(0, 1.0, len(xx))
f = np.arange(len(xx)/2+1, dtype=complex)
for index in range(len(f)):
f[index]=complex(np.sum(np.cos(2*np.pi*index*t)*xx), -np.sum(np.sin(2*np.pi*index*t)*xx))
return f
# len(x1)
xf=fft1(x1)/len(x1)
freqs = np.linspace(0, sampling_rate/2, len(x1)/2+1)
plt.figure(figsize=(16,4))
plt.plot(freqs,2*np.abs(xf),'r--')
plt.xlabel("Frequency(Hz)")
plt.ylabel("Amplitude($m$)")
plt.title("Amplitude-Frequency curve")
plt.show()
plt.figure(figsize=(16,4))
plt.plot(freqs,2*np.abs(xf),'r--')
plt.xlabel("Frequency(Hz)")
plt.ylabel("Amplitude($m$)")
plt.title("Amplitude-Frequency curve")
plt.xlim(0,20)
plt.show()
此處實現的是傳統的傅里葉變換,這種方法實際已經不用了,現在使用快速傅里葉變換,其實兩種是等價的,但是快速傅里葉變換時間復雜度要小很多。