5.11.3. Exercise: Indexing/slicing/filtering unidimensional arrays 3#
You recorded a noisy signal:
signal = np.array(
[
0.436, 0.493, 0.467, 0.467, 0.482, 0.497, 0.474, 0.493, 0.474, 0.467,
0.470, 0.455, 0.482, 0.501, 0.470, 0.482, 0.467, 0.467, 0.558, 0.413,
0.463, 0.444, 0.463, 0.417, 0.528, 0.455, 0.486, 0.459, 0.490, 0.459,
]
)
You want to smooth this signal using a moving average with a window of three samples. This means that you want to create a new array where each value is the average of the three neighbour values of the raw signal, as illustrated in Fig. 5.10.
Using only one line, apply such a filter on the raw, noisy signal. Then, plot both the raw and filtered signal on the same figure to verify your result.
Show code cell content
import numpy as np
import matplotlib.pyplot as plt
filtered = (signal[0:-2] + signal[1:-1] + signal[2:]) / 3
plt.plot(signal, label="raw")
plt.plot(filtered, label="filtered")
plt.legend();