How to automatically shift a graph back
显示 更早的评论
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 个评论
Adam Danz
2019-11-15
Try a cross correlation between the two signals in order to measure the lag of sig 2 relative to sig 1.
Nom
2019-11-15
Nom
2019-11-15
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
2019-11-15
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
2019-11-15
Adam Danz
2019-11-15
Thanks, Steven.
Just wanted to add that they were provided in the Signal Processing Toolbox prior to that (since before 2006).
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Correlation and Convolution 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
