Saturday, 29 November 2008

Butteworth Filter Design

As the first step, the various commonly used functions are discussed briefly. After that programs are given.

buttord

This functiion returns the order and natural cut-off frequency of the butterworth filter. Its input are passband freq, stopband freq, passband attenuation and stoopband attenuation. The freq must be normalised.

[N,Wn] = buttord(omp,oms,alphap,alphas);
N is the order, Wn is the cut-off.

butter
This is used to get the system function. The numeratoe and denomiantor values are obtained in an array.

[B,A] = butter(N,Wn);


abs

This gives the absolute value of a complex quantity.
m = abs(X);

angle

This will return the phase angle associated with a complex quantity.
an = angle(X);

subplot

This function is very commonly used one. It working can be described through an example.
subplot(3,1,1) means the entire window is divided into a 3X1 matrix and the active portion at the present time is the first one.

plot

It performs the plotting function of qnty. vs other.
plot(Y,X) plots Y as function of X. ie, Y on y axis and X on xaxis.

grid

It will draw grid lines to the plot.

Lowpass Filter

%To design a Butterworth Lowpass Filter
%specifications
clear all
alphap = 4; %Passband Attenuation
alphas = 30; %Stopband attenuation
fp = 400; %Passband frequency in Hz
fs = 800; %Stopband frequency in Hz
F = 2000; %Sampling Frequency in Hz
omp = 2*fp/F;
oms = 2*fs/F;

%To find cut-off frequency and order of filter
[N,Wn] = buttord(omp,oms,alphap,alphas); %N is the order of filter, and Wn is its natural frequency

%System function of filter
[B,A] = butter(N,Wn);
w = 0:0.01:pi;
[h,om] = freqz(B,A,w,'whole'); %h is the complex value of filter corres. to freq w
m = abs(h); %Absolute value of h
an = angle(h); %Angle of h

subplot(2,1,1);
plot(om/pi,20*log(m));
grid;
xlabel('Normalised frequency');
ylabel('Gain in dB');

subplot(2,1,2);
plot(om/pi,an);
grid;
xlabel('Normalised frequency');
ylabel('Phase in radian');


Bandpass Filter

%To design a Butterworth Bandpass Filter
%Specifications
clear all
alphap = 2; %Passband Attenuation
alphas = 20; %Stopband Attenuation
wp = [0.2*pi,0.4*pi]; %Passband Frequency
ws = [0.1*pi,0.5*pi];

%to find cut-off frq and order
[n,wn] = buttord(wp/pi,ws/pi,alphap,alphas);

%System function
[b,a] = butter(n,wn);
w = 0:0.01:pi;
[h,ph] = freqz(b,a,w);
m = 20*log10(abs(h));
an = angle(h);

subplot(2,1,1);
plot(ph/pi,m);
grid;
ylabel('Gain in dB');
xlabel('Normalised Frequency');

subplot(2,1,2);
plot(ph/pi,an);
grid;
ylabel('Phase in radian');
xlabel('Normalised frequency');


Bandreject Filter

%To design a Butterworth bandreject filter
%Specification
clear all
alphap = 2; %Passband attenuation
alphas = 20; %Stopband attenuation
ws = [0.2*pi,0.4*pi]; %Stopband frequency in rad/s
wp = [0.1*pi,0.5*pi]; %Passband frequency in rad/s
%To find cut-off frequency and order
[n,wn] = buttord(wp/pi,ws/pi,alphap,alphas);
%To find system function
[b,a] = butter(n,wn,'stop'); %[b,a] is [numerator,denominator]
w = 0:0.01:pi;
[h,ph] = freqz(b,a,w);
m = 20*log(abs(h));
an = angle(h);
%Plotting
subplot(2,1,1);
plot(ph/pi,m,'r');
grid;
ylabel('Gain in dB');
xlabel('Normalised frequency');
subplot(2,1,2);
plot(ph/pi,an,'b');
grid;
ylabel('Phase in rad');
xlabel('Normalised frequency');



Highpass Filter

%To design a Highpass Butterworth filter
%Specification
clear all
alphap = 4; %Passband attenuation
alphas = 30; %Stopband attenuation
fs = 400; %Stopband frequency in Hz
fp = 800; %Passband frequency
F = 2000;
omp = 2*fp/F; %Normalised
oms = 2*fs/F; %Normalised
%To find cut-off and order
[n,wn] = buttord(omp,oms,alphap,alphas)
%System function
[b,a] = butter(n,wn,'high');
w = 0:0.01:pi;
[h,om] = freqz(b,a,w);
m = 20*log(abs(h));
an = angle(h);
%Plotting
subplot(2,1,1);
plot(om/pi,m);
grid;
ylabel('Gain in db');
xlabel('Normalised frequency');
subplot(2,1,2);
plot(om/pi,an);
grid;
ylabel('Phase in rad');
xlabel('Normalised frequency');



querrymail@gmail.com

0 comments: