MATLAB Answers

0

How to plot a periodic signal with variable duty cycle?

Asked by Ana Júlia Lanzarin on 13 Oct 2018 at 19:40
Latest activity Answered by madhan ravi on 14 Oct 2018 at 2:38

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)

  0 Comments

Sign in to comment.

1 Answer

Answer by madhan ravi on 14 Oct 2018 at 2:38

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)

  0 Comments

Sign in to comment.