Tutorials › Linear Algebra › Fitting Sine and Cosine Models to Periodic Data

Applications in Networks, Economics and Data · Tutorial 952 of 1000

Fitting Sine and Cosine Models to Periodic Data

Learn to fit and interpret a sinusoidal model by turning its sine and cosine terms into columns of a design matrix.

Intermediate 12 min read

What You'll Learn

  • Construct design-matrix columns for an intercept, sine term and cosine term at a chosen frequency.
  • Solve the least-squares normal equations for periodic observations.
  • Check a fit by calculating fitted values and residuals.
  • Interpret sine and cosine coefficients through amplitude and phase.
  • Recognize when sampling times make fitted coefficients non-unique.
  • Use the positive period formula for positive or negative angular frequency.

From a Periodic Pattern to a Linear Model

Periodic measurements can be modelled using sine and cosine functions at a chosen angular frequency. The times at which measurements were taken determine the values of these functions, so the model becomes a matrix equation in its unknown coefficients. This is useful even when the observations are noisy: a least-squares fit selects coefficients whose predicted values are as close as possible to the observed data in the sum-of-squared-errors sense.

Suppose measurements \(y_1,\dots,y_m\) are recorded at times \(t_1,\dots,t_m\), and choose an angular frequency \(\omega\in\mathbb{R}\). We will fit an intercept together with a sine and cosine term. The intercept allows the average level of the data to differ from zero. The coefficients \(a\) and \(b\) determine the size and timing of the periodic variation.

Definition: The sine-and-cosine model at angular frequency \(\omega\) is \(y(t)=c+a\sin(\omega t)+b\cos(\omega t)\), where \(c,a,b\in\mathbb{R}\) are coefficients to be estimated. For observations at times \(t_1,\dots,t_m\), its design matrix and observation vector are \(\mathbf{A}=\begin{bmatrix}1&\sin(\omega t_1)&\cos(\omega t_1)\\ \vdots&\vdots&\vdots\\1&\sin(\omega t_m)&\cos(\omega t_m)\end{bmatrix}\) and \(\mathbf{y}=\begin{bmatrix}y_1\\ \vdots\\ y_m\end{bmatrix}\). Writing \(\mathbf{x}=(c,a,b)^{\top}\), the model predictions are \(\mathbf{A}\mathbf{x}\). A least-squares fit is a coefficient vector \(\mathbf{x}\) that minimizes \(\|\mathbf{y}-\mathbf{A}\mathbf{x}\|^2\).

The frequency is chosen before estimating \(c,a,b\). With \(\omega\ne0\), the sine and cosine components repeat after the positive period \(T=2\pi/|\omega|\). If \(\omega=0\), their values are constant in time, so the three columns in this model cannot in general be separately identified. Even for nonzero \(\omega\), the chosen observation times may fail to distinguish the columns; we will see this directly in an example.

Finding the Least-Squares Coefficients

For a matrix \(\mathbf{A}\) and data vector \(\mathbf{y}\), write the residual as \(\mathbf{r}=\mathbf{y}-\mathbf{A}\mathbf{x}\). The least-squares condition can be expressed as the normal equations \(\mathbf{A}^{\top}\mathbf{A}\mathbf{x}=\mathbf{A}^{\top}\mathbf{y}\). In this application, each row of \(\mathbf{A}\) gives the intercept, sine and cosine values at one observation time. Thus the coefficients are found by solving a linear system, even though the model is periodic as a function of time.

Theorem (Normal-equation criterion): Let \(\mathbf{A}\in\mathbb{R}^{m\times p}\), \(\mathbf{y}\in\mathbb{R}^m\), and \(\mathbf{x}\in\mathbb{R}^p\). Then \(\mathbf{x}\) is a least-squares coefficient vector if and only if \(\mathbf{A}^{\top}(\mathbf{y}-\mathbf{A}\mathbf{x})=\mathbf{0}\), equivalently \(\mathbf{A}^{\top}\mathbf{A}\mathbf{x}=\mathbf{A}^{\top}\mathbf{y}\).

