# Bandpass Filter Order vs Sample Numbers of Noisy Signal

I'm working on signal filtering in MATLAB. i wrote sample code that generate 50 Hz signal with some noisy frequency and use Kaiser bandpass filter to get only 50Hz frequency. this code work great! but there is a big problem!

Sample Rate is 2000 Hz and Filter Order is about 39000 , and my signal length is 50000 samples! in other hand i have to wait about 25 seconds to gathering my signal! that is too long time!, if i decrease signal length ( to about 6000 samples ) filter result is so noisy because of filter order. if i decrease filter order , filter signal result is too noisy too! the best time that i cloud wait for gathering data is about 3 seconds.

is there any way to get same result with 6000 samples of signal and high order filter? ( 39000 )

thanks.

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

Hd =  KAISERFilter;

for i=1:50

a = 5;
b = 500;
r1 = a + (b-a).*rand(1,1);
r2 = a + (b-a).*rand(1,1);
r3 = a + (b-a).*rand(1,1);
r4 = a + (b-a).*rand(1,1);

n = r1 *sin(2*pi*49.2*t) + r2 *sin(2*pi*50.5*t) + 0.1 * randn(size(t)) +  r3 *sin(2*pi*15.5*t) +  r4 *sin(2*pi*110*t) ;
x = 30 *sin(2*pi*50*t); % main frequency
y = x+n;

subplot(2,1,1);
plot(y)
title('Signal')
xlabel('time (milliseconds)');

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


and Kaiser filter code is :

function Hd = KieserFilter

% All frequency values are in Hz.
Fs = 2000;  % Sampling Frequency

Fstop1 = 49.5;              % First Stopband Frequency
Fpass1 = 49.9;            % First Passband Frequency
Fpass2 = 50.1;            % Second Passband Frequency
Fstop2 = 50.5;              % 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);


## 2 Answers

You are dealing with a time frequency uncertainty in estimation problem. It takes longer than 3 seconds for there to be enough information in a signal to tell the difference between 3 clearly separated sinewaves between 49 and 51 Hz, and only 1 fat (slightly modulated) one. And you can't filter out a signal unless there is enough information to tell whether it's even there or not.

• Thanks, I working on vibration sensor, and it's data is too noisy, i need that filter an special frequency, this frequency about 49.8 to 50.2 and my bandpass filter bandwidth in too norraw, because if I increase bandwith other environment signals effect on my main signal. But my big problem is that maximum sampling time about 4 secondes, my sampling rate is 2000 Herz. What is your idea for filtering? – Alireza Izadimehr Apr 29 '16 at 6:54
• Is there any suggestions? – Alireza Izadimehr Apr 29 '16 at 18:29
• @AlirezaIzadimehr note that hotpaw2 just explained that no matter where your data comes from, your frequency estimation can't work. – Marcus Müller Jan 15 '17 at 21:16

Here is some tutorials which you may need to refer. Zero phase filtering adjust the time delay produced by digital filters.

zero phase filtering to remove noises from signal

50 Hz and 60 Hz power hum removal from signals.

https://youtu.be/iwR1QIspjM8

high frequency noise removal tutorials in matlab