# Envelope Detection

This example shows how to implement two common methods of envelope detection. One method uses squaring and lowpass filtering. The other uses the Hilbert transform. This example illustrates MATLAB® and Simulink® implementations.

## Contents

- Introduction
- Method 1: Squaring and Lowpass Filtering
- Method 2: The Hilbert Transform
- MATLAB Example: Initialization
- MATLAB Example: Stream Processing Loop
- MATLAB Example: Envelope Detector Results
- Simulink Example: Method 1 (Squaring and Lowpass Filtering)
- Simulink Example: Method 2 (The Hilbert Transform)
- Simulink Example: Envelope Detector Model
- Simulink Example: Envelope Detector Results
- Simulink Model

## Introduction

The signal's envelope is equivalent to its outline, and an envelope detector connects all the peaks in this signal. Envelope detection has numerous applications in the fields of signal processing and communications, one of which is amplitude modulation (AM) detection. The following block diagram shows the implementation of the envelope detection using the two methods.

## Method 1: Squaring and Lowpass Filtering

This envelope detection method involves squaring the input signal and sending this signal through a lowpass filter. Squaring the signal effectively demodulates the input by using itself as its own carrier wave. This means that half the energy of the signal is pushed up to higher frequencies and half is shifted down toward DC. You then downsample this signal to reduce the sampling frequency. You can do downsampling if the signal does not have any high frequencies which could cause aliasing. Otherwise an FIR decimation should be used which applies a lowpass filter before downsampling the signal. After this, pass the signal through a minimum-phase, lowpass filter to eliminate the high frequency energy. Finally you are left with only the signal envelope.

To maintain the correct scale, you must perform two additional operations. First, you must amplify the signal by a factor of two. Since you are keeping only the lower half of the signal energy, this gain matches the final energy to its original energy. Second, you must take the square root of the signal to reverse the scaling distortion that resulted from squaring the signal.

This envelope detection method is easy to implement and can be done with a low-order filter, which minimizes the lag of the output.

## Method 2: The Hilbert Transform

This envelope detection method involves creating the analytic signal of the input using the Hilbert transform. An analytic signal is a complex signal, where the real part is the original signal and the imaginary part is the Hilbert transform of the original signal.

Mathematically the envelope e(t) of a signal x(t) is defined as the magnitude of the analytic signal as shown by the following equation.

where

is the Hilbert transform of x(t).

You can find the Hilbert transform of the signal using a 32-point Parks-McClellan FIR filter. To form the analytic signal, you then multiply the Hilbert transform of the signal by sqrt(-1) (the imaginary unit) and add it to the time-delayed original signal. It is necessary to delay the input signal because the Hilbert transform, which is implemented by an FIR filter, will introduce a delay of half the filter length.

You find the envelope of the signal by taking the absolute value of the analytic signal. The envelope is a low frequency signal compared to the original signal. To reduce its sampling frequency, to eliminate ringing and to smooth the envelope, you downsample this signal and pass the result through a lowpass filter.

## MATLAB Example: Initialization

Initialize required variables such as those for the frame size and file name. Creating and initializing your System objects before they are used in a processing loop is critical for getting optimal performance.

Fs = 22050; numSamples = 10000; DownsampleFactor = 15; frameSize = 10*DownsampleFactor;

Create a sine wave System object and set its properties to generate two sine waves. One sine wave will act as the message signal and the other sine wave will be the carrier signal to produce Amplitude Modulation.

sine = dsp.SineWave([0.4 1],[10 200], ... 'SamplesPerFrame',frameSize, ... 'SampleRate',Fs);

Create a lowpass FIR filter for filtering the squared signal to detect its envelope.

```
lp1 = dsp.FIRFilter('Numerator',firpm(20,[0 0.03 0.1 1],[1 1 0 0]));
```

Create three digital filter System objects. The first implements the Hilbert transformer, the second compensates for the delay introduced by the Hilbert transformer, and the third is a lowpass filter for detecting the signal envelope.

N = 60; % Filter order hilbertTransformer = dsp.FIRFilter( ... 'Numerator',firpm(N,[0.01 .95],[1 1],'hilbert')); delay = dsp.Delay('Length',N/2); lp2 = dsp.FIRFilter('Numerator',firpm(20,[0 0.03 0.1 1],[1 1 0 0]));

Create and configure two time scope System objects to plot the input signal and its envelope.

