how plot the best fit curve

Hi all,
the attached fig shows the daily mean time series data. how can plot the best fit curve? thank you.

 Respuesta aceptada

Star Strider
Star Strider el 12 de Dic. de 2016

0 votos

It depends on the result you want.
If you are fitting a model, you have to specify the model, and then do the linear or nonlinear regression. Plot the model with the estimated parameters with your data.
Another option is the Savitzky-Golay filter. This is essentially a low-pass filter that can smooth the noise in your signal.
Otherwise, I would do a Fourier transform, find the most prominent frequency (or frequencies, depending on the information you want from your data), then use the Signal Processing Toolbox designfilt function to create a filter to pass your frequencies-of-interest. Then, plot the filtered signal with your data.
Use the hold function to plot more than one set of data on the same axes.

10 comentarios

Lilya
Lilya el 12 de Dic. de 2016
it is a time series changes with days, so I think I didn't need to find the regression. the other solution that you suggest is filtring by Savitzky- Golay. could you please help me with it. I just want to extract the best value from this time series to plot and removing the small versions. thank you.
Star Strider
Star Strider el 12 de Dic. de 2016
My pleasure.
It is not possible to help you with the sgolayfilt function without having your data to work with. You need to experiment with it to get the result you want.
Lilya
Lilya el 12 de Dic. de 2016
can I put it here? thank you in advance
Star Strider
Star Strider el 12 de Dic. de 2016
My pleasure.
Yes.
Attach it to your next Comment.
Lilya
Lilya el 12 de Dic. de 2016
here we are
Star Strider
Star Strider el 12 de Dic. de 2016
I’m not quite certain what result you want. Here are three different results for different orders and window lengths for the sgolayfilt function. Experiment to get the result you want.
I am not posting the plots, since they are for demonstration purposes only to help you decide the appropriate parameters. I decreased the orders and increased the window lengths between the plots, but there is no suggested ‘pattern’ for these. Choose the set that does what you want.
Also, they don’t seem like temperatures but air pressure in millibars.
The Code
D = load('Lina Eyouni pp.mat');
p = D.p; % Data
t = 1:length(p); % Time
pf_1 = sgolayfilt(p, 7, 25);
figure(1)
plot(t, p, '-.')
hold on
plot(t, pf_1, '-r', 'LineWidth',1)
hold off
grid
xlabel('Days')
ylabel('Pressuure (mB)')
text(155, 1011, 'Order 7, Window 25')
pf_2 = sgolayfilt(p, 5, 45);
figure(2)
plot(t, p, '-.')
hold on
plot(t, pf_2, '-r', 'LineWidth',1)
hold off
grid
xlabel('Days')
ylabel('Pressuure (mB)')
text(155, 1011, 'Order 5, Window 45')
pf_3 = sgolayfilt(p, 3, 75);
figure(3)
plot(t, p, '-.')
hold on
plot(t, pf_3, '-r', 'LineWidth',1)
hold off
grid
xlabel('Days')
ylabel('Pressuure (mB)')
text(155, 1011, 'Order 3, Window 75')
Lilya
Lilya el 13 de Dic. de 2016
Editada: Lilya el 13 de Dic. de 2016
Thank you very much, I appreciate your help. Excuse me, I have a small question, the order number that you choose is the polynomial degree. How can this order affect on the best fit accuracy? I mean when I see fig 3 (for order 3) is much better than the two above. the other point is the window how did you choose it? your comment will help me.
once again thank you for your kind help.
Star Strider
Star Strider el 13 de Dic. de 2016
As always, my pleasure.
The degree of the polynomial affects the ‘detail’ to which it approximates your data. A first-degree polynomial is a simple linear fit: y=b(1)*x+b(2), the degree being one less than the number of parameters the polynomial estimates. The higher the polynomial degree, the more closely the polynomial fits the data. Lower degrees have the effect of ‘smoothing’ the fit, acting as a sort of lowpass filter. The highest possible degree is one less than the number of data points to be fit, although a polynomial degree higher than about 7 does not add any significant information. Choosing the polynomial degree (as with most other aspects of signal processing) is a matter of experimenting to determine what works best.
To see the effect of polynomial degree, the easiest way is to experiment with the core MATLAB functions polyfit and polyval with random data (the third argument to polyfit is the polynomial degree):
N = 25;
x = 1:N;
y = sin(2*pi*x/N) + 0.4*randn(1, N);
p1 = polyfit(x, y, 1);
y1 = polyval(p1,x);
p2 = polyfit(x, y, 3);
y2 = polyval(p2,x);
p3 = polyfit(x, y, 9);
y3 = polyval(p3,x);
figure(1)
subplot(3,1,1)
plot(x,y,'pg', x,y1,'-r')
grid
subplot(3,1,2)
plot(x,y,'pg', x,y2,'-r')
grid
subplot(3,1,3)
plot(x,y,'pg', x,y3,'-r')
grid
The 3rd-degree polynomial approximates the sine curve. Note that the 9th-degree polynomial fits the data more closely, but does not at all approximate the sine curve (and also throws an error).
Lilya
Lilya el 14 de Dic. de 2016
very well done! You are the best, Thank you as you deserve for this ongoing help.
Star Strider
Star Strider el 14 de Dic. de 2016
As always, my pleasure!
Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 12 de Dic. de 2016

Comentada:

el 14 de Dic. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by