The swdft Package

Lee Richardson

2019-04-16

Statisticians typically partition time-series methods into two groups: the time domain and the frequency domain. Time domain methods focus on the temporal dependence between observations, for example the similarity of temperature in consecutive hours, and use tools such as the auto-correlation function. Frequency domain methods imagine that a time-series is made up of oscillations, like the earth’s annual trip around the sun, and use tools based on the discrete Fourier transform (DFT) to understand these oscillations. A third group of methods combines the time and frequency domains, and we call this group the time-frequency domain. For analysis in the time-frequency domain, an important tool is the sliding window discrete Fourier transform (SWDFT). The SWDFT is widely used across science and engineering, but there isn’t an easy-to-use implementation across the R landscape. The swdft package fills this gap, and provides an implementation of the SWDFT and related functions.

The functions in the swdft package can be partitioned into three groups:

  1. The SWDFT. Implements the SWDFT and provides tools to visualize the output.
  2. Cosine regression. Fits cosine functions to data with least squares.
  3. Complex demodulation. Fits cosine functions with time-varying parameters using complex demodulation.

This vignette walks through the main functionality of each group. Before you work through the examples, make sure that the swdft package is loaded into memory:

library(swdft)

The SWDFT

The SWDFT extends the discrete Fourier transform (DFT) from the frequency domain to the time-frequency domain. Whereas the DFT makes a single large computation on the entire signal, the SWDFT takes a sequence of smaller DFTs on temporally ordered subsets of the signal. To be clear, this vignette refers to data as a signal, but you can easily substitute signal with time-series, data, input, etc. In any case, if x is a length N signal, then the DFT of x computes N complex-valued coefficients, and is defined by:

\[\begin{eqnarray} a_k &=& \sum_{j=0}^{N-1} x_j \omega_n^{-jk} \nonumber \\ k &=& 0, 1, \ldots, N - 1, \end{eqnarray}\]

where

\[\begin{eqnarray} \omega_N &=& \cos(\frac{2 \pi}{N}) + i \sin(\frac{2 \pi}{N}). \end{eqnarray}\]

Each DFT coefficient measures the correlation between x and a sine and cosine function with a particular frequency, where frequency specifies how fast the sine and cosine functions oscillate. This explains why researchers are interested in large DFT coefficients: large DFT coefficients indicate high correlation between our signal a sine or cosine function with a particular frequency.

A pictorial example of the DFT for a length N = 8 signal is given below, where the blue squares are the input signal, and the red circles are the DFT coefficients:

In contrast, the SWDFT of takes a sequence of smaller DFTs, and outputs a 2D complex array coefficients. In this 2D array, the first dimension represents frequency, and the second dimension represents time, which is why the SWDFT is a time-frequency method. For example, the SWDFT for the same length N = 8 signal used in the previous signal is:

The size of the smaller DFTs is known as the window size, so in this example, the window size is four. To see how the SWDFT works, let’s walk through the computations in detail. Since our window size is length four, step one computes a length four DFT, and stores the output in the first window position of the 2D array:

Step two moves the window one position to the right, computes the DFT of the second through fifth data-points, and stores the results in the second window position: