dsp.MovingRMS
R2026bMoving root mean square (RMS)
Description
The dsp.MovingRMS
System object™ computes the moving root mean square (RMS) 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 RMS. In the sliding window method, a window
of specified length is moved over the data, sample by sample, and the RMS is computed over the
data in the window. In the exponential weighting method, the object squares the data samples,
multiplies them with a set of weighting factors, and sums the weighed data. The object then
computes the RMS by taking the square root of the sum.
You can make the window length tunable by setting
the EnableTunableWindowLength property to true. In
this mode, use the TunableWindowLength property to change the window
length even after you pass some data to the object and the object is locked. The
MaxWindowLength property specifies the maximum allowed window
length. (since R2026b)
For more details, see Algorithms.
To compute the moving RMS of the input:
Create the
dsp.MovingRMSobject and set its properties.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
Syntax
Description
returns a moving RMS
object, movRMS = dsp.MovingRMSmovRMS, using the default properties.
sets the movRMS = dsp.MovingRMS(Len)WindowLength property to Len.
sets the MovRMS = dsp.MovingRMS(Len,Overlap)WindowLength property to Len and the
OverlapLength property to Overlap.
specifies additional properties using movRMS = dsp.MovingRMS(PropertyName=Value)Name=Value
pairs. For example, to specify an exponential weighting factor of 0.2,
set ForgettingFactor to
0.2.
Properties
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.
Moving RMS 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 RMS over the data in the window."Exponential weighting"— The object multiplies the squares of 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 RMS, the algorithm sums the weighted data, and takes a square root of the sum.
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 theWindowLengthproperty.false— The length of the sliding window is infinite. In this mode, the RMS is computed using the current sample and all past samples.
Dependencies
This property applies when you set Method
to "Sliding window".
Since R2026b
Option to enable tunable window length, specified as a scalar boolean.
true— The window length is tunable, that is, you can change its value even after you pass some data to the object and the object is locked. Use theTunableWindowLengthproperty to specify the window length.false— The window length is not tunable. Use theWindowLengthproperty to specify a fixed window length.
Dependencies
This property applies when you set:
Methodto"Sliding window"SpecifyWindowLengthtotrue
Since R2026b
Tunable sliding window length in samples, specified as a positive integer in the
range [1, MaxWindowLength]. You can change the value of this
property even when the object is locked.
When you set AutoAdjustInvalidWindowLength to
true, the object adjusts invalid values and issues a warning:
If the tunable window length value is greater than
MaxWindowLength, the object usesMaxWindowLength.If the tunable window length value is less than 1, the object uses 1.
If the tunable window length value is not an integer, the object uses the floored value.
When you set AutoAdjustInvalidWindowLength to
false, the object throws an error for invalid values.
Tunable: Yes
Dependencies
This property applies when you set:
Methodto"Sliding window"SpecifyWindowLengthtotrueEnableTunableWindowLengthtotrue
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Since R2026b
Maximum value of the tunable window length, specified as a positive integer. The
TunableWindowLength property must be less than or equal to this
value.
Dependencies
This property applies when you set:
Methodto"Sliding window"SpecifyWindowLengthtotrueEnableTunableWindowLengthtotrue
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Since R2026b
Option to automatically adjust invalid window length values, specified as a scalar boolean.
true— The object adjusts invalidTunableWindowLengthvalues and issues a warning.If the tunable window length value is greater than
MaxWindowLength, the object sets its value toMaxWindowLength.If the tunable window length value is less than 1, the object sets its value to 1.
If the tunable window length value is not an integer, the object uses the floored value.
false— The object throws an error when the tunable window length value is invalid.
Dependencies
This property applies when you set:
Methodto"Sliding window"SpecifyWindowLengthtotrueEnableTunableWindowLengthtotrue
Length of the sliding window in samples, specified as a positive integer. You cannot
tune the value of this property after the object is locked. To tune the window length,
set the EnableTunableWindowLength property to
true.
Dependencies
This property applies when you set:
Methodto"Sliding window"SpecifyWindowLengthtotrueEnableTunableWindowLengthtofalse
(since R2026b)
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.
Dependencies
This property applies when you set:
Methodto"Sliding window"SpecifyWindowLengthtotrueEnableTunableWindowLengthtofalse
(since R2026b)
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Exponential weighting factor, specified as a nonnegative 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
This property applies when you set Method
to "Exponential weighting".
Data Types: single | double
Usage
Syntax
Description
Input Arguments
Data input, specified as a vector or a matrix. If x is a
matrix, each column is treated as an independent channel. The moving RMS 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
Output Arguments
Moving RMS 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)
Examples
Compute the moving RMS of a noisy square wave signal with varying amplitude using the dsp.MovingRMS object.
Initialization
Set up movrmsWin, movrmsWin_overlap, and movrmsExp objects. movrmsWin uses the sliding window method with a window length of 20 samples and a default overlap length of 19 samples, which is one sample less than the specified window length. movrmsWin_overlap uses a window length of 20 samples and an overlap length of 15 samples. movrmsExp uses the exponentially weighting method with a forgetting factor of 0.995.
Create a time scope for viewing the output.
FrameLength = 10; Fs = 100; movrmsWin = dsp.MovingRMS(20); movrmsWin_overlap = dsp.MovingRMS(20,15); movrmsExp = dsp.MovingRMS(Method="Exponential weighting",... ForgettingFactor=0.995); scope = timescope(SampleRate=[Fs,Fs,Fs/(20-15),Fs],... TimeSpanOverrunAction="Scroll",... TimeSpanSource="Property",... TimeSpan=100,... ShowGrid=true,... YLimits=[-1.0 5.5]); title = "Moving RMS"; scope.Title = title; scope.ChannelNames = {"Original Signal",... "Sliding window of 20 samples with default overlap",... "Sliding window of 20 samples with an overlap of 15 samples",... "Exponential weighting with forgetting factor of 0.995"};
Compute the RMS
Generate a noisy square wave signal. Vary the amplitude of the square wave after a given number of frames. Apply the sliding window method and the exponential weighting method to this signal. View the output in the time scope.
count = 1; Vect = [1/8 1/2 1 2 3 4]; for index = 1:length(Vect) V = Vect(index); for i = 1:160 x = V + 0.1 * randn(FrameLength,1); y1 = movrmsWin(x); y2 = movrmsWin_overlap(x); y3 = movrmsExp(x); scope(x,y1,y2,y3); end end

