How can I find the peaks of a cfit object?

13 次查看(过去 30 天)
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')

更多回答(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