Академический Документы
Профессиональный Документы
Культура Документы
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 easy-to-use environment where problems
and solutions are expressed in 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
DAY 1
%%%%%%Some basic programs on Matrices%%%%%%
>> zeros (3)
ans =
0
>> 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]
d=
1
>> e = [1 2 3; 4 5 6; 7 8 9]
e=
1
>> 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
>> A = [1 2 3;4 5 6; 7 8 9]
A=
0.7258
>> 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)
C=
135
>> D = diag(A)
D=
11
15
19
>> E = diag(A, -1)
E=
14
18
>> F = det(A)
F=
0
>> G = norm(A)
G=
45.6601
>> H = inv(A)
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=
5.5000
6.0000
6.5000
7.0000
7.5000
8.0000
8.5000
9.0000
9.5000
>> L = mod(A,4)
L=
%%%%%%%%%Matrix Indexing%%%%%%%%%
>> A = [1 2 3; 4 5 6; 7 8 9]
A=
1
>> A(3, 2)
ans =
8
>> A(end,end-2)
ans =
7
>> A(1,:)
ans =
1
>> A(2,:)
ans =
4
>> A(:,1)
ans =
1
4
7
>> A(:,3)
ans =
3
6
9
>> 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 =
2
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:
J = immultiply(I,0.5);
subplot(1,2,1), imshow(I)
subplot(1,2,2), imshow(J)
OUTPUT:
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:
J=
6.9439
4000
3500
3000
2500
2000
1500
1000
500
0
0
50
100
150
OUTPUT:
200
250
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 ');
d=im2bw(a);
subplot(2,2,4),imshow(d);title('binary image');
rgb image
indexed image
binary 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
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 ');
c= ycbcr2rgb(b);
subplot(2,2,3),imshow(c);title('rgb image');
rgb image
ycbcr image
indexed image
OUTPUT:
% Crop an image
close all;
clear all;
clc;
I = imread('circuit.tif');
I2 = imcrop(I,[75 68 130 112]);
imview(I), imview(I2)
OUTPUT:
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');0
original image
transformed image
restored image
DAY 3
IMAGE ENHANCEMENT
% Add gaussian noise to an image
I = imread('eight.tif');
J = imnoise(I, 'gaussian');
imshow(I);
figure, imshow(J);
OUTPUT:
OUTPUT:
% Filter the noisy image with an averaging filter and display the results.
I = imread('eight.tif');
Subplot(2,2,1),imshow(I)
J = imnoise(I,'salt & pepper',0.02)
Subplot(2,2,2), imshow(J)
K = filter2(fspecial('average',3),J)/255;
figure, imshow(K)
OUTPUT:
% 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
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');
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');
figure; imshow(Blurred);title('Blurred Image');
OUTPUT:
Original Image
Blurred Image
Original Image
Blurred 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
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);
subplot(2,2,3); imshow(luc1);
Restored Image
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');
original 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
dct image
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:
subplot(2,2,4);
imshow(sharpened); title('Sharpened Image');
OUTPUT:
WAVELETS
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
% 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:
% 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
% applying the watershed transform
a=imread('football.jpg');
imshow(a);title('original image');
b=watershed(a);
figure,imshow(b);title('transformed image');
original image
transformed image
OUTPUT:
%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 %filter2- 2-D digital filter %fspecialCreate 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:
OUTPUT:
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:
%Edgetapering
original = imread('cameraman.tif');
PSF = fspecial('gaussian',60,10);
edgesTapered = edgetaper(original,PSF);
figure, imshow(original,[]);
figure, imshow(edgesTapered,[]);
OUTPUT:
SIGNAL LAB
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)
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:
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);
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);
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);
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).
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).
y(i)=y(i)+x1(i-j+1)*x2(j);
end
end
end
subplot(2,2,1)
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;
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];
[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);
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
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)');
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')
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)];
rx=zeros(1,1);
%COMPUTE PSD
sx=fft(rx,1024);
% PLOTS
subplot(2,2,1);
plot(x);
title('inputsignal');
xlabel('Time');
ylabel('Amplitude');
subplot(2,2,2);
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');
% 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);
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');
%***********************************************************
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')
xlabel('Time')
ylabel('Amplitude')
subplot(2,2,2)
stem(10*log10(abs(X)))
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,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);
%pole-zero plot
[z,p,k]=tf2zp(b,a);
subplot(2,2,4);
zplane(z,p)
COMMUNICATIONS
Program 1:
%ammod-Amplitude modulation.
%ssbmod-Single sideband amplitude modulation.
%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
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');
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) ;
title(Modulated signal) ;
s1 = amdemod(y1,Fc,Fs,0,0,num,den); % Demodulate.
figure(4) ;plot(s1) ;
title(DeModulated signal) ;
%Phase Modulation
% Prepare to sample a signal for two seconds,
% at a rate of 100 samples per second.
Fs = 100; % Sampling rate
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:
figure(3);
plot(recovpsk); title('PSK DeModulation Plot');
Program 6:
%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]
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);
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
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
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
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);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
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');
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--->');
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');
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);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
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');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
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
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);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
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
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--->');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
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');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
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
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-->');
xlabel('(b) Normalised Frequency--->');
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
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--->');
b=
Columns 1 through 7
0.0011 -0.0096
0.0384 -0.0895
0.1342 -0.1342
0.0895
1.3708
0.1993
Columns 8 through 10
-0.0384
0.0096 -0.0011
a=
Columns 1 through 7
1.0000
1.7916
2.5319
Columns 8 through 10
2.1182
0.6090
0.0431
0.0058
0.0004
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
%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]=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--->');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
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');
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
G a in in d b --->
0.5
(a)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');
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,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--->');
G a in in d b --->
10
0
-10
-20
-30
clc;
clear all;
%format long
rp=input('Enter the pass band ripple');
0.5
(b)Normalised Frequency--->
plot(o/pi,m);
ylabel('Gain in db--->');
xlabel('(c)Normalised Frequency--->');
Gain in db--->
10
0
-10
-20
-30
-40
0.5
1
(c)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');
xlabel('(d)Normalised Frequency--->');
Gain in db--->
2
0
-2
-4
-6
-8
0.5
1
(d)Normalised Frequency--->
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:
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
%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);
hd = dfilt.dffir(b);
%Now use nonzero initial conditions by setting ICs after before you filter.
hd.persistentmemory = true;
hd.states = 1;
y2 = filter(hd,x);
stem([y1 y2])
OUTPUT:
1.5
0.5
-0.5
-1
-1.5
10
20
30
40
50
60
70
80
90
100