DSPRelated.com
Forums

Hilbert transformer for envelope detection

Started by Martin Trummer January 8, 2014
Hi DSP-experts,

I'm using a FIR hilbert-transformer (designed with Matlab's fdatool, 
200 coefficients) for a envlope detection. For eg:

% Signal is x, h contains coefficients of hilbert transforemer
x_analytical = filter(h,1,x);
x_delay = filter([zeros([1,99]), 1],1,x);
envelope = sqrt(x_analytical.^2+x_delay.^2);

My problem is that the envelope doesn't fit very well since there is a 
lot ringing on the envelope itself as you can see on the following 
screenshot:

http://s14.directupload.net/images/140108/8cmreir2.png

What's the exact problem for this behavior and how can this be avoided?
Thank you in advance!

Cheers Martin

On 1/8/14 10:08 AM, Martin Trummer wrote:
> Hi DSP-experts, > > I'm using a FIR hilbert-transformer (designed with Matlab's fdatool, 200 > coefficients) for a envlope detection. For eg: > > % Signal is x, h contains coefficients of hilbert transforemer > x_analytical = filter(h,1,x); > x_delay = filter([zeros([1,99]), 1],1,x); > envelope = sqrt(x_analytical.^2+x_delay.^2); > > My problem is that the envelope doesn't fit very well since there is a > lot ringing on the envelope itself as you can see on the following > screenshot: > > http://s14.directupload.net/images/140108/8cmreir2.png >
what's the red curve?
> What's the exact problem for this behavior and how can this be avoided?
it seems to me, since it's clearly a damped sinusoid, that you *should* get a very good envelope coming outa the analytical signal. perhaps your x_delayed signal is not delayed exactly correct to line up with the hilbert transformer which, if it's causal, must also be delayed. or perhaps your hilbert transformer coefficients aren't quite right. there should be perfect odd symmetry with the coefficients, and there should be an odd number, N, of hilbert filter coefficients so that the delay is precisely and integer number of samples which is (N-1)/2 . you can also take advantage of half-band symmetry so that every other coefficient is zero. -- r b-j rbj@audioimagination.com "Imagination is more important than knowledge."
Hi Robert,

Thank you for your answer.

> what's the red curve?
Sorry, ignore that one. The cyan one is the calculated envlope
> >> What's the exact problem for this behavior and how can this be avoided? > > it seems to me, since it's clearly a damped sinusoid,
You are right.
> that you *should* get a very good envelope coming outa the analytical > signal. perhaps your x_delayed signal is not delayed exactly correct > to line up with the hilbert transformer which, if it's causal, must > also be delayed. or perhaps your hilbert transformer coefficients > aren't quite right. there should be perfect odd symmetry with the > coefficients, and there should be an odd number, N, of hilbert filter > coefficients so that the delay is precisely and integer number of > samples which is (N-1)/2 . you can also take advantage of half-band > symmetry so that every other coefficient is zero.
I'm using a 200th order causal hilbert transformer (so I was wrong with the first statement saying I have 200 coefficients) gnerated by the fdatool. This yields 201 coefficients, so that's ok. As this FIR-filter has a group delay of 100 samples the original signal has to be delayed by 100 samples. using this [zeros([1,99]), 1] vector as an impulse response for delaying the signal. Since this is a damped sine without any noise I also expected to get good results. That's the reason I am asking here. Martin
What is the frequency of X? It might be outside the frequency range where the Hilbert filter can maintain flat amplitude response (the filter response must go to 0 at DC and Fs/2).  In that case the filter output and the delay output are not equal in amplitude and the ripple will not cancel. Might need a longer filter, or a parallel-allpass approach. 

Or maybe the passband ripple spec you fed into fdatool was too large. 

