0% found this document useful (0 votes)
190 views132 pages

Lab Manul

This document provides information about MATLAB (matrix laboratory), a numerical computing environment and programming language. It was created in the late 1970s at the University of New Mexico. MATLAB has evolved over time and is widely used in academia and industry for tasks like mathematics, engineering, science, algorithm development, modeling, simulation, data analysis, and application development. Typical uses include math and computation, scientific and engineering graphics, and signal processing. The document then provides examples of basic MATLAB programs and commands for working with images, such as reading, displaying, manipulating, filtering, and transforming digital images.

Uploaded by

Chandhu Rithesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views132 pages

Lab Manul

This document provides information about MATLAB (matrix laboratory), a numerical computing environment and programming language. It was created in the late 1970s at the University of New Mexico. MATLAB has evolved over time and is widely used in academia and industry for tasks like mathematics, engineering, science, algorithm development, modeling, simulation, data analysis, and application development. Typical uses include math and computation, scientific and engineering graphics, and signal processing. The document then provides examples of basic MATLAB programs and commands for working with images, such as reading, displaying, manipulating, filtering, and transforming digital images.

Uploaded by

Chandhu Rithesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 132

DSP/DIP DEPARTMENT

LAB MANUAL

The name MATLAB stands for matrix laboratory. It was invented


in the late 1970s by Cleve Moler, then chairman of the computer
science

department

at

the

University

of

New

Mexico.

MATLAB has evolved over a period of years with input from


many users. In university environments, it is the standard
instructional tool for introductory and advanced courses in
mathematics, engineering, and science. In industry, MATLAB is
the tool of choice for high-productivity research, development and
analysis.
MATLAB was first adopted by control design engineers, Little's
specialty, but quickly spread to many other domains. It is now also
used in education, in particular the teaching of linear algebra and
numerical analysis, and is popular amongst scientists involved with
image processing.
MATLAB is a high-performance language for technical computing.
It integrates computation, visualization, and programming in an easyto-use environment where problems and solutions are expressed in
2

familiar mathematical notation. Its wide range of commands,


functions, and language constructs permit users to solve and analyze
difficult computational problems from science and engineering
without programming in a general purpose language. Typical uses
include:
1.

Math and computation


2. Algorithm development
3. Modeling, simulation and prototyping
4. Data analysis, exploration and visualization
5. Scientific and engineering graphics
6. Application development, including graphical user interface
building

REAL TIME APPLICATIONS


1. DATA SECURITY
2. IMAGE MANIPULATIONS
3

3. SIGNAL PROCESSING

%%%%%%%% -------------DAY 1---------%%%%%%%


%
%%%%%%Some basic programs on images%%%%%%
>> zeros(3)
ans =
0

>> ones(3)
ans =
1

>> eye(3)]
ans =
1

1
5

>> ones(3)*2
ans =
2

>> a = [1 2 3]
a=
1

>> b = [1,2,3]
b=
1

>> c = [1;2;3]
c=
1
2
3
>> d = [1 2;3 4]
6

d=
1

>> e = [1 2 3;4 5 6;7 8 9]


e=
1

>> magic(3)
ans =
8

>> pascal(3)
ans =
1

6
7

>> rand(3,4)
ans =
0.9501

0.4860

0.4565

0.4447

0.2311 0.8913

0.0185

0.6154

0.6068

0.8214

0.7919

0.7621

>> randint(3)
ans =
1

>> randn(3,4)%%Displays +ve and -ve values


ans =
-0.4326

0.2877

1.1892

0.1746

-1.6656 -1.1465 -0.0376 -0.1867


0.1253

1.1909

0.3273

0.7258

>> A = [1 2 3;4 5 6; 7 8 9]
A=
8

>> B = size(A)
B=
3

>> C = length(A)
C=
3
>> A = [11 12 13;14 15 16;17 18 19]
A=
11 12

13

14

15

16

17

18

19

>> B = sum(A)
B=

42

45

48

>> C = sum(B)
9

C=
135

>> D = diag(A)
D=
11
15
19

>> E = diag(A, -1)


E=
14
18
>> F = det(A)
F=
0
>> G = norm(A)
10

G=
45.6601
>> H = inv(A)
Warning: Matrix is close to singular or badly scaled.
Results may be inaccurate. RCOND = 1.387779e-017.

H=
1.0e+014 *
1.8765 -3.7530
-3.7530

1.8765

7.5060 -3.7530

1.8765 -3.7530

1.8765

>> J = A'
J=
11 14

17

12

15

18

13

16

19

>> K = A/2
K=
11

5.5000

6.0000

6.5000

7.0000

7.5000

8.0000

8.5000

9.0000

9.5000

>> L = mod(A,4)
L=
3

%%%%%%%%%Matrix Indexing%%%%%%%%%
>> A = [1 2 3;4 5 6; 7 8 9]
A=
1

>> A(3, 2)
ans =
8
12

>> A(end,end-2)
ans =
7
>> A(1,:)
ans =
1

>> A(2,:)
ans =
4

>> A(:,1)
ans =

4
7
>> A(:,3)
ans =
3
6
9
13

>> A(:,:,:)
ans =
1

>> A(:,2,:)
ans =
2
5
8
>> A(1,:,:)
ans =
1

>> A(1,2,:)
ans =
2
>> A(0+[1 2],1+[1 2])
ans =
14

%%%%%%%-------- Arithmetic Operators---------%%%%%%%


%%%
MATLAB provides these arithmetic operators.

15

16

Day 2
%%%%%%%%%------Image Processing------%%%%%%%%%
%%
% How to read an image
A=imread (filename);
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% To display the image
imshow (A);
imview(A);
%%%%%%%%%%%%%%%%%%%%%%%%%%%
X=imread(Path);
imshow(X);
% Add a constant to an image.
I = imread('rice.png');
J = imadd(I,50);
subplot(1,2,1), imshow(I)
subplot(1,2,2), imshow(J)
OUTPUT:
17

% Subtract a constant value from an image:


I = imread('rice.png');
Iq = imsubtract(I,50);
imview(I),imview(Iq)
OUTPUT:

% Scale an image by a constant factor:


I = imread('moon.tif');
J = immultiply(I,0.5);
subplot(1,2,1), imshow(I)
subplot(1,2,2), imshow(J)
OUTPUT:
18

