Python低通滤波器

# -*- coding: utf-8 -*-  
  
  
from scipy import signal  
import numpy as np  
import matplotlib.pyplot as pl  
import matplotlib  
import math  
  
N = 0#500  
fs = 5  
n = []#[2*math.pi*fs*t/N for t in range(N)]  
n1 = [2*math.pi*fs*t/N for t in range(N)]  
fileObj = open("ExcelProc\out.txt",'r')  
for line in fileObj.readlines():    #依次读取每行  
    FuelVol = line#.split() #根据空格提取每个内容  
    FuelVol = int(FuelVol)  
    n.append(FuelVol)  
    N += 1  
      
while N%12 != 0:  
    n.append(int(0))  
    N += 1  
print("N is deal ",N)  
axis_x = np.linspace(0,1,num=N)  
#设置字体文件,否则不能显示中文  
#myfont = matplotlib.font_manager.FontProperties(fname='c:\\windows\\fonts\\fzshjw_0.ttf')  
  
#频率为5Hz的正弦信号  
#x = [math.sin(i) for i in n]  
x = n  
pl.subplot(231)  
pl.plot(axis_x,x)  
pl.title(u'5Hz Sin')  
pl.axis('tight')  
  
x1 = []  
length = (len(x)*5/7)  
x1 = x[-length:]  
  
pl.subplot(232)  
axis_x1 = np.linspace(0,1,num=len(x1))  
pl.plot(axis_x1,x1)  
pl.title(u'ps')  
pl.axis('tight')  
  
x2 = x[:len(x)-length]  
pl.subplot(233)  
axis_x1 = np.linspace(0,1,num=len(x2))  
pl.plot(axis_x1,x2)  
pl.title(u'pd')  
pl.axis('tight')  
  
  
b,a = signal.butter(3,0.13,'low')  
sf = signal.filtfilt(b,a,x1)  
  
pl.subplot(234)  
axis_x1 = np.linspace(0,1,num=len(x1))  
pl.plot(axis_x1,sf)  
pl.title(u'Low Pass')  
pl.axis('tight')  
fileout = open("Out.txt","w")  
fileout.write("const unsigned int data[]@0xC000={")  
line = 0  
outx = []  
outx = x2  
for i in sf:  
    outx.append(i)  
      
pl.subplot(235)  
axis_x1 = np.linspace(0,1,num=len(outx))  
pl.plot(axis_x1,outx)  
pl.title(u'Low Pass')  
pl.axis('tight')  
  
sampling_rate = 8000  
fft_size = len(outx)  
xf = np.fft.rfft(outx)/fft_size  
freqs = np.linspace(0, sampling_rate/2, fft_size/2+1)  
xfp = 20*np.log10(np.clip(np.abs(xf), 1e-20, 1e100))  
pl.subplot(236)  
pl.plot(freqs, xfp)  
pl.xlabel(u"Freq(Hz)")  
pl.subplots_adjust(hspace=0.4)  
  
for i in outx:  
    i = int(i)  
    if(line%36 == 0):  
        fileout.write("\n")  
    fileout.write(str(i)+",")  
    line += 1  
fileout.write("};")  
fileout.close()  
pl.show()




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值