Date: Jan 22, 2013 5:08 AM Author: Torsten Subject: Re: Sine fit to data "Don" wrote in message <kdkcac$lfu$1@newscl01ah.mathworks.com>...

> I want to fit a sine function to my data, specifically this function;

> F = Ampl*SIN(phase+2*Pi*T/Period).

>

> I understand that I can use the below function to fit to my data, eliminating phase;

> F = A + B*SIN(2*Pi*T/P) + C*COS(2*Pi*T/P)

>

> where Pi = 3.14159...

> where T = a sequential number incrementing by 1 for each equally spaced data value.

> where P = the period of one cycle

> where A, B, C will be determined by the best fit algorithm.

>

> What I don't understand is how to make P a variable and;

> 1. Use only the last "P" number of data points [or maybe 1.5*P] in the fitting

> 2. Move the fitting process forward one data point at a time, generating new A,B,C.

> 3. allow P to be adjusted over time based upon past results

> 4. Best way to detrend the data with minimum lag introduced.

>

> I can somewhat do this now, using Excel and LINEST. In Excel, 3. and 4., above are crudely done by using for the trendline the endpoints of a best fit straight line for the last P data points, and then determining the last half cycle period from the last two crossing points of the data and the trendline.

>

> Questions;

> a. Can anyone see a way I can use the value "A", as it changes for each data point fitting, to develop a better trendline?

> b. Can anyone see a way to use the most recent data cycle min and max apexes, relative to the trendline, to update the period, P, twice every cycle, in addition to the trendline crossing points? I think there is too much noise or variance in the apexes of the data, so that the period would be changing too erratically.

>

> I am assuming tha Matlab software can be coded such that all data ranges can be dynamically adjusted to fit the period length, P, in a manner similar to [or easier than!] using the Excel INDIRECT function.

>

> What would be absolutely wonderful woould be that Matlab already has canned software to do what I want, including dynamically adjusting the period as the data is fitted. Also, the data mostly fits the simple condition that the ratio of the detrended amplitude [Ampl above] to Period P is near a constant, so that fact can be used, BUT the ratio must be allowed to changewithin a range of values.

> Ratio = Ampl/Period

>

> Thanks for any thoughts on this.

>

> Don

Use lsqcurvefit to fit your data to the function

F1(T) = Ampl*SIN(phase+2*Pi*T/Period)

with unknown fitting parameters Ampl, phase and Period.

Since the function

F2(T) = A + B*SIN(2*Pi*T/P) + C*COS(2*Pi*T/P)

has four unknown fitting parameters (A,B,C and P),

fitting to F2 instead of F1 cannot be equivalent.

Best wishes

Torsten.