A4 · Lesson 28 · Arrays with NumPy
Doing the same calculation on a million numbers at once
A scope captures a waveform as thousands of samples. To find its RMS value you square every sample, average them and take the square root. With NumPy, that's one line, and it doesn't care whether there are ten samples or ten million.
Predict
With import numpy as np, what does np.array([1, 2, 3]) * 2 give?
array([2, 4, 6]). Arithmetic on a NumPy array acts on every element at once. A plain Python list behaves differently: [1, 2, 3] * 2 repeats the list, giving [1, 2, 3, 1, 2, 3]. That difference is why engineering code uses arrays.
Name it
Arrays act element by element
import numpy as np brings in NumPy. Its arrays hold many numbers and do arithmetic on all of them together:
import numpy as np
t = np.linspace(0, 0.02, 1001) # 1001 times from 0 to 20 ms
v = 5 * np.sin(2 * np.pi * 50 * t) # a 50 Hz sine, sampled at each time
np.linspace(a, b, n) gives n evenly spaced values from a to b, both ends included. np.arange(a, b, step) steps from a up to, but not including, b. NumPy's functions, np.sin, np.exp, np.sqrt, np.log10 and the rest, work on whole arrays.
Reducing an array to one number: v.max(), v.min(), v.mean(), v.sum(), and len(v) for how many there are. The RMS value is
rms = np.sqrt(np.mean(v**2))
which reads exactly like its name: the root of the mean of the squares.
Picking elements. v[0] is the first element and v[-1] the last; v[10:20] is elements 10 to 19. A comparison gives an array of True and False, which can pick elements or be counted:
high = v > 2.5 # True where the voltage is above 2.5 V
np.mean(high) # the fraction of samples above it
v[high] # just those samples
Finding things. np.argmax(v) is the index of the largest element and np.argmin(np.abs(x - target)) the index of the element nearest a target.
Worked example
A 10 V amplitude, 50 Hz sine drives a 100 Ω resistor. Find the average power and the fraction of time the voltage is above 5 V in size.
Show the solution
- Sample one period and work on whole arrays. Each line acts on all 2001 samples at once.
import numpy as np
t = np.linspace(0, 0.02, 2001) # one period
v = 10 * np.sin(2 * np.pi * 50 * t)
P = np.mean(v**2) / 100 # about 0.5 W
frac = np.mean(np.abs(v) > 5) # about 0.667
- The average power. The mean of is half the amplitude squared: so 50 over 100 Ω is 0.5 W.
- The fraction above 5 V. for two-thirds of each cycle.
Answer the average power is 0.5 W, and the voltage is above 5 V in size two-thirds of the time.
Worked example
Find the times when a sampled signal first goes above a threshold.
Show the solution
- Compare the whole array with the threshold.
x >= 0.9is an array of True and False. - Find the first True.
np.argmaxreturns the first True, since True counts as 1.
t = np.linspace(0, 1, 1001)
x = 1 - np.exp(-t / 0.2)
i = np.argmax(x >= 0.9) # index of the first True
print(t[i]) # 0.461
- Check. The exact answer is
Answer the signal first reaches 0.9 at the sample at , just past the exact 0.4605.
Check
Make t, 2001 evenly spaced times from 0 to 0.05 s, and v, the voltage at each of them. Then set vmax to the largest value in v.
A = 4 and f = 50 are already defined.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The samples in v cover exactly 4 cycles of a sine of amplitude 8. Compute their RMS value as rms.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The array E12 holds the E12 values in one decade. Set nearest to the E12 resistor, in ohms, nearest to target = 5300 Ω. (Look in the decade that contains the target.)
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
For an array v of 1000 samples, what is v[-1]?
Check
The starter samples V every 0.1 µs for one period. Use np.diff to set slope to the rate of change between successive samples (in volts per second), and slew to its largest size. Then set rel to how far slew is from the exact peak rate , as a fraction.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
What is len(np.arange(0, 1, 0.25))?
Explain why
Why does np.sqrt(np.mean(v**2)) give the RMS value of a sampled waveform?
RMS stands for root mean square, and the expression does those steps from the inside out: v**2 squares every sample, np.mean averages the squares, and np.sqrt takes the square root of that average. With samples spread evenly over whole cycles, the average of the samples is a good stand-in for the average over time, so the result matches the RMS of the continuous waveform.
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.