Guide to AI and Data Courses · Lecture Notes

Chapter 16: Digital Signal Processing

A phone microphone turns sound into thousands of numbers every second; an ECG band records a heartbeat as a sequence of numbers too. Hum, vibration and noise are mixed into those numbers, and removing them does not require learning anything: exact rules are enough. Digital signal processing is the course that shows how a signal that has become a sequence of numbers is measured, filtered and split into its frequencies. These notes introduce the course and work through its three core ideas with small examples you can compute by hand.

Learning outcomes

Sampling
Determine a sufficient sampling rate for a signal and compute the frequency at which aliasing will appear.
Convolution
Convolve two short sequences by hand and interpret the result as a system's response to its input.
Filters
Say from the frequency response which frequencies a simple FIR filter passes and which it suppresses.
Fourier transform
Compute a four-point DFT by hand and show with operation counts why the FFT is needed.
Place of the course
State what separates signal processing from image processing, speech recognition, time series analysis and information theory.

16.1What question does the course ask?

How are sampled signals analyzed, filtered and transformed?

Digital signal processing analyzes, filters and transforms audio, vibration, ECG or radio signals that have been sampled at regular intervals, using mathematical transforms. The course rests on exact mathematics, not learning; it is the common foundation of the audio, image and communications courses.

Signals in nature are continuous: sound pressure, voltage and temperature take a value at every instant. A computer can only store a sequence of numbers. The course opens with this question: if we measure a continuous signal at regular intervals, do we lose information? The answer is surprising: if we sample faster than twice the highest frequency in the signal we lose nothing; if we sample slower, the loss takes the form of the signal silently turning into a different signal.

The second question is: once we have a sequence of numbers, what can we do with it? We want to delete part of it, smooth part of it, or isolate certain oscillations. For linear time-invariant systems all of these jobs reduce to a single operation, convolution. Once the rule of convolution is known, writing a filter, adding an echo or reducing noise all happen in the same framework.

16.1.1Two languages: time and frequency

The same signal can be viewed in two ways. In the time view, the signal is the numbers in order. In the frequency view, the signal is a sum of sinusoids oscillating at different rates, each with an amplitude. The Fourier transform is the bridge between the two views. Some questions are hard in one language and easy in the other: removing 50 Hz hum in time is difficult, while in frequency it is enough to delete a single line. Much of the course is about moving back and forth between these two languages. The same tools carried to two dimensions become Image Processing; combined with language models they become Speech and Audio Processing.

16.1.2At a glance

CriterionDigital Signal Processing
Approachmathematical transforms and filters
Inputsampled signal
Outputfiltered signal, spectrum
Success measuresignal-to-noise ratio, frequency response
Math loadhigh: complex numbers, Fourier
Data needsno learning; the signal suffices

16.2A short history

YearMilestone
1822In his book on heat, Fourier proposes writing functions as sums of sinusoids.
1949Shannon publishes sampling theory: a band-limited signal sampled fast enough is recovered exactly.
1965Cooley and Tukey publish the fast Fourier transform (FFT); computing spectra becomes practical.
1974Ahmed, Natarajan and Rao introduce the discrete cosine transform (DCT); JPEG and MP3 build on it.
1975Oppenheim and Schafer's Digital Signal Processing appears; the field becomes a standard course.
1978Texas Instruments' Speak & Spell brings single-chip digital speech synthesis into homes.

16.3Core concepts

The rest of the course is built on the concepts below. The following sections work through the most important ones with examples.

ConceptMeaning
SamplingMeasuring a continuous signal at equal intervals to get a sequence of numbers.
Nyquist criterionSample faster than twice the highest frequency.
AliasingWith undersampling, a high frequency shows up as a low one.
QuantizationRounding amplitude to a finite number of levels; bit depth.
ConvolutionThe response of a linear time-invariant system to its input.
Fourier and FFTA fast way to split a signal into frequency components.
FIR and IIR filtersLow-pass, high-pass and band-pass digital filters.
z-transformPoles and zeros of a filter; stability analysis.

16.4Sampling and aliasing

If we measure a continuous signal every Ts seconds, our sampling rate is fs = 1/Ts. If the highest frequency in the signal is fmax, then when fs > 2 fmax the signal can be reconstructed exactly from its samples. This is the Nyquist criterion. The frequency fs/2 is called the Nyquist frequency.

Definition 16.1 (Aliasing)

