Align two plots based on amplitude

15 views (last 30 days)
I have two plots.
Plot A and Plot B
Both have there own data sets, which need to be a little calibrated but aligning the ampliture, so all average peaks are very close.
This is my plot as an example
As you can see the bottom plot data neds to be shifted up a little. Is there a tool which can help get a good alignment in y axis?
  4 Comments
Image Analyst
Image Analyst on 15 Oct 2022
Edited: Image Analyst on 15 Oct 2022
Well the obvious solution would have been to just take a small subset of the data. Nonetheless, see what @Star Strider has done below with synthesized data and see if it's what you want. If not, then attach less than 5 MB subset of your data. It's what I would have done, which is to basically just assume one curve is just a linearly scaled and shifted version of the other curve.
Dharmesh Joshi
Dharmesh Joshi on 15 Oct 2022
Thanks i i will see if i can extract small amount of data ,

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 14 Oct 2022
Perhaps something like this —
t = linspace(0, 10, 1000);
y1 = sin(2*pi*t/2.5) + randn(size(t))*0.1;
y2 = 1.5*sin(2*pi*t/2.5) + randn(size(t))*0.1 + rand;
B = [y2(:) ones(size(y2(:)))] \ y1(:);
fprintf('\nMultiplier = %7.4f\nOffset = %7.4f\n',B)
Multiplier = 0.6536 Offset = -0.5288
y2s = [y2(:) ones(size(y2(:)))] * B;
figure
subplot(2,1,1)
plot(t, [y1; y2])
grid
xlabel('Time')
ylabel('Amplitudes')
title('Original')
subplot(2,1,2)
plot(t, [y1; y2s.'])
grid
xlabel('Time')
ylabel('Amplitudes')
title('Shifted & Scaled')
.
  2 Comments
Dharmesh Joshi
Dharmesh Joshi on 15 Oct 2022
Does both data sets need to be equal in size?
Star Strider
Star Strider on 15 Oct 2022
If you want to compare them, yes.
Changing them to have equal lengths can be done easily with the interp1 function. A linear interpolation decreasing the number of points in the either vector will not change the shape of that vector. It will only change the number of points defining it without creating new points where none previously existed. For that reason, it would be best to decrease the number of points in the longer vector and then do the compariison.
If you are doing signal processing with the vectors, use the Signal Processing Toolbox resample function instead of interp1 to change the number of pionts in the longer vector.
t1 = linspace(0, 10, 1000); % Original, 1000 Samples
y1 = sin(2*pi*t1/2.5) + randn(size(t1))*0.1;
t2 = linspace(0, 10, 250); % Original, 250 Samples
y2 = 1.5*sin(2*pi*t2/2.5) + randn(size(t2))*0.1 + rand;
figure
plot(t1, y1, '.')
hold on
plot(t2, y2, '.')
hold off
y1i = interp1(t1, y1, t2); % Resample 'y1' To 'y2' Points (250)
figure
plot(t2, y1i, '.') % Resampled To 250 Points
hold on
plot(t2, y2, '.') % Original At 250 Points
hold off
These vectors can now be compared, since they both have the same lengths and both are sampled at the same times.
This is simply an illustration using created vectors. It should work with your data.
.

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!