Skip to content

Instantly share code, notes, and snippets.

@axlevisu
Created April 9, 2016 07:10
Show Gist options
  • Save axlevisu/0afe6fbe7fec07659285195183a98c20 to your computer and use it in GitHub Desktop.
Save axlevisu/0afe6fbe7fec07659285195183a98c20 to your computer and use it in GitHub Desktop.
%Bandpass FIR filter using Kaiser Window
%Question Parameters and Specs
m = 49;
q= floor((m-1)/10);
r = m - 10*q;
B_L = 4 + (0.7*q) + 2*r;
B_H = B_L + 10;
f = 100;
d = 0.15; %Tolerance
T = 2; %Transition Bandwidth
%Discrete Time Normalised Frequencies
p_l = 2*pi*B_L/f;
p_h = 2*pi*B_H/f;
s_l = 2*pi*(B_L - T)/f;
s_h = 2*pi*(B_H + T)/f;
%Calculating Kaiser Window parameters
dw = p_l - s_l;
A = -20*log10(d);
N = ceil((1/2)*(A-7.95)/(2.285*dw)) + 10;
if A >50
al = 0.1102*(A-8.7);
elseif A>=21 && A <= 50;
al = 0.5842*(A-21)^0.4 + 0.07886*(A-21);
else
al =0;
end
bet = al/N;
%impulse response array of ideal filter(2N +1 terms)
h_i = zeros(2*N + 1);
for i = 1: 1 + 2*N
if i ~= N +1
h_i(i,i) = (sin(p_h*(i-1-N)) - sin(p_l*(i-N-1)))/(pi*(i-N-1));
else
h_i(i,i) = (p_h - p_l)/(pi);
end
end
%Kaiser Window of length 2N +1
w = kaiser(1 + 2*N, bet);
h = h_i*w;
z = tf('z');
Z_TF = 0;
for i = 1:1+ 2*N
Z_TF = Z_TF + h(i)*(z^(1 + N -i));
end
display(Z_TF);
[Z_TF_num, Z_TF_den] = tfdata(Z_TF, 'v');
[h,w] = freqz(Z_TF_num, Z_TF_den,100000);
plot(w*f/(2*pi), abs(h));% Unnormalized
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment