# SINFIT4

## Purpose:

Fits *y*(*x*) =* A *cos(*2πfx *+* B*) +* C* by iterative least squares.

## Syntax:

SINFIT4(series, freq, niter, tol)

series |
- |
A series, the input sinusoid |

freq |
- |
A real, the starting frequency for the iteration
process. Defaults to -1, estimate the frequency. |

niter |
- |
Optional. An integer, the maximum number
of least squares iterations if no convergence. Defaults to 30. |

tol |
- |
Optional. A real,
the relative frequency convergence tolerance. Defaults to 1e-6. |

## Returns:

A series, the fitted sine curve.

**(fit, coef) = SINFIT4(series)**
returns the fit and coefficients.

**(fit, coef, rmserr) = SINFIT4(series)**
returns the fit, coefficients and RMS error.

## Example:

W1: 5 + 3 * gsin(1000, .001, 4, pi) + gnorm(1000, .001)

W2: sinfit4(w1);overp(w1, lred)

Overplots the original data with the fitted sinusoid.

## Example:

(fit, coef) = sinfit4(w1)

fit
is the same as W2.

coef == {2.996476,
4.009087, 1.549703, 4.999965}} or similar such that:

A == coef[1]

f == coef[2]

B == coef[3]

C == coef[4]

## Example:

(fit, coef, rmserr) = sinfit4(w1, 4)

fit
contains the fitted data.

coef == {3.0012,
4.0000, 1.5706, 5.000} or similar.

rmserr ==
0.998756

## Remarks:

SINFIT4 uses the iterative least squares method to fit a sinusoid with
a starting input frequency as per the IEEE 1241 Standard such that:

If the input frequency is unspecified, the frequency is estimated by
using SINFIT3. SINFIT3 is also used as the starting
point for the iterative least squares fit. Given
the specified or estimated starting frequency, the frequency is adjusted
to minimize the least squares error to the tolerance value.

The frequency coefficient, *F*
= coef[2] is in Hertz and *θ,
*the phase term, *B* = coef[3]
is in radians.

The starting frequency, if specified, may differ from the resulting
fitted frequency. See SINFIT3 to perform a fit
that preserves a known frequency. SINFIT3 is
an implementation of the 3 term sinusoid fit as outlined in the IEEE 1241
Standard.

## See Also:

EFFBIT

LINFIT

LSINFIT

SINFIT

SINFIT3

## References:

IEEE Std 1241-2010 Annex B.2 *"An
algorithm for four-parameter least-squares fit
to sine-wave data"*