% Unit step function defined as 0 for input argument values % less than zero, 1/2 for input argument values equal to zero, % and 1 for input argument values greater than zero. Works % for vectors and scalars equally well. function y = u(t) zro = t == 0 ; pos = t > 0 ; y = zro/2 + pos ; % Function to compute the ramp function defined as 0 for % values of the argument less than zero and the value of % the argument for arguments greater than or equal to zero. % Works for vectors and scalars equally well. function y = ramp(t) y = t.*(t >= 0) ; % Rectangle function. Uses the definition of the rectangle % function in terms of the unit-step function. Works on % vectors or scalars equally well. function y = rect(t) y = u(t+0.5) - u(t-0.5) ; % Function to compute the triangle function. Uses the definition % of the triangle function in terms of the ramp function. Works % for vectors and scalars equally well. function y = tri(t) y = ramp(t+1) - 2*ramp(t) + ramp(t-1) ; % Function to compute sinc(t) defined as sin(pi*t)/(pi*t). % Works for vectors or scalars equally well. This function % may be intrinsic in some versions of MATLAB. % Function to compute values of the Dirichlet function. % Works for vectors or scalars equally well. % % x = sin(N*pi*t)/(N*sin(pi*t)) % function x = drcl(t,N) x = diric(2*pi*t,N) ; % Function to implement the Dirichlet function without using the % MATLAB diric function. Works for vectors or scalars % equally well. % % x = sin(N*pi*t)/(N*sin(pi*t)) % function x = drcl(t,N), num = sin(N*pi*t) ; den = N*sin(pi*t) ; I = find(abs(den) < 100*eps) ; num(I) = cos(N*pi*t(I)) ; den(I) = cos(pi*t(I)) ; x = num./den ; % Function to generate the discrete-time impulse function defined as % one for input integer arguments equal to zero and zero otherwise. % Returns "NaN" for non-integer arguments. Works for vectors and % scalars equally well. % % function y = impD(n) function y = impD(n) y = double(n == 0) ; % Impulse is one where argument % is zero and zero otherwise ss = find(round(n)~=n) ; % Find non-integer values of "n" y(ss) = NaN ; % Set corresponding outputs to % "NaN" % Unit sequence function defined as 0 for input integer argument % values less than zero, and 1 for input integer argument values % equal to or greater than zero. Returns "NaN" for non-integer % arguments. Works for vectors and scalars equally well. % % function y = uD(n) function y = uD(n) y = double(n >= 0) ; % Set output to one for non- % negative arguments ss = find(round(n)~=n) ; % Find all non-integer "n's" y(ss) = NaN ; % Set the corresponding outputs % all to "NaN" % Unit discrete-time ramp function defined as 0 for input integer % argument values equal to or less than zero, and "n" for input % integer argument values greater than zero. Returns "NaN" for non-% integer arguments. Works for vectors and scalars equally well. % % function y = rampD(n) function y = rampD(n) pos = double(n>0) ; % Set output to "n" for positive y = n.*pos ; % "n" ss = find(round(n)~=n) ; % Find all non-integer "n's" y(ss) = NaN ; % Set the corresponding outputs % all to "NaN" % Discrete-time rectangle function defined as 1 for input integer % argument values equal to or less than "W" in magnitude and zero % for other integer argument values. "W" must be an integer. % Returns "NaN" for non-integer input values. % % y = rectD(W,n) function y = rectD(W,n) if W == round(W), y = double(abs(n)<=abs(W)) ; % Set output to one if % |n|<=|W|and zero % otherwise ss = find(round(n)~=n) ; % Find all non-integer % "n's" y(ss) = NaN ; % Set the corresponding % outputs all to "NaN" else disp('In rectD, width parameter, W, is not an integer') ; end % Discrete-time periodic impulse function defined as 1 for input % integer argument values equal to integer multiples of "N" and 0 % otherwise. "N" must be an integer. Returns "NaN" for non-integer % input values. Works for vectors and scalars equally well. % % function y = impND(N,n) function y = impND(N,n) if N == round(N), y = double(n/N == round(n/N)) ; % Set output to one % for all n's which % are integer % multiples of N % and zero otherwise ss = find(round(n)~=n) ; % Find all non- % integer n's" y(ss) = NaN ; % Set the % corresponding % outputs all to % "NaN" else disp('In impND, period parameter, N, is not an integer') ; end