Lab Manul
Lab Manul
LAB MANUAL
department
at
the
University
of
New
Mexico.
3. SIGNAL PROCESSING
>> 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
>> 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
0.2877
1.1892
0.1746
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
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
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
OUTPUT:
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
21
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
binary 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
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:
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
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:
29
30
31
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
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
Blurred Image
Original Image
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
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
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
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
reconstructed image
DAY 6
segmentation
41
42
canny image
Segmentation
43
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:
46
47
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
x = ifft(X);
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.
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
n=length(x2);
sequence.
k=m+n-1;
x1=[x1 zeros(1,n-1)];
computation.
x2=[x2 zeros(1,m-1)];
computation.
y=zeros(1,k);
system.
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
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
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);
u=2*n-1;
x=[x zeros(1,n-1)];
compensate extra length
66
rx=zeros(1,1);
%COMPUTE PSD
sx=fft(rx,1024);
67
% PLOTS
subplot(2,2,1);
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);
plot(angle(sx));
title('phase spectrum');
xlabel('Frequency');
ylabel('phase');
68
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');
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')
%Filter Definition
b=[1 -1];
a=[1 0];
[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');
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);
%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:
Fs = 100;
t = [0:2*Fs+1]'/Fs;
Fc = 10; % Carrier frequency
x = sin(2*pi*t); % Sinusoidal signal
frqsingle = [0:length(zsingle)-1]*Fs/length(zsingle)/2;
78
Program 2: AM Demodulation
title(Modulated signal) ;
s1 = amdemod(y1,Fc,Fs,0,0,num,den); % Demodulate.
figure(4) ;plot(s1) ;
title(DeModulated signal) ;
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
Program 5:
82
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
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');
86
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
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');
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
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
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
clc;
clear all;
format long
rp=input('Enter the pass band ripple');
93
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
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
clc;
clear all;
format long
rp=input('Enter the pass band ripple');
96
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
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
clc;
clear all;
format long
99
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
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
clc;
clear all;
format long
102
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
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
clc;
clear all;
format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
105
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
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
G a in in d b --->
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
[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
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
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
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
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
xlabel('(a)Normalised Frequency--->');
OUTPUT OF THE BARLETT LOW PASS FLTER
G a in in d b --->
0.5
(a)Normalised Frequency--->
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
122
G a in in d b --->
10
0
-10
-20
-30
0.5
(b)Normalised Frequency--->
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
Gain in db--->
10
0
-10
-20
-30
-40
0.5
1
(c)Normalised Frequency--->
125
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
Gain in db--->
2
0
-2
-4
-6
-8
0.5
1
(d)Normalised Frequency--->
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
0.9
Phase (degrees)
1000
0
-1000
-2000
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
0.9
Phase (degrees)
1000
0
-1000
-2000
% Original signal.
b = fir1(50,.4);
hd = dfilt.dffir(b);
130
y1 = filter(hd,x);
zf = hd.states;
y2 = filter(hd,x);
stem([y1 y2])
OUTPUT:
131
1.5
0.5
-0.5
-1
-1.5
10
20
30
40
50
60
70
80
90
100
132