Sine fitting

Determine parameters of a noisy sine function
Updated 15 Nov 2023

View License

sineFit is a function to detect the parameters of a noisy sine curve, even less than one period long.
It requires only x and y values and no additional parameters as input.
No toolbox required
It is tested with R2016a and R2020a.
The mean calculation time is on my PC 13 ms with a maximum of 2400 ms.
optional: plot graphics if ommited. Do not plot if 0
x and y values, y = offs + amp * sin(2pi * f * x + phi) + noise
x and y may be row or column vectors
x may be not equidistant
SineParams(1):offset (offs)
SineParams(2): amplitude (amp)
SineParams(3): frequency (f)
SineParams(4): phaseshift (phi)
SineParams(5): MSE , if negative then SineParams are from FFT
This is a brief and not exact description of the program flow.
  • Resample x, y if samples are not equidistant
  • Estimate the offset by the mean of all y values.
  • Build the FFT with heavy zero padding.
  • Take the frequency, amplitude and phase of the largest FFT peak.If the frequency is at the Nyquist limit or the period is less than one, add extra frequencies for evaluation. Add also frequencies that have a peak value larger than 0.8 of the max. peak.
  • Take those values as initial values for the regressions.
  • Exclude results above Nyquist frequency.
  • Take the resulting MSE as rating.
  • Depending on the number of samples and the MSE, set a limit for accepted amplitude in relation to the FFT amplitude.
  • If the amplitude from regression is higher than the accepted amplitude, take the FFT parameters.
A demonstration GUI is included.
A second version, sinFit2, that has the confidence intervals as output, is included. This version requires the 'curve fitting toolbox'. The results are in nearly all cases the same as with the regular sinFit. But the process time is 10 times higher.
For more information read the enclosed pdf.

Cite As

Peter Seibold (2024). Sine fitting (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2016a
Compatible with R2020a and later releases
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!


Version Published Release Notes

Fixed a bug in the demonstration GUI


A version with output of confidence intervals, sinFit2 is included. Requires a toolbox.


Works also with non equidistant samples


Some changes in sineFitDemo in respect to 'run xy.mat'


Minor changes


No toolbox required anymore


Simplified code, faster processing, improved to work down to 0.1 periods, some more correct detections.

Image changed


Totally new approach with Fourier transformation results as initial parameters for fitting function.

Description corrected