% Divide by a constant of the rice image.


I = imread('rice.png');
J = imdivide(I,2);
subplot(1,2,1), imshow(I)
subplot(1,2,2), imshow(J)
OUTPUT:

% Adjust a low-contrast grayscale image.


I = imread('pout.tif');
J = imadjust(I);
imview(I), imview(J)
19

OUTPUT:

% A low-contrast image with its histogram.


I = imread('pout.tif');
imshow(I)
figure, imhist(I,64)
OUTPUT:
5000

4000

3000

2000

1000

0
0

50

100

150

200

250

%Entropy
%Entropy is used to characterize the texture of the input image.
I = imread('circuit.tif');
J = entropy(I)
OUTPUT:
20

J = 6.9439
%A high-contrast image with its histogram.
I = imread('cameraman.tif');
figure; imshow(I); imhist(I,64);
OUTPUT:

4000
3500
3000
2500
2000
1500
1000
500
0
0

50

100

150

200

250

%Add two images together and specifies an output class.


I = imread('rice.png');
J = imread('cameraman.tif');
K = imadd(I,J,'uint16');
imshow(K,[])
OUTPUT:

21

% To display the r, g, b components of an color image


a=imread('football.jpg');
subplot(2,2,1),imshow(a);title('rgb image');
b=a(:,:,1);
subplot(2,2,2),imshow(b);title('r component image');
c=a(:,:,2);
subplot (2,2,3),imshow(c);title('g component image');
d=a(:,:,3);
subplot(2,2,4),imshow(d);title('b component image');
22

rgb image

r component image

g component image

b component image

2)
a=imread('peppers.png');
subplot(2,2,1),imshow(a);title('rgb image');
b=rgb2gray(a);
subplot(2,2,2),imshow(b);title('gray scale image ');
c=rgb2ind(a,234);%tolerance value between (0 to 255)
subplot(2,2,3),imshow(c);title('indexed image');
d=im2bw(a);
subplot(2,2,4),imshow(d);title('binary image');

23

rgb image

indexed image

gray scale image

binary image

% To convert an indexed image to the gray scale image


[a,map]=imread('trees.tif');
imshow(a,map);
f=ind2gray(a,map);figure,
subplot(2,2,2),imshow(f);title('gray scale image');
original image

gray scale image

a=imread('peppers.png');
subplot(2,2,1),imshow(a);title('rgb image');
b=rgb2ycbcr(a);
subplot(2,2,2),imshow(b);title('ycbcr image ');
24

c= ycbcr2rgb(b);
subplot(2,2,3),imshow(c);title('rgb image');
rgb image

indexed

ycbcr image

image

% To rotate an image 35 counterclockwise and use bilinear


interpolation.
I = imread('circuit.tif');
J = imrotate(I,35,'bilinear');
imshow(I)
figure, imshow(J)
OUTPUT:

25

% Crop an image
clc;
close all;
clear all;
I = imread('circuit.tif');
I2 = imcrop(I,[75 68 130 112]);
imview(I), imview(I2)

OUTPUT:

% To increases the size of the image


I = imread('circuit.tif');
J = imresize(I,1.25);
26

imshow(I)
figure, imshow(J)
OUTPUT:

IMAGE TRANSFORMS
Image transforms
Fourier transform
a=imread('peppers.png');
subplot(2,2,1),imshow(a);title('original image');
b=fft(a);
subplot(2,2,2),imshow(b);title('transformed image');
c=ifft(b);
subplot(2,2,3),imshow(c,[]);title('restored image');

27

original image

transformed image

restored image

% Create geometric transformation structure


I = imread('cameraman.tif');
tform = maketform('affine',[1 0 0; .5 1 0; 0 0 1]);
J = imtransform(I,tform);
imshow(I), figure, imshow(J)
OUTPUT:

DAY 3
28

Image enhancemen
% Add gaussian noise to an image
I = imread('eight.tif');
J = imnoise(I, 'gaussian');
imshow(I);
figure, imshow(J);
OUTPUT:

% Add salt & pepper noise to an image


I = imread('eight.tif');
J = imnoise(I,'salt & pepper',0.02);
imshow(I)
figure, imshow(J)
OUTPUT:

29

% Filter the noisy image with an averaging filter and display


the results.
I = imread('eight.tif');
imshow(I)
J = imnoise(I,'salt & pepper',0.02);
figure, imshow(J)
K = filter2(fspecial('average',3),J)/255;
figure, imshow(K)
OUTPUT:

30

% Median filters to filter the noisy image and display the


results.
I = imread('eight.tif');
imshow(I)
J = imnoise(I,'salt & pepper',0.02);
figure, imshow(J)
L = medfilt2(J,[3 3]);
figure, imshow(L)
OUTPUT:

31

% Filtering of images, either by correlation or convolution


I = imread('coins.png');
h = ones(5,5) / 25;
I2 = imfilter(I,h);
imshow(I), title('Original Image');
figure, imshow(I2), title('Filtered Image')
OUTPUT:

32

Original Image
Filtered Image

% Blurred image.
I = imread('cameraman.tif');
subplot(2,2,1);
imshow(I); title('Original Image');
H = fspecial('motion',20,45);
MotinBlur = imfilter(I,H,'replicate');
subplot(2,2,2);
imshow(MotionBlur); title('Motion Blurred Image');
H = fspecial('disk',10);
blurred = imfilter(I,H,'replicate');
subplot(2,2,3);
imshow(blurred); title('Blurred Image');
H = fspecial('Unsharp');
sharpeneded = imfilter(I,H,'replicate');
33

subplot(2,2,4);
imshow(sharpened); title('Sharpened Image');
OUTPUT:
Original Image

Motion Blurred Image

Blurred Image

Sharpened Image

DAY 4
Image restoration
%To blur an image using a function psf.
I = imread('peppers.png');
I = I(10+[1:256],222+[1:256],:);
figure;imshow(I);title('Original Image');
LEN = 31;
THETA = 11;
PSF = fspecial('motion',LEN,THETA);
Blurred = imfilter(I,PSF,'circular','conv');
34

figure; imshow(Blurred);title('Blurred Image');


OUTPUT:
Original Image

Blurred Image

%Deblurring with the Wiener Filter:


