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
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
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spectral Estimation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!