Skip to content
Tutorials Center
My learning

Topics

Lesson 64 of 68 ยท Projects

Project: A Signal Analysis Lab

Build a signal lab: synthesize a noisy signal, find its frequencies with the FFT, filter it, then integrate and differentiate numerically and measure the errors.

Every sensor, microphone, accelerometer and radio receiver hands an engineer the same thing: a list of numbers sampled over time, buried in noise. This project builds a small laboratory for such data, using the tools from the whole course. You make a signal whose contents you know, look at it in the frequency domain, clean it up with two kinds of filter, and then integrate and differentiate it numerically.

Because you know the true signal, you can measure every error exactly, and that is the real point. Real signals never come with an answer key; synthetic ones do, so that is where you learn which methods you can trust. The background is in The Fourier Transform and the FFT, Numerical Integration and Floating-Point Error and Conditioning.

flowchart LR
    S["Synthesize<br/>clean + noise"] --> F["FFT<br/>spectrum"]
    F --> P["Find the peaks"]
    P --> L["Filter<br/>FFT mask or low-pass"]
    L --> I["Integrate<br/>(trapezoid)"]
    L --> D["Differentiate<br/>(central difference)"]
    I --> E["Compare with<br/>exact answers"]
    D --> E

The lab pipeline: because we synthesized the signal, every stage can be scored against the truth.

Step 1: synthesize the signal

Sample at fs = 1000 samples per second for 2 seconds, which gives 2,000 points. The true signal has two sine waves, 50 Hz with amplitude 1 and 120 Hz with amplitude 0.5. We add Gaussian noise with standard deviation 0.6 from a fixed random seed, so the result is the same every time.

Three quick checks tell you the data is what you meant to make. The root-mean-square (RMS) value of two sines of amplitudes A1 and A2 is sqrt(A1**2/2 + A2**2/2), which is sqrt(0.625) = 0.7906. The noise RMS should be close to its standard deviation. And the signal-to-noise ratio in decibels is 10*log10(signal power / noise power).

Python Synthesize and look at the signal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The clean RMS is 0.7906 as predicted, the noise RMS is 0.601, and the SNR is only 2.38 dB: the noise carries almost as much power as the signal. In the time plot, the two sine waves are hard to see by eye.

The sampling rate must be more than twice the highest frequency you care about. Half the sampling rate, here 500 Hz, is the Nyquist frequency. A tone above it does not disappear; it folds back and shows up at a false lower frequency, called aliasing.

Step 2: the Fourier spectrum

The FFT turns 2,000 time samples into 1,001 frequency bins (rfft keeps only the non-negative frequencies). With 2 seconds of data, the bins are spaced 1/T = 0.5 Hz apart. Dividing the magnitude by N/2 converts each bin into the amplitude of the sine wave that sits at that frequency.

Both of our tones fit a whole number of cycles in the window (100 cycles and 240 cycles), so they fall exactly on bins and there is no spectral leakage. A tone that does not fit a whole number of cycles smears across neighbouring bins unless you apply a window function.

Python Fourier spectrum

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The spectrum shows two clear spikes: 50.0 Hz with amplitude 0.956 and 120.0 Hz with amplitude 0.494, against a noise floor near 0.02. The true amplitudes are 1 and 0.5. The small differences are real: each bin contains the tone plus the random noise that landed in that bin, so measured amplitudes wobble by about the noise floor. Hidden in the time plot, the tones are obvious in the frequency domain because the signal's energy is concentrated in two bins while the noise is spread over a thousand.

Step 3: filter the noise away

Two filters, two philosophies.

An FFT mask. Transform, zero every bin except those within 3 Hz of the two peaks, transform back with irfft. This is the sharpest possible filter, and it works here because we know where the tones are. Such a "brick wall" can cause ringing near transients, and it is not usable on streaming data.

A Butterworth low-pass filter. A fourth-order filter with cutoff 150 Hz, applied with filtfilt, which runs it forward and then backward so the output has zero phase shift. It is a general tool: you only need to know that the interesting content is below 150 Hz.

Measure each by the RMS of its error against the clean signal. Lower is better.

Python Filter the noise away

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The error drops from 0.601 (unfiltered) to 0.315 with the low-pass filter and to 0.097 with the FFT mask. Both numbers can be predicted. White noise has equal power in every bin, so a filter that keeps a fraction p of the bandwidth keeps a fraction p of the noise power, and the error RMS is about 0.6 * sqrt(p). The low-pass keeps 150 of 500 Hz, so 0.6 * sqrt(0.3) = 0.33. The mask keeps 22 bins out of 1,001 (about 2.2 percent), which gives 0.6 * sqrt(0.022) = 0.09. The rest of the mask's error is the noise that sat inside those bins. The narrower the filter, the more noise it removes, but only if you know where the signal is.

