Showing posts with label matlab code. Show all posts
Showing posts with label matlab code. Show all posts

Thursday, September 15, 2011

Infinite Impulse Response IIR FILTER (IIR)


6. IIR FILTER.



AIM: To design and implement IIR (LPF/HPF) filters.

EQUIPMENTS:
Software - MATLAB

Learning Objectives: To make the students familiar with designing concepts of FIR filter
with the use of MATLAB.

THEORY:
The IIR filter can realize both the poles and zeroes of a system because it has a rational
transfer function, described by polynomials in z in both the numerator and the denominator:
The difference equation for such a system is described by the following:
M and N are order of the two polynomials.
bk and ak are the filter coefficients. These filter coefficients are generated using FDS (Filter
Design software or Digital Filter design package).
IIR filters can be expanded as infinite impulse response filters. In designing IIR
filters, cutoff frequencies of the filters should be mentioned. The order of the filter
can be estimated using butter worth polynomial. That’s why the filters are named as
butter worth filters. Filter coefficients can be found and the response can be plotted.

PROGRAM:
% IIR filters LPF & HPF
clc;clear all;close all;
disp('enter the IIR filter design specifications');
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
wp=input('enter the passband freq');
ws=input('enter the stopband freq');
fs=input('enter the sampling freq');
w1=2*wp/fs;w2=2*ws/fs;
[n,wn]=buttord(w1,w2,rp,rs,'s');
c=input('enter choice of filter 1. LPF 2. HPF \n ');
if(c==1)
disp('Frequency response of IIR LPF is:');


[b,a]=butter(n,wn,'low','s');
end
if(c==2)
disp('Frequency response of IIR HPF is:');
[b,a]=butter(n,wn,'high','s');
end
w=0:.01:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure,subplot(2,1,1);plot(om/pi,m);
title('magnitude response of IIR filter is:');
xlabel('(a) Normalized freq. -->');
ylabel('Gain in dB-->');
subplot(2,1,2);plot(om/pi,an);
title('phase response of IIR filter is:');
xlabel('(b) Normalized freq. -->');
ylabel('Phase in radians-->');

Finite Impulse Response FIR FILTER (FIR)


5. FIR FILTER.


AIM: To verify FIR filters.

EQUIPMENTS:
Constructor –MATLAB Software

Learning Objectives: To make the students familiar with designing concepts of FIR filter
with the use of MATLAB.

THEORY:
A Finite Impulse Response (FIR) filter is a discrete linear time-invariant system whose
output is based on the weighted summation of a finite number of past inputs. An FIR transversal
filter structure can be obtained directly from the equation for discrete-time convolution.
In this equation, x(k) and y(n) represent the input to and output from the filter at time n.
h(n-k) is the transversal filter coefficients at time n. These coefficients are generated by using
FDS (Filter Design Software or Digital filter design package).
FIR – filter is a finite impulse response filter. Order of the filter should be specified.
Infinite response is truncated to get finite impulse response. placing a window of finite length
does this. Types of windows available are Rectangular, Barlett, Hamming, Hanning, Blackmann
window etc. This FIR filter is an all zero filter.

PROGRAM:
%fir filt design window techniques
clc;
clear all;
close all;
rp=input('enter passband ripple');
rs=input('enter the stopband ripple');
fp=input('enter passband freq');
fs=input('enter stopband freq');
f=input('enter sampling freq ');
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
if(rem(n,2)~=0)
n1=n;
n=n-1;
end
c=input('enter your choice of window function 1. rectangular 2. triangular 3.kaiser: \n ');
if(c==1)
y=rectwin(n1);
disp('Rectangular window filter response');
end
if (c==2)
y=triang(n1);
disp('Triangular window filter response');
end
if(c==3)
y=kaiser(n1);
disp('kaiser window filter response');
end

%LPF
b=fir1(n,wp,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,1);plot(o/pi,m);
title('LPF');
ylabel('Gain in dB-->');
xlabel('(a) Normalized frequency-->');
%HPF

b=fir1(n,wp,'high',y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,2);plot(o/pi,m);
title('HPF');
ylabel('Gain in dB-->');
xlabel('(b) Normalized frequency-->');
%BPF

wn=[wp ws];
b=fir1(n,wn,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,3);plot(o/pi,m);
title('BPF');
ylabel('Gain in dB-->');
xlabel('(c) Normalized frequency-->');
%BSF
b=fir1(n,wn,'stop',y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,4);plot(o/pi,m);
title('BSF');
ylabel('Gain in dB-->');
xlabel('(d) Normalized frequency-->')

INVERSE Z TRANSFORM


4. INVERSE Z TRANSFORM



AIM: To develop a program for Computing Inverse Z-Transform

EQUIPMENTS: MATLAB 7.5

Learning Objectives: To make the students familiar with concept of inverse Z-transform
with the use of MATLAB.

THEORY:
Description: In mathematics and signal processing, the Z-transform converts a discrete
time-domain signal, which is a sequence of real or complex numbers, into a complex frequency-
domain representation. The Z-transform, like many other integral transforms, can be defined as
either a one-sided or two-sided transform.
The bilateral or two-sided Z-transform of a discrete-time signal x[n] is the function X(z)
defined as
.
Alternatively, in cases where x[n] is defined only for n = 0, the single-sided or unilateral
Z-transform is defined as
Description: X(z) = \mathcal{Z}\{x[n]\} = \sum_{n=-\infty}^{\infty} x[n] z^{-n} \
Description: X(z) = \mathcal{Z}\{x[n]\} = \sum_{n=0}^{\infty} x[n] z^{-n} \


In signal processing, this definition is used when the signal is causal.
Rational Z-transform to partial fraction form:
Consider the transfer function in the rational form i-e;
18z3
G(z)= ------------------
18z3+3z2-4z-1
We can evaluate the partial fraction form of the above system using matlab command. The
partial fraction form be,
G(z)= 0.36__ + __0.24__ + _0.4____
1 – 0.5z-1 1+0.33 z-1 (1+0.33 z-1)
Matlab command that converts rational z-transform in to partial fraction form is
‘residuez’.
If you want to see the poles and zeros in a zplane. This function displays the poles and zeros
of discrete-time systems. Use the under given matlab command
zplane(b,a)

ALGORITHM:


1. Write the poles and zeros of the input sequence.
2. Returned vector R contains the residues, Column vector contains P contains the pole
locations. And row vector contains the direct terms.



PROGRAM CODE:
%program to perform Inverse Z-Transform
b=[1,0.4*sqrt(2)];
a=[1,-0.8*sqrt(2),0.64];
[R,P,C]=residuez(b,a);
R
P
C
Zplane(b,a);


Discrete Fourier Transform & Inverse Discrete Transform (DFT AND IDFT)


3. Discrete Fourier Transform & Inverse Discrete Transform (DFT AND IDFT)



AIM: To develop a program for Computing DFT and IDFT in MATLAB

REQUIREMENTS: MATLAB 7.5

Learning Objectives: To make the students familiar with concept of DFT and IDFT with
the use of MATLAB.

THEORY: The discrete Fourier transform (DFT) X[k] of a finite-length sequence x[n] can be
easily computed in MATLAB using the function fft. There are two versions of this function.
fft(x) computes the DFT X[k] of the sequence x[n] where the length of X[k] is the same as that of
x[n]. fft(x,L) computes the L-point DFT of a sequence x[n] of length N where L = N. If L > N,
x[n] is zero-padded with L-N trailing zero-valued samples before the DFT is computed. The
inverse discrete Fourier transform (IDFT) x[n] of a DFT sequence X[k] can likewise be computed
using the function ifft, which also has two versions.

ALGORITHM (For DFT):
1 Enter the input Sequence ,x having length=4
2 Set the range of k according to the length of x.
3 Computing DFT, store the value in X(k).
4 Plotting the DFT of given Sequence,store in X(k).

PROGRAM CODE:
% Program to perform Discrete Fourier Transform:
clc;
clear all;
close all hidden;
x=input('The given i/p sequence is x(n): ');
subplot(2,2,[1,2]), stem(x);
title('i/p sequencce x(n)is:');
xlabel('---->n');
ylabel('---->x(n)');grid;
N=length(x);
for k=1:N
X(k)=0;
for n=1:N
X(k)=X(k)+x(n).*exp(-j.*2.*pi.*(n-1).*(k-1)./N);
end
end
disp('The DFT of the i/p sequence x(n) is X(n):')
p=0:(N-1);
subplot(2,2,[3,4]), stem(p,abs(X));
title('The DFT of the i/p sequence x(n) is X(n):');
xlabel('---->n');
 ylabel('---->X(n)');grid;
disp(X);

ALGORITHM (For IDFT):

1 Enter the input Sequence, x having length=4
2 Set the range of k according to the length of x.
3 Computing IDFT, store the value in X(k).
4 Plotting the IDFT of given Sequence, store in X(k).

% Program to perform Inverse Discrete Fourier Transform:
clc;
clear all;
close all hidden;
X=input('The given i/p sequence is X(n): ');
subplot(2,2,[1,2]), stem(X);
title('i/p sequencce X(n)is:');
xlabel('---->n');
ylabel('---->X(n)');grid;
N=length(X);
for n=1:N
x(n)=0;
for k=1:N
x(n)=x(n)+X(k).*exp(j.*2.*pi.*(n-1).*(k-1)./N);
x(n)=x(n)./N;
end
end
disp('The IDFT of the i/p sequence X(n) is x(n):')
p=0:(N-1);
subplot(2,2,[3,4]), stem(p,abs(x));
title('The IDFT of the i/p sequence X(n) is x(n):');
xlabel('---->n');
ylabel('---->x(n)');grid;
disp(x);