How to interpolate midpoints in a curve
6 views (last 30 days)
Show older comments
Hello to everyone and greetings
I have a curve that I intend to find its midpoints (points between each succesive minimum and maximum) and connect them to each other to plot an envelope-wise curve like the following (the green dotted-dashed curve is the envelope I try to realize):
the code I used to plot this is the trivial approach (to my opinion):
clc
clear
close all
load Data1
figure
plot(t,R66)
xlabel('time (ps)')
ylabel('\rho_{66}')
[pks,max_loc] = findpeaks(R66,t);
invert_R66 = max(R66(:)) - R66;
[pksm,min_loc] = findpeaks(invert_R66,t);
pksm = max(R66(:)) - pksm;
xmid = (max_loc(2 : end) + min_loc) ./ 2;
xmid_p = (min(R66) + max_loc(1)) / 2;
ymid = interp1(t,R66,xmid);
ymid_p = interp1(t,R66,xmid_p);
xmid = [xmid_p xmid'];
ymid = [ymid_p ymid'];
hold on
plot(xmid,ymid,'r')
axis([0 700 0 0.045])
legend('Population','Restoration')
It quite worked well for every curve I gave as an input but the following curve (which its data is attached as Data1.mat) is not responding well as you can see
I somehow believe that there must be a problem with interpolation but after all, I can not figure out where I am going wrong. It would be kind of you to help me at this.
Thanks in advance for your time given on my question.
2 Comments
Accepted Answer
Matt J
on 9 Sep 2020
Edited: Matt J
on 9 Sep 2020
The code is working fine. You think there is only one peak in the interval 200<=t<=300, but if you zoom in, you will see that there are three of them squished very close together. The same for the other intervals.
4 Comments
Matt J
on 9 Sep 2020
Edited: Matt J
on 9 Sep 2020
A Savitzky-Golay pre-filter seems to help a lot.
load('Data1.mat')
close all
figure
procfun(R66,t)
function procfun(R,t)
w=101; %window width
Rsg = sgolayfilt(R,2,w);
Rsg(1:w)=R(1:w);
D=diff(sign(diff([Rsg(1);Rsg])))~=0;
tp=t(D);
tq=(tp(1:end-1)+tp(2:end))/2;
Rq=interp1(t,R,tq);
Rp=interp1(t,R,tp);
pp=interp1(tq,Rq,'linear','pp');
hold on
plot(t,R,tp,Rp,'rx');
fplot(@(t)ppval(pp,t),[tq(1),t(end)])
xlabel('time (ps)')
ylabel('\rho_{66}')
hold off
end
More Answers (0)
See Also
Categories
Find more on Spectral Estimation 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!