If the sampling rate is too low, a sinusoid at frequency f appears in the samples at |f − k · fs|, where k is the integer that brings the result between 0 and fs/2. From the samples alone it is impossible to tell the two sinusoids apart.

Example 16.1: The same eight samples from two different signals

We take fs = 8 samples per second; the two signals are x1(t) = cos(2π · 1 · t) and x2(t) = cos(2π · 7 · t). Let us compute the first eight samples (t = n/8).

  1. 1 Hz. cos(2πn/8) for n = 0…7: 1; 0.71; 0; −0.71; −1; −0.71; 0; 0.71.
  2. 7 Hz. cos(2π · 7n/8): the angle is 2πn − 2πn/8, and since cosine is even and 2π-periodic the value equals cos(2πn/8). The same eight numbers: 1; 0.71; 0; −0.71; −1; −0.71; 0; 0.71.
  3. Result. The samples of the two signals are identical. By the formula: |7 − 1 · 8| = 1 Hz. There is no way to tell from the samples that 7 Hz was ever there; the signal looks like 1 Hz.

The Nyquist frequency is 8/2 = 4 Hz. Because 7 Hz is above this limit it is "folded" down to 1 Hz. Another example: 3 Hz and 5 Hz also give identical samples at fs = 8 (1; −0.71; 0; 0.71; −1; 0.71; 0; −0.71).

Real systems prevent this by placing a low-pass filter (an anti-aliasing filter) before the sampler: frequencies above fs/2 are suppressed before sampling. Once aliasing has happened after sampling, it cannot be undone. A higher sampling rate is not always better: beyond the Nyquist limit no new information arrives, only more data and computation.

16.4.1Quantization: the second loss

Sampling discretizes time; quantization discretizes amplitude. A B-bit converter rounds the amplitude to 2B levels. Over the range ±1, 8 bits give a step of 2/256 = 0.0078125; the error is at most half a step. Each extra bit halves the error, which raises the signal-to-noise ratio by about 6 dB.

Common mistake

Believing that "if the highest frequency is 4 kHz, sampling at 4 kHz is enough". The criterion asks for twice that: fs > 8 kHz. For example, sin(2π · 4000 · t) sampled at exactly 8 kHz gives all-zero samples: the signal looks as if it were not there at all.

16.5Convolution and filters

A system is linear and time-invariant (LTI) if summing inputs sums the outputs, and shifting the input shifts the output by the same amount. Such a system is completely determined by one sequence: its response to a single unit impulse, h[n]. Since every input is a sum of scaled and shifted impulses, the output is the convolution of the input with h.

Definition 16.2 (Convolution)

y[n] = Σk h[k] · x[n − k]. The convolution of two sequences of lengths N and M has length N + M − 1. If h has finite length, the system is called an FIR (finite impulse response) filter.

Example 16.2: A three-point sum and a two-point average

First a small convolution: x = [1, 2, 3], h = [1, 1, 1].

  1. Length 3 + 3 − 1 = 5.
  2. y[0] = 1.
  3. y[1] = 1 + 2 = 3.
  4. y[2] = 1 + 2 + 3 = 6.
  5. y[3] = 2 + 3 = 5; y[4] = 3.

The result is [1, 3, 6, 5, 3]. Now take h = [0.5, 0.5] (a two-point moving average) with x = [2, 4, 6, 4, 2, 0]: each output is the average of the current and the previous sample; the result is [1, 3, 5, 5, 3, 1, 0]. The sharp peak is smoothed: two 5s in place of a single 6. Applied to x = [1, −1, 1, −1], which changes sign at every step, the same filter gives [0.5, 0, 0, 0, −0.5]: the middle values are zero, so the fastest oscillation has been removed completely (the two end values come from the sequence starting and ending).

This example shows what a filter does at the level of frequency. Since every signal is a sum of sinusoids, to understand a filter we look at how much each sinusoid is amplified as it passes through; this is the frequency response H(ω). Here ω = 2πf/fs in radians per sample; the Nyquist frequency corresponds to ω = π.

Example 16.3: Frequency response of the moving average

For h = [0.5, 0.5], H(ω) = 0.5 + 0.5 e−jω.

  1. Magnitude |H(ω)| = |cos(ω/2)|.
  2. ω = 0 (a constant signal): |H| = 1; passes unchanged.
  3. ω = π/2 (fs/4): |H| = cos(π/4) ≈ 0.71.
  4. ω = π (the fastest oscillation): |H| = cos(π/2) = 0; fully suppressed.

So the moving average is a low-pass filter. The difference filter h = [1, −1] gives |H(ω)| = 2|sin(ω/2)|: 0 at ω = 0 and 2 at ω = π; it passes high frequencies and cuts the constant value (high-pass).

Common mistake

Believing that a filter "recognizes" noise and deletes it. A filter looks only at frequency. If the noise lies in the same frequency band as the signal, no linear filter can separate them; lengthening the window reduces noise but also smears sharp detail in the signal, such as the R peak in an ECG.

16.6The discrete Fourier transform and the FFT

To look through the frequency eye, we split a sequence of N samples into N complex sinusoids. This operation is the discrete Fourier transform (DFT). The k-th output measures the component of the sequence at frequency k · fs/N.

Definition 16.3 (Discrete Fourier transform)

X[k] = Σn=0N−1 x[n] · e−j2πkn/N, k = 0, …, N−1. The magnitude |X[k]| gives the size of the component at that frequency; for a real signal, k and N − k are two faces of the same component.

Example 16.4: A four-point DFT

x = [1, 2, 3, 4], N = 4. The transform factors are powers of e−j2π/4 = −j.

  1. X[0] = 1 + 2 + 3 + 4 = 10 (the sum, i.e. 4 times the mean).
  2. X[1] = 1 + 2(−j) + 3(−1) + 4(j) = −2 + 2j.
  3. X[2] = 1 − 2 + 3 − 4 = −2.
  4. X[3] = 1 + 2(j) + 3(−1) + 4(−j) = −2 − 2j.
  5. Check. Parseval: Σ|x|2 = 1 + 4 + 9 + 16 = 30 and (1/N) Σ|X|2 = (100 + 8 + 4 + 8)/4 = 30. Both sides agree.

A more intuitive example: x = [1, 0, −1, 0] is one period of a cosine at fs/4. Its DFT is X = [0, 2, 0, 2]. All the energy sits at k = 1 and k = 3 = N − 1; that is the cosine's one frequency.

If we compute the definition directly, each X[k] takes N multiplications, about N2 operations in all. The 1965 algorithm of Cooley and Tukey splits the sequence into even- and odd-numbered samples and applies the same step recursively, bringing the count down to the order of N log2 N. For N = 1024, N2 = 1,048,576 while N log2 N = 10,240: about 100 times fewer operations. The result is the same; the FFT is not a separate transform but a fast way to compute the DFT.

16.6.1Resolution and leakage

The DFT's frequency step is fs/N; to separate two close frequencies you need a longer record. For fs = 8000 Hz and N = 1024 the step is 8000/1024 ≈ 7.81 Hz. Also, if the record does not contain a whole number of periods of a sinusoid, energy "leaks" into neighboring bins (spectral leakage); to reduce it we multiply the sequence by a window (such as Hann) that tapers toward the ends.

Common mistake

Believing that every line in a spectrum is a real frequency component. In an unwindowed FFT, leakage produces lines and peaks that are not actually there; interpreting a spectrum without choosing a suitable window leads to wrong diagnoses.

16.7How is the course taught?

16.7.1A typical workflow

SampleADC, samplingrateTransformFFT,spectrumFilterFIR / IIRfilterAnalyzepeaks, energy,featuresConvertbackDAC ornext step
Figure 16.1: A typical workflow in Digital Signal Processing

An ECG device samples the heart signal 360 times per second. The spectrum shows 50 Hz mains noise; a notch filter suppresses only that frequency. R peaks are found in the cleaned signal and heart rate is computed from the time between them. No step involves learning.

16.7.2Topics over the semester

  1. Discrete-time signals and systems
  2. Linear time-invariant systems and convolution
  3. Sampling, aliasing and reconstruction
  4. Discrete-time Fourier transform, frequency response
  5. The z-transform: poles, zeros and stability
  6. The discrete Fourier transform (DFT) and the FFT
  7. Windowing and spectral leakage
  8. FIR filter design: the window method
  9. IIR filter design: Butterworth, Chebyshev
  10. Sample-rate conversion: decimation, interpolation
  11. Short-time Fourier transform and spectrograms
  12. Quantization noise and fixed-point implementation

This is a typical semester; the order and coverage vary between universities.

16.7.3Skills you will gain

  • designing filters with scipy.signal
  • plotting spectra in MATLAB or Octave
  • reading pole-zero diagrams
  • choosing a suitable sampling rate
  • filtering and interpreting a real signal

16.7.4Try it yourself