I = imread('peppers.png');
I = I(10+[1:256],222+[1:256],:);
figure;imshow(I);title('Original Image');
LEN = 31;
THETA = 11;
PSF = fspecial('motion',LEN,THETA);
Blurred = imfilter(I,PSF,'circular','conv');
figure; imshow(Blurred);title('Blurred Image');
wnr1 = deconvwnr(Blurred,PSF);
figure;imshow(wnr1);
35

title('Restored, True PSF');


OUTPUT:
Blurred Image

Original Image

Rest ored, True PSF

% Deblurring with a Regularized Filter


I = imread('tissue.png');
I = I(125+[1:256],1:256,:);
figure; imshow(I); title('Original Image');
PSF = fspecial('gaussian',11,5);
Blurred = imfilter(I,PSF,'conv');
V = .02;
36

BlurredNoisy = imnoise(Blurred,'gaussian',0,V);
figure;imshow(BlurredNoisy);title('Blurred and Noisy Image');
NP = V*prod(size(I));
[reg1 LAGRA] = deconvreg(BlurredNoisy,PSF,NP);
figure,imshow(reg1),title('Restored Image');
Original Image

Blurred and Noisy Image

Restored Image

Restoration algorithms
% deblurring with the lucy richardson filter
I = imread('football.jpg');
subplot(2,2,1),imshow(I);title('Original Image');
PSF = fspecial('gaussian',5,5);
% Create a simulated blur in the image and add noise.
Blurred = imfilter(I,PSF,'circular','conv');
V = .02;
BlurredNoisy = imnoise(Blurred,'gaussian',0,V);
subplot(2,2,2),imshow(BlurredNoisy);title('Blurred and Noisy ');
luc1 = deconvlucy(BlurredNoisy,PSF,5);
37

subplot(2,2,3); imshow(luc1);
title('Restored Image');
Original Image

Blurred and Noisy

Restored Image

DAY 5
COMPRESSION
Compression
a = im2double(imread('cameraman.tif'));
imshow(a);title('original image');
v = dctmtx(size(a,1));
dct = v*a*v';
figure, imshow(dct);title('dct image');

38

original image

dct image

I = imread('coins.png');
imshow(I);title('original image');
I = im2double(I);
T = dctmtx(8);
B = blkproc(I,[8 8],'P1*x*P2',T,T');
mask = [1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
B2 = blkproc(B,[8 8],'P1.*x',mask);
I2 = blkproc(B2,[8 8],'P1*x*P2',T',T);
imshow(I), figure, imshow(I2);title('dct compressed image');
OUTPUT:

39

dct compressed image

% Un sharp contrast enhancement filter


I = imread('cameraman.tif');
subplot(2,2,1);
imshow(I); title('Original Image');
H = fspecial('unsharp');
sharpened = imfilter(I,H,'replicate');
subplot(2,2,4);
imshow(sharpened); title('Sharpened Image');
OUTPUT:

Wavelets
40

a=imread('cameraman.tif');
subplot(3,2,1),imshow(a);title('original image');
[ca,ch,cv,cd]=dwt2(a,'haar');
subplot(3,2,2),imshow(mat2gray(ca));title('approximation
coefficents image');
subplot(3,2,3),imshow(ch);title('horizontal coefficients image');
subplot(3,2,4),imshow(cv);title('vertical coefficients image');
subplot(3,2,5),imshow(cd);title('diagnol coefficients image');
c=idwt2(ca,ch,cv,cd,'haar');
subplot(3,2,6),imshow(mat2gray(c));title('reconstructed image');
original image

approximation coefficents image

horizontal coefficients image

vertical coefficients image

diagnol coefficients image

reconstructed image

DAY 6
segmentation
41

% Edge-detection method that edge provides is the Sobel and


Canny method
I = imread('coins.png');
imshow(I)
BW1 = edge(I,'sobel');
BW2 = edge(I,'canny');
imshow(BW1);
figure, imshow(BW2);
OUTPUT:

42

% edge-detection method that edge provides is the prewitt


and Canny method
z= imread('cameraman.tif');
BW1 = edge(z,'prewitt');
BW2 = edge(z,'canny');
imshow(BW1);title('prewitt image');
figure, imshow(BW2);title('canny image');
OUTPUT:
prewitt image

canny image

Segmentation
43

% applying the watershed transform


a=imread('football.jpg');
imshow(a);title('original image');
b=watershed(a);
figure,imshow(b);title('transformed image');
transformed image
original image

%bwmorph means Morphological operations on binary images


% Morphological operations are create connectivity array,
% reconstruction morphologically etc.
%remove-removes interior pixels.
BW = imread('circles.png');
imshow(BW);
BW2 = bwmorph(BW,'remove');
figure, imshow(BW2);
OUTPUT:

44

%SKELETONIZATION
%To reduce all objects in an image to lines, without changing
the essential structure of the image is known as skeletonization.
BW1 = imread('circbw.tif');
BW2 = bwmorph(BW1,'skel',Inf);
imshow(BW1);
figure, imshow(BW2)
OUTPUT:

%Matrix to grayscale image convertion


45

%filter2- 2-D digital filter


%fspecial- Create predefined 2-D filter
%mat2gray- Convert matrix to grayscale image
I = imread('rice.png');
J = filter2(fspecial('sobel'),I);
K = mat2gray(J);
imshow(I), figure, imshow(K)
OUTPUT:

%Reverse black and white in a binary image.


%imcomplement- Complement image
bw = imread('text.png');
bw2 = imcomplement(bw);
subplot(1,2,1),imshow(bw)
subplot(1,2,2),imshow(bw2)
OUTPUT:

46

% To plot the border on the image.


I=imread(cameraman.tif);
imshow(I)
BW = im2bw(I);
imshow(BW)
dim = size(BW)
col = round(dim(2)/2)-90;
row = min(find(BW(:,col)))
boundary = bwtraceboundary(BW,[row, col],'N');
imshow(I)
hold on;
plot(boundary(:,2),boundary(:,1),'g','LineWidth',10);
OUTPUT:

47

%To rotate an image 35 counterclockwise and use bilinear


interpolation.
I = imread('circuit.tif');
J = imrotate(I,35,'bilinear');
imshow(I)
figure, imshow(J)
OUTPUT:

%creating a mask to an image.


I = imread('cameraman.tif');
BW = imread('text.png');
mask = BW(1:256,1:256);
f = inline('imadjust(x,[],[],0.3)');
48

I2 = roifilt2(I,mask,f);
imshow (I2)
OUTPUT:

%Edgetapering
original = imread('cameraman.tif');
PSF = fspecial('gaussian',60,10);
edgesTapered = edgetaper(original,PSF);
figure, imshow(original,[]);
figure, imshow(edgesTapered,[]);
OUTPUT:

49

Signal
lab

50

Fourier analysis
Commands covered:

dft
idft
fft
ifft
contfft

The dft command uses a straightforward method to compute the


discrete Fourier transform. Define
a vector x and compute the DFT using the command
X = dft(x)
The first element in X corresponds to the value of X(0).

The command idft uses a straightforward method to compute the


inverse discrete Fourier
transform. Define a vector X and compute the IDFT using the
command
x = idft(X)
51

the command fft which performs a Fast Fourier Transform of a


sequence of numbers. To compute
the FFT of a sequence x[n] which is stored in the vector x, use
the command
X = fft(x)
Used in this way, the command fft is interchangeable with the
command dft. For more
computational efficiency, the length of the vector x should be
equal to an exponent of 2, that is 64,
128, 512, 1024, 2048, etc. The vector x can be padded with zeros
to make it have an appropriate
length. MATLAB does this automatically by using the following
command where N is defined to be
an exponent of 2:
X = fft(x,N);
The longer the length of x, the finer the grid will be for the FFT.
Due to a wrap around effect, only
the first N/2 points of the FFT have any meaning.

The ifft command computes the inverse Fourier transform:


52

x = ifft(X);

The FFT can be used to approximate the Fourier transform of a


continuous-time signal as shown in
Section 6.6 of the textbook. A continuous-time signal x(t) is
sampled with a period of T seconds, then
the DFT is computed for the sampled signal. The resulting
amplitude must be scaled and the
corresponding frequency determined. An M-file that
approximates the Fourier Transform of a sampled
continuous-time signal is available from the ftp site and is given
below:
function [X,w] = contfft(x,T);
[n,m] = size(x);
if n<m,
x = x';
end
Xk = fft(x);
N = length(x);
53

n = 0:N-1;
n(1) = eps;
X = (1-exp(-j*2*pi*n/N))./(j*2*pi*n/N/T).*Xk.';
w = 2*pi*n/N/T;
The input is the sampled continuous-time signal x and the
sampling time T. The outputs are the
Fourier transform stored in the vector X and the corresponding
frequency vector w.

The step response y is calculated and plotted from the


following commands:
num = 2; den = [1 2];
t = 0:3/300:3; % for a time constant of 1/2
y = step(num,den,t);
plot(t,y)
For the impulse response, simply replace the word step with
impulse. For the response to an
arbitrary input stored in x, type
y = lsim(num,den,x,t);
54

plot(t,y)

Convolution
Commands :
conv
deconv
To perform discrete time convolution, x[n]*h[n], define the
vectors x and h with elements in the
sequences x[n] and h[n]. Then use the command
y = conv(x,h)
This command assumes that the first element in x and the first
element in h correspond to n=0, so
that the first element in the resulting output vector corresponds
to n=0. If this is not the case, then the
output vector will be computed correctly, but the index will have
to be adjusted. For example,
x = [1 1 1 1 1];
h = [0 1 2 3];
y = conv(x,h);
55

yields y = [0 1 3 6 6 6 5 3]. If x is indexed as described above,


then y[0] = 0, y[1] = 1,
.... In general, total up the index of the first element in h and the
index of the first element in x, this is
the index of the first element in y. For example, if the first
element in h corresponds to n = -2 and the
first element in x corresponds to n = -3, then the first element in
y corresponds to n = -5.
Care must be taken when computing the convolution of infinite
duration signals. If the vector x has
length q and the vector h has length r, then you must truncate the
vector y to have length min(q,r).
See the comments in Problem 3.7 of the textbook for additional
information.
The command conv can also be used to multiply polynomials:
suppose that the coefficients of a(s)
are given in the vector a and the coefficients of b(s) are given in
the vector b, then the coefficients of
the polynomial a(s)b(s) can be found as the elements of the
vector defined by ab = conv(a,b).
56

The command deconv is the inverse procedure to the


convolution. In this text, it is used as a means
of dividing polynomials. Given a(s) and b(s) with coefficients
stored in a and b, then the coefficients
of c(s) = b(s)/a(s) are found by using the command c =
deconv(b,a).

FUNCTION TO CALCULATE CONVOLUTION OF TWO


SEQUENCES.
% INPUTS: x1 & x2 are two input
sequences
%OUTPUT: Y CONVOLUTION OF x1
& x2.
%usage:[a]=convolve(x1,x2) at mat lab
prompt.
function y=convolve(x1,x2)
x1=5;
x2=6;
m=length(x1);

% length of first input sequence.


57

n=length(x2);
sequence.
k=m+n-1;

%length of second input


% length of convolved sequence.

x1=[x1 zeros(1,n-1)];
computation.

%append zeros for ease of

x2=[x2 zeros(1,m-1)];
computation.

% append zeros for ease of

y=zeros(1,k);
system.

% initialisation of response of thwe

for i=1:k
for j=1:i
if i==1
y(i)=x1(1)*x2(1);
else
y(i)=y(i)+x1(i-j+1)*x2(j);
end
end
end
subplot(2,2,1)
58

stem(1:1:m,x1)
title('first Input signal')
xlabel('time')
ylabel('Amplitude')

subplot(2,2,2)
stem(1:1:n,x2)
title('Second Input signal')
xlabel('time')
ylabel('Amplitude')

subplot(2,2,3)
stem(1:1:k,y)
title('Convolved signal')
xlabel('time')
ylabel('Amplitude')
% To Plot the Frequency Response of the First order System
clear all;
59

b=[1];
a=[1,-.8];
w=0:.01:2*pi;
[h]=freqz(b,a,w);
subplot(2,1,1),plot(w/pi,abs(h));
title('frquency response of first order systemh(n)=0.^nu(n)');
xlabel('Normalised frequency')
ylabel('magnitude');
subplot(2,1,2);
plot(w/pi,angle(h));
xlabel('normalised frequency');
ylabel('phase in Radians');
%To plot the frequency response of the system
clear all;
b=[1,0,.9];
a=[1,0,.4];
d=[1,-1];
f=[1,0,.4];
60

[h,ph]=freqz(b,a);
[h1,ph1]=freqz(d,f);
subplot(2,2,1);
plot(ph/pi,abs(h));
grid;
xlabel('Normalised frequency')
ylabel('magnitude');
subplot(2,2,2);
plot(ph1/pi,abs(h1));
grid;
xlabel('Normalised frequency')
ylabel('Magnitude');
subplot(2,2,3);
plot(ph/pi,angle(h));
grid;
xlabel('Normalised frequency')
ylabel('phase in radians');
subplot(2,2,4);
61

plot(ph1/pi,angle(h1));
grid;
xlabel('Normalised frequency')
ylabel('phase in Radians');
%UNIT STEP SEQUENCE

clc;
clear all;
N=input('Enter the length of unit step sequence:');
t=0:1:N-1;
y=ones(1,N);
subplot(2,1,1);
stem(t,y,'k','filled');
axis([0 N 0 2]);
xlabel('----------->t');
ylabel('----------->amplitude');
title(' Unit step sequence');
%DESCREATE TIME SIGNAL
62

fs=input('Enter the Sampling Rate')


ts=1/fs;
n=25:25;
nts=n*ts;
dt= 0.00005;
t=-.005:dt:.005;
xa=exp(-1000*abs(t));
xn=exp(-1000*abs(nts));
subplot(3,1,1);
plot(t*1000,xa);
xlabel('----->t in msec');
ylabel('x(t)');
title('Continuous Time Signalx(t)');
subplot(3,1,2);
stem(xn);
xlabel('----->n');
ylabel('x(n)');
63

title('discreate Time SIgnal x(t)');

title('ts=0.1 msec');
%reconstruction of the signal from its sample
xa=xn*sin(fs*ones(length(n),1)*t-nts*ones(1,length(t)));
subplot(3,1,3);
plot(t,xa);
xlabel('----->t in msec');
ylabel('x(t)');
title('Reconstructed signalx(t)fromx(n)');

64

%FINDING REAL AND IMAGINARY PARTS OF


COMPLEX NUMBER
%inputs: comp_number------complex number
%outputs re and img-----real and imaginary
numbers respectively
function[Re,img]=reimg(comp_number);
comp_number=3+4i;
N=length(comp_number);
for i=1:N
re(i)=abs(comp_number(i))*cos(angle(comp_number(i)));
img(i)=abs(comp_number(i))*sin(angle(comp_number(i)));
end
subplot(2,2,1)
stem(1:1:N,comp_number);
title('input signal');
xlabel('time');
ylabel('amplitude')
65

subplot(2,2,2)
stem(1:1:length(re),re);
title('real part of signal')
xlabel('time');
ylabel('amplitude')
subplot(2,2,3)
stem(1:1:length(img),img)
title('imaginary part of signal')
xlabel('time');
ylabel('amplitude')
%CALCULATION OF POWER SPECTRAL DENSITY
(PSD)
function sx=psd(x);
x=100;
n=length(x);

%length of the input sequence

u=2*n-1;
x=[x zeros(1,n-1)];
compensate extra length

%insert %extra zeros to

66

rx=zeros(1,1);

% initialize auto corellation

%CALUCLATE THE AUTO CORRELATION OF A


SEQUENCE
for i=1:u
for j=1:i
if i==1
rx(i)=rx(i)+x(i-j+1)*x(j);
end
end
end

%COMPUTE PSD
sx=fft(rx,1024);

67

% PLOTS

subplot(2,2,1);

%original input signal

plot(x);
title('inputsignal');
xlabel('Time');
ylabel('Amplitude');
subplot(2,2,2);
psd

%Magnitude spectrum of

plot(10*log10(abs(sx)));
title('magnitude spectrum');
xlabel('Frequency');
ylabel('power in db');
subplot(2,2,3);

%phase spectrum of psd

plot(angle(sx));
title('phase spectrum');
xlabel('Frequency');
ylabel('phase');
68

% IMPULSE RESPONSE OF THE SYSTEM


function y=outsys(x,h)
x=2;
h=10;
m=length(x);% Length of excitation
n=length(h);% Length of impulse response of system
k=m+n-1;%length of response
x=[x zeros(1,n-1)];% Append zeros for ease of computation
h=[h zeros(1,m-1)];% Append zeros for ease of computation
y=zeros(1,k);% Initialisation of response of the system
%compute the output of the system
for j=1:i
if i==1
y(i)=x(1)*h(1);
end
end
subplot(2,2,1)
stem(1:1:m,x);
69

xlabel('time');
ylabel('ampltue');
subplot(2,2,2);% impulse response of the system
stem(n,h);
title('impulse response of system');
xlabel('time');
ylabel('ampltude');
subplot(2,2,3);%output of the system
stem(k,y);
title('response of system');
xlabel('time');
ylabel('ampltude');

%FUNCTION TO CALCULATE THE DFT OF A GIVEN


SEQUENCE
%INPUTS: x---Input sequence
%OUTPUT: X---DFT of a given sequence

70

%**************************************************
********
function[X]=DFT2(x)
%**************************************************
*********
x=5;
N=length(x);%length of input sequence

for k=0:N-1
for n=0:N-1
wn(k+1,n+1)=exp((j*2*pi/N)*k*n);
end
end
%DFT of a given sequence
X=wn*x';
subplot(2,2,1)
stem(1:1:N,x)
title('Input Signal')
71

xlabel('Time')
ylabel('Amplitude')

subplot(2,2,2)
stem(10*log10(abs(X)))
title('MAGNITUDE RESPONSE OF PSD')
xlabel('Frequency')
ylabel('Amplitude')
subplot(2,2,3)
stem(angle(X))
title('PHASE RESPONSE OF PSD')
xlabel('Frequency')
ylabel('Phase')

RESPONSE OF A DIGITAL FILTER(FIR FILTER)

% Frequency response of a digital filter


%y(n)=x(n)-x(n-1)
72

%Filter Definition
b=[1 -1];
a=[1 0];

% Frequency response implementation

[h,th]=freqz(b,a,32);
% Frequency response plot
clf
subplot(2,2,1);
plot(th,abs(h));
title('maginitude Response');
subplot(2,2,2);
plot(th,angle(h));
title('phase Response');
xlabel('Radians');

% IMPULSE RESPONSE CALCLATION AND PLOT


73

x=[1 zeros(1,10)];
y=filter(b,a,x);
subplot(2,2,3);
stem(y);
xlabel('seconds');
title('impulse response');

%POLE-ZERO PLOT

[z,p]=tf2zp(b,a);
subplot(2,2,4);
zplane(z,p);

RESPONSE OF A DIGITAL FILTER(IIR FILTER)

% Frequency response of a digital filter


74

%y(n)=x(n)-x(n-1)
%Filter Definition
b=[.0013 .0064 .0128 .0128 .0064 .0013];
a=[1.0 -2.9754 3.8060 -2.5453 0.8811 -0.1254];
% Frequency response implimentation
[h,th]=freqz(b,a,128);
% Frequency response plot
clf
subplot(2,2,1);
plot(th,abs(h));
title('maginitude Response');
subplot(2,2,2);
plot(th,angle(h));
title('phase Response');
xlabel('Radians');
% IMPULSE RESPONSE CALCLATION AND PLOT
x=[1 zeros(1,20)];
y=filter(b,a,x);
75

subplot(2,2,3);
stem(y);
xlabel('seconds');
title('impulse response');

%pole-zero plot
[z,p,k]=tf2zp(b,a);
subplot(2,2,4);
zplane(z,p)

Communications

Program 1:

% Sample the signal 100 times per second, for 2 seconds.


%ammod-Amplitude modulation.
%ssbmod-Single sideband amplitude modulation.
76

%fft-Discrete Fourier transform.


%abs-Absolute value and complex magnitude.

Fs = 100;
t = [0:2*Fs+1]'/Fs;
Fc = 10; % Carrier frequency
x = sin(2*pi*t); % Sinusoidal signal

% Modulate x using single- and double-sideband AM.


ydouble = ammod(x,Fc,Fs);
ysingle = ssbmod(x,Fc,Fs);

% Compute spectra of both modulated signals.


zdouble = fft(ydouble);
zdouble = abs(zdouble(1:length(zdouble)/2+1));
frqdouble = [0:length(zdouble)-1]*Fs/length(zdouble)/2;
zsingle = fft(ysingle);
zsingle = abs(zsingle(1:length(zsingle)/2+1));
77

frqsingle = [0:length(zsingle)-1]*Fs/length(zsingle)/2;

% Plot spectra of both modulated signals.


figure;
subplot(2,1,1); plot(frqdouble,zdouble);
title('Spectrum of double-sideband signal');
subplot(2,1,2); plot(frqsingle,zsingle);
title('Spectrum of single-sideband signal');

78

Program 2: AM Demodulation

%butter- Design Butterworth IIR digital filter using the


specifications in filter specification object.
%ammod- Amplitude modulation
%amdemod- Amplitude demodulation
t = .01;
Fc = 10000; Fs = 80000;
t = [0:1/Fs:0.01]';
s = sin(2*pi*300*t)+2*sin(2*pi*600*t); % Original signal
figure(1) ;plot(s) ;
title(Modulating signal) ;
[num,den] = butter(10,Fc*2/Fs); % Lowpass filter
figure(2) ;plot([num,den]) ;
title(Carrier signal) ;
y1 = ammod(s,Fc,Fs); % Modulate.
figure(3) ;plot(y1) ;
79

title(Modulated signal) ;

s1 = amdemod(y1,Fc,Fs,0,0,num,den); % Demodulate.
figure(4) ;plot(s1) ;
title(DeModulated signal) ;

Program 3: Freequency Modulation

%fmmod- Freequency Modulation


t = .01;
Fc = 10000; Fs = 80000;
t = [0:1/Fs:0.01]';
s = sin(2*pi*300*t)+2*sin(2*pi*600*t); % Original signal
figure(1) ;plot(s) ;
title(Modulating signal) ;
[num,den] = butter(10,Fc*2/Fs); % Lowpass filter
figure(2) ;plot([num,den]) ;
title(Carrier signal) ;
80

dev=50;
y1 = ammod(s,Fc,Fs,dev); % Modulate.
figure(3) ;plot(y1) ;
title(Modulated signal) ;
.

Program 4:

%Phase Modulation
% Prepare to sample a signal for two seconds,
% at a rate of 100 samples per second.
Fs = 100; % Sampling rate
81

t = [0:2*Fs+1]'/Fs; % Time points for sampling


% Create the signal, a sum of sinusoids.
x = sin(2*pi*t) + sin(4*pi*t);
figure(1) ;plot(x) ;
Fc = 10; % Carrier frequency in modulation
phasedev = pi/2; % Phase deviation for phase modulation
y = pmmod(x,Fc,Fs,phasedev); % Modulate.
figure(2) ;plot(y) ;
title(Modulated signal);
z = pmdemod(y,Fc,Fs,phasedev); % Demodulate.
figure(3) ;plot(z) ;
title(Demodulated signal);

Program 5:
82

%PSK Modulation and Demodulation.


len = 10000; % Number of symbols
M = 16; % Size of alphabet
msg = randint(len,1,M); % Original signal
figure(1);plot(msg);
% Modulate using both PSK
txpsk = pskmod(msg,M);
figure(2);
plot(rxpsk); title('PSK Modulation Plot')
% Demodulate the received signals.
recovpsk = pskdemod(rxpsk,M);
figure(3);
plot(recovpsk); title('PSK DeModulation Plot');

Program 6:

83

%Quantization
%partition- To specify a partition in MATLAB
%codebook- codebook tells the quantizer which common
value to assign
%quantiz-Produce quantization index and quantized output
value
%legend-Graph legend for lines and patches
t = [0:.1:2*pi]; % Times at which to sample the sine
function
sig = sin(t); % Original signal, a sine wave
partition = [-1:.2:1]; % Length 11, to represent 12 intervals
codebook = [-1.2:.2:1]; % Length 12, one entry for each
interval
[index,quants] = quantiz(sig,partition,codebook); %
Quantize.
plot(t,sig,'x',t,quants,'.')
legend('Original signal','Quantized signal');
axis([-.2 7 -1.2 1.2]

84

%CHEBYCHEV TYPE-1 LOW PASS FILTER


clc;
clear all;
format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb1ord(w1,w2,rp,rs,'s');
[b,a]=cheby1(n,rp,wn,'s');
w=0:.01:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
85

subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
title('magnitude of the system');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
title('phase of the system');

OUT PUT OF THE %CHEBYCHEV TYPE-1 LOW PASS


FILTER

86

Enter the pass band ripple0. 2


Enter the stop band ripple45
Enter the pass band freq1300
Enter the stop band freq1500
Enter the Sampling Frequency10000

magnitude of the system

Gain in db--->

0
-100
-200
-300
-400

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->
phase of the system

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

4
2
0
-2
-4

%CHEBYCHEV TYPE-1 HIGH PASS FILTER


87

clc;
clear all;
format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb1ord(w1,w2,rp,rs,'s');
[b,a]=cheby1(n,rp,wn,high,'s');
w=0:.01/pi:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
88

ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
title('magnitude of the system');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
title('phase of the system');

OUTPUT OF THE CHEBYCHEV TYPE-1 HIGH PASS


FILTER

Enter the pass band ripple0.3


Enter the stop band ripple60
Enter the pass band freq1500
Enter the stop band freq2000
Enter the Sampling Frequency9000
89

magnitude of the system

Gain in db--->

0
-50
-100
-150
-200

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->
phase of the system

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

4
2
0
-2
-4

%CHEBYCHEV TYPE-1 BAND PASS FILTER

clc;
clear all;
format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
90

fs=input('Enter the Sampling Frequency');


w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb1ord(w1,w2,rp,rs,'s');
wn=[w1,w2];
[b,a]=cheby1(n,rp,wn,bandpass','s');
w=0:.01/pi:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
91

CHEBYCHEV TYPE-1 BAND PASS FILTER

Enter the pass band ripple0.4


Enter the stop band ripple35
Enter the pass band freq2000
Enter the stop band freq2500
Enter the Sampling Frequency10000

92

Gain in db--->

0
-200
-400
-600

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

4
2
0
-2
-4

%CHEBYCHEV TYPE-1 BAND STOP FILTER

clc;
clear all;
format long
rp=input('Enter the pass band ripple');
93

rs=input('Enter the stop band ripple');


wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb1ord(w1,w2,rp,rs,'s');
wn=[w1,w2];
[b,a]=cheby1(n,rp,wn,stop','s');
w=0:.01/pi:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
subplot(2,1,2);
94

plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');

CHEBYCHEV TYPE-1 BAND STOP FILTER

Enter the pass band ripple0.25


Enter the stop band ripple40
Enter the pass band freq2500
Enter the stop band freq2750
Enter the Sampling Frequency7000

95

Gain in db--->

50

-50

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

4
2
0
-2
-4

%CHEBYCHEV TYPE-2 LOW PASS FILTER

clc;
clear all;
format long
rp=input('Enter the pass band ripple');
96

rs=input('Enter the stop band ripple');


wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb2ord(w1,w2,rp,rs,'s');
[b,a]=cheby2(n,rp,wn,'s');
w=0:.01:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
subplot(2,1,2);
plot(om/pi,an);
97

ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');

OUTPUT OF THE CHEBYCHEV TYPE-2 LOW PASS


FILTER

Enter the pass band ripple.0.35


Enter the stop band ripple35
Enter the pass band freq1500
Enter the stop band freq2000
Enter the Sampling Frequency8000

98

Gain in db--->

0
-20
-40
-60

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

2
1
0
-1
-2

%CHEBYCHEV TYPE-2 HIGH PASS FILTER

clc;
clear all;
format long
99

rp=input('Enter the pass band ripple');


rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb2ord(w1,w2,rp,rs,'s');
[b,a]=cheby2(n,rp,wn,'high','s');
w=0:.01/pi:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
subplot(2,1,2);
100

plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');

CHEBYCHEV TYPE-2 HIGH PASS FILTER

Enter the pass band ripple0.25


Enter the stop band ripple40
Enter the pass band freq1400
Enter the stop band freq1800
Enter the Sampling Frequency7000

101

Gain in db--->

10
0
-10
-20
-30

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

2
1
0
-1
-2

%CHEBYCHEV TYPE-2 BAND PASS FILTER

clc;
clear all;
format long
102

rp=input('Enter the pass band ripple');


rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb2ord(w1,w2,rp,rs,'s');
wn=[w1,w2];
[b,a]=cheby2(n,rp,wn,'high','s');
w=0:.01/pi:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
103

subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');

CHEBYCHEV TYPE-2 BAND PASS FILTER

Enter the pass band ripple0.4


Enter the stop band ripple40
Enter the pass band freq1400
Enter the stop band freq2000
Enter the Sampling Frequency9000

104

Gain in db--->

50
0
-50
-100

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

2
1
0
-1
-2

%CHEBYCHEV TYPE2 BAND STOP FILTER

clc;
clear all;
format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
105

wp=input('Enter the pass band freq');


ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb1ord(w1,w2,rp,rs,'s');
wn=[w1,w2];
[b,a]=cheby1(n,rp,wn,stop','s');
w=0:.01/pi:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
subplot(2,1,2);
plot(om/pi,an);
106

ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');

CHEBYCHEV TYPE-2 BAND STOP FILTER

Enter the pass band ripple0.3


Enter the stop band ripple46
Enter the pass band freq1400
Enter the stop band freq2000
Enter the Sampling Frequency8000

107

Gain in db--->

5
0
-5
-10
-15

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

2
1
0
-1
-2

% BUTTER WORTH LOW PASS FILTER


clc;
clear all;
format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
108

ws=input('Enter the stop band freq');


fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,rp,rs,'s');
%[b,a]=zp2tf(z,p,k);
[b,a]=butter(n,wn,'s');
w=0:.01:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
109

xlabel('(b) Normalised Frequency--->');

OUTPUT OF THE BUTTERWORTH LOW PASS FILTER


Enter the pass band ripple.6
Enter the stop band ripple.9
Enter the pass band freq1200
Enter the stop band freq1800

G a in in d b --->

Enter the Sampling Frequency4000


0
-2
-4

P h a s e in ra d ia n s -->

-6

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

0
-0.5
-1
-1.5

110

% BUTTER WORTH HIGH PASS FILTER


clc;
clear all;
%format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
111

[n,wn]=buttord(w1,w2,rp,rs,'s');
%[z,p,k] = butter(n,Wn,'s')
%[b,a] = butter(n,Wn)
%[b,a]=butter(n,wn,'s');
[b,a] = butter(9,300/500,'high')
w=0:.01:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
OUTPUT OF THE BUTTERWORTH HIGH PASS FILTER
112

Enter the pass band ripple.6


Enter the stop band ripple.9
Enter the pass band freq1200
Enter the stop band freq1800
Enter the Sampling Frequency4000

b=

Columns 1 through 7

0.0011 -0.0096
0.0895

0.0384 -0.0895

0.1342 -0.1342

Columns 8 through 10

-0.0384

0.0096 -0.0011

113

a=

Columns 1 through 7

1.0000
0.1993

1.7916

2.5319

2.1182

1.3708

0.6090

Columns 8 through 10

0.0431

0.0058

0.0004

114

Gain in db--->

20
0
-20
-40
-60

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

4
2
0
-2
-4

% BUTTER WORTH BAND PASS FILTER


clc;
clear all;
%format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
115

ws=input('Enter the stop band freq');


fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n]=buttord(w1,w2,rp,rs,'s');
wn=[w1,w2];
%[z,p,k] = butter(n,Wn,'s')
%[b,a] = butter(n,Wn)
%[b,a]=butter(n,wn,'s');
[b,a] = butter(n,wn,'bandpass','s');
w=0:.01:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
116

subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');

OUTPUT OF THE BUTTERWORTH BAND PASS FLTER

Enter the pass band ripple0.6


Enter the stop band ripple9
Enter the pass band freq100
Enter the stop band freq800
Enter the Sampling Frequency4000

117

Gain in db--->

0
-5
-10
-15
-20

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

2
1
0
-1
-2

% BARLETT LOW PASS FLTER

clc;
clear all;
%format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
fp=input('Enter the pass band freq');
118

fs=input('Enter the stop band freq');


f=input('Enter the Sampling Frequency');
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
y=bartlett(n1);
b=fir1(n,wp,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,1);
plot(o/pi,m);
ylabel('Gain in db--->');
119

xlabel('(a)Normalised Frequency--->');
OUTPUT OF THE BARLETT LOW PASS FLTER

Enter the pass band ripple0.04


Enter the stop band ripple0.02
Enter the pass band freq1500
Enter the stop band freq2000

G a in in d b --->

Enter the Sampling Frequency8000


0
-10
-20
-30
-40

0.5
(a)Normalised Frequency--->

% BARLETT HIGH PASS FLTER

120

clc;
clear all;
%format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
fp=input('Enter the pass band freq');
fs=input('Enter the stop band freq');
f=input('Enter the Sampling Frequency');
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
y=bartlett(n1);
121

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);
ylabel('Gain in db--->');
xlabel('(b)Normalised Frequency--->');
OUTPUT OF THE BARLETT HIGH PASS FLTER

Enter the pass band ripple0.04


Enter the stop band ripple0.02
Enter the pass band freq1500
Enter the stop band freq2000
Enter the Sampling Frequency8000

122

G a in in d b --->

10
0
-10
-20
-30

0.5
(b)Normalised Frequency--->

% BARLETT BAND PASS FLTER

clc;
clear all;
%format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
fp=input('Enter the pass band freq');
fs=input('Enter the stop band freq');
f=input('Enter the Sampling Frequency');
123

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
y=bartlett(n1);
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);
ylabel('Gain in db--->');
xlabel('(c)Normalised Frequency--->');
124

OUTPUT OF THE BARLETT BAND PASS FLTER

Enter the pass band ripple0.04


Enter the stop band ripple0.02
Enter the pass band freq1500
Enter the stop band freq2000
Enter the Sampling Frequency8000

Gain in db--->

10
0
-10
-20
-30
-40

0.5
1
(c)Normalised Frequency--->

125

% BARLETT BAND STOP FLTER

clc;
clear all;
%format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
fp=input('Enter the pass band freq');
fs=input('Enter the stop band freq');
f=input('Enter the Sampling Frequency');
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;
126

end
y=bartlett(n1);
wn=[wp ws];
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);
ylabel('Gain in db--->');
xlabel('(d)Normalised Frequency--->');
OUTPUT OF THE BARLETT BAND STOP FLTER

Enter the pass band ripple0.04


Enter the stop band ripple0.02
Enter the pass band freq1500
Enter the stop band freq2000
Enter the Sampling Frequency8000
127

Gain in db--->

2
0
-2
-4
-6
-8

0.5
1
(d)Normalised Frequency--->

%fir1-Window-based finite impulse response filter design


b = fir1(48,[0.35 0.65]);
freqz(b,1,512);

OUTPUT:

128

Magnitude (dB)

50
0
-50
-100

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8

Normalized Frequency ( rad/sample)

0.9

Phase (degrees)

1000
0
-1000
-2000

%fir1-Window-based finite impulse response filter design


load chirp

% Load y and fs.

b = fir1(34,0.48,'high',chebwin(35,30));
freqz(b,1,512)

OUTPUT:

129

Magnitude (dB)

50
0
-50
-100
-150

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8
Normalized Frequency ( rad/sample)

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8

Normalized Frequency ( rad/sample)

0.9

Phase (degrees)

1000
0
-1000
-2000

%Filter a signal using a filter with various initial conditions (IC)


or no initial conditions.
x = randn(100,1);

% Original signal.

b = fir1(50,.4);

% 50th-order linear-phase FIR filter.

hd = dfilt.dffir(b);

% Direct-form FIR implementation.

% Do not set specific initial conditions.

130

y1 = filter(hd,x);
zf = hd.states;

% 'PersistentMemory' is 'false' (default).


% Final conditions.

%Now use nonzero initial conditions by setting ICs after before


you filter.
hd.persistentmemory = true;
hd.states = 1;

% Uses scalar expansion.

y2 = filter(hd,x);
stem([y1 y2])

% Different sequences at the beginning.

OUTPUT:

131

1.5

0.5

-0.5

-1

-1.5

10

20

30

40

50

60

70

80

90

100

132

You might also like