Professional Documents
Culture Documents
No. Practical
1. Introduction of MATLAB
2. Generation of various discrete-time sequences:
(a)unit sample (d)Exponential
(b)unit step (e) Sinusoidal
(c)ramp (f) Square
3. Convolution and Deconvolution
4. System response:
(a) Impulse response (c) Frequency response
(b) Step response (d) Phase response
5. Z-Transform
Inverse Z-Transform
6. Sampling:
(a)Up sampling
(b) Down sampling
(c) Re-sampling
7. Discrete Fourier Transform Or FFT and IFFT:
(a)4-point
(b)8-point
8. Simulink
System response
Convolution/Deconvolution
Up/Down Sampling
9. DSP Application
Waveread, Wavwrite, Imread, Imwrite, Imshow
10. Design Digital IIR Butterworth
a) Low-pass
b) High-pass
c) Band-pass
d) Band-stop
11. Analog-to-Digital Filter Transformation by
a) Impulse invariance
b) Bilinear Transformation
12. FIR filter design using windowing
Development Environment:
This is the set of tools and facilities that help you use MATLAB functions and files. Many of these
tools are GUIs. It includes MATLAB desktop and Command Window, a command history, an
editor and debugger, and browsers for viewing help, the workspace, files, and the search path.
Graphics:
MATLAB has extensive facilities for displaying vectors and matrices as graphs, as well as
annotating and printing these graphs. It includes high – level functions for two dimensional and
three dimensional data visualization, image processing, animation and presentation graphics. It
also includes low level functions that allow you to fully customize the appearance of graphics as
well as to build complete graphical user interface on your MATLAB applications.
1. From the command line, evaluate these expressions (a-d) using the following values, t=3.25,
x=4.5,y=6.97,z=1.03
a.) M = 4 x 2 + 3 y + 10
b.) N = e log10 y + 2 z x
1 1
c.)O = +
( x + y ) (t + z )
π
− x
d .) P = 4e 2
− sin
π y
Ans:
echo on
t=3.25
x=4.5
y=6.97
z=1.03
M=4*x^2+3*y+10
N=exp(log10(y))+2*z^x
O=sqrt(1/(x+y)+1/(t+z));
P=4*exp(-pi/2)-abs(sin(x/(pi*y)));
2.
a. Create a vector X with a starting value of 0.0 and an ending value of 50, with increments of 0.1
between values
b. Create a vector Y with a starting value of 0.1 and an ending value of 50 and with the same
number of elements as in X. The elements must be evenly spaced.
Ans:
X=0:0.1:50;
Y=linspace(.1,50,length(X));
3.
a. Using MATLAB commands create an array, labeled A, consisting of the first 10 prime numbers
listed in increasing order.
b. Using MATLAB commands on the array A to create the matrix B
13 17 19 23 29
B=
13 11 7 5 3
Ans:
A=primes(30)
B=[A(6:10);A(6:-1:2)]
Ans:
C=B'
BC=B*C
CB=C*B
5. Use a MATLAB command to create directly from matrix B, above, the matrices
19
D=
7
E = [19 7 ]
Ans:
D=B(:,3);
E=B(:,3)';
7. Write a MATLAB code to complete the magic square (not using the command magic) so that the
x1 x2 x3
sum from all sides is 15, x4 x5 x6 . Hint : Use solution to system of equations
4 x7 2
Ans:
A=[1 1 1 0 0 0 0 15;
0 0 0 1 1 1 0 15;
0 0 0 0 0 0 1 9;
1 0 0 1 0 0 0 11;
0 1 0 0 1 0 1 15;
0 0 1 0 0 1 0 13;
1 0 0 0 1 0 0 13;
0 0 1 0 1 0 0 11];
X=A(:,1:7)\A(:,8);
Magic_Sq=[X(1:3)';
X(4:6)';
4 X(7) 2]
% Ramp sequence
subplot(3,3,3)
stem(t,t,'r')
ylabel('Amlitude')
xlabel('time')
title('ramp')
% Exponential sequence
a=0.5
y2=a.^t
subplot(3,3,4)
stem(t,y2,'y')
ylabel('Amlitude')
xlabel('time')
title('Exp decaying')
a1=1.5
y2=a1.^t
subplot(3,3,5)
stem(t,y2,'g')
ylabel('Amlitude')
xlabel('time')
title('Exp Growing')
% Cosine sequence
y4=cos(t)
subplot(3,3,7)
stem(t,y4,'m')
ylabel('Amlitude')
xlabel('time')
title('Cosine')
% Square sequence
y5=square(t)
subplot(3,3,8)
stem(t,y5,'r')
ylabel('Amlitude')
xlabel('time')
title('Square')
close all;
clear all;
clc;
x = input(' Enter First Seq : ');
y = input(' Enter Second Seq : ');
%x = [1 2 3 4 5];
%y = [3 4 6];
n= length(x)+length(y)-1;
yl = length(y);
xl = length(x);
x0 = zeros(1,n+xl);
x0(yl:yl+xl-1)=x;
y0 = zeros(1,n+yl);
y0(1:yl)=y(yl:-1:1);
for i = 1:n
h(i)=0;
for k = yl:yl+xl-1
h(i) = h(i)+x0(k)*y0(k);
end
for t = length(y0):-1:2
y0(t)=y0(t-1);
end
y0(1)=0;
end
disp('Without using inbuilt Command');
h
disp('With using inbuilt Command');
h0 = conv(x,y)
disp('Deconvolution')
[q,r]=deconv(h0,y);
disp(q)
disp('Deconvolution')
[q,r]=deconv(h0,x);
disp(q)
h=
1 2 3 4 3 2 1
h0 =
1 2 3 4 3 2 1
Deconvolution
1 1 1 1
Deconvolution
1 1 1 1
Output:
c=
1.0000
1.5000
1.7500
1.8750
1.9375
1.9688
1.9844
1.9922
1.9961
1.9980
1.9990
1.9995
1.9998
1.9999
p=
1.0000
0.5000
k=
1
sos =
1.0000 0 0 1.0000 0.1682 0
1.0000 0 0 1.0000 0.3318 5.9442
g=
1
a=
1 0 0 0
b=
1.0000 0.5000 6.0000 1.0000
clear all;
close all;
%x= [1 3 2 5 7 8 4 2];
fc = .3*pi;
n= 0:40;
x = sin(2*pi*fc*n);
figure(1);
subplot(2,1,1);
stem(x);
title('original signal');
y = upsample(x,2);
subplot(2,1,2);
stem(y);
title('upsampled signal *2');
figure(2);
subplot(2,1,1);
z = downsample(x,2);
stem(z);
title('downsampled signal /2');
w = resample(x,2,3);
subplot(2,1,2);
stem(w);
title('re sampled signal(2/3)');
figure(3);
%upsampling
subplot(2,1,1)
x3 = interp(x,2);
stem(x3);
N = 2;
len = length(x);
len1 = N*len;
x1 = zeros(1,len1);
x1(1:2:len1) = x;
subplot(2,1,2);
stem(x1);
figure(5);
subplot(2,1,1);
stem(x1);
subplot(2,1,2);
x2 = decimate(x,2);
stem(x2);
Part – A
function y = twiddle_f(n,k,N);
y = exp(-2*pi*j*n*k/N);
Part – B
clear all;
close all;
%Decimation in Time FFT algorithm
% implementation
x = [1 2 3 4 5 6 7 8];
xo = x(1:2:length(x));
xe = x(2:2:length(x));
xoo = xo(1:2:length(xo));
xeo = xo(2:2:length(xo));
xoe = xe(1:2:length(xe));
xee = xe(2:2:length(xe));
for i = 1:2
A(i) = 0;
B(i) = 0;
C(i) = 0;
D(i) = 0;
for j = 1:2
A(i) = A(i)+ xoo(j)*twiddle_f(j-1,i-1,2);
B(i) = B(i)+ xeo(j)*twiddle_f(j-1,i-1,2);
C(i) = C(i)+ xoe(j)*twiddle_f(j-1,i-1,2);
D(i) = D(i)+ xee(j)*twiddle_f(j-1,i-1,2);
end
end
for i = 1:4
if i<=2
G(i) = A(i) + twiddle_f(i-1,1,4)*B(i);
H(i) = C(i) + twiddle_f(i-1,1,4)*D(i);
else
for i = 1:8;
if i<=4
X(i) = G(i) + twiddle_f(i-1,1,8)*H(i);
else
X(i) = G(i-4) + twiddle_f(i-1,1,8)*H(i-4);
end
end
X1 =fft(x,8);
subplot(2,1,1);
stem(abs(X));
title('FFT using algorithm');
subplot(2,1,2);
stem(abs(X1));
title('FFT using in-built function');
Simulink Library:
clc;
clear all;
close all;
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,rp,rs);
[b,a]=butter(n,wn);
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(1);
subplot(2,1,1);
plot(om/pi,m);
xlabel('Normalised Freq');
ylabel('Gain in dB');
%plot(om/pi,m);
subplot(2,1,2);
plot(om/pi,an);
xlabel('Normalised Freq');
ylabel('Phase in Radians');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,rp,rs);
[b,a]=butter(n,wn,'high');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(2);
subplot(2,1,1);
plot(om/pi,m);
xlabel('Normalised Freq');
ylabel('Gain in dB');
%plot(om/pi,m);
subplot(2,1,2);
plot(om/pi,an);
xlabel('Normalised Freq');
ylabel('Phase in Radians');
w1=2*wp/fs;
w2=2*ws/fs;
[n]=buttord(w1,w2,rp,rs);
wn=[w1 w2];
[b,a]=butter(n,wn,'bandpass');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(3);
subplot(2,1,1);
subplot(2,1,2);
plot(om/pi,an);
xlabel('Normalised Freq');
ylabel('Phase in Radians');
w1=2*wp/fs;
w2=2*ws/fs;
[n]=buttord(w1,w2,rp,rs);
wn=[w1 w2];
[b,a]=butter(n,wn,'stop');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(4);
subplot(2,1,1);
plot(om/pi,m);
xlabel('Normalised Freq');
ylabel('Gain in dB');
%plot(om/pi,m);
subplot(2,1,2);
plot(om/pi,an);
xlabel('Normalised Freq');
ylabel('Phase in Radians');
b) Impulse invariance
close all;
clear all;
clc
a=input('Enter the Numerator : ');
b=input('Enter the Denumerator : ');
f=input('Enter the Sampling Freq : ');
sys=tf(a,b);
[numd dend]=impinvar(a,b,f);
disp(numd);
disp(dend);
disp(sys);
Output:
Enter the Numerator : [1 2]
Enter the Denumerator : [1 3 4]
Enter the Sampling Freq : 0.3
3.3333 -0.0014
1.0000 0.0040 0.0000
tf object: 1-by-1
c) Bilinear Transformation
close all;
clear all;
clc
a=input('Enter The Numerator : ');
b=input('Enter The Denumerator : ');
sys=tf(a,b);
f=input('Enter The Sampling Freq : ');
[numd dend]=bilinear(a,b,f);
disp(numd);
disp(dend);
Output:
Enter The Numerator : [1 2]
Enter The Denumerator : [2 3 4]
Enter The Sampling Freq : .4
0.3646 0.5208 0.1563
1.0000 0.7083 0.3750