From help-request at octave dot org Sat Apr 8 05:50:20 2006 Subject: Re: square waves From: Paul Kienzle To: Doug Stewart Cc: help at octave dot org, NZG Date: Sat, 8 Apr 2006 06:48:14 -0400 On Apr 7, 2006, at 10:12 PM, Doug Stewart wrote: > NZG wrote: > >> Is there a function for generating square waves in octave? > > If you know how many 1s and zeros you need then you can do it this way: > > a(100)=0; > b(250)=0; > b=b+1; > > c=[a b a b a b a b] > > or do some for loops ;-) You can vectorize the above: n = 8*350; x=zeros(1,n); x(mod([0:n-1],9)>=100) = 1; Octave-forge has the compatibility functions pulstran and rectpuls which can do the same thing: n = 8*350; x = pulstran([1:n],[101:350:n],'rectpuls',250); I'm not sure that's any easier though. See also the function square just added to octave-forge. - Paul -- ## s = square(t,duty) ## ## Generate a square wave of period 2 pi with limits +1/-1. ## ## If the duty cycle is specified, the square wave is +1 for ## that portion of the time. ## ## on time ## duty cycle = ------------------ ## on time + off time ## function v = square (t,duty) if nargin == 1, duty = .5; elseif nargin != 2, usage('v = square(t [, duty])'); endif t /= 2*pi; v = ones(size(t)); v(t-floor(t) >= duty) = -1; endfunction ------------------------------------------------------------- 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 -------------------------------------------------------------