Monitor the RMS level of a streaming signal whose amplitude changes over time. Adapt the window length inversely to the signal amplitude so that loud transients are tracked with a shorter window for fast response and quiet passages use a longer window for a smooth baseline.
Create Input Signal
Create a sine wave signal with a frequency of 10 Hz to simulate a baseline audio signal. Set the sampling rate to 1000 Hz and the frame size to 256 samples.
Fs = 1000;
frameSize = 256;
numFrames = 40;
sine = dsp.SineWave(Frequency=10,SampleRate=Fs,...
SamplesPerFrame=frameSize)sine =
dsp.SineWave with properties:
Amplitude: 1
Frequency: 10
PhaseOffset: 0
ComplexOutput: false
Method: 'Trigonometric function'
SamplesPerFrame: 256
SampleRate: 1000
OutputDataType: 'double'
Create Moving RMS Object
Create a dsp.MovingRMS object with tunable window length enabled. Set the maximum window length to 50 and the initial tunable window length to 40 (long window for smooth baseline). Enable auto-adjustment to handle edge cases.
movRMS = dsp.MovingRMS(EnableTunableWindowLength=true,... TunableWindowLength=40,... MaxWindowLength=50,... AutoAdjustInvalidWindowLength=true)
movRMS =
dsp.MovingRMS with properties:
Method: 'Sliding window'
SpecifyWindowLength: true
EnableTunableWindowLength: true
TunableWindowLength: 40
MaxWindowLength: 50
AutoAdjustInvalidWindowLength: true
Create a timescope object to view the input signal and the RMS level.
scope = timescope(SampleRate=Fs,... TimeSpanSource="property",TimeSpan=numFrames*frameSize/Fs,... TimeSpanOverrunAction="Scroll",... ShowGrid=true,YLimits=[-4 4],ChannelNames={"Noisy Signal","RMS Level"},... LayoutDimensions=[2 1],NumInputPorts=2);
Apply Adaptive RMS Level Monitoring
The signal amplitude increases linearly over time. At each frame, scale the sine wave by a growing gain factor and add noise. Adapt the window length inversely to the amplitude so that louder signals are tracked with a shorter window for faster response.
As the signal amplitude increases over time, the adaptive algorithm uses a progressively shorter window to track the rising RMS level more closely.
for index = 1:numFrames x = sine(); gain = 0.1 + 2.9*(index/numFrames); noisySignal = gain*x + 0.2*randn(frameSize,1); wl = round(50 - 45*(index/numFrames)); movRMS.TunableWindowLength = wl; y = movRMS(noisySignal); scope(noisySignal,y) end

