我在Scilab中使用以下代码来使用矢量化方法生成脉冲宽度调制。但是我在改变周期数,TimePeriod,百分比时会得到不合适的情节。可以帮助我吗?
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
答案 0 :(得分: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);