Bob
On 1/8/14 11:35 AM, Martin Trummer wrote:
> Hi Robert, > > Thank you for your answer. > >> what's the red curve? > > Sorry, ignore that one. The cyan one is the calculated envlope > >> >>> What's the exact problem for this behavior and how can this be avoided? >> >> it seems to me, since it's clearly a damped sinusoid, > > You are right. > > >> that you *should* get a very good envelope coming outa the analytical >> signal. perhaps your x_delayed signal is not delayed exactly correct >> to line up with the hilbert transformer which, if it's causal, must >> also be delayed. or perhaps your hilbert transformer coefficients >> aren't quite right. there should be perfect odd symmetry with the >> coefficients, and there should be an odd number, N, of hilbert filter >> coefficients so that the delay is precisely and integer number of >> samples which is (N-1)/2 . you can also take advantage of half-band >> symmetry so that every other coefficient is zero. > > I'm using a 200th order causal hilbert transformer (so I was wrong with > the first statement saying I have 200 coefficients) gnerated by the > fdatool. This yields 201 coefficients, so that's ok. As this FIR-filter > has a group delay of 100 samples the original signal has to be delayed > by 100 samples. using this [zeros([1,99]), 1] vector as an impulse > response for delaying the signal. > > Since this is a damped sine without any noise I also expected to get > good results.
and you should. first, get your Hilbert transformer designed with an *odd* number of coefficients. now i just hate, from the deepest recesses of my soul, the inflexible 1-based indexing of MATLAB, but i'll put it in terms of that. so design your Hilbert transformer with 199 coefficients, instead of 200. then confirm that h(1)=-h(199), h(2)=-h(198),... all the way to h(99)=-h(101). h(100) is the center coefficient and should be equal to zero. the output of that filter is your delayed quadrature signal. and it's delayed by 99 samples. then make sure your in-phase signal is also delayed precisely by 99 samples. then square them both and add. it should work quite well and the envelope should come out smoothly. -- r b-j rbj@audioimagination.com "Imagination is more important than knowledge."
On 1/8/14 12:03 PM, radams2000@gmail.com wrote:
> What is the frequency of X? It might be outside the frequency range where the Hilbert filter can maintain flat amplitude response (the filter response must go to 0 at DC and Fs/2). In that case the filter output and the delay output are not equal in amplitude and the ripple will not cancel. Might need a longer filter, or a parallel-allpass approach. > > Or maybe the passband ripple spec you fed into fdatool was too large. >
good point, Bob. i forgot about that. as i recall, the solution is to design a virtual all-pass filter with a phase shift of -45 degrees (plus the delay necessary to realize it). flip the coefs around and you have the +45 degree filter (with the same delay) and the outputs of those two filters are always 90 degrees apart and at *every* frequency, their gain is the same. ripple or not. -- r b-j rbj@audioimagination.com "Imagination is more important than knowledge."
On 2014-01-08 17:12:38 +0000, robert bristow-johnson said:

> as i recall, the solution is to design a virtual all-pass filter with a > phase shift of -45 degrees (plus the delay necessary to realize it). > flip the coefs around and you have the +45 degree filter (with the same > delay) and the outputs of those two filters are always 90 degrees apart > and at *every* frequency, their gain is the same. ripple or not.
Is that the the paper by Clay Turner? I will try that. I just recognized that my sine wave is close to 0 in relation to fs. (4 vs 1000 Hz). Martin
The allpass approach is different in the sense that both outputs have flat magnitude at all frequencies but can't hold the phase difference as you approach DC or PI, whereas the FIR Hilbert approach is guaranteed to hold phase at all frequencies but can't maintain unity amplitude as you approach DC or PI.  I cheat and use the Matlab optimizer for wiggling the allpass sections. 

Bob
On 1/8/14 12:21 PM, Martin Trummer wrote:
> On 2014-01-08 17:12:38 +0000, robert bristow-johnson said: > >> as i recall, the solution is to design a virtual all-pass filter with >> a phase shift of -45 degrees (plus the delay necessary to realize it). >> flip the coefs around and you have the +45 degree filter (with the >> same delay) and the outputs of those two filters are always 90 degrees >> apart and at *every* frequency, their gain is the same. ripple or not. > > Is that the the paper by Clay Turner?
i think Clay wrote something like that. i cannot completely remember. Clay's pretty smart, so i wouldn't doubt it.
> I will try that. I just recognized > that my sine wave is close to 0 in relation to fs. (4 vs 1000 Hz).
that might kill you to. but, in any case, make awful damn sure you get your delays lined up. even being off by a sample or a half sample could get you the result you're seeing. -- r b-j rbj@audioimagination.com "Imagination is more important than knowledge."
On 1/8/14 12:25 PM, radams2000@gmail.com wrote:
> The allpass approach is different in the sense that both outputs have flat magnitude at all frequencies but can't hold the phase difference as you approach DC or PI, whereas the FIR Hilbert approach is guaranteed to hold phase at all frequencies but can't maintain unity amplitude as you approach DC or PI.
i was thinking of an FIR all-pass with 45 degrees plus delay (just like the Hilbert realized as an FIR all-pass with 90 degrees plus delay) and even though they do not maintain unity gain for all frequencies, they have *equal* gain (to each other) for all frequencies.
> I cheat and use the Matlab optimizer for wiggling the allpass sections.
cheater. :-) -- r b-j rbj@audioimagination.com "Imagination is more important than knowledge."