# -*- 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()
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()