A4 · Lesson 34 · Lab: measuring and plotting signals
A scope's measurements, a design sweep and plots worth showing, all in NumPy
A digital oscilloscope is mostly arithmetic on arrays: it captures a record of samples, then computes peak-to-peak, mean, RMS and frequency from them. Lessons 28 to 33 gave you the same tools: arrays, vectorized arithmetic and masks, two-dimensional grids, and plots that communicate. This lab builds a scope's measurements, sweeps a design over a grid, and makes the plots that would go in a report, with checks that the numbers are right and not merely plausible.
Predict
A record holds 61.7 cycles of . Its plain mean() is which?
A little off: 0.407 for one such record. The extra 0.7 of a cycle adds part of a hump that doesn't cancel, biasing the mean and the RMS. Scopes fix this by measuring over a whole number of cycles, between the first and last crossings of the same edge, which brings the mean back to 0.400 within a few microvolts.
Name it
Measurements on a record
- Peak-to-peak:
v.max() - v.min(). - Crossings: with
s = v - level, the upward crossings arenp.nonzero((s[:-1] < 0) & (s[1:] >= 0))[0]. Interpolating between the two samples around each one, , times it to a small fraction of a sample. - Frequency: the number of whole cycles between the first and last upward crossings, divided by the time between them.
- Mean and RMS: over the samples from the first upward crossing to the last, a whole number of cycles. The AC RMS is the RMS of
v - mean.
Sweeps on a grid
Make one parameter a column and the other a row, and broadcasting fills the whole table in one expression; argmax(axis=1) then finds the best column in each row.
Plots for a report
One idea per figure, labeled axes with units, a legend when there's more than one curve, log axes for quantities that span decades, and markers on the points that matter. Everything the reader needs should be on the figure itself.
Worked example
A 100 kHz capture of 5000 samples holds V. Measure its peak-to-peak, mean, AC RMS and frequency.
Show the solution
- Build the capture and measure it. Peak-to-peak from the extremes, the frequency from interpolated upward crossings, and the mean and RMS over whole cycles only.
import numpy as np
fs = 100e3
t = np.arange(5000) / fs
v = 0.4 + 2.0 * np.sin(2 * np.pi * 1234 * t)
vpp = v.max() - v.min() # 4.000 V
s = v - v.mean()
up = np.nonzero((s[:-1] < 0) & (s[1:] >= 0))[0]
tc = t[up] - s[up] / (s[up + 1] - s[up]) / fs # interpolated crossing times
f = (len(tc) - 1) / (tc[-1] - tc[0]) # 1234.0000 Hz
seg = v[up[0] + 1:up[-1] + 1] # 61 whole cycles
mean = seg.mean() # 0.400 V
rms = np.sqrt(np.mean((seg - mean)**2)) # 1.4143 V
- Peak-to-peak. It is 4.000 V.
- Frequency. From 61 cycles between interpolated crossings it is 1234.0000 Hz, far finer than the 100 kHz sample spacing suggests.
- Mean and RMS. Over the whole record the mean would read 0.407 V and the RMS 1.4135 V; over the 61 whole cycles they're 0.400 V and 1.4143 V, against the true 0.4 V and
- Why whole cycles. Measuring over whole cycles is worth a factor of about a thousand in accuracy here.
Answer 4.000 V peak-to-peak, a mean of 0.400 V, an AC RMS of 1.4143 V and a frequency of 1234.0000 Hz.
Worked example
A 1 V source with source resistance 50, 75, 100 or 600 Ω drives a load from 10 Ω to 1 kΩ. Find each source's best load and the power it delivers.
Show the solution
- Lay out every source against every load. Source resistances down a column and loads along a row give a (4, 201) table of powers.
Rs = np.array([50, 75, 100, 600])[:, None] # a column
RL = np.logspace(1, 3, 201)[None, :] # a row
P = 1.0**2 * RL / (Rs + RL)**2 # shape (4, 201)
best = RL[0, np.argmax(P, axis=1)]
print(best, P.max(axis=1))
- The best loads. They come out at 50.1, 75.9, 100 and 602.6 Ω, the grid points nearest each source resistance.
- The powers. They are 5.000, 3.333, 2.500 and 0.417 mW, which is Each source delivers the most power into a load equal to its own resistance.
- The grid limit. The grid's spacing, 2.3% from one point to the next, sets how close
argmaxcan land.
Answer each source does best into a load equal to its own resistance (50.1, 75.9, 100 and 602.6 Ω on the grid), delivering 5.000, 3.333, 2.500 and 0.417 mW.
Check
The starter captures v = −1.2 + 1·sin(2π·440·t) at 100 kHz for 5000 samples. Using the lesson's recipe, set vpp, freq (from interpolated upward crossings of v - v.mean()), and mean and rms (AC RMS) over the whole cycles between the first and last upward crossings.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A source of V = 5 V with internal resistance Rs drives a load RL. With Rs from the starter as a column and RL = np.logspace(0, 4, 401) as a row, compute the load power on the whole grid with broadcasting (no loops). Set P (shape (4, 401)), best to each row's best load and Pmax to each row's largest power.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
On one figure, plot the gain in dB of a second-order low-pass, , with f0 = 200 Hz, for = 0.5, 0.707 and 2, over f = np.logspace(np.log10(f0) - 2, np.log10(f0) + 2, 400). Use a log frequency axis, label both axes with units, and give each curve a legend label containing its Q, such as "Q = 0.707".
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The starter makes 10 000 readings with noise of standard deviation s = 0.5 mV around 1.000 V. Set counts, edges = np.histogram(err_mV, bins=50) for the errors in millivolts, within1 to the fraction of errors within one sample standard deviation of zero, and within3 to the fraction within three.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
An amplifier with gain g = 3 clips at ±L = 5 V. For an input of 3 V amplitude at 50 Hz over 40 ms (2000 samples), plot the output against time in ms as a line, then mark only the clipped samples with red dots ('r.'), using a mask. Label both axes. Set frac to the fraction of samples clipped.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A sine of amplitude 5 V is sampled finely over whole cycles. What fraction of the samples exceed V in size?
Explain why
Why does measuring a sine's mean and RMS over a whole number of cycles give a much better answer than using the whole record?
A sine averages to zero, and its square averages to exactly half its peak squared, only over whole cycles. A leftover fraction of a cycle adds a piece of a hump that has no matching piece to cancel it, so the mean picks up a bias and the RMS a small error, both of which shrink only slowly as the record gets longer. Cutting the record at the first and last crossings of the same edge keeps exactly whole cycles, so the cancellation is complete and the remaining error comes only from the sampling, which is far smaller.
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.