Arrays and plots

English translation

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

Before practice

01 What do array rows and columns represent?

Description

Define what the data represent, such as samples and features or time and channels, before interpreting axis-wise operations.

02 Which signal features disappear with stronger smoothing?

Description

Sudden changes and small features are reduced, and some filters may also introduce delay.

Practice

Preview practice notebookDownload practice notebook

1. 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

Items to record and points to check
Operations
Expected shape
Actual shape
Value and meaning

Points to check

  • Do the inner dimensions match for matrix multiplication?
  • Did you check (A@B)[0,1] rather than the meaning of A@B[0,1]?

2. 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

Items to record and points to check
Interval [s]
ΔV [V]
Δt [s]
Slope [V/s]

Points to check

  • Did you handle the difference in length between sample values and interval values?
  • Did you label the axes with units as well as numbers?

3. 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

Items to record and points to check
Window
Comparison interval
RMSE
Observed changes

Points to check

  • Did you use the same boundary handling and comparison interval?
  • Did you avoid generating new noise for each condition?