C11 · Lesson 28 · Project: characterizing a filter from noisy data
Measuring a Bode plot and fitting its corner
A filter on the bench, a function generator on its input and a two-channel scope on its input and output: that's the setup for measuring a frequency response. At each frequency the scope captures a noisy record of both channels. A cursor reading of each trace's peak would be crude; a few lines of code give the amplitude and phase of each sine to a fraction of a percent, even through noise, by projecting each record onto a sine and cosine at the known frequency (lesson 16's lock-in, done in software). Then a least-squares fit turns a dozen noisy gain readings into one corner frequency. In this project you'll write all three steps.
- The dots are six measured gains of an RC low-pass, each with a few tenths of a dB of noise. The curve is the first-order model for the corner you choose.
- The readout is the sum of squared differences between the dots and the curve. Move to make it as small as you can: that's a least-squares fit, done by hand.
- The best fit lands within a few hertz of the true 1.59 kHz, even though no single dot sits exactly on the true curve.
Predict
A filter's output measures −1.45 dB relative to its input at 1 kHz. If it's a first-order low-pass, where is its corner?
About 1.6 kHz. Solving gives , so kHz. A single reading this close to 0 dB is sensitive to noise, though: a 0.1 dB error moves the answer by about 4%. A fit over many frequencies does much better.
Name it
The pieces
Amplitude and phase by projection. Over a whole number of periods of frequency f, a record v(t) of N samples gives
and if plus noise and any DC, then and :
The DC and the noise at other frequencies average out over whole periods.
A Bode point. With amplitude and phase for input and output at the same frequency, the gain is dB and the phase is , wrapped into −180° to 180°.
The fit. For a first-order low-pass, . Choose to minimize . Search in , since corners span decades: a coarse scan finds the valley, then golden-section search narrows it.
Single-point formulas
From one gain reading, ; from one phase reading, . They're useful checks, but each rests on one noisy number.
Worked example
Projection of a record at 1 kHz gives a = 0.537 V and b = 0.273 V. What are the amplitude and phase?
Show the solution
- Amplitude.
- Phase.
Answer an amplitude of 0.602 V and a phase of −26.9°.
Worked example
A low-pass shows −32.1° of phase at 1 kHz. Where is its corner?
Show the solution
- Corner from the phase. This agrees with the gain reading.
- Where phase works best. Phase is most sensitive near the corner, where it changes fastest, so phase readings between 0.3 and 3 times give the best single-point estimates.
Answer the corner is at 1.59 kHz.
Check
A first-order low-pass measures −0.5 dB at 1000 Hz. Where is its corner?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
A first-order low-pass shows −15° of phase at 1000 Hz. Where is its corner?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
Projection gives a = 0.8 V and b = −0.6 V. What is the sine's amplitude?
Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.
Check
The input's amplitude is 1 V and the output's 0.05 V. What is the gain in dB?
Check
Write amp_phase(t, v, f): t and v are lists of sample times and values covering a whole number of periods of frequency f. Return (A, phi), the amplitude and phase (radians) of the component , by projection.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write bode_point(t, vin, vout, f): return (gain_db, phase_deg) of the output relative to the input at frequency f, with the phase wrapped into −180° to 180°. Use your amp_phase (or the projection inside it).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write fit_corner(f, g): f is a list of frequencies and g the measured gains in dB of a first-order low-pass. Return the corner frequency that minimizes .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does fitting a model to many gain readings give a better corner frequency than solving for it from the single reading nearest the corner?
Each reading carries its own random error, and solving for the corner from one reading passes that error straight into the answer. A least-squares fit uses every reading at once: the random errors at different frequencies are independent, so they partly cancel, and the corner that best explains all of them is much less sensitive to any one. Readings far from the corner also pin down the model's shape (flat below, falling 20 dB per decade above), so the fit uses information a single point can't. The error of the fit shrinks roughly as one over the square root of the number of useful readings.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.