Signal Processing Basics
Time Domain
First we will work on signals in the time domain. This requires measuring a signal at a constant interval over time. The frequency with which we measure a signal is referred to as the sampling frequency. The units of this are typically described inDot Product
To understand convolution, we first need to familiarize ourselves with the dot product. The dot product is simply the sum of the elements of a vector weighted by the elements of another vector. This method is commonly used in signal processing, and also in statistics as a measure of similarity between two vectors. Finally, there is also a geometric interpretation which is a mapping between vectors (i.e., the product of the magnitudes of the two vectors scaled by the cosine of the angle between them). For a more in depth overview of the dot product and its relation to convolution, you can watch this optional video.Dot Product: 446

Dot Product: 666.01

Convolution
Convolution in the time domain is an extension of the dot product in which the dot product is computed iteratively over time. One way to think about it is that one signal weights each time point of the other signal and then slides forward over time. Let's call the timeseries variable signal and the other vector the kernel. To gain an intuition of how convolution works, let's play with some data. First, let's create a time series of spikes. Then let's convolve this signal with a boxcar kernel.
You can see that after convolution, each spike has now become the shape of the kernel.
Signal: 100, Kernel: 10, Convolved: 109 samplesStep Through Convolution
Use the slider to step through the convolution one timepoint at a time. Watch the kernel (red) slide across the signal and see the output build up.
np.convolve:

Oscillations
Ok, now let’s move on to studying time-varying signals that have the shape of oscillating waves. Let’s watch a short video by Mike X Cohen to get some more background on sine waves. Don’t worry too much about the matlab code as we will work through similar Python examples in this notebook.

Time & Frequency Domains
We have seen above how to represent signals in the time domain. However, these signals can also be represented in the frequency domain. Let’s get started by watching a short video by Mike X Cohen to get an overview of how a signal can be represented in both of these different domains.Frequency Domain
In the previous example, we generated a complex signal composed of multiple sine waves oscillating at different frequencies. Typically in data analysis, we only observe the signal and are trying to uncover the generative processes that gave rise to the signal. In this section, we will introduce the frequency domain and how we can identify if there are any frequencies oscillating at a consistent frequency in our signal using the fourier transform. The fourier transform convolves different frequencies of sine waves with our data to identify oscillatory components. One important assumption: stationarity — the generative processes don't vary over time. See this video or a more in depth discussion on stationarity. In practice, this assumption is rarely true. Often it can be useful to use other techniques such as wavelets to look at time x frequency representations. We will not be covering wavelets here, but see this series of videos for more information.Discrete Time Fourier Transform
We will gain an intution of how the fourier transform works by building our own discrete time fourier transform. Let’s watch this short video about the fourier transform by Mike X Cohen. Don’t worry too much about the details of the discussion on the matlab code as we will be exploring these concepts in python below.Complex Sine Waves
You may have noticed that we are computing complex sine waves using thenp.exp function instead of the np.sin function.
np.real or the imaginary using np.imag.
We can visualize complex sine waves in three dimensions. For more information, watch this video. If you need a refresher on complex numbers, you may want to watch this video.
In this plot, we show this complex signal in 3 dimensions and also project on two dimensional planes to show that the real and imaginary create a unit circle, and are phase offset by 
Create a filter bank
Ok, now let’s create a bank of n-1 linearly spaced complex sine waves and the plot first 5 waves to see their frequencies. Remember the first basis function is zero frequency (DC) component and reflects the mean offset over the entire signal.
Estimate Fourier Coefficients
Now let’s take the dot product of each of the sine wave basis set with our signal to get the fourier coefficients. We can scale the coefficients to be more interpretable by dividing by the number of time points and multiplying by 2. Watch this video if you’re interested in a more detailed explanation. Basically, this only needs to be done if you want the amplitude to be in the same units as the original data. In practice, this scaling factor will not change your interpretation of the spectrum.Recall: 
freq = [3, 10, 5, 15, 35], amplitude = [5, 15, 10, 5, 7]
Let's learn a few more important details about the DFT:
Inverse Fourier Transform
The fourier transform allows you to represent a time series in the frequency domain. This is a lossless operation, meaning that no information in the original signal is lost by the transform. This means that we can reconstruct the original signal by inverting the operation. Thus, we can create a time series with only the frequency domain information using the inverse fourier transform. Watch this video if you would like a more in depth explanation.
Phase Spectrum
The FFT provides frequency, amplitude, AND phase. Below we show both spectra. Phase carries timing/structural info.
Phase Scrambling
Keep the power spectrum but randomize phases. The signal looks completely different — phase carries the structure!
Fast Fourier Transform
In practice,np.fft.fft is used. Scale by dividing by N.
Convolution Theorem
Convolution in the time domain is the same as multiplication in the frequency domain. This means that time domain convolution computations can be performed much more efficiently in the frequency domain via simple multiplication. (The opposite is also true that multiplication in the time domain is the same as convolution in the frequency domain. Watch this Video for an overview of the convolution theorem and convolution in the frequency domain.
Let's prove it:
Filters
Filters can be classified as finite impulse response (FIR) or infinite impulse response (IIR). These terms describe how a filter responds to a single input impulse. FIR filters have a response that ends at a discrete point in time, while IIR filters have a response that continues indefinitely. Filters are constructed in the frequency domain and have several properties that need to be considered.- ripple in the pass-band
- attenuation in the stop-band
- steepness of roll-off
- filter order (i.e., length for FIR filters)
- time-domain ringing
High Pass
High pass filters only allow high frequency signals to remain, effectively removing any low frequency information. Here we will construct a high pass butterworth filter and plot it in frequency space.
Low Pass
Low pass filters only retain low frequency signals, which removes any high frequency information. We usefiltfilt for zero-phase distortion.
Bandpass
Bandpass filters permit retaining only a specific frequency. Morlet wavelets are an example of a bandpass filter. or example a Morlet wavelet is a gaussian with the peak frequency at the center of a bandpass filter. Let’s try selecting removing specific frequencies
Band-Stop
Bandstop filters remove a specific frequency from the signal
Interactive Filter Explorer
Explore all filter types interactively:
Exercises
Exercise 1: Create a simulated time series with 7 different frequencies with noise
Ellipsis
Exercise 2: Show that you can identify each signal using a FFT
Ellipsis
Exercise 3: Remove one frequency with a bandstop filter
Ellipsis
Exercise 4: Remove frequency with a bandstop filter in the frequency domain and reconstruct the signal in the time domain with the frequency removed and compare it to the original
Ellipsis
Graded assignment
The graded version of these exercises is the Signal Processing assignment at the end of this page, which opens in a drawer from the Assignment button in the header. Open it from that page (in molab or by download), sign in with your Dartmouth account inside the notebook, and submit each question when you are ready.Assignment: Signal Processing
- Setup
- Q1. Simulate a time series with 7 frequencies plus noise
- Q2. Identify each frequency with an FFT
- Q3. Remove one frequency with a bandstop filter
- Q4. Remove the same frequency in the frequency domain
- Q5. Interpretation
Open in molab
Opens in a drawer at the bottom of the page, so you can keep reading while you work. Autosaves in this browser.