How to automatically shift a graph back
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nom
el 15 de Nov. de 2019
Comentada: Adam Danz
el 16 de Nov. de 2019
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/248460/image.png)
9 comentarios
Adam Danz
el 15 de Nov. de 2019
Thanks, Steven.
Just wanted to add that they were provided in the Signal Processing Toolbox prior to that (since before 2006).
Respuesta aceptada
Adam Danz
el 15 de Nov. de 2019
Editada: Adam Danz
el 15 de Nov. de 2019
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')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/248472/image.png)
4 comentarios
Adam Danz
el 16 de Nov. de 2019
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)
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!