相关文章:傅立叶级数展开初探(Python)

这里做一下记录,关于FFT就不做介绍了,直接贴上代码,有详细注释的了:

import numpy as np
from scipy.fftpack import fft,ifft
import matplotlib.pyplot as plt
import seaborn


#采样点选择1400个,因为设置的信号频率分量最高为600赫兹,根据采样定理知采样频率要大于信号频率2倍,所以这里设置采样频率为1400赫兹(即一秒内有1400个采样点,一样意思的)
x=np.linspace(0,1,1400)

#设置需要采样的信号,频率分量有180,390和600
y=7*np.sin(2*np.pi*180*x) + 2.8*np.sin(2*np.pi*390*x)+5.1*np.sin(2*np.pi*600*x)

yy=fft(y) #快速傅里叶变换
yreal = yy.real # 获取实数部分
yimag = yy.imag # 获取虚数部分

yf=abs(fft(y)) # 取绝对值
yf1=abs(fft(y))/len(x) #归一化处理
yf2 = yf1[range(int(len(x)/2))] #由于对称性,只取一半区间

xf = np.arange(len(y)) # 频率
xf1 = xf
xf2 = xf[range(int(len(x)/2))] #取一半区间


plt.subplot(221)
plt.plot(x[0:50],y[0:50])
plt.title('Original wave')

plt.subplot(222)
plt.plot(xf,yf,'r')
plt.title('FFT of Mixed wave(two sides frequency range)',fontsize=7,color='#7A378B') #注意这里的颜色可以查询颜色代码表

plt.subplot(223)
plt.plot(xf1,yf1,'g')
plt.title('FFT of Mixed wave(normalization)',fontsize=9,color='r')

plt.subplot(224)
plt.plot(xf2,yf2,'b')
plt.title('FFT of Mixed wave)',fontsize=10,color='#F08080')


plt.show()

结果:


2017/7/11更新

再添加一个简单的例子

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
import seaborn



Fs = 150.0; # sampling rate采样率
Ts = 1.0/Fs; # sampling interval 采样区间
t = np.arange(0,1,Ts) # time vector,这里Ts也是步长

ff = 25; # frequency of the signal
y = np.sin(2*np.pi*ff*t)

n = len(y) # length of the signal
k = np.arange(n)
T = n/Fs
frq = k/T # two sides frequency range
frq1 = frq[range(int(n/2))] # one side frequency range

YY = np.fft.fft(y) # 未归一化
Y = np.fft.fft(y)/n # fft computing and normalization 归一化
Y1 = Y[range(int(n/2))]

fig, ax = plt.subplots(4, 1)

ax[0].plot(t,y)
ax[0].set_xlabel('Time')
ax[0].set_ylabel('Amplitude')

ax[1].plot(frq,abs(YY),'r') # plotting the spectrum
ax[1].set_xlabel('Freq (Hz)')
ax[1].set_ylabel('|Y(freq)|')

ax[2].plot(frq,abs(Y),'G') # plotting the spectrum
ax[2].set_xlabel('Freq (Hz)')
ax[2].set_ylabel('|Y(freq)|')

ax[3].plot(frq1,abs(Y1),'B') # plotting the spectrum
ax[3].set_xlabel('Freq (Hz)')
ax[3].set_ylabel('|Y(freq)|')

plt.show()

更多相关文章

  1. SHOW STATUS 查看各种类型SQL执行的频率
  2. 如何在Android设备上读取CPU频率

随机推荐

  1. Android Shareperferences使用
  2. Android 坐标系统
  3. Android两种 旋转Bitmap方法
  4. Android Bluetooth Stream Non-blocking
  5. Android 程序优化
  6. ANDROID NDK makefile 链接静态库的方法
  7. android Studio导入source文件
  8. Android UI 开源组件
  9. Android平台上部署OSGI
  10. BroadcastReceiver.PendingResult