Implementing Hilbert Function to obtain envelope

7 views (last 30 days)
I'm trying to implement a Hilbert filter in order to get the envelope of a signal. I ideally want to use:
Hilbert = firpm(64 , [0.05 0.95] , [1 1] , 'Hilbert');
I have produced some test code which I have tested using a sinusoid, but find that the output has an amplitude lower than the peak of the signal. I have some code which produces the correct output (below):
z = sin((1:10000)./100);
Envelope = abs(hilbert(z));
figure;
plot(1:10000, z, 1:10000, Envelope);
and some code using the firpm() function which doesn't look as accurate:
z = sin((1:10000)./100);
% Design Hilbert Filter
HilbertOrder = 64;
Hilbert = firpm(HilbertOrder , [0.05 0.95] , [1 1] , 'Hilbert');
% Apply Hilbert Filter
Hilbert_out = filter(Hilbert,1,z);
% Calculate envelope including delay of 1/2 HilbertOrder
Envelope = sqrt((z(1:end-HilbertOrder/2)).^2 + (Hilbert_out((HilbertOrder/2+1):(end))).^2);
% Plot results
plot(1:10000, z, 1:length(Envelope), Envelope);
Why does the output of the second code not look as good as the first? Is anything wrong with the second code? What can I do to make the envelope a better approximation?

Answers (2)

Binaya
Binaya on 19 Jan 2024
Hi William,
I understand that you want to know why the "hilbert" function and "firpm" specified with "hilbert" option are generating different outputs.
The "firpm" function is used to design a FIR filter which produces real coefficients for the filter. "hilbert" inbuilt function provides a discrete-time analytical signal as output of the hilbert transform which is complex in nature.
When the envelope is computed using "firpm", it only considers real co-efficients and generates the envelope produced in the question.
When the envelope is computed using "hilbert" transform, "abs" function is used which also incorporates the imaginary part of the transform when calculating the magnitude. This leads to the flat envelope when using "hilbert" transform.
Using a "real" function inside "abs" will lead to same and correct results by both the filters.
z = sin((1:10000)./100);
Envelope = abs(real(hilbert(z)));
figure;
plot(1:10000, z, 1:10000, Envelope);
I hope this solves your query.

Star Strider
Star Strider on 19 Jan 2024
The envelope function was introduced in R2015b. It makes these problems easier.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!