A10 · Lesson 5 · Lab: Fourier series in code
Coefficients, synthesis, Gibbs and Parseval by computer
Lessons 1 to 4 found Fourier coefficients by integrating by hand. A computer does those integrals numerically for any waveform, including measured ones with no formula. In this lab you'll compute a square wave's coefficients, rebuild it from its harmonics and measure the Gibbs overshoot that never goes away, confirm Parseval's theorem by summing ten thousand terms, compute total harmonic distortion, and filter the square wave harmonic by harmonic.
- The curve is the sum of a ±1 square wave's first odd harmonics, .
- Add harmonics: the edges sharpen and the ripple crowds toward them, but the first overshoot stays at about 9% of the jump.
- That's the Gibbs phenomenon: a finite sum can't follow a jump, and the overshoot's height doesn't shrink, only its width.
Predict
The partial sums of a ±1 square wave's series overshoot the jump. As you add more and more harmonics, the peak of the overshoot:
It stays near 1.179. The overshoot moves closer to the jump and gets narrower, so the series still converges at every point away from the edge, but its height approaches , about 9% of the jump above the flat top. Filters with sharp cutoffs ring the same way on step inputs, for the same reason.
Name it
Coefficients by integration
For a period sampled finely, the sine coefficients are
and the trapezoid rule (np.trapezoid) evaluates the integral. For a measured waveform the same sum works on the samples, and that's what an FFT does efficiently for all at once.
Synthesis
Adding up to terms rebuilds the waveform. Smooth waveforms converge quickly; ones with jumps converge slowly and show the Gibbs overshoot of about 9% of the jump.
Power and distortion
Parseval's theorem: the mean square is . Total harmonic distortion compares everything above the fundamental with the fundamental:
For a square wave it's .
Filtering harmonic by harmonic
A linear filter multiplies each harmonic by and shifts its phase. Low-pass filtering a square wave cuts the higher harmonics more, rounding the corners.
Worked example
A ±1 V square wave at 1 kHz passes through an RC low-pass with its corner at 1 kHz. How do the 3rd harmonic and the fundamental compare at the output?
Show the solution
- At the input. The 3rd harmonic is of the fundamental.
- The filter's gains. The filter passes the fundamental with gain and the 3rd with .
- At the output. The output looks much more like a sine than the input does, though a single pole is a gentle filter.
Answer at the output the 3rd harmonic is 0.149 of the fundamental, down from a third at the input.
Worked example
What is the THD of a ±1 square wave?
Show the solution
- The harmonics. They are for odd , and the total mean square is 1.
- The fundamental's share.
- The harmonics' share. The rest is in the harmonics:
- The THD.
- What it means. A function generator's square output is 48% distortion by this measure, which is why "THD" is only meaningful for something meant to be a sine.
Answer the THD is 0.483, or 48%.
Check
A ±1 square wave at 1 kHz passes through an RC low-pass with its corner at 2 kHz. What is the ratio of the output's 3rd harmonic to its fundamental?
Check
A ±1 square wave is rebuilt from its odd harmonics up to and including 5. What RMS error remains?
Check
For a ±1 square wave of period 1, sampled at 200 001 points, compute , and by the trapezoid rule and set b. (The wave is np.sign(np.sin(2 * np.pi * t)).)
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Sum the first 200 odd harmonics ( up to 399) of the ±1 square wave on 40 001 points over and set peak to the largest value.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Check Parseval's theorem: set total to over the square wave's odd harmonics up to = 20 001, which should approach the mean square, 1.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write thd(amps) returning the total harmonic distortion of a list of harmonic amplitudes (fundamental first). Set t_sq for the square wave's odd harmonics up to = 9999 (with zeros for the even ones), and exact to .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Filter the square wave through an RC low-pass with its corner at the fundamental: multiply each odd harmonic (up to 999) by . Set ratio3 to the output's 3rd harmonic over its fundamental and thd_out to the output's THD.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why doesn't the Gibbs overshoot shrink as you add more harmonics, even though the series converges?
Each added harmonic sharpens the transition, so the overshoot crowds closer to the jump and gets narrower, roughly in proportion to . But its height is set by the shape of the transition, which is the same shape scaled narrower: the partial sum near the jump approaches a fixed function of , the sine integral, whose peak is 1.179. At any fixed point away from the jump, the overshoot eventually passes by and the series converges there; it just never converges uniformly. In practice it means a perfectly sharp low-pass filter will always ring by about 9% on a step, and smoother filters trade that ringing for a slower edge.
Practice
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.