截断的正弦信号

Truncated sinusoidal signal

我想绘制截断的正弦信号。我试过的是:

tstep = 0.1;
t = -1:0.05:2;
f0 = 1;
fi = 10;
t = 0:tstep:2;
s = sin(2*pi*f0*t+fi);
plot(t,s);

我必须得到这样的东西(对于f0 = 1):


问题的正文是:

Write a Matlab program that plots a truncated sinusoidal signal:
s(t) = PT (t) sin (2πf0t + φ)
where:
T = 1 fixed
f0 is an integer number between 1 and 10 input from GUI (keyboard)
φ is a randomly generated phase between 0 and radiants

您已定义fi=10fi 应该根据您的问题陈述在 0 之间随机生成。此行也没有执行任何操作: t = -1:0.05:2; 因为您在几行之后重新定义了 t 。要生成矩形脉冲,您可以使用 pulstran(需要信号处理工具箱)。包含修复的完整代码如下:

tstep=0.005;     t=-1:tstep:2;      fi = 2*pi*rand(1);
%Loop to take f0 as an integer number between 1 to 10 input from keyboard
while 1
    f0 = input('f0 = ');  %requesting user input
    if mod(f0,1) || f0<1 || f0>10
       %mod checks whether the value is not an integer and then we check 
       %if it doesn't belong to [1,10]
       disp('Wrong value entered. Please enter an integer between 1 to 10');
    else, break;  %break if the correct value is entered
    end
end

PT = pulstran(t,0.5,'rectpuls');  %Generating rectangular pulse
SW = sin(2*pi*f0*t+fi);           %Generating sinusoidal wave

%I have added two plots just so you know what's happening
subplot(1,2,1); 
plot(t, PT, t, SW, 'linewidth', 2);
legend({'Rectangular Pulse','Sinusoidal Wave'},'location','northoutside','NumColumns',2);
xlabel('Time (t)');    ylabel('Signals');

subplot(1,2,2);
y = PT.*SW;
plot(t, y, 'linewidth', 2);
legend('Truncated Sinusoidal Wave','location','northoutside');
xlabel('Time (t)');    ylabel('s(t)');

fi=pif0=1 时的结果: