+ All Categories
Home > Documents > DSP Lab Matlab)

DSP Lab Matlab)

Date post: 07-Apr-2015
Category:
Upload: rsdelhi123
View: 223 times
Download: 7 times
Share this document with a friend
27
DIGITAL SIGNAL PROCESSING LAB VISHAVJIT SINGH CSE-1(6 TH SEM)
Transcript
Page 1: DSP Lab Matlab)

DIGITAL SIGNAL PROCESSING LAB

VISHAVJIT SINGHCSE-1(6TH SEM)0581322707 (053)

Page 2: DSP Lab Matlab)

EXPERIMENT: 1

AIM:- Introduction to MATLAB/C

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. Typical uses include

Math and computation Algorithm development Data acquisition Modeling, simulation, and prototyping Data analysis, exploration, and visualization Scientific and engineering graphics Application development, including graphical user interface building

MATLAB is an interactive system whose basic data element is an array that does not require dimensioning. This allows you to solve many technical computing problems, especially those with matrix and vector formulations, in a fraction of the time it would take to write a program in a scalar noninteractive language such as C or Fortran.

The name MATLAB stands for matrix laboratory. 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.

The MATLAB system consists of five main parts:

Development Environment: This is the set of tools and facilities that help you use MATLAB functions and files. Many of these tools are graphical user interfaces. It includes the MATLAB desktop and Command Window, a command history, an editor and debugger, and browsers for viewing help, the workspace, files, and the search path.

The MATLAB Mathematical Function Library: This is a vast collection of computational algorithms ranging from elementary functions, like sum, sine, cosine, and complex arithmetic, to more sophisticated functions like matrix inverse, matrix eigenvalues, Bessel functions, and fast Fourier transforms.

The MATLAB Language: This is a high-level matrix/array language with control flow statements, functions, data structures, input/output, and object-oriented programming features. It allows both "programming in the small" to rapidly create quick and dirty throw-away programs, and "programming in the large" to create large and complex application programs.

Page 3: DSP Lab Matlab)

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 interfaces on your MATLAB applications.

The MATLAB Application Program Interface (API): This is a library that allows you to write C and Fortran programs that interact with MATLAB. It includes facilities for calling routines from MATLAB (dynamic linking), calling MATLAB as a computational engine, and for reading and writing MAT-files.

Page 4: DSP Lab Matlab)

EXPERIMENT: 2

AIM:- Generate the following basic signals (discrete) using MATLAB

(i) Unit Step Signal(ii) Unit Sample Signal(iii) Unit Ramp Sequence(iv) Exponential Sequence(v) Sine Sequence(vi) Cosine Sequence

(i) Unit Step Signal >> n = -4:1:4;>> y = [zeros(1,4) ones(1,5)]>> subplot(3,2,1)>> stem(n,y)>> title('Unit Step Signal')>> xlabel('time')>> ylabel('y')

(ii) Unit Sample Signal >> n = -4:1:4;>> y = [zeros(1,4) ones(1,1) zeros(1,4)]>> subplot(3,2,2)>> stem(n,y)>> title('Unit Sample Signal')>> xlabel('time')>> ylabel('y')

(iii) Unit Ramp Sequence >> n = 0:0.2:6;>> subplot(3,2,3)>> stem(n,n)>> title('Unit Ramp Sequence')>> xlabel('time')>> ylabel('y')

(iv) Exponential Sequence >> n = 0:0.2:6;>> y = exp(n);>> subplot(3,2,4)>> stem(n,y)>> title('Exponential Sequence')>> xlabel('time')>> ylabel('y')

Page 5: DSP Lab Matlab)

(v) Sine Sequence >> n = 0:0.2:2*pi;>> y = sin(2*n);>> subplot(3,2,5)>> stem(n,y)>> title('Sine Sequence ')>> xlabel('time')>> ylabel('y')

(vi) Cosine Sequence >> n = 0:0.2:2*pi;>> y = cos(2*n);>> subplot(3,2,6)>> stem(n,y)>> title('Cosine Sequence ')>> xlabel('time')>> ylabel('y')

OUTPUT:

Page 6: DSP Lab Matlab)

EXPERIMENT: 3

AIM:- Generate the following basic signals (continuous) using MATLAB

(i) Unit Step Signal(ii) Unit Sample Signal(iii) Unit Ramp Sequence(iv) Exponential Sequence(v) Sine Sequence(vi) Cosine Sequence

(i) Unit Step Signal >> n = -4:0.01:4;>> y = [zeros(1,400) ones(1,401)]>> subplot(3,2,1)>> plot(n,y)>> title('Unit Step Signal')>> xlabel('time')>> ylabel('y')

(ii) Unit Sample Signal >> n = -4:0.01:4;>> y = [zeros(1,400) ones(1,1) zeros(1,400)]>> subplot(3,2,2)>> plot(n,y)>> title('Unit Sample Signal')>> xlabel('time')>> ylabel('y')

(iii) Unit Ramp Sequence >> n = 0:0.01:6;>> subplot(3,2,3)>> plot(n,n)>> title('Unit Ramp Sequence')>> xlabel('time')>> ylabel('y')

(iv) Exponential Sequence >> n = 0:0.01:6;>> y = exp(n);>> subplot(3,2,4)>> plot(n,y)>> title('Exponential Sequence')>> xlabel('time')>> ylabel('y')

Page 7: DSP Lab Matlab)

(v) Sine Sequence >> n = 0:0.01:2*pi;>> y = sin(2*n);>> subplot(3,2,5)>> plot(n,y)>> title('Sine Sequence ')>> xlabel('time')>> ylabel('y')

(vi) Cosine Sequence >> n = 0:0.01:2*pi;>> y = cos(2*n);>> subplot(3,2,6)>> plot(n,y)>> title('Cosine Sequence ')>> xlabel('time')>> ylabel('y')

OUTPUT:

Page 8: DSP Lab Matlab)

EXPERIMENT: 4

AIM:- Determine the first 41 samples of the impulse response samples of the casual LTI system defined by

y(n) + 0.7y(n-1) – 0.45y(n-2) – 0.6y(n-3) = 0.8x(n) – 0.44x(n-1) + 0.36x(n-2) + 0.02 x(n-3)

SOLUTION:y(n) + 0.7y(n-1) – 0.45y(n-2) – 0.6y(n-3) = 0.8x(n) – 0.44x(n-1) + 0.36x(n-2) + 0.02 x(n-3)

Taking Z – Transform on both the sidesY(Z) + 0.7 z-1 Y(Z) - 0.45 z-2 Y(Z) - 0.6 z-3 Y(Z) = 0.8 X(Z) – 0.44 z-1 X(Z) + 0.36 z-2 X(Z) + 0.02 z-3 X(Z)H(Z) = Y(Z) = 1 + 0.7 z -1 - 0.45 z -2 - 0.6 z -3 . X(Z) 0.8 - 0.44 z-1 + 0.36 z-2 + 0.02 z-3

MATLAB CODE:>> y = [1, 0.7, -0.45, -0.6];>> x = [0.8, -0.44, 0.36, 0.02];>> stepz(x, y, 41);>> impz(x, y, 41);

OUTPUT:

Page 9: DSP Lab Matlab)
Page 10: DSP Lab Matlab)

EXPERIMENT: 5

AIM:- To perform addition, subtraction and multiplication of two matrices of size m x n, where m and n are given by the user.

MATLAB CODE:>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];

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

>> A+Bans = 10 10 10 10 10 10 10 10 10

>> A-Bans = -8 -6 -4 -2 0 2 4 6 8

>> A*Bans = 30 24 18 84 69 54 138 114 90

Page 11: DSP Lab Matlab)

EXPERIMENT: 5

AIM:- To perform addition, subtraction and multiplication of two matrices of size m x n, where m and n are given by the user.

MATLAB CODE:clc; close all; clear all;m=input('Enter the number of rows of first matrix: ');n=input('Enter the number of columns of first matrix: ');A=zeros(m,n);disp('Enter values of first matrix row-column wise: ');for i=1:1:m for j=1:1:n A(i,j)=input('Enter value: '); endend

p=input('\nEnter the number of rows of second matrix: ');q=input('Enter the number of columns of second matrix: ');B=zeros(m,n);disp('Enter values of second matrix row-column wise: ');for i=1:1:p for j=1:1:q B(i,j)=input('Enter value: '); endend

for i=1:1:p for j=1:1:q C(i,j) = A(i,j)+ B(i,j); endend

for i=1:1:m for j=1:1:n D(i,j) = A(i,j)- B(i,j); endend

E=zeros(m,q);for i=1:1:m for j=1:1:q E(i,j)=A(i,:)*B(:,j); endend

Page 12: DSP Lab Matlab)

disp('Addition');Cdisp('Substraction');Ddisp('Multiplication');E

OUTPUT:Enter the number of rows of first matrix: 3Enter the number of columns of first matrix: 3Enter values of first matrix row-column wise: Enter value: 1Enter value: 2Enter value: 3Enter value: 4Enter value: 5Enter value: 6Enter value: 7Enter value: 8Enter value: 9

Enter the number of rows of second matrix: 3Enter the number of columns of second matrix: 3Enter values of second matrix row-column wise: Enter value: 9Enter value: 8Enter value: 7Enter value: 6Enter value: 5Enter value: 4Enter value: 3Enter value: 2Enter value: 1

AdditionC = 10 10 10 10 10 10 10 10 10

SubstractionD = -8 -6 -4 -2 0 2 4 6 8

MultiplicationE = 30 24 18 84 69 54 138 114 90

Page 13: DSP Lab Matlab)

EXPERIMENT: 6

AIM:- Write a matlab program to perform Discrete Fourier Transform.

MATLAB CODE:clc;close all;clear all;x=input('Enter x(n):');N=length(x);m=0:1:N-1;subplot(2,2,1),stem(m,x);title('Input Sequence x(n)');xlabel('n-->');ylabel('x(n)-->');X=zeros( 1,N);for k=0:1:N-1for n=0:1:N-1X(k+ 1)=X(k+ 1)+(x(n+ 1)*(cos(2*pi*n*k/N)-(sin(2*pi*n*k/N))*i));endenddisp('DFT of x(n) is :');disp(X);subplot(2,2,3);stem(m,real(X));title('Real Part of X(k)');ylabel('real X(k)-->');subplot(2,2,4);stem(m,imag(X));title('Imaginary Part of X(k)');ylabel('imag X(k)-->');

Page 14: DSP Lab Matlab)

OUTPUT:Enter x(n):[1, 2, 3, 4, 5]DFT of x(n) is : 15.0000 -2.5000 + 3.4410i -2.5000 + 0.8123i -2.5000 - 0.8123i -2.5000 - 3.4410i

Page 15: DSP Lab Matlab)

EXPERIMENT: 7

AIM:- Write a matlab program to perform Inverse Discrete Fourier Transform.

MATLAB CODE:clc;clear all;X=input('Enter X(k): ');N=length(X);m=0:1:N-1;subplot(2,2,1);stem(m,real(X));title('Real part of X(k)');ylabel('real X(k) -->');subplot(2,2,2);stem(m,imag(X));title('Imaginary part of X(k)');ylabel('imag X(k) -->');x=zeros( 1,N);for n=0:1:N-1x(n+1)=0;for k=0:1:N-1x(n+1)=x(n+1)+(X(k+1)*(cos(2*pi*n*k/N)+i*sin(2*pi*n*k/N)));endendx=x/N;disp('IDFT of x(n) is :');disp(x);subplot(2,2,[3,4]),stem(m,x);title('Output sequence x(n)');xlabel('n -->');ylabel('x(n) -->');

Page 16: DSP Lab Matlab)

OUTPUT:Enter X(k): [(-1.5 + 2.56i) (-3.76 + 2.65i) (0 + 0.0i) (-1.5 - 2.56i) (-3.76 - 2.46i) ]IDFT of x(n) is : -0.5221 + 0.9380i -1.7950 + 1.1143i -0.6358 + 0.6526i -0.4083 + 1.2233i 0.7509 + 0.7616i

Page 17: DSP Lab Matlab)

EXPERIMENT: 8

AIM:- Write a matlab program to compute cross-correlation.

MATLAB CODE:clear all;clc;y=input('Input the first signal: ');m=length(y);n=0:1:m-1;subplot(2,2,1),stem(n,y);title('First input signal');xlabel('n -->'),ylabel('Amplitude -->');z=input('Input the second signal: ');m=length(z);n=0:1:m-1;subplot(2,2,2),stem(n,z);title('Second input signal');xlabel('n -->'),ylabel('Amplitude -->');p=xcorr(y,z );disp('The output signal is');pj=0:1:length(p)-1;subplot(2,2,[3,4]),stem(j,p);title('Cross-correlation');xlabel('n -->'),ylabel('Amplitude -->');

Page 18: DSP Lab Matlab)

OUTPUT:Input the first signal: [1 2 3 4]Input the second signal: [4 3 2 1]The output signal is

p =

1.0000 4.0000 10.0000 20.0000 25.0000 24.0000 16.0000

Page 19: DSP Lab Matlab)

EXPERIMENT: 9

AIM:- Write a matlab program to design Butterworth low-pass IIR Filter.

ALGORITHM:1. Get the passband and stopband ripples.2. Get the passband and stopband edge frequencies.3. Get the sampling frequency.4. Calculate the order of the filter.5. Find the filter coefficients.6. Draw the magnitude and the phase responses.

MATLAB CODE:clear all; close all;clc;format longrp = input('Enter the passband ripple: ');rs = input('Enter the stopband ripple: ');wp = input('Enter the passband frequency: ');ws = input('Enter the passband frequency: ');fs = input('Enter the sampling frequency: ');w1 = 2*wp/fs; w2=2*ws/fs;[n,wn] = buttord(w1,w2,rp,rs);[b,a] = butter(n,wn);w=0:.01:pi;[h,om] = freqz(b,a,w);m = 20*log10(abs(h));an = angle(h);subplot(2,1,1); plot(om/pi,m);xlabel('Normalized frequency -->');ylabel('Gain in dB -->');subplot(2,1,2); plot(om/pi,an);xlabel('Normalized frequency -->');ylabel('Phase in radians -->');

Page 20: DSP Lab Matlab)

OUTPUT:Enter the passband ripple: 0.5Enter the stopband ripple: 50Enter the passband frequency: 1200Enter the passband frequency: 2400Enter the sampling frequency: 10000

Page 21: DSP Lab Matlab)

EXPERIMENT: 10

AIM:- Write a matlab program to design Chebyshev type-1 low-pass IIR Filter.

ALGORITHM:1. Get the passband and stopband ripples.2. Get the passband and stopband edge frequencies.3. Get the sampling frequency.4. Calculate the order of the filter.5. Find the filter coefficients.6. Draw the magnitude and the phase responses.

MATLAB CODE:clear all; close all;clc;format longrp = input('Enter the passband ripple: ');rs = input('Enter the stopband ripple: ');wp = input('Enter the passband frequency: ');ws = input('Enter the passband frequency: ');fs = input('Enter the sampling frequency: ');w1 = 2*wp/fs; w2=2*ws/fs;[n,wn] = cheb1ord(w1,w2,rp,rs);[b,a] = cheby1(n,rp,wn);w=0:.01:pi;[h,om] = freqz(b,a,w);m = 20*log10(abs(h));an = angle(h);subplot(2,1,1); plot(om/pi,m);xlabel('Normalized frequency -->');ylabel('Gain in dB -->');subplot(2,1,2); plot(om/pi,an);xlabel('Normalized frequency -->');ylabel('Phase in radians -->');

Page 22: DSP Lab Matlab)

OUTPUT:Enter the passband ripple: 0.2Enter the stopband ripple: 45Enter the passband frequency: 1300Enter the passband frequency: 1500Enter the sampling frequency: 10000


Recommended