# Shifting Sequences Using MATLAB

I'm reading the book Vinay Ingle, John Proakis - Digital Signal Processing Using MATLAB® (3rd Edition) and I came across a function to shift sequences. Mathematically,

$$y(n) = \{x(n-k) \}$$

if $$m = n-k$$, then $$y(m+k) = \{ x(m) \}$$

function [y,n] = sigshift(x,m,k)
n = m + k;
y = x;
end


This function shifts nothing. I've tried to find errata list of the book, but I couldn't find it. Is this function correct?

This function just changes the x-axis for plotting signals. It's indeed pretty trivial but the idea is to use it like this:

n = 0:4;
x = [1 2 3 2 1];           % some signal
[y,m] = sigshift(x,n,3);   % shift to the right by 3 samples
subplot(2,1,1), stem(n,x)
subplot(2,1,2), stem(m,y)


In a lot of discrete signal processing applications, one uses a circular shift, ie samples on the right are moved circularly to the left (or the converse). In practice, one can handle a standard shift via signal padding and consistent windowing, followed by circular shifts. Matlab has a built-in circshift.m function. An example os use is given below:

lData = 128;
lag = 13;
data = rand(lData,1).*window(@hamming,lData);
dataShift = circshift(data,lag);
plot(linspace(0,lData-1,lData),[data,dataShift]);
grid on; legend('Data','Data shifted');axis tight


In order to shift in MATLAB you need to play with the indices of the signal vector. for example, given vector x - vX of length L, to shift it in time such that the first sample is the fifth you'll do this:

vXShifted = x(5:L);


Pay attention that the signal is shorter by 4 samples.

Now, sometimes, for periodic signals, a cyclic shifting is required which can be done using circshift() function in MATLAB.