Function Repository Resource:

TrigFit

Source Notebook

Construct a least-squares trigonometric fit to data

Contributed by: Jan Mangaldan

ResourceFunction["TrigFit"][data,n,x]

gives the least‐squares trigonometric fit to data up to cos(nx) and sin(nx), with fundamental period 2π.

ResourceFunction["TrigFit"][data,n,{x,L}]

gives the least‐squares trigonometric fit to data up to cos(2πnx/L) and sin(2πnx/L), with fundamental period L.

ResourceFunction["TrigFit"][data,n,{x,x0,x1}]

gives the least‐squares trigonometric fit to data up to cos(2πn(x-x0)/(x1-x0)) and sin(2πn(x-x0)/(x1-x0)), with fundamental period x1-x0.

Details

ResourceFunction["TrigFit"] assumes that data comes from equally spaced sample points, and that it covers exactly one period of a periodic function.
data is assumed to have been sampled with spacing period/n, where period is the period of the function and n is the number of elements in data.
If data is a matrix, ResourceFunction["TrigFit"] is applied to each column.
ResourceFunction["TrigFit"] uses Fourier to quickly find a least‐squares trigonometric fit to the data.

Examples

Basic Examples (3) 

Generate data corresponding to one period of a periodic function:

In[1]:=
data = Table[
   Sin[x]/(5/4 - Cos[x]) + Cos[x]/(5/4 + Sin[x]), {x, 0, 2 \[Pi] - 2 \[Pi]/11, 2 \[Pi]/11}];

Construct a low-order trigonometric fit:

In[2]:=
tf[x_] = ResourceFunction["TrigFit"][data, 4, x]
Out[2]=

Show the fit along with the original data:

In[3]:=
Show[Plot[tf[x], {x, 0, 2 \[Pi]}], ListPlot[data, DataRange -> {0, 2 \[Pi] - 2 \[Pi]/11}, PlotStyle -> ColorData[97, 4]]]
Out[3]=

Scope (2) 

Generate samples from a periodic function:

In[4]:=
dat = Table[{1, Cos[x], Cos[2 x], Sin[x], Sin[2 x]}, {x, 0, 2 \[Pi] - 2 \[Pi]/7, 2 \[Pi]/7}] . RandomVariate[NormalDistribution[], 5]
Out[4]=

Trigonometric fit over [0,2π]:

In[5]:=
ResourceFunction["TrigFit"][dat, 1, x]
Out[5]=

Trigonometric fit over [0,]:

In[6]:=
ResourceFunction["TrigFit"][dat, 1, {x, 0, \[ScriptCapitalL]}]
Out[6]=

Trigonometric fit over [x0,x1]:

In[7]:=
ResourceFunction[
 "TrigFit"][dat, 1, {x, Subscript[x, 0], Subscript[x, 1]}]
Out[7]=

Sample points over a closed curve:

In[8]:=
dat = Table[
  N[{JacobiP[5, 1, 2, Cos[t]], JacobiP[5, 1, 2, Sin[t]]}], {t, 0, 2 \[Pi] - \[Pi]/25, \[Pi]/25}]
Out[8]=

Plot the trigonometric fit along with the original data:

In[9]:=
ParametricPlot[
 ResourceFunction["TrigFit"][dat, 3, {t, 2 \[Pi]}] // Evaluate, {t, 0,
   2 \[Pi]}, Epilog -> {Directive[AbsolutePointSize[4], ColorData[97, 2]], Point[dat]}]
Out[9]=

Properties and Relations (3) 

Generate samples from a periodic function:

In[10]:=
dat = Table[{1, Cos[x], Cos[2 x], Cos[3 x], Sin[x], Sin[2 x], Sin[3 x]}, {x, 0, 2 \[Pi] - 2 \[Pi]/11, 2 \[Pi]/11}] . RandomVariate[NormalDistribution[], 7]
Out[10]=

Use TrigFit to construct the trigonometric fit:

In[11]:=
ResourceFunction["TrigFit"][dat, 3, {x, 2 \[Pi]}]
Out[11]=

Use Fit to construct the trigonometric fit:

In[12]:=
Fit[Transpose[{Table[x, {x, 0, 2 \[Pi] - 2 \[Pi]/11, 2 \[Pi]/11}], dat}], Prepend[Flatten[Table[{Cos[k x], Sin[k x]}, {k, 3}]], 1], x]
Out[12]=

Neat Examples (3) 

Sample points on a knot:

In[13]:=
knotf8 = Table[
   KnotData["FigureEight", "SpaceCurve"][x], {x, 0, 2 \[Pi] - \[Pi]/32, \[Pi]/32}];

Construct a low-order trigonometric fit from the data:

In[14]:=
f8trig[t_] = ResourceFunction["TrigFit"][knotf8, 6, t]
Out[14]=

Plot the trigonometric fit of the knot, and compare with the result of KnotData:

In[15]:=
{ParametricPlot3D[f8trig[t], {t, 0, 2 \[Pi]}, Axes -> False, Boxed -> False, Mesh -> None, PlotRange -> All, PlotStyle -> Tube[0.2, Method -> {"Caps" -> False}], ViewPoint -> {0, 0.01, 5}], KnotData["FigureEight"]} // GraphicsRow
Out[15]=

Version History

  • 1.0.0 – 25 January 2021

Related Resources

Author Notes

This is a re-implementation of the function TrigFit from the old add-on package NumericalMath`TrigFit`.

License Information