### 5.27.10 Apply a moving average filter to a signal : moving_average

moving_average takes two arguments: an array A of numeric values representing the sampled signal and a positive integer n. It returns an array B obtained by applying a moving average filter of length n to A. The elements of B are defined by

B[i]= |
1
n
n−1
∑
j=0
| A[i+j]

for i=0,1,…,L−n, where L is the length of A.

Moving average filters are fast and useful for smoothing time-encoded signals. For example, input :

snd:=soundsec(2):;
noise:=randvector(length(snd),normald,0,0.05):;
data:=0.5*threshold(3*sin(2*pi*220*snd),[-1.0,1.0])+noise:;
plotwav(createwav(data),range=[1000,1500])

Output :

Input :

fdata:=moving_average(data,25):;
plotwav(createwav(fdata),range=[1000,1500])

Output :