Вы находитесь на странице: 1из 50

A LAB REPORT ON SIGNAL PROCESSING

Submitted as a requirement for the partial fulfillment of Bachelors Degree From Rajasthan Technical University, Kota

SUBMITTED BY SURYA PRAKASH Final year ECE

M.L.V.Textile and engineering College. ( An Autonomous Engineering College of Govt. Of Rajasthan) Bhilwara, Rajasthan
1

ACKNOWLEDGMENT
First and foremost, we would like to express my sincere gratitude to our lab in charge Mrs. Sarita Chauhan . We were privileged to experience a sustained enthusiastic and involved interest from her side. This fueled our enthusiasm even further and encouraged us to boldly step into what was a totally dark and unexplored expanse before us. We also like to thank the MLVTEC staff members and the institute, in general, for extending a helping hand at every juncture of need.

Keerti vyas Yogesh kumar

INDEX
Object
Page no.

1. Introduction to MATLAB..5 2. Explain following with example....6 3. Explain commands with example.9 4. Write a program (WAP) for adding five numbers at run time.14 5. WAP checking whether a number is prime or not (i) Using function.15 (ii) Without using function...15 6. WAP to find a number is divisible by 2 or 3 or both...16 7. WAP to find factorial of a given number.17 8. WAP to perform convolution...18 9. WAP to plot sine and cosine functions (i) On same Window.19 (ii) On different Window..20 10. WAP to add two sine functions..21 11. WAP to perform folding of a sequence..22 12. WAP to plot Gaussian curve...23 13. WAP to plot Rayleigh probability curve24 14. WAP to design bode plot25 15. WAP to perform auto correlation and cross correlation26 16. WAP to compare the order of butterworth filter..28
3

17. WAP to compare the 4th order butterworth filter for frequency

w=100 and w=200...29 18. WAP to plot the following function on different window...........................33 a) Unit step b) Ramp function c) Unit impulse 19. WAP to plot the following function on same window36 a) Unit step b) Ramp function c) Unit impulse 20. WAP to generate the impulse sequence at no. sample laying between n1<= n0 <= n238 21. WAP Draw a signal and apply following properties on it..39 a) Shifting b) Scaling c) Inverse 22. WAP perform x=an *u(n).......................40 23. WAP to generate complex exponential function41 24. WAP to design finite impulse response filter (low pass)....42 25. WAP to design Kaiser window...43 26. WAP to perform equi ripple technique to optimize Kaiser window solution...44 27. WAP to (i) Filter order control..45 (ii) Transition Width control.46 (iii)Stop Band attenuation47 (iv)Design Containing..............48 28. WAP for Down sampler..49 29. WAP for Up sampler...50

EXPERIMENT NO 1
Object- Write the introduction to MATLAB. Theory: MATLAB is a powerful computing system for handling scientific and engineering calculations. The name MATLAB stands for Matrix Laboratory, because the system was designed to make matrix computations particularly easy. It intended to provide easy access to the matrix libraries developed by Linpack and Eispack projects. These are carefully tested high quality programming packages for solving linear equations and eigen value problems. OR, MATLAB is a high-level technical computing language and interactive environment for algorithm development, data visualization, data analysis, and numerical computation. Using MATLAB, you can solve technical computing problems faster than with traditional programming languages, such as C, C++, and Fortran. You can use MATLAB in a wide range of applications, including signal and image processing, communications, control design, test and measurement, financial modeling and analysis, and computational biology. Add-on toolboxes (collections of special-purpose MATLAB functions) extend the MATLAB environment to solve particular classes of problems in these application areas. MATLAB provides a number of features for documenting and sharing your work. You can integrate your MATLAB code with other languages and applications, and distribute your MATLAB algorithms and applications.

MATLAB is originally written in FORTRAN developed by Cleve Moler, the chairman of computer science department at the university of new mexico, started developing MATLAB in 1970 s. Key Features
y y y y

y y y

High-level language for technical computing Development environment for managing code, files, and data Interactive tools for iterative exploration, design, and problem solving Mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization, and numerical integration 2-D and 3-D graphics functions for visualizing data Tools for building custom graphical user interfaces Functions for integrating MATLAB based algorithms with external applications and languages, such as C, C++, Fortran, Java , COM, and Microsoft Excel.

EXPERIMENT NO 2
Object- Explain the following with suitable examples. (i) (ii) (iii) Theory :
(i) For Loop :

For loop If-else Switch case

for loops are often used when a sequence of operations is to be performed a predetermined number of times. For example computing the average of a list of numbers requires adding up a known number of values.
Syntax

Loop counter incremented by one:

for i = start value : end value x = ... y = ... ...


end

i is the loop counter. On the first pass through the loop, i is set to start value. On the second pass through the loop i is set to start Value+1. The Matlab statements between the for and the end are evaluated until i>end value.
Example:

Compute the sum of the _rst n integers n = 10; s = 0; for i=1:n s = s + i; end

(ii)

If-else :

Syntax : if expression statements elseif expression statements else statements end

Description : if expression, statements, end evaluates an expression, and executes a group of statements when the expression is true. Else-if and else are optional, and execute statements only when previous expressions in the if block are false. An if block can include multiple else-if statements. An evaluated expression is true when the result is nonempty and contains all nonzero elements (logical or real numeric). Otherwise, the expression is false. Expressions can include relational operators (such as < or ==) and logical operators (such as &&, ||, or ~). MATLAB evaluates compound expressions from left to right, adhering to operator precedence rules. Examples :
x=-5:0.1:5 for i=1:101 if x(i)>=0 y(i)=1 else y(i)=0 end end stem(x,y) (iii)

Switch case:

Syntax switch switch_expression case case_expression statements case case_expression statements : otherwise statements end Description A switch block conditionally executes one set of statements from several choices. Each choice is a case. An evaluated switch_expression is a scalar or string. An evaluated case_expression is a scalar, a string, or a cell array of scalars or strings. The switch block tests each case until one of the cases is true. A case is true when:  For numbers, eq(case_expression,switch_expression).  For strings, strcmp(case_expression,switch_expression).  For objects that support the eq function, eq(case_expression,switch_expression).
7

For a cell array case_expression, at least one of the elements of the cell array matches switch_expression, as defined above for numbers, strings, and objects. When a case is true, MATLAB executes the corresponding statements, and then exits the switch block. otherwise is optional, and executes only when no case is true. Example:
mynumber = input('Enter a number:'); switch mynumber case -1 disp('negative one'); case 0 disp('zero'); case 1 disp('positive one'); otherwise disp('other value'); end

EXPERIMENT NO 3
Object- Explain the following commands with example. a) b) c) d) whos sum diag magic e) f) g) h) zeros ones eyes randn

a) whos : whos lists current variable, long form. It lists all the variables in the current workspace , together with information about their size, shape, bytes, class.

Example:

b) sum : y B= sum(A) returns sum along different dimensions of array. If a is floating point, that is double or single. y If A is a vector, sum(A) returns the sum of elements. y If a is a matrix, sum(A) treats the column of A as vectors, returning a row vector of the sum of each column.

Example :

C) Diag: Syntax :

x=diag(v,k)
x=diag(v) Description : x=diag(v,k) where v is the vector of n components, returns a square matrix x of order n+abs(k), k=0 represents the main diagonal, k>0 above the main diagonal, and k<0 below the main diagonal.

10

Example :

c) Magic: Syntax: m=magic(n) Description: m=magic(n) returns an nxn matrix constructed from the integers 1 through n2 with equal row and column sums. The order n must be a scalar

Example:

11

d) Zeros : Syntax : zeros(n) or zeros (m,n) Description : y It defines a matrix of order with all entries zero. y An error message will appear if n is not a scalar. Example :

e) Ones : Syntax : ones(n) or ones(m,n) Description: it will generate a matrix with all entries o 1 . Where n must be a scalar. Example:

f) Eyes: Syntax : eye(n) Description : y y It will generate a identity matrix of order nxn. The argument n must be scalar and it represents the order of square matrix.

12

Example:

g) randn : Syntax : randn(n) or randn(m,n) Description : y Normally distributed pseudorandom numbers. y A=randn(n), returns a matrix containing pseudorandom values drawn from the standard normal distribution. Example :

13

EXPERIMENT NO 4
Object- Write a program for addition of five numbers at run time. Program :
sum=0; for i=1:5 u=input('enter ur number'); sum=sum+u ; end disp('sum of number ='); disp(sum);

Output:

14

EXPERIMENT NO 5
Object- Write a program for checking whether a number is prime or not with and without using function isprime . Program : (i) Using function isprime

u=input(Enter your number); x=isprime(u); if x==1 disp(number is prime); else disp(number is not prime);

(ii)

Without using isprime

v=0; s=1; u=input('enter ur num'); for i=2:u-1 s=rem(u,i); if s==0 disp('Number is not prime'); break; end end if s~=0 v=1; disp('no is prime'); end

Output :

15

EXPERIMENT NO 6
Object- Write a program to find the number divisible by 2 or 3 or both. Program :
u=input('enter ur num'); a=rem(u,2); if a==0 disp('num is divisible by 2'); end b=rem(u,3); if b==0 disp('num is divisible by 3'); end c=rem(u,6); if c==0 disp('num is divisible by both 2 and 3'); end

Output:

16

EXPERIMENT NO 7
Object- Write a program to find factorial of a given number. Program :
u=input('Enter your number'); s=1; for i=1:u s=s*i; end disp('factorial of a given number is'); disp(s);

Output :

17

EXPERIMENT NO 8
Object- Write a program to perform convolution. Program :
u=input('Enter first sequence'); h=input('Enter second sequence'); subplot(3,1,1); stem(u); title('First input sequence'); subplot(3,1,2); stem(h); title('Second input sequence'); subplot(3,1,3); y=conv(u,h); stem(y) title('convolution sequence');

Output :

18

EXPERIMENT NO 9
Object- Write a program to plot sine and cosine function on same and different window. Program : (i) On same window

x=0:0.01:4*pi; y=sin(x); z=cos(x); subplot(2,1,1); plot(y); xlabel('x-axis'); ylabel('y-axis'); title('sine Function'); subplot(2,1,2) plot(z); xlabel('x-axis'); ylabel('y-axis');

Output :

19

(ii)

On different Window

x=0:0.01:4*pi; y=sin(x); z=cos(x); figure(1) plot(y); xlabel('x-axis'); ylabel('y-axis'); title('sine Function'); figure(2) plot(z); xlabel('x-axis'); title('cosine Function'); ylabel('y-axis');

Output :

20

EXPERIMENT NO 10
Object- Write a program for addition of two sine functions. Program :
t1=0:.01:2*pi; t2=0:.01:2*pi; y=sin(t1); z=sin(t2); u=y+z; subplot(3,1,1); plot(y); title('First Sine function'); subplot(3,1,2); plot(z); title('second Sine function'); subplot(3,1,3); plot(u); title('Addition');

Output:

21

EXPERIMENT NO 11
Object- Write a program for folding of original sequence. Program :
x=input('enter original sequence='); y=fliplr(x); subplot(2,1,1); stem(x); xlabel('x-axis'); ylabel('amplitude'); title('Original sequence'); subplot(2,1,2); stem(y); xlabel('x-axis'); ylabel('amplitude'); title('Folded sequence');

Input: [1 2 3 4 5 6] Output:

22

EXPERIMENT NO 12
Object- Write a program to plot Guassian Curve. Program :
x=0:0.1:10; y=gaussmf(x,[2,5]); plot(x,y); title('guassian curve');

Output :

23

EXPERIMENT NO 13
Object- Write a program to plot Rayleigh Probability Curve. Program :
x=0:.1:3; p=raylpdf(x,1); plot(x,p); title('rayleigh probability curve');

OUTPUT :

24

EXPERIMENT NO 14
Object- Write a program to design Bode plot for a function. Program :
g=(s^2+0.1*s+7.5)/((s^4)+0.12*s^3+9*s^2); h=tf([1 0.1 7.5],[1 0.12 9 0 0]); bode(h); grid on;

OUTPUT :

25

EXPERIMENT NO 15
Object- Write a program auto-correlation and cross correlation. Program : Auto-correlation
x=input('enter the sequence='); y=xcorr(x,x); figure(1); subplot(2,1,1); stem(x); xlabel('x-axis'); ylabel('amplitude'); title('input'); subplot(2,1,2); stem(fliplr(y)); xlabel('x-axis'); ylabel('amplitude'); title('Output'); disp('the resultant signal is');

Input : [1 2 3 4] Output:

26

(2)Cross-Correlation
x=input('enter the first sequence='); h=input('enter the second sequence='); y=xcorr(x,h); figure(1); subplot(3,1,1); stem(x); title('first Input'); xlabel('n-->'); ylabel('amplitude'); subplot(3,1,2); stem(h); xlabel('n-->'); ylabel('amplitude'); title('Second Input'); subplot(3,1,3); stem(fliplr(y)); xlabel('n-->'); ylabel('amplitude'); title('Output'); disp('the resultant signal is');

Given Inputs: [1 2 3 4] and [4 2 3 1] Output:

27

EXPERIMENT NO 16
Object- Write a program to compare the order of butterworth filter. Program :
s=tf('s'); w=100; g=((w^5)/((s+w)*((s*s)+(0.765*w*s)+(w*w))*((s*s)+(1.85*s*w)+(w*w)))); bode(g); grid on; title('Fifth Order Butterworth Filter'); h=((w^4)/(((s*s)+(0.765*w*s)+(w*w))*((s*s)+(1.85*s*w)+(w*w)))); figure(2); bode(h); grid on; title('Fourth Order Butterworth Filter'); k=((w^3)/((s+w)*((s*s)+(w*s)+(w*w)))); figure(3); bode(k); grid on; title('Third Order Butterworth Filter'); u=((w^2)/(((s*s)+(sqrt(2)*w*s)+(w*w)))); figure(4); bode(u); grid on; title('Second Order Butterworth Filter');

28

29

30

Experiment No- 17
Object - To compare the 4th order butterworth filter for frequency w=100 and w=200. Program :
s=tf('s'); w=100; h=((w^4)/((s*s)+(0.765*w*s)+(w*w)*((s*s)+(1.85*s*w)+(w*w)))); figure(1); bode(h); grid on; title('fourth order butterworth filter for w=100'); W=200; g=((w^4)/((s*s)+(0.765*w*s)+(w*w)*((s*s)+(1.85*s*w)+(w*w)))); figure(2); bode(g); grid on; title('fourth order butterworth filter for w= 200');

output:

31

32

EXPERIMENT NO 18
Object-Write a program to plot the following function on different window. d) Unit step Program : a) Unit step
x=-5:0.1:5 for i=1:101 if x(i)>=0 y(i)=1 else y(i)=0 end end stem(x,y) xlabel('x-axis') ylabel('y-axis') axis([-5,5,-5,5]) title('Unit step response') Output:

b) Ramp function

c) Unit impulse

33

b) Ramp Function
x=-5:0.1:5 for i=1:101 if x(i)>=0 y(i)=x(i) else y(i)=0 end end stem(x,y) xlabel('x-axis') ylabel('y-axis') axis([-5,5,-5,5]) title('Comtinuous Ramp signal')

Output:

34

c) Unit Impulse
x=-5:0.1:5 for i=1:101 if x(i)==0 y(i)=1; else y(i)=0; end end stem(x,y) xlabel('x-axis') ylabel('y-axis') axis([-5,5,-5,5]) title('unit Impulse response')

Output:

35

EXPERIMENT NO 19
Object- Write a program to plot the following function on same window. a) Unit step Program :
x=-5:0.1:5 for i=1:101 if x(i)>=0 y(i)=1 else y(i)=0 end end subplot(2,2,1) plot(x,y) xlabel('x-axis') ylabel('y-axis') axis([-5,5,-5,5]) title('Unit step response') for i=1:101 if x(i)>=0 y(i)=x(i) else y(i)=0 end end subplot(2,2,2) plot(x,y) xlabel('x-axis') ylabel('y-axis') axis([-5,5,-5,5]) title('Comtinuous Ramp signal') for i=1:101 if x(i)==0 y(i)=1; else y(i)=0; end end subplot(2,2,3) plot(x,y) xlabel('x-axis') ylabel('y-axis') axis([-5,5,-5,5]) title('unit Impulse response')

b) Ramp function

c) Unit impulse

36

37

EXPERIMENT-20
Object- write a program to generate the impulse sequence at no. sample laying

between n1<= n0 <= n2 and get the output as shown

Program n1=input('enter the starting point of the sequence'); n2=input('enter the ending point of the sequence'); n0=input('enter the place of impulse sequence in the given range'); k=[n1:1:n2]; for n=1: length(k) if n== n0; x(n)=1; else x(n)=0; end end stem(k,x); title('impulse sequence');

38

EXPERIMENT NO 21
Object- Draw a signal and apply following properties on it b) Shifting Program :
x=1:0.01:4*pi; y=sin(x); figure(1); plot(y); title('Original sequence') p=x-2; z=sin(p); figure(2); plot(z); title('Shifted sequence') q=2*x; y=sin(q); figure(3); plot(y); title('Scaling'); p=-x; z=sin(p); figure(4); plot(z); title('inverse');

b) Scaling

c) Inverse

Output:

39

Experiment No- 22
Object: write a program to perform X= an * u*(n) Program:
ns=input('enter the strating point of the sequence='); ne=input('enter the ending point of the sequence=')' a=input('the value of a='); n=[ns:ne]; u=[(n-ns)>=0]; subplot(2,2,1); stem(n,u); title('unit signal u(n)'); xlabel('Samples n'); ylabel('u(n)'); e=a*exp(n); subplot(2,2,2); stem(n,e); title('exponential signal e=(a^n)'); xlabel('Samples n') ylabel('e=(a^n)'); m=u.*e; subplot(2,2,3); stem(n,m); title('multiplied signal m=u*e'); xlabel('Samples n'); ylabel('m=a*exp(n).*u');

output:

40

EXPERIMENT -23
Object : write a program to generate complex exponential function X = exp (complex(sigma,w)*n) ; sigma=0.3,w= 0.2 Program:
sigma=input('enter the value of sigma='); w=input('enter the value of w='); n=[1:100]; x=exp(complex(sigma,w)*n); stem(n,x); title('generating the exponential sequence for sigma and omega'); xlabel('samples n'); ylabel('amplitude of x(n)'); pause; clc; clear all; close all;

output:

41

EXPERIMENT-24
Object: write a program to design finite impulse response filter (low pass) for fc=0.6 & N=20. Program:
fc=0.6; N=20; hf=fdesign.lowpass('N,fc',N,fc); hd(1)=design(hf,'window','window',@hamming); hd(2)=design(hf,'window','window',{@chebwin,50}); hfvt=fvtool(hd); legend(hfvt,'hamming window design','dolph chebyshev window design');

output:

Magnitude Res pons e (dB) 0

-10

-20

M agnitu de (dB)

-30

-40

-50

hamming w indow des ign dolph c heby s hev w indow des ign

-60

-70 0 0.1 0.2 0.3 0.4 0.5 0.6 Normaliz ed Frequenc y ( vT rad/s ample) 0.7 0.8 0.9

42

EXPERIMENT-25
Object: write a program to design kaiser window design Fp=0.6, fst=0.7725, Ap=0.01, Ast=90 Program:
fc=0.6; n=20; hf=fdesign.lowpass('n,fc',n,fc); fp=0.6; fst=0.7725; ap=0.01; ast=90; setspecs(hf,'fp,fst,ap,ast',fp,fst,ap,ast); hd(4)=design(hf,'kaiserwin'); hfvt=fvtool(hd(4));

output:
Magnitude Res pons e (dB) 0

-20

-40 M agnitu de (dB)

-60

-80

-100

0.1

0.2

0.3

0.4 0.5 0.6 Normaliz ed Frequenc y ( vT rad/s ample)

0.7

0.8

0.9

43

EXPERIMENT-26
Object: write a program to perform equiripple technique to optimize Kaiser window solution. Program:
fc=0.6; N=20; hf=fdesign.lowpass('N,fc',N,fc); fp=0.6; fst=0.7725; ap=0.01; ast=90; setspecs(hf,'fp,fst,ap,ast',fp,fst,ap,ast); hd(4)=design(hf,'kaiserwin'); hfvt=fvtool(hd(4)); hd(5)=design(hf,'equiripple'); hfvt=fvtool(hd(4:5)); legend(hfvt,'kaiser window design,filter order 68','equiripple design,filter order 53'); output:

Magnitude Res pons e (dB) 0

-20

kais er w indow des ign,f ilter order 68 equiripple des ign,f ilter order 53

-40 M agnitu de (dB)

-60

-80

-100

0.1

0.2

0.3

