How can I find the peaks of a cfit object?
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Matt Tearle
le 15 Avr 2014
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 commentaire
Plus de réponses (2)
Montgomery
le 15 Avr 2014
Modifié(e) : Montgomery
le 15 Avr 2014
1 commentaire
Matt Tearle
le 18 Avr 2014
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);
Voir également
Catégories
En savoir plus sur Fit Postprocessing dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!