scope1 = dsp.TimeScope( ... 'NumInputPorts',2, ... 'Name','Envelope detection using Amplitude Modulation', ... 'SampleRate',[Fs,Fs/DownsampleFactor], ... 'TimeDisplayOffset',[(N/2+frameSize)/Fs,0], ... 'TimeSpanSource','Property', ... 'TimeSpan',0.45, ... 'YLimits',[-2.5 2.5], ... 'TimeSpanOverrunAction','Scroll'); pos = scope1.Position; scope2 = dsp.TimeScope( ... 'NumInputPorts',2, ... 'Name','Envelope detection using Hilbert Transform', ... 'Position',[pos(1)+pos(3),pos(2:4)], ... 'SampleRate',[Fs,Fs/DownsampleFactor], ... 'TimeDisplayOffset',[(N/2+frameSize)/Fs,0], ... 'TimeSpanSource','Property', ... 'TimeSpan',0.45, ... 'YLimits',[-2.5 2.5], ... 'TimeSpanOverrunAction','Scroll');

## MATLAB Example: Stream Processing Loop

Create the processing loop to perform envelope detection on the input signal. This loop uses the System objects you instantiated.

for i = 1:numSamples/frameSize sig = sine(); sig = (1 + sig(:,1)) .* sig(:, 2); % Amplitude modulation % Envelope detector by squaring the signal and lowpass filtering sigsq = 2 * sig .* sig; sigenv1 = sqrt(lp1(downsample(sigsq,DownsampleFactor))); % Envelope detector using the Hilbert transform in the time domain sige = abs(complex(0, hilbertTransformer(sig)) + delay(sig)); sigenv2 = lp2(downsample(sige,DownsampleFactor)); % Plot the signals and envelopes scope1(sig,sigenv1); scope2(sig,sigenv2); end release(scope1); release(scope2);

## MATLAB Example: Envelope Detector Results

In the plots, for the envelope detection method using Hilbert transform the envelope amplitude does not match the actual signal, because the Hilbert transform which was implemented using the FIR filter is not ideal. That is, the magnitude response is not one for all frequencies. The shape of the envelope still matches the actual signal's envelope.

## Simulink Example: Method 1 (Squaring and Lowpass Filtering)

As above, Method 1 works by squaring the input signal and sending it through a lowpass filter.

In this Simulink example, a simple minimum-phase lowpass filter is used to remove the high frequency energy. In order to maintain the correct scaling, two more operations are included. The first is to place a gain of 2 on the signal. Since we are only keeping the lower half of the signal energy, this gain boosts the final energy to match its original energy. Finally, the square root of the signal is taken to reverse the scaling distortion from the input signal squaring operation.

This method is useful because it is very easy to implement and can be done with a low-order filter, minimizing the lag of the output.

## Simulink Example: Method 2 (The Hilbert Transform)

As above, Method 2 works by creating the analytic signal of the input using a Hilbert transformer.

In this Simulink example, the Hilbert transform of the signal is found using a 32-point Parks-McClellan FIR filter. The Hilbert transform of the signal is then multiplied by i (the imaginary unit) and added to the original signal. The original signal is time-delayed before being added to the Hilbert transform to match the delay caused by the Hilbert transform, which is one-half the length of the Hilbert filter.

The envelope of the signal can be found by taking the absolute value of the analytic signal. In order to eliminate ringing and smooth the envelope, the result is subjected to a lowpass filter.

Note that the **Analytic Signal** block found in the DSP System Toolbox™ could also be used to implement this envelope detection design.

## Simulink Example: Envelope Detector Model

The all-platform floating-point version of the Simulink model is shown below. When you run the model, you will see the original signal and the results of both envelope detectors.

## Simulink Example: Envelope Detector Results

This example shows the results of the two different envelope detectors for two different types of input signals. The input choices are a sample speech signal or a 100 Hz sine wave that turns on and off.

The model has a switchable input and two outputs which are routed to scopes for easy viewing. If a signal is not visible, double-click on the Scope block to open it.

The input scope plot shows the original signal. The signal lasts a total of 5 seconds, with 1 second of data being shown at a time.

The first output scope plot shows the output of the first envelope detector. This is the result of squaring the original signal and sending it through a low-pass filter. You can clearly see that the envelope was successfully extracted from the speech signal.

The second output scope plot shows the output of the second envelope detector, which employs a Hilbert transform. Though the output of this envelope detector looks very similar to the first method, you can see differences between them.

## Simulink Model

All-platform floating-point version: dspenvdet