# What will be the code for this following signal in MATLAB?

I was trying to generate a signal like this:

I have done this so far like this:

clc;
clear all;
close all;
t = 0:.1:10;
f=.5;
y=.2*sin(2*pi*f*t);
plot(t,abs(y));
axis([0 10 -2 2]);
grid on;


What can I do to get desired output?

• In general, I would recommend that you label your axes (in any diagram) because it is not immediately obvious what is depicted (I thought it was something like a signal flow graph but it turned out to be plain simple time domain, i.e. (time, amplitude) kind of thing). Also, the way this question is posed, makes it off topic for DSP.SE because it requests MATLAB code explicitly. If you think you would be benefited from more general responses about the generation of signals with the depicted waveform then please re-phrase the question. – A_A Jan 31 '18 at 23:09

Well, so far using your code, you are getting the following:

clc;
clear all;
close all;
t = 0:.1:10;
f=.5;
y=.2*sin(2*pi*f*t);
plot(t,abs(y));
axis([0 10 -2 2]);
grid on;


So now, the only missing part is to zero-out all samples using PWM signal with 25% duty cycle. For example, this can be done using the square wave. Its values are between +1 and -1, so simply add 1 and use the signum function (or divide it by 2):

Putting all that together:

clc;
clear all;
close all;
t = 0:.1:10;
f=.5;
y=.2*sin(2*pi*f*t);
s=sign(square(t/2*pi, 25)+1);
x=y.*s;
plot(t,x);
axis([0 10 -2 2]);
grid on;