Documentation

dsp.MovingAverage System object

Moving average

Description

Thedsp.MovingAverageSystem 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 is moved over the data, sample by sample, and the average is computed over the data in the window. 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, seeAlgorithms.

The object accepts multichannel inputs, that is,m-by-nsize inputs, wherem≥ 1, andn> 1. The object also accepts variable-size inputs. Once the object is locked, you can change the size of each input channel. However, the number of channels cannot change. This object supports C and C++ code generation.

To compute the moving average of the input:

  1. Create adsp.MovingAverageobject and set the properties of the object.

  2. Callstepto compute the moving average.

Note

Alternatively, instead of using thestep方法来执行操作defined by the System object, you can call the object with arguments, as if it were a function. For example,y = step(obj,x)andy = obj(x)perform equivalent operations.

Construction

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

movAvg = dsp.MovingAverage(Len)sets theWindowLengthproperty toLen.

movAvg = dsp.MovingAverage(Name,Value)specifies additional properties usingName,Valuepairs. Unspecified properties have default values.

Example:

movAvg = dsp.MovingAverage('Method','Exponential weighting','ForgettingFactor',0.9);

Properties

expand all

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

  • 'Sliding window'— A window of length specified bySpecifyWindowLengthis 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, seeAlgorithms.

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 theWindowLengthproperty.

  • 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.

This property applies when you setMethodto'Sliding window'.

Length of the sliding window, specified as a positive scalar integer. This property applies when you setMethodto'Sliding window'andSpecifyWindowLengthtotrue.

Exponential weighting factor, specified as a positive real scalar in the range (0,1]. This property applies when you setMethodto'Exponential weighting'. 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. All the past samples are given an equal weight. This property is tunable. You can change its value even when the object is locked.

Methods

reset Reset internal states ofSystem object
step Moving average of input signal
Common to All System Objects
release

Allow System object property value changes

Examples

expand all

计算一个嘈杂的斜坡信号的移动平均线using thedsp.MovingAverageobject.

Initialization

Set upmovavgWindowandmovavgExpobjects.movavgWindowuses the sliding window method with a window length of 10.movavgExpuses the exponentially weighting method with a forgetting factor of 0.9. Create a time scope for viewing the output.

FrameLength = 1001; Fs = 1000; movavgWindow = dsp.MovingAverage(10); movavgExp = dsp.MovingAverage('Method','Exponential weighting',...'ForgettingFactor',0.9); scope = dsp.TimeScope('SampleRate',Fs,...'TimeSpanOverrunAction','Scroll',...'TimeSpan',2,...'ShowGrid',true,...'YLimits',[-0.5 1.5]); title ='Sliding Window Average(blue) and Exponentially Weighted Average(red)'; scope.Title = title;

Compute the Average

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

fori = 1:500 t = (0:0.001:1)'; unitstep = t>=0; ramp = t.*unitstep; x = ramp + 0.1 * randn(FrameLength,1); y1 = movavgWindow(x); y2 = movavgExp(x); scope([x,y1,y2]);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

Introduced in R2016b

Was this topic helpful?