Fitting a sinusoidal curve to a set of data points

41 views (last 30 days)
I have a few sets of data points that I need to fit a sine curve of the form A*sin(B*X+C)+D on each data set. I don't know A, B, C and D (which defer for each data set) but I have access to cftool. I wrote a function (by generating the code from cftool) which doesn't work well for some data sets and I believe it's because of the wrong "start point". I wonder how to determine the startpoint for each fitting?! and if this is not possible, is there any other advice to fit the sine curve and get the coefficients (or generated data)?!
Thanking in advance,
Maryam
  5 Comments
John D'Errico
John D'Errico on 16 Feb 2015
There is a tool on the FEX, called fminspleas . It does all of the hard work for you.
All you will need to do is define the function to be used. Here they would be something like...
funslist = {1, @(coef,xdata) cos(coef*xdata), @(coef,xdata) sin(coef*xdata)};
Fminspleas will return 4 coefficients, in two different return variables. Here, Bstart is the starting value for B. It is the ONLY one you need to provide.
[B,ILP] = fminspleas(funslist,Bstart,x,y);
D = ILP(1);
C = atan2(ILP(2),ILP(3));
A = ILP(2)/sin( C );
(I think I got those back-transformations right, but I wrote them down quickly, so worth checking.)
Image Analyst
Image Analyst on 16 Feb 2015
Maryam's "Flag" moved here to be a comment:
Thanks for the detailed answer!

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 15 Feb 2015
The easiest way to get ‘B*X’ is to use the fft function. You know ‘X’, so calculate to estimate ‘B’. (I leave the details of that to you.)
Find the mean of the curve to estimate ‘D’, and find the maximum of the absolute value of the function after subtracting the mean to estimate ‘A’.
This leaves ‘C’ without an initial estimate, but with three relatively close initial estimates for the others, ‘C’ should be relatively straightforward to estimate in your regression.
Good luck!
  1 Comment
Star Strider
Star Strider on 16 Feb 2015
@Maryam —
With respect to ‘C’, since it is a phase term and likely between 0 and 2*pi, an initial parameter estimate with any number in that range, for instance 1 as you suggested, would probably work.
As for an example, an initial parameter estimates vector, ‘B0’ here, would be (in ABCD order) and denoting your data as ‘y’:
B0 = [max(abs(y-mean(y))), |B|, 1, mean(y)];
I left ‘B’ as is because you would get that estimate from the fft of your data.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!