2016-04-19 52 views
1

我在Scilab中使用以下代码来生成使用矢量化方法的脉冲宽度调制。但是,在改变周期数,TimePeriod百分比时,我得到了不希望的图。请问任何人都可以帮助我?使用Scilab生成PWM

percent=input("Enter the percentage:"); 
TimePeriod=input("Enter the time period:"); 
Cycles=input("Enter the number of cycles:"); 

x=0:Cycles*TimePeriod; 
t=(percent/100)*TimePeriod; 

for n=0:0.01:Cycles 
    y(((n*TimePeriod)< x) & (x<(n*TimePeriod+t))) = 1; 
    y(((n*TimePeriod+t)< x)& (x<((n+1)*TimePeriod))) = 0; 
    plot(y,'b','LineWidth',2) 
end 

回答

1

我修改了你的代码,现在它工作。由于许多调用的for循环,您的代码非常慢。 (Scilab和Matlab被用来做矩阵运算,所以它们在循环中被削弱了,我把调用的次数减少到了循环次数

percent=input("Enter the percentage:"); 
TimePeriod=input("Enter the time period:"); 
Cycles=input("Enter the number of cycles:"); 
ts = 0.01; // sample time 

x=ts:ts:Cycles*TimePeriod; 
on = ones(1, percent/100*TimePeriod/ts); 
off = zeros(1, (1 - percent/100)*TimePeriod/ts); 
signal = []; 
for cycle = 1:Cycles 
signal = [signal, on, off]; 
end 

figure(1) 
plot2d(x, signal);