import numpy as np
import matplotlib.pyplot as plt
import random
from scipy import signal
def csin(length, amp,lenf,n):"""生成模拟CSIN波形的函数:param length: 波形长度:param amp: 波形振幅:return: CSIN波形数据"""amp = 100 if (amp==0) else 100/amp# 生成时间序列t = np.linspace(0, n, length)csin_data = np.sin(2 * np.pi * t)*amp*lenf#+np.tan(2 * np.pi * t)*amp*lenf # 返回波形数据return csin_data# 设置波形参数
length = 4000 # 波形长度
amp =5 # 波形振幅# 生成波形数据
b,c = signal.butter(5,0.08,'lowpass',analog = False);
print(b,c)
csin_data = csin(length, amp,32767,1)
a = csin_data.tolist()
mina = 0
minb = 0
bbu = 1 if (amp==0) else (100-amp)/100
amp = 1 if (amp==0) else amp
for i in range(0,len(a)):if(a[i]>=32767):mina = a[i] = 32767-i*5/amp#print(i*4)elif(a[i]<=-32768):minb = a[i] = -32768+i*5/amp
#print(a)
csin_dataa = np.array(a)
print(len(csin_dataa))
csin_datad = signal.lfilter(b,c,csin_dataa)
csin_dataf = csin_datad.tolist()
for i in range(0,len(csin_dataf)):csin_dataf[i] = round(csin_dataf[i])
csin_datae = np.array(csin_dataf)
print(csin_datae)
# 绘制波形图
plt.plot(csin_datae)
plt.title('CSIN Waveform (amp={})'.format(amp))
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.show()