How can I find the peaks of a cfit object?
19 次查看(过去 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.
0 个评论
采纳的回答
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 个评论
更多回答(2 个)
Montgomery
2014-4-15
编辑:Montgomery
2014-4-15
1 个评论
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);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!