---
url: /dyad/dev/stdlib/DiscreteComponents/components/MovingAverageFilter.md
description: >-
  Dyad MovingAverageFilter docs.  Moving average filter with input-output
  relation y(z) = dfrac1N sum_i=0^N-1 u(z-i).
---

# `MovingAverageFilter` {#MovingAverageFilter}

Moving average filter with input-output relation $y(z) = \dfrac{1}{N} \sum\_{i=0}^{N-1} u(z-i)$.

Please note: this implementation of a moving average filter is not optimized for very large number of filter taps `N`.

The initial condition is selected through the `initialization` enum. Since the state is a shift register of delayed input samples, every variant sets all delayed samples to one common value:

* `InitialOutput(y0=...)`: the delayed input samples are chosen such that the first output equals `y0`, given the first input sample.

* `SteadyState`: the input history equals the first input sample, so the filter starts in equilibrium (the first output equals the first input).

* `InitialState(x0=...)`: all delayed input samples are set to `x0`.

For `N = 1` the filter has no delayed samples and `initialization` has no effect.

# Structural parameters: {#Structural-parameters:}

* `N`: Number of samples to average over

# Connectors: {#Connectors:}

* `u`: Input signal

* `y`: Output signal

## Usage {#Usage}

`DiscreteComponents.MovingAverageFilter(taps_init=_maf_taps_init(initialization))`

## Parameters: {#Parameters:}

|             Name |                       Description | Units |             Default value |
| ----------------:| ---------------------------------:| -----:| -------------------------:|
|              `N` | Number of samples to average over |     – |                         3 |
| `initialization` |   Initial-condition specification |     – | DiscreteCom...t(; y0=0.0) |

## Connectors {#Connectors}

* `u` - This connector represents a real signal as an input to a component ([`RealInput`](/stdlib/Dyad/connectors/RealInput#RealInput))

* `y` - This connector represents a real signal as an output from a component ([`RealOutput`](/stdlib/Dyad/connectors/RealOutput#RealOutput))

## Variables {#Variables}

|   Name |           Description | Units |
| ------:| ---------------------:| -----:|
| `taps` | Delayed input samples |     – |

## Behavior {#Behavior}

$$ \begin{align}
\mathtt{taps}\_{1}\left( t \right) &= u\left( t \right) \\
y\left( t \right) &= \frac{SymbolicUtils.Mapreducer{typeof(identity), typeof(Base.add_sum), Colon, Nothing}(identity, Base.add_sum, Colon(), nothing)\left( \mathtt{taps}\left( t \right) \right)}{3} \\
\mathtt{taps}\_{2}\left( t \right) &= Shift(t, -1)\left( \mathtt{taps}\_{1}\left( t \right) \right) \\
\mathtt{taps}\_{3}\left( t \right) &= Shift(t, -1)\left( \mathtt{taps}\_{2}\left( t \right) \right)
\end{align}
$$

## Source {#Source}

```dyad
"""
Moving average filter with input-output relation ``y(z) = \dfrac{1}{N} \sum_{i=0}^{N-1} u(z-i)``.

Please note: this implementation of a moving average filter is not optimized for very large number of filter taps `N`.

The initial condition is selected through the `initialization` enum. Since the state is a shift register of delayed input samples, every variant sets all delayed samples to one common value:
- `InitialOutput(y0=...)`: the delayed input samples are chosen such that the first output equals `y0`, given the first input sample.
- `SteadyState`: the input history equals the first input sample, so the filter starts in equilibrium (the first output equals the first input).
- `InitialState(x0=...)`: all delayed input samples are set to `x0`.

For `N = 1` the filter has no delayed samples and `initialization` has no effect.

# Structural parameters:
- `N`: Number of samples to average over

# Connectors:
- `u`: Input signal
- `y`: Output signal
"""
component MovingAverageFilter@[input clk extends Discrete]
  "Input signal"
  u = RealInput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -100, "y1": 450, "x2": 0, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "Output signal"
  y = RealOutput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 1000, "y1": 450, "x2": 1100, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "Number of samples to average over"
  structural parameter N::Integer = 3
  "Initial-condition specification"
  structural parameter initialization::InitialCondition = DiscreteComponents.InitialCondition.InitialOutput(y0 = 0.0)
  "Common initial value of the delayed input samples (only used by the InitialState variant)"
  final parameter taps_init::Real = _maf_taps_init(initialization)
  "Delayed input samples"
  variable taps::Real[N]
relations
  taps[1]@clk = u@clk
  for i in 2:N
    taps[i]@clk = taps[i - 1]@(clk-1)
  end
  y = sum(taps) / N
  switch initialization
    case InitialOutput
      initial taps@(clk-1) = fill((N * initialization.y0 - u@clk) / (N - 1), N)
    case SteadyState
      initial taps@(clk-1) = fill(u@clk, N)
    case InitialState
      initial taps@(clk-1) = fill(taps_init, N)
  end
metadata {
  "Dyad": {"icons": {"default": "dyad://DiscreteComponents/MovingAverageFilter.svg"}}
}
end
```

```dyad
"""
Moving average filter with input-output relation ``y(z) = \dfrac{1}{N} \sum_{i=0}^{N-1} u(z-i)``.

Please note: this implementation of a moving average filter is not optimized for very large number of filter taps `N`.

The initial condition is selected through the `initialization` enum. Since the state is a shift register of delayed input samples, every variant sets all delayed samples to one common value:
- `InitialOutput(y0=...)`: the delayed input samples are chosen such that the first output equals `y0`, given the first input sample.
- `SteadyState`: the input history equals the first input sample, so the filter starts in equilibrium (the first output equals the first input).
- `InitialState(x0=...)`: all delayed input samples are set to `x0`.

For `N = 1` the filter has no delayed samples and `initialization` has no effect.

# Structural parameters:
- `N`: Number of samples to average over

# Connectors:
- `u`: Input signal
- `y`: Output signal
"""
component MovingAverageFilter
  "Input signal"
  u = RealInput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -100, "y1": 450, "x2": 0, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "Output signal"
  y = RealOutput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 1000, "y1": 450, "x2": 1100, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "Number of samples to average over"
  structural parameter N::Integer = 3
  "Initial-condition specification"
  structural parameter initialization::InitialCondition = DiscreteComponents.InitialCondition.InitialOutput(y0 = 0.0)
  "Common initial value of the delayed input samples (only used by the InitialState variant)"
  final parameter taps_init::Real = _maf_taps_init(initialization)
  "Delayed input samples"
  variable taps::Real[N]
relations
  taps[1]@clk = u@clk
  for i in 2:N
    taps[i]@clk = taps[i - 1]@(clk-1)
  end
  y = sum(taps) / N
  switch initialization
    case InitialOutput
      initial taps@(clk-1) = fill((N * initialization.y0 - u@clk) / (N - 1), N)
    case SteadyState
      initial taps@(clk-1) = fill(u@clk, N)
    case InitialState
      initial taps@(clk-1) = fill(taps_init, N)
  end
metadata {
  "Dyad": {"icons": {"default": "dyad://DiscreteComponents/MovingAverageFilter.svg"}}
}
end
```

## Test Cases {#Test-Cases}

No test cases defined.

## Related {#Related}

* Examples

* Experiments

* Analyses
