在yjmwxwx 网友的帖子中,有关spwm作为信号源的滤波器设计
在spice仿真软件中没有spwm信号,spice中只有一个pwl文件支持信号源
于是写了一个生成spwm的pwl文件的程序
程序原理根据sin表生成一个pwl文件
说明:
一个pwm波表示如下图:
Tr为上升时间
Tf为下降时间
Ton为从pwm表中读取的数值
Tx为末尾与下一个pwm的间隙
python程序如下:
spwm_table=[14,18,21,24,26,27,26,24,21,18,14,9,6,3,1,0,1,3,6,9]
def get_period(f):
return 1/f
def float2str(v,scale):
return str(int(v*scale))+'p'
def get_pwl_line(t,v):
return float2str(t,1e6)+','+str(v)
#stm32定时器频率
freq=56
spwm_period=28 ##arr寄存器
#stm32波形参数
Vinitial=0.0
Von=3.3
tr=1e-4
tf=1e-4
start=0.0
period=get_period(freq)*(spwm_period);
f=open('d:\spwm.txt','w')
for t in spwm_table:
print(t)
if (t==0) :
msg=get_pwl_line(start,Vinitial)
print(msg)
f.write(msg)
f.write('\r\n')
msg=get_pwl_line(start+period-1e-4,Vinitial)
print(msg)
f.write(msg)
f.write('\r\n')
else:
ton=t/freq
msg=get_pwl_line(start,Vinitial)
print(msg)
f.write(msg)
f.write('\r\n')
msg=get_pwl_line(start+tr,Von)
print(msg)
f.write(msg)
f.write('\r\n')
msg=get_pwl_line(start+ton,Von)
print(msg)
f.write(msg)
f.write('\r\n')
msg=get_pwl_line(start+ton+tf,Vinitial)
print(msg)
f.write(msg)
f.write('\r\n')
msg=get_pwl_line(start+period-1e-5,Vinitial)
print(msg)
f.write(msg)
f.write('\r\n')
print('---')
start=start+period
f.close()
|