##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 <fabian@tu-cottbus.de> 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