The Try It: Cleaning a Noisy Pulse slide in the presentation lets you solve a typical problem of this course yourself: A chest-strap ECG samples at 200 Hz; 50 Hz mains hum and muscle noise have crept into the recording. Change the moving-average window length and watch the signal in both time and frequency.

16.8Are you ready for this course?

16.8.1Prerequisites

CourseWhy it is needed
Calculusintegrals, series and complex numbers: the language of the Fourier transform
Differential Equationsbehavior and stability of linear systems
Programming with Cto write real-time filters on embedded devices

16.8.2Ask yourself

For every "no", the right-hand column shows where to start. If you can say "yes" to most of them, you are ready.

QuestionIf not, start with
Can you write a complex number in polar form and explain Euler's formula?Complex numbers and Euler's formula from calculus
Can you read the amplitude, frequency and phase of a sinusoid from its plot?A quick review of trigonometry and sinusoid graphs
Can you evaluate a sum (Σ) and a simple integral?Series and integrals from calculus
Can you generate an array in Python or MATLAB and plot it?Intro programming, then a short NumPy and Matplotlib tutorial

16.9Neighboring courses

Image ProcessingSpeech and AudioTime SeriesInformation TheorySignal Processing
subfieldbuilds onshares methodsapplication
Figure 16.2: Neighbors of Digital Signal Processing and the kind of relation

16.9.1Where it overlaps

RelationCourseShared ground
builds onImage Processing builds on this course2D signals, convolution, Fourier transform
builds onSpeech Recognition and Audio Processing builds on this coursespectrogram, MFCC, filters
shares methodsShares methods with Time Series Analysis and Forecastingfrequency analysis, filtering
shares methodsShares methods with Information Theorycompression, sampling, channels

16.9.2Where it differs

  • Image Processing: Signal processing works with one-dimensional, time-dependent signals; image processing carries the same tools to a 2D pixel grid.
  • Speech Recognition and Audio Processing: Signal processing provides general tools; speech recognition combines them with language models and learning to output words.
  • Time Series Analysis and Forecasting: Both look at time-dependent data; time series analysis needs forecasting and statistics, signal processing needs frequency and filters.
  • Information Theory: Information theory gives the measure of information and the limits of compression; signal processing gives the algorithms that act on the signal.

16.10In practice

16.10.1Applications

AreaWhat is done
Audio and musicEqualizers, noise suppression, MP3 compression.
CommunicationsModems, Wi-Fi, 4G/5G modulation.
BiomedicalFiltering ECG and EEG, finding peaks.
Radar and sonarMeasuring range and speed from echoes.
Vibration monitoringDetecting machine faults from the spectrum.
Image and videoTransforms in JPEG and video coding.

16.10.2Risks and ethics

RiskExplanation
AliasingWithout a low-pass filter before sampling, high frequencies show up as fake low ones and cannot be undone.
Unstable IIR filtersA pole outside the unit circle makes the output blow up; coefficient rounding in fixed point can push it there.
Spectral leakageAn FFT taken without a suitable window shows frequency components that are not there and leads to wrong diagnoses.
Phase distortionThe nonlinear phase of an IIR filter distorts waveforms such as ECG, which can affect a diagnosis.

These issues are discussed in depth in Explainable AI and AI Ethics.

16.10.3Tools and datasets

Tools: MATLAB, GNU Octave, NumPy, SciPy (scipy.signal), librosa, GNU Radio.

Sample signals and datasets: MIT-BIH Arrhythmia (ECG), PhysioNet, TIMIT (speech), CWRU bearing vibration data, Synthetic sines and noise.

In class a synthetic signal is often enough: summing known frequencies and adding noise shows clearly what a filter does.

16.11Common misconceptions

Common mistake

A higher sampling rate is always better.

Beyond the Nyquist limit you gain no information; only data and computation grow.

Common mistake

The FFT is a different transform from the Fourier transform.

The FFT is an algorithm that computes the discrete Fourier transform (DFT) fast; the result is the same.

Common mistake

A filter tells noise from signal and removes it.

A filter separates by frequency; if noise shares the signal's band, the filter cannot separate it.

Common mistake

Deep learning has made signal processing obsolete.

Audio and sensor models usually take a spectrogram or a filtered signal as input.

