2016-04-19 27 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);