MATLAB Answers

Shifting a signal to the right or left

622 views (last 30 days)
SSG_newbiecoder
SSG_newbiecoder on 1 Feb 2018
Answered: Jos (10584) on 20 Sep 2019
How can I shift a signal to the left or right? Is there any inbuilt command for it?

  5 Comments

Show 2 older comments
Jos (10584)
Jos (10584) on 6 Feb 2018
maybe you should give an example of the input and expected output?
SSG_newbiecoder
SSG_newbiecoder on 6 Feb 2018
The below code works well so problem solved. :)
s = zeros(size(x));
if shift >0
s(shift+1:end) = x(1:end-shift);
elseif shift <0
s(1:end+shift) = x(-shift+1:end);
end
mathematics
mathematics on 15 Sep 2019
Could you please explain how to use this code?
Thank you

Sign in to comment.

Answers (3)

SSG_newbiecoder
SSG_newbiecoder on 20 Sep 2019
I'll try to explain with an example.
Let X={1 2 3 4 5} be our sequence. s = zeros(size(X))
s={0 0 0 0 0}
Right shift
If we want to shift this function to the right by three points,
shift=3 i.e, shift>0
s(shift+1:end) = x(1:end-shift)
ie, s(4,5)=X(1,2)
so shifted output will be s={0 0 0 1 2}
Left shift
If we want to shift this function to the left by three points,
shift=-3 i.e, shift<0
s(1:end+shift) = x(-shift+1:end)
ie, s(1,2)=X(4,5)
so shifted output will be s={4 5 0 0 0}
Hope this makes sense. Please note that this is not the circular shift and we have inbuilt code for circular shift.

  0 Comments

Sign in to comment.


Jos (10584)
Jos (10584) on 20 Sep 2019
x = 1:5
shift = 3
x = circshift(x,shift)
N = numel(x)
ix = (1:N) - shift
tf = ix < 1 | ix > N
x(tf) = 0

  0 Comments

Sign in to comment.


Jos (10584)
Jos (10584) on 1 Feb 2018
x = 1:10
k = 3
xs = circshift(x,k)

  0 Comments

Sign in to comment.

Sign in to answer this question.

Tags