Hauptinhalt

dsp.MovingMaximum

R2026b

Moving maximum

Description

The dsp.MovingMaximum System object™ determines the moving maximum of the input signal along each channel, independently over time. The object uses the sliding window method to determine the moving maximum. In this method, a window of specified length is moved over each channel, sample by sample, and the object determines the maximum of the data in the window.

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. For more details, see Algorithms. (since R2026b)

The dsp.MovingMaximum object and the movmax function both compute the moving maximum 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 determine the moving maximum of the input:

  1. Create the dsp.MovingMaximum 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

movMax = dsp.MovingMaximum returns a moving maximum object, movMax, using the default properties.

movMax = dsp.MovingMaximum(Len) sets the WindowLength property to Len.

example

movMax = dsp.MovingMaximum(PropertyName=Value) sets properties using one or more name-value arguments. For example, to enable tunable window length, set EnableTunableWindowLength to true and to specify a maximum window length of 30, set MaxWindowLength to 30.

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.

Option 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 object determines the maximum of the current sample and all the past samples.

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 the TunableWindowLength property to specify the window length.

  • false — The window length is not tunable. Use the WindowLength property to specify a fixed window length.

Dependencies

This property applies when you set SpecifyWindowLength to true.

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 uses MaxWindowLength.

  • 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 SpecifyWindowLength to true and EnableTunableWindowLength to true.

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 SpecifyWindowLength to true and EnableTunableWindowLength to true.

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 invalid TunableWindowLength values and issues a warning.

    • If the tunable window length value is greater than MaxWindowLength, the object sets its value to MaxWindowLength.

    • 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 SpecifyWindowLength to true and EnableTunableWindowLength to true.

Length of the sliding window in samples, specified as a positive scalar 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 SpecifyWindowLength to true and EnableTunableWindowLength to false. (since R2026b)

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

Usage

Description

y = movMax(x) determines the moving maximum of the input signal, x, using the sliding window method.

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 maximum is determined along each channel. The object also 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 | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

expand all

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

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

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 maximum of a sum of three sine waves with varying amplitude. Use a sliding window of length 30.

Initialization

Set up an input signal that is a sum of three sine waves with frequencies at 2 Hz, 5 Hz, and 10 Hz. The sampling frequency is 100 Hz. Create a dsp.MovingMaximum object with a window length of 30.

sin = dsp.SineWave(SampleRate=100,...
    Frequency=[2 5 10],...
    SamplesPerFrame=100);
movMax = dsp.MovingMaximum(30)
movMax = 

  dsp.MovingMaximum with properties:

          SpecifyWindowLength: true
    EnableTunableWindowLength: false
                 WindowLength: 30

Create a time scope for viewing the output.

scope  = timescope(SampleRate=100,...
    TimeSpanOverrunAction="Scroll",...
    TimeSpanSource="Property",...
    TimeSpan=10,ShowGrid=true,...
    YLimits=[-4.5 4.5]);

Compute the Moving Maximum

Each sine wave component of the input signal has a different amplitude that varies with the iteration. Use the movMax object to determine the maximum value of the current sample and the past 29 samples of the input signal.

for index = 1:100
    sin.Amplitude = rand(1,3);
    x = sum(sin(),2);
    xmax = movMax(x);
    scope([x,xmax])
end

Compute the moving maximum of a chirp signal and adapt the window length based on the instantaneous frequency. Use a longer window at low frequencies to capture the broad envelope. Use a shorter window at high frequencies to follow the rapid oscillations.

Create Chirp Signal

Create a dsp.Chirp object that sweeps from 1 Hz to 20 Hz. Set the sampling rate to 200 Hz and the frame size to 100 samples.

Fs = 200;
frameSize = 100;
numFrames = 20;
f0 = 1;
f1 = 20;
chirp = dsp.Chirp(InitialFrequency=f0,...
    TargetFrequency=f1,...
    SampleRate=Fs,...
    SamplesPerFrame=frameSize)
chirp = 
  dsp.Chirp with properties:

                 Type: 'Linear'
       SweepDirection: 'Unidirectional'
     InitialFrequency: 1
      TargetFrequency: 20
            SweepTime: 1
    InheritTargetTime: false
           TargetTime: 1
         InitialPhase: 0
      SamplesPerFrame: 100
           SampleRate: 200
       OutputDataType: 'double'

Create Moving Maximum Object

Create a dsp.MovingMaximum object with tunable window length enabled. Set the maximum window length to 100 and enable auto-adjustment to handle edge cases.

movMin = dsp.MovingMaximum(EnableTunableWindowLength=true,...
    TunableWindowLength=100,...
    MaxWindowLength=100,...
    AutoAdjustInvalidWindowLength=true)
movMin = 
  dsp.MovingMaximum with properties:

              SpecifyWindowLength: true
        EnableTunableWindowLength: true
              TunableWindowLength: 100
                  MaxWindowLength: 100
    AutoAdjustInvalidWindowLength: true

Create a time scope to view the output.

scope = timescope(SampleRate=Fs,...
    TimeSpanSource="property",TimeSpan=numFrames*frameSize/Fs,...
    TimeSpanOverrunAction="Scroll",...
    ShowGrid=true,YLimits=[-2 2],...
    Title="Adaptive Moving Maximum of Chirp Signal");

Compute the Adaptive Moving Maximum

At each frame, estimate the instantaneous frequency and derive the window length from it. As the chirp frequency increases, reduce the window length to track the faster oscillations. Add noise to the chirp signal to simulate a realistic scenario.

for index = 1:numFrames
    x = chirp() + 0.2*randn(frameSize,1);
    instFreq = f0 + (f1 - f0) * index/numFrames;
    wl = round(100 * (1 - instFreq/f1) + 5);
    movMin.TunableWindowLength = wl;
    xmin = movMin(x);
    scope([x, xmin])
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