0.4 0.5 0.6 Normaliz ed Frequenc y ( vT rad/s ample)

0.7

0.8

0.9

44

Experiment No- 27
Object: write a program to filter order control N=20 Percentage order =100 to 101 Coefficients setspecs(hf, N,fc,Ap,Ast ,N,,fc,Ap,Ast) Program: (i) filter order control :
N=20; Setspecs (hf,'N,fc,ap,ast',N,fc,ap,ast); hd(6)=design(hf,'equiripple'); hfvt=fvtool(hd(5:6)); legend (hfvt,'equiripple design,53 coefficents','equiripple design,20 coefficents' );

output:

Magnitude Res pons e (dB) 0 -10 -20 -30 M agnitu de (dB) -40 -50 -60 -70 -80 -90 -100 0 0.1 0.2 0.3 0.4 0.5 0.6 Normaliz ed Frequenc y ( vT rad/s ample) 0.7 0.8 0.9 equiripple des ign,53 c oef f ic ents equiripple des ign,20 c oef f ic ents

45

ii) transition width control :


setspecs(hf,'N,fp,fst',N,fp,fst); hd(7)=design(hf,'equiripple'); measure(hd(7)); hfvt=fvtool(hd(5),hd(7)); legend(hfvt,'equiripple design,53 coefficents','equiripple design,20 coefficents' );

output:
Magnitude Res pons e (dB) 0 -10 -20 -30 M agnitu de (dB) -40 -50 -60 -70 -80 -90 -100 0 0.1 0.2 0.3 0.4 0.5 0.6 Normaliz ed Frequenc y ( vT rad/s ample) 0.7 0.8 0.9 equiripple des ign,53 c oef f ic ents equiripple des ign,20 c oef f ic ents

46

iii) Stop bend attenuation:


fc=0.6; n=20; hf=fdesign.lowpass('n,fc',n,fc); fp=0.6; fst=0.7725; ap=0.01; ast=90; setspecs(hf,'N,fp,fst',N,fp,fst); hd(8)=design(hf,'equiripple','wstop',(5)); measure(hd(8)); hfvt=fvtool(hd(7:8));

Output:

Magnitude Res pons e (dB) 0

-10

-20 M agnitu de (dB)

-30

-40

-50

-60

-70 0 0.1 0.2 0.3 0.4 0.5 0.6 Normaliz ed Frequenc y ( vT rad/s ample) 0.7 0.8 0.9

47

iv) Design containing:


fc=0.6; n=20; hf=fdesign.lowpass('n,fc',n,fc); fp=0.6; fst=0.7725; ap=0.01; ast=90; setspecs(hf,'fp,fst,ap,ast',fp,fst,ap,ast); hd(9)=design(hf,'equiripple'); hfvt=fvtool(hd(8:9)); legend(hfvt,'equiripple design using weights','equiripple design containing the stop band');

output:

Magnitude Res pons e (dB) 0 -10 -20 -30 M agnitu de (dB) -40 -50 -60 -70 -80 -90 -100 0 0.1 0.2 0.3 0.4 0.5 0.6 Normaliz ed Frequenc y ( vT rad/s ample) 0.7 0.8 0.9 equiripple des ign us ing w eights equiripple des ign c ontaining the s top band

48

EXPERIMENT-28
Object: write a program for down sampler. Program:
fs=9; t=[0:74]/fs; a1=input('enter the values of amplitude a1='); s1=a1*sin(2*pi*t*.42); subplot(1,2,1); stem(s1); xlabel('sinewave with f=.42Hz'); ylabel('amplitude'); fs=3; t=[0:74]/fs; s2=a1*sin(2*pi*t*.42); subplot(1,2,2); stem(s2); xlabel('sinewave with f=.42Hz'); ylabel('amplitude');

Output:

49

EXPERIMENT-29
Object: write a program for up sampler. Program:
fs=9; t=[0:74]/fs; a1=input('enter the values of amplitude a1='); s1=a1*sin(2*pi*t*.12); subplot(1,2,1); stem(s1); xlabel('sinewave with f=.12Hz'); ylabel('amplitude'); s2=upsample(s1,3); subplot(1,2,2); stem(s2); xlabel('sinewave with f=.12Hz'); ylabel('amplitude');

Output :

50

Вам также может понравиться