Generate C code for a moving RMS computation with a tunable window length parameter. Verify that the window length appears as a tunable parameter in the generated code.
Create Entry-Point Function
Create an entry-point function that instantiates a dsp.MovingRMS object with tunable window length enabled. The function accepts a data input and a tunable window length value.
type movingRMSTunableWL.mfunction y = movingRMSTunableWL(x,wl) %#codegen
%movingRMSTunableWL Moving RMS with tunable window length
% y = movingRMSTunableWL(x,wl) computes the moving RMS of
% the input x using a window length that can be changed at runtime.
persistent movRMS
if isempty(movRMS)
movRMS = dsp.MovingRMS(EnableTunableWindowLength=true,...
MaxWindowLength=31,...
AutoAdjustInvalidWindowLength=true);
end
movRMS.TunableWindowLength = wl;
y = movRMS(x);
end
Generate C Code
Generate C code for the entry-point function using codegen. Specify the input data as a 256-by-1 column vector of doubles and the window length as a scalar double.
codegen movingRMSTunableWL -args {zeros(256,1),0} -config:lib -report
Code generation successful: View report
Verify Tunable Parameter in Generated Code
Inspect the generated code to verify that TunableWindowLength appears as a parameter that can be changed at runtime without regenerating code.
The generated code contains the TunableWindowLength field in the persistent object state, confirming that you can change the window length at runtime in the deployed code.
Algorithms
In the sliding window method, the output for each input sample is the RMS of the current sample and Len – 1 previous samples. Len is the length of the window in samples. To compute the first output sample, the algorithm waits until it receives the hop size number of input samples. Hop size is defined as window length – overlap length. Remaining samples in the window are considered to be zero. As an example, if the window length is 5 and the overlap length is 2, then the algorithm waits until it receives 3 samples of input to compute the first sample of the output. After generating the first output, it generates the subsequent output samples for every hop size number of input samples.
When you do not specify the window length, the algorithm chooses an infinite window length. In this mode, the output is the moving RMS of the current sample and all the previous samples in the channel.
Consider an example of computing the moving RMS of a streaming input data using the sliding window method. The algorithm uses a window length of 4 and an overlap length of 3. With each input sample that comes in, the window of length 4 moves along the data.
![Sliding window method for moving RMS with window length 4 and input samples [–1, –2, 3, 2, 5, 2]. At each time step n=0 through n=5, the window fills with zeros until length 4 is reached, then moves along the data. Moving RMS computes rms for each window position, producing values 0.5, 1.118, 1.871, 2.121, 3.24, 3.24 at n=5](movrms_slidewin.png)
In the exponential weighting method, the moving RMS is computed recursively using these formulas:
— Moving RMS at the current sample
— Square of the current input data sample
— Moving RMS at the previous sample
λ — Forgetting factor
— Weighting factor applied to the current data sample
— Effect of the previous data on the RMS
For the first sample, where N = 1, the algorithm chooses = 1. For the next sample, the weighting factor is updated and used to compute the RMS, as per the recursive equation. As the age of the data increases, the magnitude of the weighting factor decreases exponentially and never reaches zero. In other words, the recent data has more influence on the current RMS than the older data.
The value of the forgetting factor determines the rate of change of the weighting factors. 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 previous samples are given an equal weight.
Here is an example of computing the moving RMS using the exponential weighting method. The forgetting factor is 0.9.
![Exponential weighting method for moving RMS with forgetting factor 0.9. Three input frames: [2,3,4,5] at n=0 producing outputs [2.000, 2.575, 3.176, 3.798], [6,7,8,9] at n=1 producing outputs [4.437, 5.093, 5.765, 6.451], and [3,4,6,8] at n=2 producing outputs [6.025, 5.761, 5.796, 6.151]](movrms_expwei.png)
When you specify the window length to be tunable, the moving RMS algorithm maintains an internal buffer of size MaxWL−1 to store past input samples, where MaxWL is the maximum window length. At each time step, the moving RMS algorithm performs these operations:
Concatenates the buffer with the current input to form an extended data sequence.
Extracts the most recent window length samples from the extended data sequence.
Computes the moving RMS over the extracted samples using a sliding window and discards the endpoints.
Stores the most recent MaxWL−1 samples in the buffer for the next time step.
This approach allows you to change the window length at each time step while the algorithm maintains continuity of the output signal.
References
[1] Bodenham, Dean. “Adaptive Filtering and Change Detection for Streaming Data.” PH.D. Thesis. Imperial College, London, 2012.
Extended Capabilities
Usage notes and limitations:
See System Objects in MATLAB Code Generation (MATLAB Coder).
When you set
Methodto"Sliding window"andSpecifyWindowLengthtotrue, and generate code from this object, the variable-size behavior of the output depends on the input frame length and whether the size of the input signal is fixed or variable.See this table for more details.
Input signal Input size Output signal Fixed-size m-by-n When the input frame length is a multiple of the hop size, the output signal has a fixed-size of (m/hop size)-by-n.
When input frame length is not a multiple of the hop size, the output signal is variable-sized and has an upper bound of
ceil(m/hop size)-by-n.Variable-size m-by-n Output is a variable-size signal.
Output has an upper bound size of
ceil(m/hop size)-by-n.
Version History
Introduced in R2016bYou can now change the window length even after the object is locked by setting the
EnableTunableWindowLength property to true. Use
the TunableWindowLength property to specify the window length. The
MaxWindowLength property defines the upper bound for the tunable
window length. Use the AutoAdjustInvalidWindowLength property to
control whether invalid window length values are automatically adjusted with a warning or
cause an error.
You can now specify a forgetting factor of 0 in the dsp.MovingRMS
object.
Starting in R2022b, if you generate code from this object with the
Method property set to "Sliding
window" and the SpecifyWindowLength
property set to true, and if you input a signal with
a frame length that is a multiple of the hop size (window length −
overlap length), this object generates a fixed-size output signal in the
generated code. For more details, see Code Generation.
See Also
Objects
dsp.MovingAverage|dsp.MovingMaximum|dsp.MovingMinimum|dsp.MovingStandardDeviation|dsp.MovingVariance|dsp.MedianFilter
Blocks
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)