Shifting signals to the right

Hi, I have a matrix, M which is 10*2000 double (10 rows of signals and each signal contains 2000 variables). I want to shift each of the signal in this matrix to the right by say 1. I have a code (below) that can shift a single signal (that is if M contains a single signal, 1*2000). How do I modify this to shift each signal in M at the same time?
The code code for shifting a single signal is:
p = M;
Shift = 1;
Shifted_M = circshift(p,Shift);
In summary, I want a code that can shift each independent row signal in a matrix and output the shifted matrix as Shifted_M. After that I would like to plot this shifted matrix with the original data to visualize it.
Thanks

1 Comment

% consider your matrix is M then:
M=[1 2 3; 4 5 6; 7 8 9];
Shift = 1;
Shifted_M = circshift(M',Shift);
rotated_matrix = Shifted_M'

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 18 Jun 2020
If you'd like to circularly shift the data "A" by "K" positions along the "dim" dimension,
Y = circshift(A,K,dim) (click for more info)
So, you'll need to specify the dimension.

10 Comments

Excellent! Y = circshift(A,1,2) works. I did not need the "dim" code. if I want to shift by 2 I just replace one with 2.
Any hint or idea how I can plot all the shifted data with the original using subplot to compare them?
Adam Danz
Adam Danz on 18 Jun 2020
Edited: Adam Danz on 18 Jun 2020
If you used circshift(A,1,2), then you did use the dim input. The dim input is the 3rd input.
I didn't understand your followup question.
you are absolutely correct.. after reading about it further that's what it is, the 3rd input is the dim. As for the follow up, I wanted to plot the shifted data together with the original (unshifted data) for visualization purposes to compare them. I want to use subplot (2,1,1), plotting shifted and original in the same plot, probably with an offset.
circshift shifts the values circularly so that the value values on the end are wrapped to the beginning. Are you sure this is what you want?
Alternatively, you could use a linear shift where all the values are shifted by 1 unit such as
[1 2 3 4] ---> [2 3 4 5]
Anyway, it's still not clear what the plot should look like. If your data are 10x2000, do you want 20,000 dots? 10 lines? 2000 lines? What defines the (x,y) coordinate of each point? I don't have a mental image of what you're looking for.
Thanks again for your response. In this case how would a code for a linear shift (say by 1) looks like? A code or a guide to a linear shift would be great!
As for the plot, say x1 is the original matrix of 10by2000. After shifting it’s called x2, still 10by2000. Subplot(2,1,1) plot(x1,’r—-) hold Plot(x2,’k-) Title(‘comparing shifted to Original)
Anytime I plot this I get a weird plot.
To shift coordinates (r,q) to the right by 1 unit,
plot(r+1, q);
or
r2 = r+1;
plot(r2, q);
But I still don't know how you plan to map the matrix values onto the plot. Is it a 2D plot? 3D plot? For example, let's say your matrix is
m = [ 2 4 1 0 8
8 2 1 9 0
1 9 2 0 3];
The first value is 2. What does that mean? How is the value of 2 plotted?
Curious Mind
Curious Mind on 19 Jun 2020
Edited: Curious Mind on 19 Jun 2020
Hi, the code is just adding 1 to all the variables and not shifting their positions. As for the plot the x axis can just represent the number of variables. They should be shifted in such a way that when you try to overlay the shifted and non-shifted data you can clearly see that there is a shift.
"The code is just adding 1 to all the variables and not shifting their positions."
That's incorrect. When you plot two variable using plot(r,q) the x-coordinates are defined by r. To shift them one unit to the right, you would add 1 to the x-coordinates. plot(r+1,q).
It makes sense, right? If r=3 and you want to shift it by 1 unit to the right, then r0=3+1.
Here's an example.
r = 1:10;
q = sort(randn(1,10));
cla()
plot(r, q, 'r-o')
hold on
plot(r+1, q, 'b-o')
It's still not clear how your matrix values should be visualized. For example, for matrix M,
m = [ 2 4 1 0 8
8 2 1 9 0
1 9 2 0 3];
if the x coordinates are x=[1 2 3 4 5] and the y coordinates are y=[1 2 3], then at coordinate (1,1) the value of m is 2. Does that mean you want a 3D plot were m contains the z-values?
You are correct! I was adding the 1 to the wrong thing. It works now. Also I figured out the plot part. I appreciate your patience and help! Thank you.
Ah, good! Sometimes describing the problem is more difficult than finding the solution. Glad it all worked out!

Sign in to comment.

More Answers (1)

clc;
clear;
close all;
Fs = 1000;
t = -0.02:1/Fs:0.02;
fc = 100;
modulation_index = 1;
M=[1 2 3; 4 5 6; 7 8 9];
Shift = 1;
Shifted_M = circshift(M',Shift);
rotated_matrix = Shifted_M'
m_t = [t ./ (1 + t.^2)];
c_t = cos(2 * pi * fc * t);
am_signal = (1 + modulation_index * m_t) .* c_t;
subplot(3, 1, 1);
plot(t, m_t);
title('Message Signal');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(3, 1, 2);
plot(t, c_t);
title('Carrier Signal');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(3, 1, 3);
plot(t, am_signal);
title('Amplitude Modulated Signal (100% Modulation Index)');
xlabel('Time (s)');
ylabel('Amplitude');
shift the signal for 5 units

Products

Release

R2019b

Asked:

on 18 Jun 2020

Answered:

on 4 Dec 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!