MATLAB: How to draw the corresponding exponential decay curve to fit a damped sine wave

curve ftiingdamped

I want to draw the exponential curve that fits the peaks of the damped signal. I have done this very crudely by plotting the x and y values of the peaks on the same figure as the damped signal, but is there a better way to do this, without having to search values manually on the graph. I am relatively new to matlab so any help would be appreciated. Thanks!

Best Answer

You want to fit an exponential envelope function to the curve, essentially a least upper bound function. So the exponential mode would be something of the form...
Y_e = a + b*exp(-c*x)
If you know that the lower asymptote is exactly 1, then your model would be
Y_e = 1 + b*exp(-x)
We might estimate those coefficients by a simple log transformation, or we could use a nonlinear estimation. I'll show the log transformation. First, I'll create a damped sine wave curve as an example.
x = 0:100;
y = 1 + sin(x - pi/3).*exp(-0.2*x);
First, use only those points that fall above 1. So assuming vectors of points x and y...
ind = (y > 1);
x1 = x(ind);
% and transform y by a log transformation
y1 = log(y(ind) - 1);
A = [ones(numel(x1),1),x1(:)];
% estimate the model coeffs = lsqlin(A,y1(:),-A,-y1(:),[],[],[],[],[],optimset('algorithm','active-set','display','off'));
coeffs =
-0.00091842
-0.19999
Don't forget that when we logged the model, we transformed the coefficient b. We need to exponentiate
b = exp(coeffs(1))
b =
0.99908
c = coeffs(2);
% now plot the data and the curve
plot(x,y,'ro',x,1 + b*exp(c*x),'b-)
I used lsqlin from the optimization toolbox to do the fit. The fundamental ideas in this solution were:
- Transform the problem to linearity.
- Solve the linear least squares problem, such that all residuals had the proper sign.
If you don't know the lower asymptote for the curve, then you would need to use a nonlinear optimization tool for the fit. The constraints would be such that the necessary optimizer would be fmincon.
Related Question