Borrar filtros
Borrar filtros

How can I find the peaks of a cfit object?

13 visualizaciones (últimos 30 días)
Montgomery
Montgomery el 15 de Abr. de 2014
Comentada: Chenhan el 24 de Dic. de 2018
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.

Respuesta aceptada

Matt Tearle
Matt Tearle el 15 de Abr. de 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 comentario
Chenhan
Chenhan el 24 de Dic. de 2018
This is really helpful and saves me loads of time.
Thank you very much!

Iniciar sesión para comentar.

Más respuestas (2)

Montgomery
Montgomery el 15 de Abr. de 2014
Editada: Montgomery el 15 de Abr. de 2014
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 comentario
Matt Tearle
Matt Tearle el 18 de Abr. de 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);

Iniciar sesión para comentar.


Montgomery
Montgomery el 19 de Abr. de 2014
Got it working in the end, thanks

Categorías

Más información sobre Fit Postprocessing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by