How to find the best parameters to fit damped oscillations curves

18 views (last 30 days)
Louise on 22 Jan 2020
Commented: Bjorn Gustavsson on 23 Jan 2020
My goal is to fit the best these filtered data (attached) with the equation :
Here is my code (below) but this algorithm doesn't give a good fitting.
I don't know if it is a problem of parameters (for example, how to choose x0, ub and lb. I read some information on Matlab website but it doesn't help me)...
Thanks in advance for your help !
fun = @(x,t) x(1)*exp(-x(2)*t).*sin(2*pi*x(3)*t+x(4));
x0 = [2,1,1,1]
options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt');
lb = [0,0,0,-1]
ub = [2,100,100,1]
[x,resnorm] = lsqcurvefit(fun,x0,t,data_TRT,lb,ub,options)
legend('Experimental Data','Modeled Data')
title('Data and Fitted Curve')
xlabel('Temps (secondes)')
Damping_Nigg =x(2)
Frequence_Nigg =x(3)

Accepted Answer

Star Strider
Star Strider on 22 Jan 2020
Edited: Star Strider on 22 Jan 2020
D = load('Louise data_TRT_HELP.mat');
data = D.data_TRT;
t= linspace(0, numel(data), numel(data));
Fn = Fs/2; % Nyquist Frequency
L = size(t,2);
y = detrend(y); % Remove Linear Trend
fty = fft(y)/L;
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector
plot(Fv, abs(fty(Iv)))
Wp = 30/Fn; % Passband (Normalised)
Ws = 40/Fn; % Stopband (Normalised)
Rp = 1; % Passband Ripple
Rs = 50; % Stopband Ripple
[n,Wp] = ellipord(Wp,Ws,Rp,Rs); % Filter Order
[z,p,k] = ellip(n,Rp,Rs,Wp); % Transfer Function Coefficients
[sos,g] = zp2sos(z,p,k); % Second-Order-Section For Stability
freqz(sos, 2^14, Fs)
yf = filtfilt(sos,g,y);
yu = max(yf);
yl = min(yf);
yr = (yu-yl); % Range of ‘y’
yz = yf-yu+(yr/2);
zt = t(yz(:) .* circshift(yz(:),[1 0]) <= 0); % Find zero-crossings
per = 2*mean(diff(zt)); % Estimate period
ym = mean(y); % Estimate offset
fit = @(b,x) b(1) .* exp(b(2).*x) .* (sin(2*pi*x./(b(3)+b(4).*x) + 2*pi/b(5))) + b(6); % Objective Function to fit
fcn = @(b) sum((fit(b,t) - yf).^2); % Least-Squares cost function
s = fminsearch(fcn, [yr; -1; per; 0.01; -1; ym]) % Minimise Least-Squares
xp = linspace(min(t),max(t));
hold on
plot(t,yf,'g', 'LineWidth',2)
plot(xp,fit(s,xp), 'r', 'LineWidth',1.5)
hold off
title('Sample Data 2')
legend('Original Data', 'Lowpass-Filtered Data', 'Fitted Curve')
How to find the best parameters to fit damped oscillations curves - 2020 01 22.png
Bjorn Gustavsson
Bjorn Gustavsson on 23 Jan 2020
One thing to consider further is that after the low-pass filtering there might be an "over-filtering" of the initial variations (time < 20). If that's the case then it might be interesting to run the fitting to the original data using the best parameters from the fiting to the filtered data. The general idea is that the filtering reduces the risk of the fiting ending up in some local minima giving you a fit to high-frequency noise, but after the first fitting you would be close enough to the "proper" best-parameters that you now can fit to the original data.

Sign in to comment.

More Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 22 Jan 2020
If you plot your data you should be able to see that the zero-crossings are increasingly further apart.
That means that your mode-function will not be able to capture the decay nicely. You might have to introduce another parameter in your function to model this chirp.




Community Treasure Hunt

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

Start Hunting!

Translated by