# Design Hilbert filter at matlab (pure 90 phase and magnitude 0db)

I need to design a filter able to take a measurement of alternate current and shift the phase of the input by 90°.

The filter should work between 40 and 60Hz and at this range should have unity gain. Outside this range I dont care about the gain or phase.

I research about Hilbert filter and sounds that this kind of filter is what I need, but I dont know how to use.

Let me paste my matlab code (fixed afther Matt recomendation):

%     close all
clear all
clc

%Period
Fs = 1000; %sample frequency

fl = 40;   %lower frequency

f_min=fl/(Fs/2);

b = firpm(40,[f_min (1-f_min)],[1 1],'h');   % Bandpass Hilbert
%     fvtool(b)

t = 0:1/Fs:1.2;
t_window=0.2;

%input signal Frequency

f=40;
%Generate AC signal (input)- at 40hz (lower limit)
y1 = sin(2*pi*f*t);

%Generate filtered signal-40hz
y1f=filter(b,1,y1);
y1 = [zeros(1,(length(b)-1)/2), y1(1:end-(length(b)-1)/2)];

f=60;
%Generate AC signal (input)- at 60Hz (upper limit)
y2 = sin(2*pi*f*t);

%Generate filtered signal-60hz
y2f=filter(b,1,y2);
y2 = [zeros(1,(length(b)-1)/2), y2(1:end-(length(b)-1)/2)];

figure
hold on

plot(t,y1,'*-')
plot(t,y1f)
plot(t,y2,'*-')
plot(t,y2f)

hold off
grid

legend('input','filtered')
axis([max(t)-t_window max(t) -1.1 1.1])

range(y1f(end-(length(t)/10):end))/range(y2f(end-(length(t)/10):end))


I was expecting that my signal yf is 90° shifted, but this doesnt work.

Someone have some idea about how to solve this issue and what is wrong at my code?

Part 2 - Thanks Hilmar and Richard. Let me fix my code and use your code and show what is happening. Done. But this dont work yet...

• Objective 1- 90 phase shift -ok! (thanks guys!)
• Objective 2- Magnitude without change- Not OK. The signal was atenuate by 5% if I change the input frequency from 60 to 40Hz.

Could you help me to explain why this is happen at magnitude?and What I should do to fix this?.... I need magnitude as flat as possible between this frequencies. I tried to increase the order of the filter(40->60) but dont solve my issue yet.

Part 3 - Thanks Matt. Let me fix my code and use your suggestion. Now work!

Now my filtered signal is shifted properly and with low attenuation between 40 to 60hz. The attenuation is less than 0.4% at this range.

Thank again guys(Matt, Richard and Hilmar)

@Rodrigo PG: Your Matlab Hilbert transformer is causal. This means, in order to synchronize-in-time your $y$ sequence with your $yf$ sequence, you need to delay your $y$ sequence by (length of $b$ minus 1)/2 samples when the length of $b$ is odd. This will delay your input $y$ by the same amount as the Hilbert transformer's inherent time delay. Insert the following line of code after your yf = filter(b,1,y); command:

y = [zeros(1,(length(b)-1)/2), y];


to see the desired 90-deg phase shift.

• Richard, take a look at my edited post. Regards – Rodrigo PG Oct 26 '15 at 12:05

Hilbert Transformers are non-causal, i.e. they need to be delayed to be implementable. So you get the 90 degree phase-shift plus a bulk delay of 20 samples (half the filter length).

You see the 90 degree phase shift if you delay the original signal by 20 samples as well.

EDIT for Part 2:

Your lower bandpass cutoff is too high. It currently sits at 50 Hz. It needs to go down to 40 Hz or potentially a little lower. You may also have to crank up the number of points to get less amplitude ripple in the pass band. Something like

b = firpm(60,[38 950]*2/2000,[1 1],'h');


gets you about 0.1 dB ripple. Cranking the number of tabs up to 96 will reduce the pass band ripple to 0.01dB.

• Hilmar, take a look at my edited post. Regards – Rodrigo PG Oct 26 '15 at 12:05

Regarding the magnitude response of the FIR Hilbert transformer, it is never ideally flat. The passband magnitude ripple can be reduced by increasing the filter order. However, in your case the frequency $40$ (whatever units) at a sampling frequency of $2000$ is outside the passband of the designed filter, because your lower band edge is $0.05$ (which corresponds to a frequency of $50$ for the given sampling rate).

So, if you want the Hilbert transformer to work well at a frequency of $40$, this frequency must be inside the passband, i.e. you must lower the lower band edge to say $0.035$. If the resulting passband ripple is too high, then you also must increase the filter order.

• I tried to upvote all the answers, but this wasnt possible. So I choose the user with lower reputation to help him to grow his reputation. – Rodrigo PG Oct 27 '15 at 12:38

@Rodrigo PG: Now that your Hilbert filter is working, you can start experimenting.

[1] Window your Hilb filter’s coefficients with a Hanning (Hann) or Hamming window to reduce the passband ripple. But make sure your signal of interest is still within the Hilb filter’s passband.

[2] Decimate your input signal by a factor of two using a halfband filter. That will allow you to use a simpler (fewer coefficients) Hilb filter.

[3] Insert a zero-valued coefficient in between each of your original Hilb filter’s coefficients. That will enable you to perform a Hilbert transform on lower-frequency signals.