---
title: "Discrete Fourier"
author: Jed Rembold
date: February 20, 2025
slideNumber: true
theme: tokyo-night-light
highlightjs-theme: tokyo-night-light
width: 1920
height: 1080
transition: slide
---


## Announcements
- I'll be working on HW2 feedback over the next week
- HW3 is out
  - Don't forget the check-in form this weekend!
  - 5 min for partner meet-n-greet in just a moment
- `fftutils.r` library posted [here](../libs/fftutils.r), though I did include in starting repo

## HW3 Groups!
- Since I forgot on Tuesday, you have 5 min now to touch base with your new partner!

::::::{.cols style='align-items: flex-start'}
::::col
:::{.block name='Left side'}
- Conor & Clay
- M & Ema
- Mamadou & Gabby
- Aurora & Elliott
- Maddie & Sadie
- Jared & Luca
- Salem & Izzy
:::
::::

::::col
:::{.block name='Right side'}
- Tegan & Sawyer
- Luna & Sage
- Lucca & Sergio
- Pearson & Greg
- Evyn & Evan
- Oscar & Felicity
:::

::::
::::::

## Recall
- Measuring periodicity for noisy data can be very sensitive to initial guesses
- Any periodic signal can be represented as a combination of sine waves
- The Fourier transform wraps a signal at different periods and measures the "overlap" of that signal at each period
  - Moves a signal from "time-space" to "frequency-space"
  - The signal's power spectrum is the square of the amplitude of its Fourier transform
- Can use `fft` or `rfft` to compute the power-spectrum, and `fftfreq` (or `rfftfreq`) to compute the corresponding frequencies

## Today's Plan
- Understanding how the Fourier Transform can compute other wave parameters
- Going from frequency-space to time-space
- Understanding discrete data's effect on the Fourier Transform
- Quiz

# Wave Properties
## The Classic Periodogram
- Though we've touched on it, the classic, or Schuster, periodogram is defined as:
  $$ P_S(f) = \frac{1}{N}\left|\mathcal{F}(f)\right|^2 $$
  where $N$ is the number of discrete measurements in the observing window
- Differs from the power spectrum by a factor of $\tfrac{1}{N}$, which accounts for the fact that otherwise longer signals will have higher power spectrum values
- Technically, the periodogram is our observational statistic, which serves as an _estimator_ for the underlying power spectrum

## Amplitudes and Phases
- Periodograms can help identify the prominent frequencies, but what if you want the other sinusoidal parameters?
  - Amplitude: determined from just the magnitude of the FFT scaled by the number of observations
    $$ A = \frac{1}{N} \left| \mathcal{F}(f) \right| \times 2 $$
    - The $\times 2$ comes from the symmetric nature of the FFT
  - Phase: determined from the angle formed by the real and imaginary parts of the FFT
    $$ \phi = \arctan\left(\frac{\operatorname{Im}(\mathcal{F}(f))}{\operatorname{Re}(\mathcal{F}(f))}\right) $$
    - The returned phase is in radians

# Back it up
## The Inverse FFT
- You can also go backwards!
- The _Inverse Fourier Transform_ moves you back from the frequency-domain to the time-domain
- In Python, this is given by `ifft`
- In R, use the `inverse = TRUE` flag inside `fft`
- Make it possible to filter out certain frequencies, and then transform back to a clean signal


## Activity!
- I've generated noisy data of a single oscillation [here](../demos/dopplerdata.csv).
- Your task is to determine the period/frequency, filter out everything else by setting it to 0, and then transform that signal back and plot it atop the original noise


# Discrete Effects
## Common Transforms

![Important Fourier Transform Pairs](../images/python/important_fts.png)


## Convolutions
- Mathematically, a convolution is defined as:
  $$ [f * g](t) = \int_{-\infty}^{\infty} f(t)g(t-\tau)\,d\tau $$
- Conceptually, this is the same as: 
  - Taking the second function and flipping it about the y-axis
  - Then "sliding" that function across the other, from left to right
  - Each step, summing the area beneath both functions


## Visual Convolutions

![https://commons.wikimedia.org/wiki/File:Convolution_of_box_signal_with_itself2.gif](https://upload.wikimedia.org/wikipedia/commons/6/6a/Convolution_of_box_signal_with_itself2.gif){width=100%}

## Why do we care?
- Fourier Transforms have a particular attribute:
  $$ \mathcal{F}\{f * g\} = \mathcal{F}\{f\} \cdot \mathcal{F}\{g\} $$
  or
  $$ \mathcal{F}\{f \cdot g\} = \mathcal{F}\{f\} * \mathcal{F}\{g\} $$
- In other words: convolutions in one space are the same as just multiplying the function point-wise in the other space


## Example 1: Window

![](../images/python/conv1_annotated.svg)


## Example 2: Discrete Measurements

![](../images/python/conv2_annotated.svg)


## Our Powers Combined...
- In practice, most real world data consists of both effects:
  - Data was measured over some duration: the window
    - Will cause broadening of our peaks. The narrower the window, the greater the broadening.
  - Data was collected at some frequency: the discrete measurements
    - Will cause _aliases_ of the signal, spaced according to the sampling rate
    - The slower the sampling rate, the more densely packed the aliases


## The Nyquist Limit
- Note that if the window of observations gets too small, or the time between observations too large, our Fourier Transform peaks will begin to overlap!
- In this case not all of the frequency information can be recovered
- This is called the Nyquist Limit, and occurs at a frequency of half the sampling frequency
- The FFT algorithm generally measures frequencies _up to_ but not beyond this point, so you shouldn't see aliases in your results, but your results might not capture what you were hoping to see.


## Nyquist Visual

![](../images/python/conv3_annotated.svg)

# Quiz 1
## Quiz Time!
- Put your notes away and have just a writing implement and a calculator out!
- Show as much work or your thought process as you can on all problems for the potential of partial credit
- When you are finished you can leave

<!--
## Non-uniform Observations
- Non-uniform observations add what will seem to be "noise" to the FFT
  - Effects of small segments of periodicity between various observations

![](../images/python/conv4_annotated.svg)

## Non-uniform FFT
- In practice, computing a DFT using a computational FFT can be problematic, since the returned frequencies are computed assuming a uniform spacing
  - The `d` in `fftfreq(n, d)`
- You could super-sample the observations, adding an evenly spaced grid with 0's wherever no data exists
  - Still might be difficult to get an evenly spaced grid that exactly contains all observations
- A better option though is to shift to a slightly different estimator: the _Lomb-Scargle Periodogram_

## Lomb-Scargle
- Lomb and Scargle considered a more generalized form of the periodogram, with added functions $A$, $B$, and $\tau$, and then showed that you could choose those functions such that:
  - The periodogram reduces to the classical form for evenly spaced observations
  - The periodogram's statistics are analytically computable
  - The periodogram is insensitive to global time-shifts in the data
- The same basic insights we had with the classic periodogram will still hold qualitatively with Lomb-Scargle, even if not exactly the same quantitatively
- The real price you pay vs the classic periodogram is that you have to deal with aliases

## Homework 3 Groups!
The remainder of class has been set aside for you to meet with your partner/group for HW3 to plan.

::::::{.cols style='align-items: flex-start;'}
::::col
:::{.block name='Left Side'}
- Sophia and Jennifer
- Owyn and Kendall
- Teo and Siera
- Dash and Michael
- Mia and Nico
- Brandon and Leila
:::
::::

::::col
:::{.block name='Right Side'}
- Alex and Matthew
- Indi and Paul
- Nathaniel and Sam
- Kara and Zachary
- Trajan, Teddy, and Ben
:::
::::
::::::
-->
