From help-request at octave dot org Mon Apr 10 20:17:40 2006 Subject: Re: square waves From: Paul Kienzle To: NZG Cc: Octave Help , Doug Stewart Date: Mon, 10 Apr 2006 21:15:51 -0400 On Apr 10, 2006, at 11:39 AM, NZG wrote: >> See also the function square just added to octave-forge. > Same thing Matlab has, nice for compatibility but I don't like it > because you > don't get what you expect when you plot it, interpolation gives you a > trianglish wave instead of a square. > I think it really needs to use more points to more closely approx. a > true > square. What is wrong with the following? axis([-0.1 1.1 -1.1 1.1]) t=[0:0.001:1]; # A 7 Hz square wave sampled at 1kHz with a .25 duty cycle plot(t,square(2*pi*t*7,0.25)) # A 7 Hz sine wave plot(t,sin(2*pi*t*7)) If you are using really coarse sampling then you are probably better off using a stem plot: t=[0:0.01:1];stem(t,square(2*pi*t*7,0.25)) Stairs is misleading because of the aliasing. t=[0:0.01:1];stairs(t,square(2*pi*t*7,0.25)) If you just want to plot the edges, then you can use e.g., f=7;duty=0.25; t=[[0:1/f:1];[0:1/f:1]+duty/f](:); stairs(t,(-1).^[0:length(t)-1]) - Paul ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------