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 don`t care about the gain or phase.
I research about Hilbert filter and sounds that this kind of filter is what I need, but I don`t 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 doesn`t 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 don`t 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 don`t 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)