How to automatically shift a graph back
Show older comments
Hello,
So I have multiple graphs which are out of sync to say.
I do not have the signal processing toolbox so I am forced to do this manually, but what I want is for the largest peak in a graph to be synced up with all the other graphs I will be working with.
I have this code at the very top which only records the x-axis location of the largest peak in plot #1:
offset = time(find(y == max(y)));
I then have this code where I tried to change the x-axis myself based on the offset factor as shown below.
This is what every other plot other than #1 get flagged for.
%If the peak is found to not equal the same x-axis array location than flag AND if the second peak is further to the right than max peak from plot #1 then get flagged
if offset ~= time(find(y == max(y))) && time(find(y == max(y))) - offset > 0
offsetFactor = time(find(y == max(y))) - offset;
time = time - offsetFactor;
elseif offset ~= time(find(y == max(y))) && time(find(y == max(y))) - offset < 0
offsetFactor = time(find(y == max(y))) - offset;
time = time + offsetFactor;
end
Sadly this did not work.
In simple terms, all I want to do is sync all the rest of my plots up to where the max peak for the plot #1 is... if that makes any sense.
I've attached a picture below to show what I want.

9 Comments
Adam Danz
on 15 Nov 2019
Try a cross correlation between the two signals in order to measure the lag of sig 2 relative to sig 1.
Nom
on 15 Nov 2019
Nom
on 15 Nov 2019
Hmmmm when I check for the required Matlab products for xcorr, it doesn't list the Sig Proc Toolbox. It just lists Matlab.
I see.... in the 2017b documentation (your version), xcorr() is only provided in the Sig Proc TB. But in the current documentation, xcorr() is provided with the basic Matlab package.
Nom
on 15 Nov 2019
You could set up a loop that circularly shifts signal #2 one unit at a time and computes the correlation between both signals on each iteration. Then you just need to locate the max correlation. It's index value is the lag between the two signals that maximizes the correlation.
Look into these functions:
- circshift()
- corr()
- corrcoef()
Steven Lord
on 15 Nov 2019
Adam Danz
on 15 Nov 2019
Thanks, Steven.
Just wanted to add that they were provided in the Signal Processing Toolbox prior to that (since before 2006).
Accepted Answer
More Answers (0)
Categories
Find more on Correlation and Convolution in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!