4
votes

I'm working on signal filtering in MATLAB. I wrote a signal with 3 different frequencies:

Fs = 8000;                  %// Sampling frequency
T = 1/Fs;                   %// Sample time
L = 16000;                  %// Length of signal
t = (0:L-1)*T;              %// Time vector

y = 40*sin(2*pi*50*t) + 500*sin(2*pi*51*t) + 500*sin(2*pi*49*t);

Signal y

Now I want to extract the 50Hz signal by bandpass window filtering using a Hanning window.
Here is my code to design the filter:

function Hd = HannFilter1

Fs = 8000;         %// Sampling Frequency

N    = 4096;       %// Order
Fc1  = 49.5;       %// First Cutoff Frequency
Fc2  = 50.5;       %// Second Cutoff Frequency
flag = 'scale';    %// Sampling Flag
win = hann(N+1);

b  = fir1(N, [Fc1 Fc2]/(Fs/2), 'bandpass', win, flag);
Hd = dfilt.dffir(b);

After that, I do filtering using filter like this:

yfilter = filter(Hd.Numerator,1,y);

NFFT = 2^nextpow2(L);
Y = fft(yfilter,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);

figure;
subplot(2,1,1);
plot(yfilter);
subplot(2,1,2);
plot(f,2*abs(Y(1:NFFT/2+1)))

filtered Signal

  • Why is this filter unable to extract the 50Hz signal?
  • I'm doing something wrong in this simulation?
  • How can I filter out the 50Hz signal?

what is the best sample rate for 50Hz signal? and very important question! in real world, like balancing system, the main signal is about 20Hz and environment is very too noisy and filtering by my solution does not give a correct answer. in this case,how can i use or choose the best filtering algorithm?

if my sample rate be 8000Hz and I can buffered only 20000 samples, how can Designing a narrow bandpass filter?

1
This is an absurdly nature passband. Have you checked the frequency response of your filter? - Oliver Charlesworth
yes, filter response is ok, there is a funny point that i found it! when i decrease amplitude of 49Hz and 51Hz signal to 10 ( from 500 ), every thing work fine! why?! - Alireza Izadimehr
there is a another point too! maximum of amplitude of filtered signal strongly related to other signals! ( 49Hz and 51Hz ) in other hand if amplitude of 49Hz signal grow up to 2000, ( from 500 ), filtering result amplitude has been growing up!!! but main signal ( 50Hz) amplitude is fixed! i'm going to crazy!!!!! - Alireza Izadimehr
Check the filter with fvtool(Hd) . You will see that your filter is approx. -1dB @ 49Hz and 51Hz which is probably not what you want. - Matt
Yes! You right, but I can not increase filter order. If I increase filter order to 65536, result will be improved. But I can't change filter order, is there any other way to improve filter performance? And another subject is why other signals amplitude has strong effect on main signal (50hz)? And how can I decrease this effect? - Alireza Izadimehr

1 Answers

0
votes

So, I Solve problem by decrease sample rate and increase sample data by this code: ( as Matt was Said )

Fs = 1000;                    % Sampling frequency
T = 1/Fs;                     % Sample time
L = 60000;                     % Length of signal
t = (0:L-1)*T;                % Time vector

for j=1:20

 r1 = 5 + (1000-5).*rand(1,1);
 r2 = 5 + (1000-5).*rand(1,1);

y = 10*sin(2*pi*14.8*t) + r1*sin(2*pi*14.2*t) +  r2*sin(2*pi*15.5*t) + 1.1*rand(size(t));


yfilter = filter(Hd.Numerator,1,y);
max(yfilter(40000:50000))

end

my filter is KAISER ( FIR Badpass filter ) :

Fs = 1000;  % Sampling Frequency

Fstop1 = 14.2;            % First Stopband Frequency
Fpass1 = 14.6;            % First Passband Frequency
Fpass2 = 15;              % Second Passband Frequency
Fstop2 = 15.2;            % Second Stopband Frequency
Dstop1 = 1e-06;           % First Stopband Attenuation
Dpass  = 0.057501127785;  % Passband Ripple
Dstop2 = 1e-06;           % Second Stopband Attenuation
flag   = 'scale';         % Sampling Flag

% Calculate the order from the parameters using KAISERORD.
[N,Wn,BETA,TYPE] = kaiserord([Fstop1 Fpass1 Fpass2 Fstop2]/(Fs/2), [0 ...
                             1 0], [Dstop1 Dpass Dstop2]);

% Calculate the coefficients using the FIR1 function.
b  = fir1(N, Wn, TYPE, kaiser(N+1, BETA), flag);
Hd = dfilt.dffir(b);

and filter amplitude for 20 iteration and random noise signal is :

max(yfilter(40000:50000))

10.01
10.02
10.01
10.00
10.01
10.03
10.01
10.02
....

that is a great result for me, and filtered signal is : Filtered Signal

but there is some problem :

1- my sample data length is 60000 bytes, in other hand by sampling rate at 1000Hz, I wait for 60 Seconds for gathering data, and that is too long time!!! when i decrease sample data length to about 3000 samples, filtered result is so bad because of number of filter's coefficients is about 4097. how can i filter my signal when it's length is 3000 samples and my filter's coefficients is about 4097 bytes? when i decrease filter's coefficients , filtered signal result is so noisy.

2- what is the best sample rate fo 15 Hz signal?

thanks.