How to automatically shift a graph back

8 ビュー (過去 30 日間)
Nom
Nom 2019 年 11 月 15 日
コメント済み: Adam Danz 2019 年 11 月 16 日
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 件のコメント
Steven Lord
Steven Lord 2019 年 11 月 15 日
xcorr and xcov are included in MATLAB as of release R2019a.
Adam Danz
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).

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2019 年 11 月 15 日
編集済み: Adam Danz 2019 年 11 月 15 日
If you don't have access to xcorr() you can easily set up a loop the circularly shifts signal-2 one unit at a time and computes the correlation between signal-1 and shifted sig-2 on each iteration. Then you just have to find the index of the max correlation which determines how many units you need to counter-shift sig2.
This uses corr() which requires the Stats and Machine Learning toolbox.
Here's a demo
sig1 = sin(0:pi/8:6*pi).*linspace(.2,1.5,49);
sig2 = [zeros(1,8), sig1(1:end-8)];
clf()
subplot(2,1,1)
plot(sig1, 'k-')
hold on
plot(sig2, 'r--')
title('Raw data')
n = numel(sig2);
c = zeros(1,n);
for i = 1:n
temp = circshift(sig2,i,2); %for row-vector data
c(i) = corr(sig1(:),temp(:));
end
% Find max corr index
[~, maxIdx] = max(c);
% Shift sig2 by -maxIdx units
sig2Shift = circshift(sig2, -maxIdx, 2);
% plot
subplot(2,1,2)
plot(sig1, 'k-')
hold on
plot(sig2Shift,'r--')
title('sig2 shifted')
  4 件のコメント
Nom
Nom 2019 年 11 月 15 日
編集済み: Nom 2019 年 11 月 15 日
EDIT: Sorry for the stupid question, I just substracted the x-value of the peak from all my x-axis values so in this case
newX = x - 10
On this same note of using circshift.
Is there a way I can shift the first peak to be the 0 on the axis?
Let's say the location of the start of the peak is 10 on the x-axis,
I wrote the code below to shift the data 10 units to the left so that the start of the peak can be 0.
test123 = circshift(x',-10)';
However this does not shift the x-axis and then leaves a horizontal line across my plot because the data is not organized
Adam Danz
Adam Danz 2019 年 11 月 16 日
You could used [pks,locs] = findpeaks(data) to get locs, the location of each peak. That algorithm may require some experimentation with the optional input parameters to optimally find your peaks. Once you have the location of the first peak, let's say it's 11, you can circularly shift the x and y values by -11 so that the 1st peak starts at 0. It would look something like this
[pks,locs] = findpeaks(data);
newX = cirshift(x, -locs(1));
newY = cirshift(y, -locs(1));
plot(newX, newY)
Note that this will not trim any of your data. It will merely shift the entire seqence of values, circularly. If there is a flat line in your data, it will remain in your data at a different position.
If you'd like to get rid of all data prior to the first peak,
[pks,locs] = findpeaks(data);
x(1:locs(1)) = [];
y(1:locs(1)) = [];
plot(x, y)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by