How do I fit a damped sine wave to my data?

59 views (last 30 days)
How do I fit a damped sine wave to my data?
Hello,
I'm very new to matlab (joined a day ago) and am trying to fit a damped sine wave to my data from google sheets. I found out how to fit polynomials, etc. using the pre-installed program on Matlab; however, this pre-installed program doesn't have a damped sine wave of best fit. I've been trying to figure out how to fit my data with a damped sine wave for some time now but nothing has worked out. Attached to this question are two data sets, the (1,1) data set represents the x-axis data whereas the (1,2) data set represents the y-axis data. If someone could help me fit my data to a damped sine wave I would much appreciate it..
  4 Comments
Jan
Jan on 20 Apr 2021
Confusions and questions for clarifications belong to the nature of this forum.
Stephen23
Stephen23 on 30 May 2023
Original question by Edison Doko retrieved from Google Cache:
How do I fit a damped sine wave to my data?
Hello,
I'm very new to matlab (joined a day ago) and am trying to fit a damped sine wave to my data from google sheets. I found out how to fit polynomials, etc. using the pre-installed program on Matlab; however, this pre-installed program doesn't have a damped sine wave of best fit. I've been trying to figure out how to fit my data with a damped sine wave for some time now but nothing has worked out. Attached to this question are two data sets, the (1,1) data set represents the x-axis data whereas the (1,2) data set represents the y-axis data. If someone could help me fit my data to a damped sine wave I would much appreciate it.

Sign in to comment.

Accepted Answer

Jan
Jan on 20 Apr 2021
Edited: Jan on 20 Apr 2021
x = csvread('PH 1140 - 3R (1,1) - Sheet1.csv');
y = csvread('PH 1140 - 3R (1,2) - Sheet1.csv');
plot(x, y);
hold on;
This is the equation of a damped sine:
f = @(p) p(1) * sin(p(2) * x - p(3)) .* exp(-p(4) * x);
A fair start value for p found manually:
p0 = [0.17, 6, 1.2, 0.13];
h = plot(x, f(p0), 'r');
Now it matters, if you are wanted to write your own parameter estimation tool or if you can use Matlab's toolbox functions for this job. In the latter you can use fminsearch :
p0 = [0.17, 6, 1.2, 0.13];
p = fminsearch(@(p) norm(f(p) - y), p0)
% 0.1659, 5.935, 1.783, 0.1512
h2 = plot(x, f(p), 'r');
Not satisfying. My initial guess looked nicer. The final part does not oscillate around 0, so a vertical shift might solve the problem:
f2 = @(p) p(1) * sin(p(2) * x - p(3)) .* exp(-p(4) * x) + p(5);
p20 = [0.17, 6, 1.2, 0.13, -0.006];
% mean(y)
format long g
p2 = fminsearch(@(p) norm(f2(p) - y), p20);
% [0.165487, 5.934186, 1.777052, 0.150949, -0.005647]
Well, I'm still not satisfied. Modifying the frequency and the damping looks better:
p2_guess = [0.165487, 6.0, 1.777052, 0.130949, -0.005647]
Any ideas, what I'm doing wrong?
  3 Comments
Alex Sha
Alex Sha on 21 Apr 2021
Hi, Edison, if you don't mind the length, the fitting function below should be much better:
y=p1*sin(p2*x-p3)*exp(-p4*x)+p6*cos(p7*x-p8)*exp(p9*x)+p10;
Root of Mean Square Error (RMSE): 0.00297794529932042
Sum of Squared Residual: 0.00259837035428316
Correlation Coef. (R): 0.99744510981767
R-Square: 0.994896747099183
Parameter Best Estimate
---------- -------------
p1 0.0736813934626548
p2 6.10084592065804
p3 9.7061325271986
p4 0.0811845187962298
p6 0.216025844636208
p7 -5.86321132404066
p8 28.7124412480121
p9 -0.302318685948081
p10 -0.00587533278341315
Durva Naik
Durva Naik on 14 Oct 2021
Hi Alex, can you please share how to obtain the R2 value for the fitted curve?

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!