How can I find the peaks of a cfit object?

조회 수: 7 (최근 30일)
Montgomery
Montgomery 2014년 4월 15일
댓글: Chenhan 2018년 12월 24일
Hi, I am trying to find the peaks of a fitted curve that I have got using the curve fitting toolbox. I have a fitting function that is doing this:
ft = fittype( 'smoothingspline' );
opts = fitoptions( 'Method', 'SmoothingSpline' );
opts.SmoothingParam = 1.5029271581647606E-4;
fitresult, gof] = fit( xData, yData, ft, opts );
And a main function that I'm trying to get working using something like this
[fit,gof] = findfit(Z2);
test = coeffvalues(fit);
peaks = findpeaks(test.coefs);
with an error: "Expected X to be a vector"
I think my question boils down to, how do I use something like findpeaks() on a cfit object that I get returned from my graphing function.
Thanks for all your help.
Edit: It might be worthwhile to know Z2 3x295(3 principle components per column), test.coefs 884 x 4. The purpose of using the smoothed curve is that prior to this the curve was too 'jagged' and so I would not be able to build something that easily finds the peaks & troughs of a signal. Apologies if this is simple, I'm a beginner.

채택된 답변

Matt Tearle
Matt Tearle 2014년 4월 15일
test.coefs is giving you the coefficients of the cubic splines that make up the fit. If I interpret your intention correctly, you're wanting the actual fitted curve. To do that, use feval:
yFitted = feval(fit,xData);
Now you can use findpeaks on yFitted (assuming that yData was a vector originally, so this is a 1-D fit).
xData = sort(rand(295,1));
yData = sin(6*pi*xData) + 0.1*randn(295,1);
ft = fittype( 'smoothingspline' );
opts = fitoptions( 'Method', 'SmoothingSpline' );
opts.SmoothingParam = 0.9999;
[fitresult, gof] = fit( xData, yData, ft, opts );
yfitted = feval(fitresult,xData);
plot(xData,yData,xData,yfitted)
[ypk,idx] = findpeaks(yfitted);
xpk = xData(idx);
hold on
plot(xpk,ypk,'o')
  댓글 수: 1
Chenhan
Chenhan 2018년 12월 24일
This is really helpful and saves me loads of time.
Thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Montgomery
Montgomery 2014년 4월 15일
편집: Montgomery 2014년 4월 15일
Hi Matt, that looks great but I'm still a little confused. Currently in main I have
[fit,gof] = findfit(Z2);
and findfit() is this:
function [fitresult, gof] = find(Z2)
[xData, yData] = prepareCurveData( [], Z2 );
ft = fittype( 'smoothingspline' );
opts = fitoptions( 'Method', 'SmoothingSpline' );
opts.Smoothing
[fitresult, gof] = fit( xData, yData, ft, opts );
Are you suggesting that I replace my current fitfunction with the one you gave above?
Also what does this do?
yData = sin(6*pi*xData) + 0.1*randn(295,1);
Sorry If I'm being slow, its 10.20PM in my time, Thanks for taking the time to respond
  댓글 수: 1
Matt Tearle
Matt Tearle 2014년 4월 18일
My code was just as an example to show how feval would work. The lines xData = ... and yData = ... are just making some example data (since I don't have access to yours).
Your function is mostly fine, except that you define xData in there as a local variable, and you need that for feval. So you may want to modify findfit to something like this:
function [fitresult, gof,yFitted] = find(Z2)
[xData, yData] = prepareCurveData( [], Z2 );
ft = fittype( 'smoothingspline' );
opts = fitoptions( 'Method', 'SmoothingSpline' );
opts.SmoothingParam = 1.5029271581647606E-4;
[fitresult, gof] = fit( xData, yData, ft, opts );
yFitted = feval(fitresult,xData);
Then in your calling code,
[fit,gof,yFit] = findfit(Z2);
peaks = findpeaks(yFit);

댓글을 달려면 로그인하십시오.


Montgomery
Montgomery 2014년 4월 19일
Got it working in the end, thanks

카테고리

Help CenterFile Exchange에서 Fit Postprocessing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by