MATLAB Examples

# Detect Signal Energy

This example shows how to compute the energy of a signal from the signal's RMS value and compares the energy value with a specified threshold. Detect the event when the signal energy is above the threshold.

Create a dsp.MovingRMS System object™ to compute the moving RMS of the signal. Set this object to use the sliding window method with a window length of 20. Create a dsp.TimeScope object to view the output.

```FrameLength = 20; Fs = 100; movrmsWin = dsp.MovingRMS(20); scope = dsp.TimeScope('SampleRate',Fs,... 'TimeSpanOverrunAction','Scroll',... 'TimeSpan',100,... 'ShowGrid',true,... 'YLimits',[-1.0 350],'LayoutDimensions',[3 1],'NumInputPorts',3); ```
```scope.ActiveDisplay = 1; scope.YLimits = [0 5]; scope.Title = 'Input Signal'; ```
```scope.ActiveDisplay = 2; scope.Title = 'Compare Signal Energy with a Threshold'; ```
```scope.ActiveDisplay = 3; scope.YLimits = [0 2]; scope.PlotType = 'Stairs'; scope.Title = 'Detect When Signal Energy Is Greater Than the Threshold'; ```

Create the input signal. The signal is a noisy staircase with a frame length of 20. The threshold value is 200. Compute the energy of the signal by squaring the RMS value and multiplying the result with the window length. Compare the signal energy with the threshold value. Detect the event, and when the signal energy crosses the threshold, mark it as 1.

```count = 1; Vect = [1/8 1/2 1 2 3 4 3 2 1]; index = 1; threshold = 200; for index = 1:length(Vect) V = Vect(index); for i = 1:80 x = V + 0.1 * randn(FrameLength,1); y1 = movrmsWin(x); y1ener = (y1(end)^2)*20; event = (y1ener>threshold); scope(y1,[y1ener,threshold],event); end end ```

You can customize the energy mask into a pattern that varies by more than a scalar threshold. You can also record the time for which the signal energy stays above or below the threshold.