A219 of 29
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 N samples spread evenly over one cycle of Acos⁡(θ+φ). The dashed curve is the wave they came from.
  • Change N, 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?

Name it

Samples in a list

A wave sampled at rate fs is recorded at the times tn=n/fs. 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:

Vrms=1N∑n=0N−1vn2

Over whole cycles of a sine wave this gives A/2.

Zero crossings and frequency

An upward zero crossing lies between a negative sample vn and a non-negative one vn+1. Drawing a straight line between them puts the crossing at

t=1fs(n+−vnvn+1−vn)

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:

I=2N∑nvncos⁡2πnN,Q=2N∑nvnsin⁡2πnN

For vn=Acos⁡(2πn/N+φ), these give I=Acos⁡φ and Q=−Asin⁡φ, so A=I2+Q2 and φ is the angle of the point (I,−Q). 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
  1. Average the squares. The squares are 0, 1, 0 and 1, which average to 24=0.5
  2. RMS. 0.5=0.707
  3. Amplitude. 0.707×2=1 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 −0.3 V at t = 1.0 ms and 0.5 V at t = 1.1 ms. Where does the wave cross zero?

Show the solution
  1. 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.
  2. Fraction of the way. 0.30.8=0.375
  3. Crossing time. t=1.0+0.375×0.1=1.0375ms

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 I and Q.

Show the solution
  1. List the cosines and sines. With N=4, the cosines at n=0,1,2,3 are 1,0,−1,0 and the sines are 0,1,0,−1.
  2. The sum I. I=24(1.414+0+1.414+0)=1.414
  3. The sum Q. Q=24(0−1.414+0−1.414)=−1.414
  4. Amplitude. A=1.4142+1.4142=2
  5. Phase. The angle of (I,−Q)=(1.414,1.414) is 45°.

Answer the amplitude is 2 and the phase is 45°: the samples came from 2cos⁡(θ+45°).

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 −0.6 V at t = 1 s and 0.9 V at t=1+0.1 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 Acos⁡(2πfn/fs+φ) for n=0,1,…,N−1, 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 I and Q 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?

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.