The FFT mask looks almost too good because we cheated: we told it the exact frequencies. On real data you would read them off the spectrum, as in Step 2, and a mistake in the guess removes real signal.

Step 4: integrate and differentiate numerically

For the clean signal the exact integral and derivative are known:

  • Integral from 0 to t: (1 - cos(w1*t))/w1 + 0.5*(1 - cos(w2*t))/w2, with w1 = 2*pi*50 and w2 = 2*pi*120.
  • Derivative: w1*cos(w1*t) + 0.5*w2*cos(w2*t).

cumulative_trapezoid approximates the running integral and np.gradient the derivative using central differences. Run each on the clean samples and on the noisy ones. Integration averages noise; differentiation amplifies it, because a difference of two noisy neighbours divided by a tiny dt is a huge number. That asymmetry is one of the most practically important facts in signal processing.

Then the step-size study: sample a pure 50 Hz tone at several rates and measure the derivative error. Central differences have an error proportional to dt**2, so doubling the sampling rate should cut the error by four.

Python Integrate and differentiate numerically

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Integration shrugs off the noise: the trapezoid sum is off by 0.00011 on clean samples and 0.0161 on noisy ones (the true integral never exceeds about 0.008, so this error is the noise piling up as a random walk, with a typical size of 0.6 * dt * sqrt(2000), about 0.03; compare that with the raw noise amplitude of 0.6). Differentiation behaves very differently. On clean samples the derivative error is 39.9, about 6 percent of the 691 amplitude, which is the truncation error of a 1 ms step on a 120 Hz wave. On noisy samples the error is 1,661, more than twice the size of the true derivative: differencing multiplies the noise standard deviation by about 1/(sqrt(2)*dt) = 707, giving a typical noise derivative of 424 and peaks above 1,500 over 2,000 samples.

The step-size study confirms the second-order rule: 76.4, 20.3, 5.14, 1.29, 0.323 as fs doubles, each about a quarter of the previous one.

The practical conclusion is the lesson of the whole lab: filter first, then differentiate, or fit a smooth model to the data and differentiate that. Never difference raw noisy samples.

Extending the lab

  • Add a third tone at 300 Hz and sample at 500 Hz instead of 1000: watch it alias to a false frequency.
  • Shift a tone off the bin grid (say 50.25 Hz) and compare the spectrum with and without a Hann window (np.hanning).
  • Differentiate after filtering: apply the low-pass filter first, then np.gradient, and measure the new error.
  • Measure SNR improvement in decibels for each filter: 20*log10(0.601/0.315) for the low-pass is about 5.6 dB.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. The signal is sampled at 1000 Hz. What is the highest frequency it can represent without aliasing?

    The Nyquist frequency is half the sampling rate. A tone above 500 Hz would fold back to a false lower frequency.
  2. 2. With 2 seconds of data, how far apart are the FFT bins?

    Bin spacing is 1/T, where T is the duration of the recording. Longer recordings give finer frequency resolution.
  3. 3. Why is the error after a 150 Hz low-pass filter (0.315) larger than after the FFT mask (0.097)?

    White noise is spread evenly over frequency, so the noise power kept is proportional to the bandwidth kept. A wide filter lets in more noise.
  4. 4. Why did differentiating the noisy samples give such a huge error compared with integrating them?

    Differentiation amplifies high-frequency content (noise lives there); integration smooths it. This is why you filter before differentiating.
  5. 5. Doubling the sampling rate cut the central-difference derivative error by about a factor of 4. Why?

    Central differences have a truncation error proportional to dt^2, so halving dt divides the error by four.

Summary

  • A synthetic signal with known contents lets you measure the error of every method exactly.
  • The FFT shows hidden tones: 50 Hz and 120 Hz stand out against a noise floor near 0.02 even though the SNR is only 2.4 dB.
  • Filtering removes noise power in proportion to the bandwidth removed: error RMS fell from 0.601 to 0.315 (low-pass) and 0.097 (FFT mask).
  • Integration averages noise (error 0.016), differentiation amplifies it (error 1,661 versus a signal amplitude of 691).
  • Central-difference derivative error scales as dt**2: 76.4, 20.3, 5.14, 1.29, 0.323 as the rate doubles.
  • Filter first, then differentiate; never differentiate raw noisy samples.

Next: Common Mistakes in Calculus and How to Avoid Them.