# Apply a moving average filter to a signal: moving_average

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

The moving_average command applies a moving average to a sample.

- moving_average takes two arguments:
  - A, an array of numeric values representing a sampled signal.
  - n, a positive integer n.
- moving_average(A,n) 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.

#### Examples.

- 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: