Anda di halaman 1dari 11

Indian Institute of Technology Kharagpur

A Report on

Implementation of Digital Filter

Author: Neeraj Mishra 07EE3202 Department of Electrical Engg. IIT Kharagpur Pratik Beley 11EE62R04 Department of Electrical Engg IIT Kharagpur

Introduction:
Electronic filters are electronic circuits which perform signal processing functions, specifically to remove unwanted frequency components from the signal. There are two types of filters Analog filters and digital filters. Digital filters operate on digitized analog signals, so the digitization process is important and can be critical in the system design. Digitization requires the analog signal to be sampled and then converted into a digital value, based on the amplitude of the sample

Filter Design:
Problem Statement: To design a notch filter of following specification Pass band 1: 1 Hz Stop band 1: 10 Hz Stop band 2: 100 Hz Pass band 2: 1000 Hz Pass band gain: 20 db Stop band gain: -20 db The Butterworth transfer function is very simple. It is merely: H () =

where n is the order. It follows from this that at w= 1, H (w) = 1/4, or 0.7071. This is -3dB relative to the zero frequency point, no matter what value of filter order is considered, since 1" = 1. For this reason the -3 dB cut-off frequency is considered the natural pass band edge. Attenuation for any filter order at other frequencies can be found by substituting different values of o and PZ. For example, at w = 3 and IZ = 5. Attenuation = = 47.7dB. Another way of looking at this is to find the filter order that will satisfy the attenuation requirements.
2

N= A= R=

Convert the frequency into radians Wp1 = 1* 2 * Hz Wp2 = 1000* 2 * Ws1 = 10* 2 * Ws2 = 100* 2 * Dp = Ds = Ws= = 11.1 Hz

Hz Hz

For normalized Low Pass Filter Dp = Ds = Now

= 0.891 = 0.01

Nb

= 2.193

Nb = 3 Now Dp =

Wc = 1.419

Normalized low Pass transfer function H(s) = Now b = 2* For k = 1 b=1 H(s) =

MATLAB Program:
%% Analog Filter s=tf('s'); grid on; s1=(999*2*pi*s)/(s^2+(1000*4*pi*pi)); H=(1.419^3)*10/((s1+1.419)*(s1^2+1.419*s1+1.419^2)) H3=minreal(H) [num,den]=tfdata(H3); den{1} num{1} figure(1) bode(H3) grid on hold on %% Digital Filter s=tf('s');
4

% z=tf('z',1/20000) % s=40000*(z-1)/(z+1); s3=(6329.095*s)/(s^2+(39805.17997)); H2=(1.419^3)*10/((s3+1.419)*(s3^2+1.419*s3+1.419^2)) H4=minreal(H2); [num1,den1]=tfdata(H4)

[numd,dend]=bilinear(num1{1},den1{1},20000)

figure(2) freqz(numd,dend) Frequency response is shown below

Many devices (particularly low-cost ones) use fixed point numbers, and the mathematics requires some thought. MATLAB does calculation in floating point so we have to first write code in fixed point and then implement it. We can either have 8 bit or 16 bit representation.

MATLAB code for fixed point representation:


USING FIXED POINT: QUANTIZING THE COEFFICIENTS USING A KNOWN NUMBER OF BITS

%Finding the maximum and minimum of coefficients

num_largest = max(numd); num_smallest = min(numd); den_largest = max(dend); den_smallest = min(dend);

if (num_largest >= den_largest) largest = num_largest; else largest = den_largest; end

if (num_smallest <= den_smallest) smallest = num_smallest;


6

else smallest = den_smallest; end if (ceil(log(abs(largest))/log(2)) > ceil(log(abs(smallest))/log(2))) ; int_bits = ceil(log(abs(largest))/log(2)); else int_bits = ceil(log(abs(smallest))/log(2)); end

frac_bits = (N_bits - 1) - int_bits; num_binary = zeros(length(numd),1+frac_bits); den_binary = zeros(length(dend),1+frac_bits);

for k = 1:length(numd)

if (numd(k) < 0) num_binary(k,1) = 1; numd(k) = 2^(int_bits) - abs(numd(k)); end

residue = numd(k) - floor (numd(k)); for l = 1:frac_bits if(residue * 2 >= 1)


7

num_binary(k,1+l) = 1; end

residue = 2*residue - floor(2*residue); end

sum = 0; for m = 1:frac_bits sum = sum + num_binary(k,1+m) * 2^(-m); end

numd(k) = -num_binary(k,1) * 2^int_bits + floor(numd(k)) + sum;

end

for k = 1:length(dend)

if (dend(k) < 0) den_binary(k,1) = 1; dend(k) = 2^(int_bits) - abs(dend(k)); end

residue = dend(k) - floor (dend(k));


8

for l = 1:frac_bits if(residue * 2 >= 1) den_binary(k,1+l) = 1; end

residue = 2*residue - floor(2*residue); end

sum = 0; for m = 1:frac_bits sum = sum + den_binary(k,1+m) * 2^(-m); end

dend(k) = -den_binary(k,1) * 2^int_bits + floor(dend(k)) + sum;

end

Hd = tf(numd,dend,fsamp); display('------------------------------------------------------------------------------------------'); display('THE DIGITAL DOMAIN FILTER TRANSFER FUNCTION WITH FIXED POINT COEFFICIENTS IS:'); display(Hd); sum1 = 0;

sum2 = 0; gain = zeros(1,31416); phase = zeros(1,31416); for df = 1:31416 for k = 1:length(numd) sum1 = sum1 + numd(k)*exp((length(numd)-k)*(df-1)/10000*1i); end

for l = 1:length(dend) sum2 = sum2 + dend(l)*exp((length(dend)-l)*(df-1)/10000*1i); end

fr = sum1/sum2; gain(df) = 20 * log10 (abs(fr)); phase(df) = angle(fr)*180/pi; end

subplot(2,1,2); df = 1:31416; semilogx((df-1)/31416*fsamp/2, gain(df)); title('DIGITAL DOMAIN MAGNITUDE RESPONSE WITH FIXED POINT COEFFICIENTS'); xlabel('Frequency [Hz]');

10

ylabel('Gain (dB)'); grid on; Now if we see the results for 8 bit representation we observe that the response of the filter becomes distorted as seen below

Hence we will go for 16 bit representation for better results

11