Main Content

dsp.MovingAverage

Moving average

Description

The dsp.MovingAverage System object™ computes the moving average of the input signal along each channel independently over time. The object uses either the sliding window method or the exponential weighting method to compute the moving average. In the sliding window method, a window of specified length moves over the data sample by sample, and the block computes the average over the data in the window. To change the length of the sliding window during simulation, pass the window length as an input to the object along with the data input. For an example, see Tune Window Length During Simulation. (since R2026a) In the exponential weighting method, the object multiplies the data samples with a set of weighting factors. The average is computed by summing the weighted data. For more details on these methods, see Algorithms.

The dsp.MovingAverage object and the movmean function both compute the moving average of the input signal. However, the object can process large streams of real-time data and handle system states automatically. The function performs one-time computations on data that is readily available and cannot handle system states. For a comparison between the two, see System Objects vs MATLAB Functions.

To compute the moving average of the input:

  1. Create the dsp.MovingAverage object and set its properties.

  2. Call the object with arguments, as if it were a function.

To learn more about how System objects work, see What Are System Objects?

Creation

Description

movAvg = dsp.MovingAverage returns a moving average object, movAvg, using the default properties.

movAvg = dsp.MovingAverage(Len) sets the WindowLength property to Len.

movAvg = dsp.MovingAverage(Len,Overlap) sets the WindowLength property to Len and the OverlapLength property to Overlap.

movAvg = dsp.MovingAverage(PropertyName=Value) specifies additional properties using Name,Value pairs. Unspecified properties have default values.

Example: movAvg = dsp.MovingAverage(Method="Exponential weighting",ForgettingFactor=0.9);

example

Properties

expand all

Unless otherwise indicated, properties are nontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and the release function unlocks them.

If a property is tunable, you can change its value at any time.

For more information on changing property values, see System Design in MATLAB Using System Objects.

Averaging method, specified as "Sliding window" or "Exponential weighting".

  • "Sliding window" — A window of length specified by SpecifyWindowLength is moved over the input data along each channel. For every sample the window moves by, the object computes the average over the data in the window.

  • "Exponential weighting" — The object multiplies the samples with a set of weighting factors. The magnitude of the weighting factors decreases exponentially as the age of the data increases, never reaching zero. To compute the average, the algorithm sums the weighted data.

For more details on these methods, see Algorithms.

Flag to specify a window length, specified as a scalar boolean.

  • true — The length of the sliding window is equal to the value you specify in the WindowLength property.

  • false — The length of the sliding window is infinite. In this mode, the average is computed using the current sample and all the past samples.

Dependencies

This property applies when you set Method to "Sliding window".

Since R2026a

Source of the window length, specified as one of these:

  • "Property" –– Specify the window length using the WindowLength property.

  • "Input port" –– Specify the window length as an input argument.

Dependencies

To enable this property, set:

  • Method to "Sliding window".

  • SpecifyWindowLength to true.

Data Types: char | string

Length of the sliding window in samples, specified as a positive integer that is less than or equal to the value you specify in the MaxWindowLength property.

Dependencies

To enable this property, set:

  • Method to "Sliding window".

  • SpecifyWindowLength to true.

  • WindowLengthSource to "Property". (since R2026a)

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Since R2026a

Maximum length of the sliding window in samples, specified as a positive integer.

Dependencies

To enable this property, set:

  • Method to "Sliding window".

  • SpecifyWindowLength to true.

  • WindowLengthSource to "Input port".

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Overlap length between sliding windows, specified as a nonnegative integer. The value of overlap length varies in the range [0, WindowLength − 1]. If not specified, the overlap length is WindowLength − 1.

When you set the WindowLengthSource property to "Input port", the object assumes an overlap length of WindowLength − 1 and provides one sample of output for every sample of input. (since R2026a)

Dependencies

To enable this property, set:

  • Method to "Sliding window".

  • SpecifyWindowLength to true.

  • WindowLengthSource to "Property". (since R2026a)

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Exponential weighting factor, specified as a non-negative real scalar in the range [0,1]. A forgetting factor of 0.9 gives more weight to the older data than does a forgetting factor of 0.1. A forgetting factor of 1.0 indicates infinite memory and all the past samples are given an equal weight. A forgetting factor of 0 indicates no memory and the past samples have no weight on the current computation.

Since this property is tunable, you can change its value even when the object is locked.

Tunable: Yes

Dependencies

To enable this property, set Method to "Exponential weighting".

Data Types: single | double

Usage

Description

y = movAvg(x) computes the moving average of the input signal, x, using either the sliding window method or exponential weighting method.

example

y = movAvg(x,WL) computes the moving average of the input signal using a sliding window of length WL. (since R2026a)

example

Input Arguments

expand all

Data input, specified as a vector or a matrix. If x is a matrix, each column is treated as an independent channel. The moving average is computed along each channel.

The object accepts variable-size inputs. Once the object is locked, you can change the size of each input channel, but you cannot change the number of channels.

Data Types: single | double
Complex Number Support: Yes

