Digital Signal Processing Reference
In-Depth Information
function [A] = shift(A,n)
%
% Shift moves each element down by n steps along vector A,
% treating the positions j as though they are on a circle,
% so that A(j) moves to position (j+n), and A(L-n+1:L) occupy
% positions (1:n), where L is the number of elements in A.
% If n is positive the shift is in the forward direction
% whereas if negative, the shift is backwards.
%
% Usage: [A] = shift(A,n)
%
% Input: A, array whose elements will be shifted
% n, number of shifts to perform
% If n is non-integer it is rounded to
% the nearest integer
%
% Output: A, array with shifted elements
%
% Notes: (1) n cannot be a vector or matrix.
% (2) A must be either row or column vector.
%
change = 0;
[r,c] = size(A);
if (r > 1)&(c == 1)
A = A'; change = 1;
end
[r,c] = size(A);
if (r == 1)&(c >= 1)
[r,c] = size(n);
if (r == 1)&(c == 1)
n = round(n);
L = length(A);
n = rem(n,L);
if n < 0 n = L + n; end
T = A;
A(n+1:L) = T(1:L-n);
A(1:n) = T(L-n+1:L);
else
disp('Shift warning: number of shifts cannot be a vector or matrix');
end
else
disp('Shift warning: input must be row or column vector');
end
if change == 1 A = A'; end;
return
Figure 2.12 A Matlab
function to shift elements in a row or column vector.
To illustrate the procedure, Figure 2.14 (a) shows the output when the input signal
x = sin(
0.02 t ) is filtered with a 99-point low-pass filter with cutoff
at 0.2, using a 99-point FFT in the algorithm given in Figure 2.13. Note that the
filter output starts at L /2 = 49, and that both frequencies are passed. In part (b) of
the figure, the input signal is x = sin(
0.06 t ) + sin(
0.4 t ), giving the normalized
sum and difference frequencies at 0.82 and 0.02, respectively; the lower
0.42 t )
×
sin(
Search WWH ::




Custom Search