16.12Summary

  • Signal processing measures, filters and transforms sampled signals with exact mathematics; the course rests on linear system theory, not on learning.
  • Without fs > 2 fmax aliasing occurs: a 7 Hz sinusoid sampled at 8 Hz gives the same samples as a 1 Hz sinusoid and cannot be recovered.
  • The output of a linear time-invariant system is the convolution of the input with the impulse response; the length of a convolution is N + M − 1.
  • A filter works by frequency: [0.5, 0.5] is low-pass (|H| = cos(ω/2)), [1, −1] is high-pass.
  • The DFT turns time into frequency; the FFT gives the same result in N log2 N operations instead of N2. Resolution is fs/N; a window is used against leakage.

16.13Test Yourself

  1. A sensor's highest frequency is 3 kHz. What must the sampling rate be at least? At what frequency does a 5 kHz sinusoid sampled at 8 kHz appear?

    Answer

    By the Nyquist criterion fs > 2 · 3 = 6 kHz. 5 kHz is above the Nyquist frequency 4 kHz; it appears at |5 − 8| = 3 kHz. (The samples are the same as those of a 3 kHz sinusoid: 1; −0.71; 0; 0.71; −1; 0.71; 0; −0.71.)

  2. Convolve x = [1, 2, 1] and h = [1, 3].

    Answer

    Length 3 + 2 − 1 = 4. y[0] = 1·1 = 1; y[1] = 1·3 + 2·1 = 5; y[2] = 2·3 + 1·1 = 7; y[3] = 1·3 = 3. The result is [1, 5, 7, 3].

  3. What is the magnitude response of the filter y[n] = x[n] − x[n−1] at ω = π/2 and ω = π? What kind of filter is it?

    Answer

    The impulse response is [1, −1]; |H(ω)| = 2|sin(ω/2)|. At ω = π/2: 2 sin(π/4) ≈ 1.41. At ω = π: 2 sin(π/2) = 2. Since it is 0 at ω = 0, it is a high-pass filter.

  4. Compute the four-point DFT of x = [1, 1, 0, 0].

    Answer

    X[0] = 1 + 1 = 2. X[1] = 1 + (−j) = 1 − j. X[2] = 1 − 1 = 0. X[3] = 1 + j. The result is [2, 1 − j, 0, 1 + j]. Parseval: 1 + 1 = 2 and (4 + 2 + 0 + 2)/4 = 2.

  5. With fs = 44100 Hz, what is the frequency step of a N = 4096-point FFT? For the step to be at most 5 Hz, how many samples and how many seconds of recording are needed at least?

    Answer

    The step is 44100/4096 ≈ 10.77 Hz. For a step of at most 5 Hz we need N ≥ 44100/5 = 8820 samples; the duration is 8820/44100 = 0.2 s.

  6. An ADC is 12-bit over a ±1 V range. What is the quantization step? If the bit count goes to 16, how much smaller does the step become?

    Answer

    The step is 2/212 = 2/4096 ≈ 0.000488 V. At 16 bits it is 2/65536 ≈ 0.0000305 V; the step shrinks by 24 = 16 times. That is an improvement of about 4 · 6 = 24 dB in signal-to-noise ratio.

16.14After this course

Digital signal processing is the gateway to the third part: extended to two dimensions it becomes image processing, combined with language models it becomes speech recognition. Time series analysis and information theory also use its tools. On the hardware side, you can move on to embedded AI and communications.

Courses that build on or extend this one: Image Processing, Speech Recognition and Audio Processing.

Related careers: DSP engineer, Audio software engineer, Communications engineer, Embedded systems engineer, Biomedical engineer.

16.15Further reading

  • A. V. Oppenheim and R. W. Schafer, Discrete-Time Signal Processing (3rd ed., 2010): the standard textbook for the course; treats sampling, convolution, the z-transform and the DFT rigorously.
  • S. W. Smith, The Scientist and Engineer's Guide to Digital Signal Processing (1997): light on mathematics and heavy on intuition; suits anyone meeting convolution and the FFT for the first time.
  • J. G. Proakis and D. G. Manolakis, Digital Signal Processing (4th ed., 2007): works through filter design and spectral analysis with many solved examples.

16.16References

  1. Oppenheim and Schafer, Discrete-Time Signal Processing, 3rd ed., Pearson, 2010.
  2. Proakis and Manolakis, Digital Signal Processing: Principles, Algorithms, and Applications, 4th ed., Pearson, 2007.
  3. Smith, The Scientist and Engineer's Guide to Digital Signal Processing, California Technical Publishing, 1997.
  4. Cooley and Tukey, "An Algorithm for the Machine Calculation of Complex Fourier Series", Mathematics of Computation 19(90), 1965.

Related pages: Slides · Slides (PDF) · Course page