Proof. Set \(\mathbf{r}=\mathbf{y}-\mathbf{A}\mathbf{x}\). First suppose that \(\mathbf{x}\) is a least-squares coefficient vector. Let \(\mathbf{a}_j\) be column \(j\) of \(\mathbf{A}\), and change only coefficient \(x_j\) by an amount \(s\in\mathbb{R}\). The resulting residual is \(\mathbf{r}-s\mathbf{a}_j\), so its squared norm is \(\|\mathbf{r}-s\mathbf{a}_j\|^2=\|\mathbf{r}\|^2-2s\mathbf{a}_j^{\top}\mathbf{r}+s^2\|\mathbf{a}_j\|^2\). This expression has a minimum at \(s=0\). If \(\mathbf{a}_j^{\top}\mathbf{r}\ne0\), choosing \(s\) with the same sign as \(\mathbf{a}_j^{\top}\mathbf{r}\) and sufficiently small nonzero magnitude makes the linear term negative with larger magnitude than the quadratic term; the expression then decreases. Therefore \(\mathbf{a}_j^{\top}\mathbf{r}=0\). This holds for every column, so \(\mathbf{A}^{\top}\mathbf{r}=\mathbf{0}\). Conversely, suppose \(\mathbf{A}^{\top}\mathbf{r}=\mathbf{0}\). For any change \(\mathbf{d}\in\mathbb{R}^p\), the new residual is \(\mathbf{r}-\mathbf{A}\mathbf{d}\), and \(\|\mathbf{r}-\mathbf{A}\mathbf{d}\|^2=\|\mathbf{r}\|^2-2\mathbf{r}^{\top}\mathbf{A}\mathbf{d}+\|\mathbf{A}\mathbf{d}\|^2=\|\mathbf{r}\|^2+\|\mathbf{A}\mathbf{d}\|^2\ge\|\mathbf{r}\|^2\). Thus \(\mathbf{x}\) minimizes the squared residual norm. Finally, expanding \(\mathbf{A}^{\top}(\mathbf{y}-\mathbf{A}\mathbf{x})=\mathbf{0}\) gives the stated normal equations. \(\square\)

Least-squares solutions exist for finite data; this is the projection result from the earlier material on residual geometry. The criterion above says exactly which coefficient vectors are solutions. A further useful distinction is whether the coefficients are unique. For a fixed design matrix, they are unique precisely when its columns are linearly independent.

Theorem (Uniqueness criterion): If a least-squares solution exists for \(\mathbf{A}\in\mathbb{R}^{m\times p}\) and \(\mathbf{y}\in\mathbb{R}^m\), it is unique if and only if the columns of \(\mathbf{A}\) are linearly independent.

Proof. Suppose the columns are linearly independent, so the only \(\mathbf{v}\in\mathbb{R}^p\) satisfying \(\mathbf{A}\mathbf{v}=\mathbf{0}\) is \(\mathbf{v}=\mathbf{0}\). If \(\mathbf{x}\) and \(\mathbf{z}\) are both least-squares solutions, the normal-equation criterion gives \(\mathbf{A}^{\top}(\mathbf{y}-\mathbf{A}\mathbf{x})=\mathbf{0}\). The residual comparison in that criterion, applied to the change \(\mathbf{d}=\mathbf{z}-\mathbf{x}\), shows that \(\|\mathbf{y}-\mathbf{A}\mathbf{z}\|^2=\|\mathbf{y}-\mathbf{A}\mathbf{x}\|^2+\|\mathbf{A}(\mathbf{z}-\mathbf{x})\|^2\). Both solutions have the minimum residual norm, so \(\|\mathbf{A}(\mathbf{z}-\mathbf{x})\|^2=0\). Hence \(\mathbf{A}(\mathbf{z}-\mathbf{x})=\mathbf{0}\), and independence implies \(\mathbf{z}-\mathbf{x}=\mathbf{0}\). Thus the solution is unique. Conversely, if the columns are dependent, there is a nonzero \(\mathbf{v}\) such that \(\mathbf{A}\mathbf{v}=\mathbf{0}\). For any least-squares solution \(\mathbf{x}\), the predictions from \(\mathbf{x}+\mathbf{v}\) equal those from \(\mathbf{x}\), since \(\mathbf{A}(\mathbf{x}+\mathbf{v})=\mathbf{A}\mathbf{x}\). Therefore \(\mathbf{x}+\mathbf{v}\) is another solution and is different from \(\mathbf{x}\). The solution cannot be unique. \(\square\)

Worked Fits at a Chosen Frequency

Worked Example: Exact Fit to Four Periodic Observations

Fit the model \(y(t)=c+a\sin(\frac{\pi}{2}t)+b\cos(\frac{\pi}{2}t)\) to observations \(1,5,3,-1\) at times \(t=0,1,2,3\).

At these times, the sine values are \(0,1,0,-1\), and the cosine values are \(1,0,-1,0\). Hence

$$ \mathbf{A}= \begin{bmatrix} 1&0&1\\ 1&1&0\\ 1&0&-1\\ 1&-1&0 \end{bmatrix}, \qquad \mathbf{y}= \begin{bmatrix}1\\5\\3\\-1\end{bmatrix}. $$

Using the normal equations, the column dot products give \(\mathbf{A}^{\top}\mathbf{A}=\begin{bmatrix}4&0&0\\0&2&0\\0&0&2\end{bmatrix}\). The data dot products are the sum \(1+5+3-1=8\), the sine-weighted sum \(5+1=6\), and the cosine-weighted sum \(1-3=-2\). Thus

$$ \begin{bmatrix}4&0&0\\0&2&0\\0&0&2\end{bmatrix} \begin{bmatrix}c\\a\\b\end{bmatrix} = \begin{bmatrix}8\\6\\-2\end{bmatrix}, \qquad (c,a,b)=\left(2,3,-1\right). $$

Check: Multiplying the design matrix by the coefficient vector gives \(\mathbf{A}\begin{bmatrix}2\\3\\-1\end{bmatrix}=\begin{bmatrix}1\\5\\3\\-1\end{bmatrix}=\mathbf{y}\). The residual is zero, so this is a least-squares fit with exact agreement at all four observation times.

Worked Example: A Noisy Fit and Its Residuals

Keep the same observation times and frequency, but now let the observations be \(1,6,2,0\). Find the least-squares model and check that its residual satisfies the normal-equation criterion.

The design matrix is unchanged, so \(\mathbf{A}^{\top}\mathbf{A}=\begin{bmatrix}4&0&0\\0&2&0\\0&0&2\end{bmatrix}\). For the new data vector \(\mathbf{y}=\begin{bmatrix}1&6&2&0\end{bmatrix}^{\top}\), the column dot products are \(1+6+2+0=9\), \(6-0=6\), and \(1-2=-1\). Solving the normal equations gives

$$ \begin{bmatrix}4&0&0\\0&2&0\\0&0&2\end{bmatrix} \begin{bmatrix}c\\a\\b\end{bmatrix} = \begin{bmatrix}9\\6\\-1\end{bmatrix}, \qquad (c,a,b)=\left(\frac94,3,-\frac12\right). $$

The fitted values, calculated in time order, are \(\frac94-\frac12=\frac74\), \(\frac94+3=\frac{21}{4}\), \(\frac94+\frac12=\frac{11}{4}\), and \(\frac94-3=-\frac34\). Therefore

$$ \mathbf{A}\begin{bmatrix}\frac94\\3\\-\frac12\end{bmatrix} = \begin{bmatrix}\frac74\\\frac{21}{4}\\\frac{11}{4}\\-\frac34\end{bmatrix}, \qquad \mathbf{r}=\mathbf{y}-\mathbf{A}\mathbf{x} = \begin{bmatrix}-\frac34\\\frac34\\-\frac34\\\frac34\end{bmatrix}. $$

Check: The residual sums to zero, its sine-weighted sum is \(\frac34-\frac34=0\), and its cosine-weighted sum is \(-\frac34+\frac34=0\). Thus \(\mathbf{A}^{\top}\mathbf{r}=\mathbf{0}\), as required. The squared residual norm is \(4(\frac34)^2=\frac94\).

Interpreting Coefficients and Sampling Limits

The coefficients \(a\) and \(b\) can also be expressed as amplitude and phase. The identity \(R\sin(\omega t+\phi)=R\cos(\phi)\sin(\omega t)+R\sin(\phi)\cos(\omega t)\) shows that, when \(R=\sqrt{a^2+b^2}>0\), the fitted periodic part has amplitude \(R\) and phase \(\phi\) satisfying \(\cos(\phi)=a/R\) and \(\sin(\phi)=b/R\). Equivalently, \(\phi\) is the angle with cosine and sine given by those ratios. The intercept \(c\) is the vertical shift. If \(a=b=0\), the fitted periodic part is constant and its phase is not determined.

Worked Example: When the Sampling Times Do Not Identify Every Coefficient

Use angular frequency \(\omega=\frac{\pi}{2}\), but observe only at \(t=0\) and \(t=2\), with data \(4\) and \(2\). Determine whether the fitted coefficients are unique.

At \(t=0\), the sine and cosine values are \(0,1\); at \(t=2\), they are \(0,-1\). Therefore

$$ \mathbf{A}= \begin{bmatrix}1&0&1\\1&0&-1\end{bmatrix}, \qquad \mathbf{y}= \begin{bmatrix}4\\2\end{bmatrix}. $$

The sine column is zero at both sampled times. The normal equations are \(\begin{bmatrix}2&0&0\\0&0&0\\0&0&2\end{bmatrix}\begin{bmatrix}c\\a\\b\end{bmatrix}=\begin{bmatrix}6\\0\\2\end{bmatrix}\). Thus \(c=3\), \(b=1\), while \(a\) is unrestricted. Taking \(a=0\) gives fitted values \(3+1=4\) and \(3-1=2\), exactly the observations. Every other value of \(a\) gives the same fitted values because the sine column is zero at both sample times. The data identify the predictions at these times, but not the sine coefficient.

Common pitfall: using a signed period. A tempting but incorrect calculation is to say that \(\omega=-2\) gives period \(2\pi/\omega=-\pi\). A period is positive, and the sine and cosine components repeat after \(T=2\pi/|\omega|=2\pi/2=\pi\). In general, use the absolute value in the denominator. Also, do not infer that a visually plausible fitted curve proves the coefficients are unique: uniqueness depends on whether the design-matrix columns are independent at the actual observation times.

A fitted model is an approximation tied to the frequency and sampling scheme that were chosen. Its residuals record the differences between observed and fitted values; the normal equations ensure that those residuals have zero dot product with each design column. The model therefore balances errors against the constant, sine and cosine patterns represented by the columns. Changing the chosen frequency changes those columns and can change both the fitted coefficients and the quality of the approximation.

Check Your Understanding

Use exact arithmetic, and check each proposed fit by multiplying the design matrix by its coefficient vector.

  1. For \(\omega=\frac{\pi}{2}\), write the design matrix for observations taken at \(t=0,1,2\).
  2. At times \(0,1,2,3\) with \(\omega=\frac{\pi}{2}\), solve the normal equations for data \(2,4,2,0\).
  3. For the coefficients \(a=3\) and \(b=4\), find the amplitude of the periodic part and state the equations that determine its phase.
  4. Explain why two observations at \(t=0,2\) with \(\omega=\frac{\pi}{2}\) cannot determine the sine coefficient.
  5. What is the positive period of sine and cosine components when \(\omega=-\frac{\pi}{3}\)?

Test yourself: Linear Algebra final exam practice

Ready to check what you know? Work a full-length practice final, then open the complete worked solutions.

Want all 1,114 practice problems?

This tutorial covers one topic. Calc Pass covers the full Calculus AB & BC curriculum (the core of Calculus I and II): full review, every practice question, and step-by-step solutions to each one.

QR code to download the Calc Pass app

Get the Calc Pass App

Scan to download and practice on the go, with instant scoring.

Calc Pass Volume 1: 1,114 Practice Questions book cover

Vol. 1: 1,114 Questions

The complete practice question bank, organized by AB & BC topic.

View on Amazon
Calc Pass Volume 2: Solutions Manual book cover

Vol. 2: Solutions Manual

Fully worked solutions to every question in Volume 1.

View on Amazon