How to fit a curve to a damped sine wave in matlab

隐身守侯 提交于 2019-12-03 21:51:14

The damped sin function can be created using the following code:

f=f*2*pi;
t=0:.001:1;
y=A*sin(f*t + phi).*exp(-a*t);

plot(t,y);
axis([0 1 -2.2 2.2]);

Now you can use "cftool" from matlab and load your data then set the equation type to custom and enter the formula of the damped sin function. Here you can see what I found so far...

I think the distribution of the data makes it hard for the fitting tool to do a good fit. It may need more data or more distributed data. In addition, there are other tools and methods as well, for instance check this for now: docstoc.com/docs/74524947/Mathcad-Damped-sine-fit-mcd

For all these methods that actually trying to search and find a local optimum (for each fitting parameters), the most important thing is the initial condition. I believe if you choose a good initial condition (initial guess), the fitting tool works well.

Good Luck

I wouldn't use the curve fitting toolbox for this, I'd use a curve-fitting function, e.g. lsqcurvefit. Here is an example taken from something I did a while back:

% Define curve model functions
expsin = @(a, f, phi, tau, t)a * sin(omega * t + phi) .* exp(-tau * t);
lsqexpsin = @(p, t)expsin(p(1), p(2), p(3), p(4), t);

% Setup data params
a = 1;          % gain
f = 10;         % frequency
phi = pi/2;     % phase angle
tau = 0.9252523;% time constant

fs = 100;   % sample rate
N = fs;     % length
SNR = 10;   % signal to noise ratio

% Generate time vector
dt = 1/fs; 
t = (0:N-1)*dt; 

omega = 2 * pi * f; % angular freq
noiseGain = 10^(-SNR/20); % gain for given SNR

% Generate dummy data: decaying sinusoid plus noise
x = expsin(a, omega, phi, tau, t);
noise = noiseGain * rand(size(x));
noise = noise - mean(noise);
x = x + noise;

close all; figure; hold on;
plot(t, x, 'k-', 'LineWidth', 2);

% Count zero crossings to find frequency
zCross = find(x(1:end-1) .* x(2:end) < 0);
T = mean(diff(zCross) * dt) * 2;
fEstimate = 1 / T;
omegaEstimate = 2 * pi * fEstimate;

% Fit model to data
init = [0.5, omegaEstimate, 0, 0.5];
[newparams, err] = lsqcurvefit(lsqexpsin, init, t, x);

plot(t, lsqexpsin(newparams, t))

Here some data with known parameters is generated, and some random noise added; the data is plotted. The parameters [a, phi, tau] are estimated from the data and a curve with the estimated parameters plotted on top.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!