A2 · Lesson 19 · Lab: measuring sine waves
What a digital oscilloscope does with its samples
A digital oscilloscope never sees a smooth sine wave. It sees a list of numbers, samples taken thousands or millions of times a second, and every measurement it shows you (amplitude, RMS, frequency, phase) is computed from that list. In this lab you'll write those measurements yourself in Python, using only loops, lists and the math module. If you haven't started A4 yet, its lessons on functions, loops and lists (lessons 1 to 9) cover everything used here.
- The stems are samples spread evenly over one cycle of . The dashed curve is the wave they came from.
- Change , the amplitude and the phase. The samples land in different places on the curve.
- Watch the readout of the samples' RMS. Does it depend on where the samples land?
Predict
Eight samples are spread evenly over exactly one cycle of a 1 V sine wave. What is their RMS?
It's exactly 0.7071 V. The squares of evenly spaced samples of a cosine always average to exactly ½ over a whole cycle, as long as there are at least three samples per cycle: , and the terms cancel when added around the circle. Samples that miss the peaks are made up for by others that land on the steep parts. Over a fraction of a cycle the same measurement would be off, which is why scopes measure over whole cycles.
Name it
Samples in a list
A wave sampled at rate is recorded at the times . In Python, a list comprehension builds the whole record in one line:
v = [A * math.cos(2 * math.pi * f * n / fs + phi) for n in range(N)]
RMS
The RMS of a list is the square root of the mean of the squares:
Over whole cycles of a sine wave this gives .
Zero crossings and frequency
An upward zero crossing lies between a negative sample and a non-negative one . Drawing a straight line between them puts the crossing at
The time from the first upward crossing to the last, divided by the number of cycles between them, is the period.
Amplitude and phase from sums
Multiply each sample of one cycle by a cosine and by a sine at the same frequency, and add:
For , these give and , so and is the angle of the point . In Python that angle is math.atan2(-Q, I), which picks the right quadrant. This is how lock-in amplifiers and network analyzers measure phase.
Worked example
The four samples 0, 1, 0, −1 cover one cycle of a sine wave. What are their RMS and the wave's amplitude?
Show the solution
- Average the squares. The squares are 0, 1, 0 and 1, which average to
- RMS.
- Amplitude. Four samples are enough, even though two of them are zero.
Answer the RMS is 0.707, and the amplitude is 1.
Worked example
A record sampled every 0.1 ms has V at = 1.0 ms and V at = 1.1 ms. Where does the wave cross zero?
Show the solution
- Compare the rises. The voltage rises by 0.8 V over 0.1 ms, and it has to rise 0.3 V to reach zero.
- Fraction of the way.
- Crossing time.
Answer the wave crosses zero at 1.0375 ms.
Worked example
One cycle is sampled four times: 1.414, −1.414, −1.414, 1.414. Find the amplitude and phase from the sums and .
Show the solution
- List the cosines and sines. With , the cosines at are and the sines are .
- The sum I.
- The sum Q.
- Amplitude.
- Phase. The angle of is 45°.
Answer the amplitude is 2 and the phase is 45°: the samples came from .
Check
4 samples are spread evenly over exactly one cycle of a sine wave of amplitude 2.5 V. What is their RMS?
Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.
Check
Two neighboring samples are V at = 1 s and V at s. Where does a straight line between them cross zero?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Check
Write samples(A, f, phi_deg, fs, N), returning a list of the N values for , with the phase given in degrees. Then write rms(v).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write up_crossings(v, fs), returning the times of every upward zero crossing (a negative sample followed by one that's zero or positive), found by straight-line interpolation. Then write frequency(v, fs), which uses the first and last crossings to measure the frequency.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write amplitude_phase(v) for a list v holding exactly one cycle. It forms the sums and from the reading above and returns (A, phi_deg), the amplitude and the phase in degrees, using math.atan2(-Q, I) for the angle.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
A scope measures a sine wave's RMS from a record that holds three and a half cycles, and reads a little off. Why, and what does measuring over whole cycles fix?
The RMS averages , and for a sine wave : a constant plus a ripple at twice the frequency. Over whole cycles the ripple averages to exactly zero, leaving , so the RMS is exactly . A record of three and a half cycles includes a leftover half cycle, and the ripple over that piece doesn't average to zero, so it adds or takes away a little depending on where the record starts. The error shrinks as the record gets longer, since the leftover piece is a smaller share of the whole. Cutting the record to whole cycles, found from the zero crossings, removes it entirely, which is why scopes and meters measure "over an integer number of cycles".
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.