From octave-sources-request at bevo dot che dot wisc dot edu Fri May 19 03:57:19 2000 Subject: 'zigf.m' and 'recf.m' : FAST zigzag and rectangle wave functions From: Rolf Fabian To: "'octave-sources UWISC'" Date: Fri, 19 May 2000 10:54:05 +0200 Hi, I've developped two algorithms depending on tricky combinations of octaves ,abs' and ,floor' builtin funtions to calculate trigonometric - like zigzag and rectangle wave functions in a very FAST way, because **NO LOOPS of IF STATEMENTS ** are needed !! Attached, please find * zigzag (sewsaw) - function -- /\/\/\/\ -- ,zigf(x,MODE,PERIOD)' * rectangle wave -function -- ``|_| ``|_|``|_|`` -- ;recf(x,MODE,PERIOD)' * a graphical demo ,recf_zigf_X( N )' NOTE: Have a closer look at demo number N=3, to get an impression about interesting combinations of these two functions Any feedback welcome! Bye Rolf -------------------------------------------------------------------------------------------- ##USAGE y = zigf( x {,m=0 {,p=2*pi}} ) ## ## zigzag-function -- /\/\/\ -- ## ##INPUT ##m mode of phase x: 0 p/4 p/2 3/4p ## 0 : cosine 1 0 -1 0 ## 1 : sine 0 1 0 -1 ##p (scalar) period ## ##NOTES * works elementwise on matrices x ## * very FAST algorithm (no if-loops) ## * zigf(x,1)./zigf(x) follows tan(x) ## ##ASSOC recf ##AUTHOR (C) 2000 Rolf Fabian 0519 #EXA N=501; t=linspace(-5,5,N)'; # plot(t,[zigf(t),cos(t),zigf(t,1),sin(t)]) # plot(t,[zigf(t,1)./zigf(t),tan(t)]) #990404 * argument number reduced # * intro fast calculation using 'abs' and 'floor' #000519 minor changes function y=zigf(x,m,p) if (nargin>3||nargin<1) fprintf(stderr,\ "USAGE y=zigf(x {,m=0 ,p=2*pi}} ) .. quit"); y=[]; return; endif if (nargin<3) p=2*pi; endif if (nargin<2) m =0; endif if (m) x=x-p/4; endif # define sine-phase y = 4*(abs(x/p-floor(x/p)-1/2)-1/4); endfunction -------------------------------------------------------------------------------------------- ##USAGE y = recf( x {,m=0 {,p=2*pi}} ) ## _ _ ## rectangle-function -- |_| |_| |_| -- ## ##INPUT ##m mode of phase x: 0 p/4 p/2 3/4p ## 0 : cosine - like 1 0 -1 0 ## 1 : sine - like 0 1 0 -1 ##p (scalar) period ## ##NOTES * works elementwise on matrices x ## * very FAST algorithm (no if-loops) ## ##ASSOC zigf ##AUTHOR (C) 2000 Rolf Fabian 0519 #EXA N=501; t=linspace(-5,5,N)'; # plot(t,[recf(t),cos(t),recf(t,1),sin(t)]) #990404 * argument number reduced # * intro fast calculation using 'floor' #000519 minor changes function y=recf(x,m,p) if (nargin>3||nargin<1) fprintf(stderr,\ "USAGE y=recf( x {,m=0 {,p=2*pi}} ) .. quit."); y=[]; return; endif if (nargin<3) p=2*pi; endif if (nargin<2) m=0; endif if (!m) x=x+p/4; endif # define cos-phase y = -2*(floor(2*x/p)-2*floor(x/p)-1/2); endfunction -------------------------------------------------------------------------------------------- ##USAGE recf_zigf_X( { N=3 } ) ## select N=1 or N=2 or N=3 ## recf_zigf - demo ##AUTHOR (C) 2000 Rolf Fabian 0519 function recf_zigf_X(N) if nargin<1 N=3; end M=501; t=linspace(-4,4,M)'; gset key; gset grid; xlabel('x'); ylabel('y'); if (N==1) title('recf example') gset yrange [-2:2]; plot( t,recf(t) ,';cos phase (M=0);',t,cos(t),';;',\ t,recf(t,1),';sin phase (M=1);',t,sin(t),';;') elseif (N==2) gset yrange [-2:2]; title('zigf example') plot( t,zigf(t) ,';cos phase (M=0);',t,cos(t),';;',\ t,zigf(t,1),';sin phase (M=1);',t,sin(t),';;') elseif (N==3) gset yrange [-4:4]; plot( t,tan(t),';tan(t);',\ t,zigf(t,1)./zigf(t),';zigf(t,1) / zigf(t);',\ t,zigf(t,1)./recf(t),';zigf(t,1) / recf(t);',\ t,recf(t,1)./zigf(t),';recf(t,1) / zigf(t);',\ t,recf(t,1)./recf(t),';recf(t,1) / recf(t);'); else disp('unknown example tag .. select N=1 -or- N=2 -or- N=3') end ----------------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.che.wisc.edu/octave/octave.html How to fund new projects: http://www.che.wisc.edu/octave/funding.html Subscription information: http://www.che.wisc.edu/octave/archive.html -----------------------------------------------------------------------