Skip to content
LIBRARY
MovingAverageFilter.md

MovingAverageFilter ​

Moving average filter with input-output relation       .

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

Usage ​

DiscreteComponents.MovingAverageFilter(taps_init=_maf_taps_init(initialization))

Parameters: ​

NameDescriptionUnitsDefault value
NNumber of samples to average over–3
initializationInitial-condition specification–DiscreteCom...t(; y0=0.0)

Connectors ​

  • u - This connector represents a real signal as an input to a component (RealInput)

  • y - This connector represents a real signal as an output from a component (RealOutput)

Variables ​

NameDescriptionUnits
tapsDelayed input samples–

Behavior ​

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
Flattened 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 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 ​

No test cases defined.

  • Examples

  • Experiments

  • Analyses