Reference

Types

SinusoidalRegressions.SinusoidPMethod
SinusoidP{T}(f, DC, Q, I)

Construct a SinusoidP{T} with the given parameters, promoting to a common type T if necessary.

Example

julia> SinusoidP(10, 1, -0.5, 1.2)
Sinusoidal parameters SinusoidP{Float64}:
  Frequency (Hz)      : 10.0
  DC                  : 1.0
  Sine amplitude (Q)  : -0.5
  Cosine amplitude (I): 1.2
SinusoidalRegressions.SinusoidPMethod
SinusoidP{T}(; f, DC, Q, I)

Construct a SinusoidP{T} specifying each parameter by name, promoting to a common type T if necessary.

Example

julia> SinusoidP(DC = 1, Q = -0.5, I = 1.2, f = 10)
Sinusoidal parameters SinusoidP{Float64}:
  Frequency (Hz)      : 10.0
  DC                  : 1.0
  Sine amplitude (Q)  : -0.5
  Cosine amplitude (I): 1.2
SinusoidalRegressions.SinusoidPMethod
(P::SinusoidP)(t)

Evaluate the sinusoidal function specified by the parameters P at the values given by t, which may be a scalar or a collection.

Example

julia> P = SinusoidP(DC = 1, Q = -0.5, I = 1.2, f = 10)
julia> t = range(0, 0.7, length = 5)
julia> P(t)
5-element Vector{Float64}:
  2.2
  1.4999999999999996
 -0.2000000000000004
  0.49999999999999944
  2.200000000000001
SinusoidalRegressions.MixedLinearSinusoidPMethod
MixedLinearSinusoidP{T}(f, DC, Q, I, m)

Construct a MixedLinearSinusoidP{T} with the given parameters, promoting to a common type T if necessary.

Example

julia> MixedLinearSinusoidP(10, 0, -1.2, 0.4, 2.1)
Mixed Linear-Sinusoidal parameters MixedLinearSinusoidP{Float64}:
  Frequency (Hz)       : 10.0
  DC                   : 0.0
  Sine amplitude (Q)   : -1.2
  Cosine amplitude (I) : 0.4
  Linear term (m)      : 2.1
SinusoidalRegressions.MixedLinearSinusoidPMethod
MixedLinearSinusoidP(; f, DC, Q, I, m)

Construct a MixedLinearSinusoidP{T} specifying each parameter by name.

Example

julia> MixedLinearSinusoidP(f = 10, DC = 0, m = 2.1, Q= -1.2, I = 0.4)
Mixed Linear-Sinusoidal parameters MixedLinearSinusoidP{Float64}:
  Frequency (Hz)       : 10.0
  DC                   : 0.0
  Sine amplitude (Q)   : -1.2
  Cosine amplitude (I) : 0.4
  Linear term (m)      : 2.1
SinusoidalRegressions.MixedLinearSinusoidPMethod
(P::MixedLinearSinusoidP)(t)

Evaluate the mixed linear-sinusoidal function specified by the parameters P at the values given by t, which may be a scalar or a collection.

Example

julia> P = MixedLinearSinusoidP(f = 10, DC = 0, m = 2.1, Q= -1.2, I = 0.4)
julia> t = range(0, 0.7, length = 5)
julia> P(t)
5-element Vector{Float64}:
  0.4
  1.5674999999999997
  0.3349999999999989
 -0.09750000000000014
  1.870000000000002

IEEE 1057

Sinusoidal regressions specified by 1057-2017, IEEE Standard for Digitizing Waveform Recorders. These are the same algorithms specified in 1241-2010, IEEE Standard for Terminology and Test Methods for Analog-to-Digital Converters.

SinusoidalRegressions.ieee1057Function
ieee1057(X, Y, f::Real) :: SinusoidP

Calculate a three-parameter sinusoidal fit of the independent variables X and dependent variables Y, using the algorithm described by the IEEE 1057 Standard. Argument f is the exact frequency of the sinusoid, in hertz.

The data is fit to the model $f(x; DC, Q, I) = DC + Q\sin(2πfx) + I\cos(2πfx)$.

See also SinusoidP.

ieee1057(X, Y ; f = nothing, iterations = 6) :: SinusoidP

Calculate a four-parameter sinusoidal fit of the independent variables X and dependent variables Y, using the algorithm described by the IEEE 1057 Standard.

Argument f is an estimate of the frequency, in hertz. If not provided, then it is calculated using the integral equations method (see sinfit_j).

Argument iterations specifies how many iterations to run. The default value is 6, which is the value recommended by the standard.

Method of integral equations

Four types of sinusoidal regressions using the algorithms proposed by J. Jacquelin in "Régressions et Equations Intégrales". These algorithms are not iterative, and they do not require an initial estimate of the frequency or any other parameter. They may be used by themselves, or to calculate initial estimates for more-precise least-squares methods based on non-linear optimization (described below).

SinusoidalRegressions.sinfit_jFunction
sinfit_j(X, Y) :: SinusoidP

Perform a four-parameter sinusoidal fit of the independent variables X and dependent variables Y, using the method of integral equations described in J. Jacquelin, "Régressions et équations intégrales", 2014 (available at https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales)

The data is fit to the model $s(x; f, DC, Q, I) = DC + Q\sin(2πfx) + I\cos(2πfx)$. No initial guess as to the values of the parameters f, DC, Q, or I is required. The values in X must be sorted in ascending order.

See also SinusoidP.

SinusoidalRegressions.mixlinsinfit_jFunction
mixlinsinfit_j(X, Y) :: MixedLinearSinusoidP

Perform a mixed linear-sinusoidal fit of the independent variables X and dependent variables Y, using the method of integral equations.

The data is fit to the model $s(x; f, DC, Q, I, m) = DC + mx + Q\sin(2πfx) + I\sin(2πfx)$. No initial guess as to the values of the parameters f, DC, Q, I, or m is required. The values in X must be sorted in ascending order.

See also sinfit_j

Non-linear optimization

Wrappers for LsqFit.curve_fit(), which define the appropriate model, calculate initial parameter estimates if not provided by the user, and wrap the returned fit in the appropriate subtype of SinusoidalFunctionParameters.

SinusoidalRegressions.sinfitFunction
sinfit(X, Y, guess::SinusoidP = sinfit_j(X, Y) ; kwargs...)

Perform a four-parameter least-squares sinusoidal fit of the independent variables X and dependent variables Y, using the non-linear optimization solver from LsqFit.jl.

The data is fit to the model $s(x; f, DC, Q, I) = DC + Q\sin(2πfx) + I\cos(2πfx)$. The values in X must be sorted in ascending order.

The Levenberg-Marquardt algorithm used by LsqFit.jl requires an initial guess of the parameters f, DC, Q and I. If no initial guess is provided, then one is calculated using sinfit_j.

All keyword arguments provided are directly passed to LsqFit.curve_fit.

See also SinusoidP, sinfit_j, LsqFit.jl, curve_fit

sinfit(X, Y, f ; kwargs...)

Perform a three-parameter least-squares sinusoidal fit of the independent variables X and dependent variables Y, assuming a known frequency f, using the non-linear optimization solver from LsqFit.jl.

The data is fit to the model $s(x; DC, Q, I) = DC + Q\sin(2πfx) + I\cos(2πfx)$. The values in X must be sorted in ascending order.

The Levenberg-Marquardt algorithm used by LsqFit.jl requires an initial guess of the parameters, DC, Q and I. If no initial guess is provided, then one is calculated using the linear regression algorithm from IEEE 1057 (see ieee1057).

All keyword arguments provided are directly passed to LsqFit.curve_fit.

See also SinusoidP, ieee1057, curve_fit.

SinusoidalRegressions.mixlinsinfitFunction
mixlinsinfit(X, Y, guess::MixedLinearSinusoidP = mixlinsinfit_j(X, Y) ; kwargs...)

Perform a least-squares mixed linear-sinusoid fit of the independent variables X and dependent variables Y, using the non-linear optimization solver from LsqFit.jl.

The data is fit to the model $s(x, f, DC, Q, I, m) = DC + Q\sin(2πfx) + I\cos(2πfx) + mx$. The values in X must be sorted in axcending order.

The Levenberg-Marquardt algorithm used by LsqFit.jl requires an initial guess of the parameters, DC, Q I, and m. If no initial guess is provided, then one is calculated then one is calculated using mixlinsinfit_j.

All keyword arguments provided are directly passed to LsqFit.curve_fit.

See also MixedLinearSinusoidP, mixlinsinfit_j, curve_fit.

Error measurement

Functions to easily compare a calculated fit to actual data, using root mean-square errors, or mean absolute errors.

SinusoidalRegressions.rmseFunction
rmse(fit::T, exact::T, x) where {T <: SinusoidalFunctionParameters}

Calculate the root mean-square error between fit and exact sampled at collection x.

See also: mae

SinusoidalRegressions.maeFunction
mae(fit::T, exact::T, x) where {T <: SinusoidalFunctionParameters}

Calculate the mean absolute error between fit and exact sampled at collection x.

See also: rmse

Plotting

This package provides recipes for Plots.jl. This makes it very easy to plot data and the calculated fit.

There are two recipes included: plotting one fit, and plotting data and up to two fits.

Plotting a fit

plot(X, fit::T, ; fitlabel = "") where {T <: SinusoidalFunctionParameters}

Plot the model with parameters given by fit at the points specified in collection X. The optional keyword argument fitlabel controls the label (legend) of the plot.

Example
using SinusoidalRegressions
X = range(0, 1, length = 100)
Y = 2 .+ 3cos.(2*pi*5*X) .- 0.2sin.(2*pi*5*X) .+ 0.1*randn(100)
fit = ieee1057(X, Y, 5)
plot(X, fit, fitlabel = "example")

Plotting data and one or two different fits

plot(X, Y, fit::T ; exact :: Union{T, Nothing} = nothing,
                    samples    = 100,
                    fitlabel   = "fit",
                    datalabel  = "data",
                    exactlabel = "exact") where {T <: SinusoidalFunctionParameters}

Plot the data Y and the model with parameters given by fit evaluated at the points specified in the collection X. The following keyword arguments are supported (with default values in parenthesis):

  • exact::T = nothing: add one more model to the plot. This allows plotting the exact function, the data and the fit in a single plot.
  • samples::Int = 100: fit and exact are evaluated for range(first(X), last(X), length = samples).
  • fitlabel::String = "fit": the label (legend) for the fit plot.
  • datalabel::String = "data": the label for the data plot.
  • exactlabel::String = "exact": the label for the exact plot.
Example
X = range(0, 1, length = 20)
exact = SinusoidP(5, 2, -0.2, 3)
Y = exact.(X) .+ 0.3*randn(20)
fit = sinfit(X, Y)
plot(X, Y, fit, exact = exact,
     datalabel = "measurements",
     fitlabel = "NLS",
     exactlabel = "true")