A4 Python for engineers · Practice
Vectorized arithmetic, broadcasting and masks
Problems dealt one at a time, with new numbers every time. The kinds you miss come back more often, and you can keep going as long as you like.
Check
For the sine vin in the starter, set vout to the output of an amplifier with gain g = 3 that clips at ±L = 12 V, and frac to the fraction of samples that are clipped (where ), with no loops.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set vout to a half-wave rectifier's output with a diode drop of Vd = 0.6 V: the input minus Vd where that's positive, otherwise 0. Then set avg to its mean.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Using broadcasting, set H to a table of RC low-pass gains with one row per corner in fcs and one column per frequency in f. Then set col to the column for 1 kHz.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
For the sampled signal in the starter, set ups to the array of indexes i where it crosses zero going up (sample i below 0, sample i + 1 at or above 0), with no loops, and peak_at to the index of its largest sample.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The starter samples a PWM signal (0 or 3.3 V) at 1 MHz for 20 periods. Using masks and no loops, set duty to the fraction of samples that are high, rises to the indexes i where sample i is low and sample i + 1 is high, and period to the average spacing between rises, in seconds.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set n_in to how many samples of x (made in the starter) lie between lo = 1 and hi = 3 inclusive, using a combined mask.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A constant current I = 0.5 A is sampled every dt = 0.001 s for 100 samples. Set q to the running charge np.cumsum(i) * dt, and q_end to its last value.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
A kind of problem counts as solid once you've got it right on the first try twice in a row. Back to the lesson · Practice every topic in A4