home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 8.ddi / SIGNAL.DI$ / SQUARE.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  1.1 KB  |  39 lines

  1. function s = square(t,duty)
  2. %SQUARE    Square wave generation.
  3. %    SQUARE(T) generates a square wave with period 2*Pi for the
  4. %    elements of time vector T.  SQUARE(T) is like SIN(T), only
  5. %    it creates a square wave with peaks of +1 to -1 instead of
  6. %    a sine wave.
  7. %    SQUARE(T,DUTY) generates a square wave with specified duty
  8. %    cycle. The duty cycle, DUTY, is the percent of the period
  9. %    in which the signal is positive.
  10. %
  11. %    For example, generate a 30 Hz square wave:
  12. %         t = 0:.001:2.5;
  13. %         y = SQUARE(2*pi*30*t), plot(t,y)
  14.  
  15. %    L. Shure 2-23-88
  16. %    (c) Copyright 1988, by The MathWorks, Inc.
  17.  
  18. % If no duty specified, make duty cycle 50%.
  19. if nargin < 2
  20.     duty = 50;
  21. end
  22.  
  23. % Compute normalized frequency for breaking up the interval (0,2*pi)
  24. w0 = 2*pi*duty/100;
  25.  
  26. % Compute values of t normalized to (-2*pi,2*pi)
  27. tmp = rem(t,2*pi);
  28.  
  29. % Assign 0 values to normalized t between (0,w0) and (-2*pi,w0-2*pi),
  30. % 1 elsewhere
  31. nodd = (tmp > w0 | tmp < w0-2*pi);
  32.  
  33. % Vector with 1s where t < 0, 0s for t >= 0
  34. neg = (t < 0);
  35.  
  36. % The actual square wave computation
  37. s = 2*rem(neg+nodd+1,2) - 1;
  38.  
  39.