Course materials · Microprocessor Applications

Arrays and plots

English translation

Learning objectives

Misinterpreting array axes or time intervals can give a physically incorrect meaning even when the calculation runs.

Array axes and two kinds of multiplication

Starting conditions

A=[123456]A=\begin{bmatrix}1&2&3\\4&5&6\end{bmatrix}, B=[201301]B=\begin{bmatrix}2&0\\1&3\\0&1\end{bmatrix}.

Procedure

  1. Before execution, write down A's sums and means by axis and the shapes of A*A and A@B.
  2. Calculate using NumPy and check the (0,1) element of A@B by hand.
  3. Record the result array and shape for each axis and operation in a table.

Results to retain

import numpy as np
A=np.array([[1,2,3],[4,5,6]])
B=np.array([[2,0],[1,3],[0,1]])
print(A@B)
# TODO: Complete the axis aggregations and predicted-shape table.

Record results and self-check

Operation:

Expected shape:

Actual shape:

Value and meaning:

Points to check

Time series axes and units

Starting conditions

Use t=[00.10.20.30.4]t=\begin{bmatrix}0&0.1&0.2&0.3&0.4\end{bmatrix} s and V=[00.81.31.61.8]V=\begin{bmatrix}0&0.8&1.3&1.6&1.8\end{bmatrix} V.

Procedure

  1. Check the number of elements in the voltage and time arrays, and draw a line plot.
  2. Use np.diff(V)/np.diff(t) to calculate interval slopes and plot them at interval midpoints.
  3. Calculate the first interval's slope by hand and compare with the code.

Results to retain

import numpy as np
import matplotlib.pyplot as plt
t=np.array([0,.1,.2,.3,.4]);v=np.array([0,.8,1.3,1.6,1.8])
plt.plot(t,v,'o-');plt.xlabel('Time [s]');plt.ylabel('Voltage [V]');plt.grid()
# TODO: Calculate the slope and midpoint of each interval.

Record results and self-check

Interval [s]:

ΔV [V]:

Δt [s]:

Slope [V/s]:

Points to check

Noise and smoothing

Starting conditions

With seed=7, create 101 points over 0≤t≤20\le t\le 2 s and add noise with a standard deviation of .2 to sin⁡(2πt)\sin(2\pi t).

Procedure

  1. Apply np.convolve(...,mode="same") to the same noisy signal using uniform kernels of lengths 3 and 9.
  2. Plot the original signal, noisy signal, and two filter outputs on the same time axis. Calculate both RMSE values from the difference between the original signal and filter output over the common interval [4:-4], excluding the four samples at each end.
  3. Record kernel length, comparison sample count, and RMSE in two rows. Use the plots and RMSE to distinguish apparent smoothness from closeness to the original signal.

Results to retain

rng=np.random.default_rng(7);t=np.linspace(0,2,101)
truth=np.sin(2*np.pi*t);observed=truth+rng.normal(0,.2,len(t))
# TODO: Compare moving averages using np.convolve and the common interior interval.

Record results and self-check

Window:

Comparison interval:

RMSE:

Observed changes:

Points to check