How to interpolate midpoints in a curve

6 次查看(过去 30 天)
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 个评论
Matt J
Matt J 2020-9-8
编辑:Matt J 2020-9-8
The Data1.mat file contains several data sets. Which is the one that doesn't work?
Proman
Proman 2020-9-9
R66 is the one that goes wrong. Already indicated in the code

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2020-9-9
编辑:Matt J 2020-9-9
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 个评论
Matt J
Matt J 2020-9-9
编辑:Matt J 2020-9-9
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
Proman
Proman 2020-9-9
What a miracle this filter is *_*
Thanks again for your care and extremely productive help so far my friend!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Spectral Estimation 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by