Function Repository Resource:

# TrigFit

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]:=

Construct a low-order trigonometric fit:

 In[2]:=
 Out[2]=

Show the fit along with the original data:

 In[3]:=
 Out[3]=

### Scope (2)

Generate samples from a periodic function:

 In[4]:=
 Out[4]=

Trigonometric fit over [0,2π]:

 In[5]:=
 Out[5]=

Trigonometric fit over [0,]:

 In[6]:=
 Out[6]=

Trigonometric fit over [x0,x1]:

 In[7]:=
 Out[7]=

Sample points over a closed curve:

 In[8]:=
 Out[8]=

Plot the trigonometric fit along with the original data:

 In[9]:=
 Out[9]=

### Properties and Relations (3)

Generate samples from a periodic function:

 In[10]:=
 Out[10]=

Use TrigFit to construct the trigonometric fit:

 In[11]:=
 Out[11]=

Use Fit to construct the trigonometric fit:

 In[12]:=
 Out[12]=

### Neat Examples (3)

Sample points on a knot:

 In[13]:=

Construct a low-order trigonometric fit from the data:

 In[14]:=
 Out[14]=

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

 In[15]:=
 Out[15]=

## Version History

• 1.0.0 – 25 January 2021

## Author Notes

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