Since R2026a

Length of the sliding window in samples, specified as a positive integer that is less than or equal to the value you specify in the MaxWindowLength property.

To enable this input argument, set:

  • Method to "Sliding window".

  • SpecifyWindowLength to true.

  • WindowLengthSource to "Input port".

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

expand all

Moving average of the input signal, returned as a vector or a matrix.

When you input a signal of size m-by-n to the object, and if you set Method to "Sliding window" and SpecifyWindowLength to true, the output has an upper bound size of ceil(m/hop size)-by-n. Hop size is window length − overlap length. In other cases, the output has a size of m-by-n.

When you generate code from this object, the variable-size behavior of the output in the generated code depends on the input frame length and whether the size of the input signal is fixed or variable. For more details, see Code Generation.

Data Types: single | double
Complex Number Support: Yes

Object Functions

To use an object function, specify the System object as the first input argument. For example, to release system resources of a System object named obj, use this syntax:

release(obj)

expand all

stepRun System object algorithm
releaseRelease resources and allow changes to System object property values and input characteristics
resetReset internal states of System object

Examples

collapse all

Compute the moving average of a noisy ramp signal using the dsp.MovingAverage object.

Initialization

Set up movavgWindow, movavgWindow_overlap, and movavgExp objects. movavgWindow uses the sliding window method with a window length of 50 samples and a default overlap length of 49 samples, which is one sample less than the specified window length. movavgWindow_overlap uses a window length of 50 samples and an overlap length of 45 samples. movavgExp uses the exponentially weighting method with a forgetting factor of 0.95.

Create a time scope for viewing the output.

FrameLength = 1001;
Fs = 1000;
movavgWindow = dsp.MovingAverage(50);
movavgWindow_overlap = dsp.MovingAverage(50,45);
movavgExp = dsp.MovingAverage(Method='Exponential weighting',...
    ForgettingFactor=0.95);
scope  = timescope(SampleRate=[Fs, Fs, Fs/(50-45), Fs],...
    TimeSpanOverrunAction='Scroll',...
    ShowGrid=true,...
    YLimits=[-0.5 1.5]);
title = 'Moving Average';
scope.Title = title;
scope.ChannelNames = {'Original Signal',...
    'Sliding window of 50 samples with default overlap',...
    'Sliding window of 50 samples with an overlap of 45 samples',...
    'Exponential weighting with forgetting factor of 0.95'};

Compute the Average

Generate a ramp signal with an amplitude of 1 and a time span of 2 seconds. Apply the sliding window average and exponentially weighted average to the ramp. View the output in the time scope.

for i = 1:500
    t = (0:0.001:1)';
    unitstep = t>=0;
    ramp = t.*unitstep;
    x = ramp + 0.1 * randn(FrameLength,1);
    y1 = movavgWindow(x);
    y2 = movavgWindow_overlap(x);
    y3 = movavgExp(x);
    scope(x,y1,y2,y3);
end

Since R2026a

Create two dsp.MovingAverage objects, one with a fixed window length of 50, and the other whose window length varies with the input noise power. Compute the moving average of a noisy square wave signal using these objects. Initialize a time scope to view and compare the moving average outputs.

FrameLength = 100;
Fs = 100;
movAvgFixedWL = dsp.MovingAverage(WindowLength=50);
movavgWL = dsp.MovingAverage(WindowLengthSource="Input port",MaxWindowLength=1000);
scope  = timescope(SampleRate=Fs,...
    TimeSpanOverrunAction='Scroll',...
    TimeSpanSource='Property',...
    TimeSpan=1000,...
    ShowGrid=true,...
    BufferLength=1e7,...
    YLimits=[-30,40]);
title = 'Moving Average with Tunable Window Length';
scope.Title = title;
scope.ChannelNames = {'Noisy signal',...
    'Moving average with fixed window length (50)',...
    'Moving average with varying window length'};

Create a noisy square wave signal. The variance of the noise changes every time the signal amplitude changes. Vary the window length of one of the dsp.MovingAverage objects such that it is directly proportional to the noise power. Higher the noise power, greater is the window length.

Compute the moving average of the noisy square wave signal with a fixed window length of 50 and with the varying window length. Compare the two outputs in the time scope.

When the window length is large, the moving average output is smooth even though there is more noise.

count = 1;
noisepower =  [1 7 10 1];
signalBase = 10*[1 2 -1 3];
for index = 1:length(noisepower)
    np = noisepower(index);
    yexp = signalBase(index)*ones(FrameLength,1);
    WL = 50*np;
    for i = 1:250
        x = yexp+sqrt(np)*randn(FrameLength,1);
        y1 = movAvgFixedWL(x);
        y2 = movavgWL(x,WL);
        scope(x,y1,y2)
    end
end

Algorithms

expand all

References

[1] Bodenham, Dean. “Adaptive Filtering and Change Detection for Streaming Data.” PH.D. Thesis. Imperial College, London, 2012.

Extended Capabilities

expand all

Version History

Introduced in R2016b

expand all