How to plot a periodic signal with variable duty cycle?

1 view (last 30 days)
Hi, I would like to plot a periodic function with a variable duty cycle. I think my ploblem is using the plot function, should I store the values of 'x' in a vector?
N = 500; %number of interactions
fs = 1000; %frequency of sampling
ts = 1/fs;
f0 = 100;
t0 = 1/f0;
dc = 0.5; %duty cycle=50%
t = 0;
Na = t0/ts; %number of samples
for n = 1:N
if rem(n, Na*dc) == 0
x = 1
else
x = 0
end
t = t+ts;
end
plot(t,x)

Answers (1)

madhan ravi
madhan ravi on 14 Oct 2018
N = 500; %number of interactions
fs = 1000; %frequency of sampling
ts = 1/fs;
f0 = 100;
t0 = 1/f0;
dc = 0.5; %duty cycle=50%
t = 0;
Na = t0/ts; %number of samples
for n = 1:N
if rem(n, Na*dc) == 0
x(n) = 1
else
x(n) = 0
end
end
t=0:ts:numel(x);
plot(t(1:numel(x)),x)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!