DSP Lab
DSP Lab
t=-3:1:3; y=[zeros(1,3),ones(1,1),zeros(1,3)]; stem(t,y) %%% Sin wave Generation %%%%% t=0.01; n=0:1:200; y=sin(2*pi*n*t); stem(n,y) grid xlabel('time') ylabel('magnitude') %%% Square wave generation %%%%% t=0:0.01:1; y=square(2*pi*t) plot(t,y) % Unit Step Sequence Generation clc; n=input('Enter the no of n value'); t=0:n-1; y=ones(1,n); stem(t,y)
% Addition and subtraction of two signal n=0:19; y=[zeros(1,10),ones(1,10)]; subplot(2,2,1); stem(n,y) x=[zeros(1,5),ones(1,10),zeros(1,5)]; subplot(2,2,2); stem(n,x) z=x+y; subplot(2,2,3) stem(z) w=x-y; subplot(2,2,4) stem(n,w)
Example: Find the magnitude and phase of the complex number z = 3 + j4. z = 3+j*4 zm = abs(z); gives the magnitude of z zp = angle(z); gives the phase of z in radians Addition or subtraction of numbers (real or complex): z = z1+z2; addition z = z1z2; subtraction Multiplication or division of numbers (real or complex): z = z1*z2; multiplication z = z1/z2; division Vectors and Matrices Generate the vectors x = [1 3 5] and y = [2 0 4 5 6]. x = [1 3 5]; generates the vector of length 3 y = [2 0 4 5 6]; generates the vector of length 5 Addition or subtraction of vectors x and y of same length: z = x+y; addition z = xy; subtraction Multiplication or division of vectors x and y of same length: z = x. * y; multiplication z = x./y; division Note: The dot after x is necessary since x is a vector and not a number. Creating one-dimensional and two-dimensional vector spaces using MATLAB The command: x = linspace(x1,x2,N); generates N points between x1 and x2; and stores it in the vector x x = linspace(x1,x2,N1); generates N 1 points between x1 and x2; and stores it in the vector x y = linspace(y1,y2,N2); generates N 2 points between y1 and y2; and stores it in the vector y [X,Y] = meshgrid(x,y); generates the two dimensional meshgrid [X,Y] Programming with vectors Programs involving vectors can be written using either FOR loop or vector commands. Since MATLAB is basically a vector-based program, it is often more efficient to write programs using vector commands. However, FOR loop commands give a clearer understanding of the program, especially for the beginner: Example: Sum the following series: S = 1 + 3 + 5 + 99. FOR loop approach: S = 0.0; initializes the sum to zero >> for i = 1 : 2 : 99 S = S + i
end S; gives the value of the sum Vector approach: i =1: 2 : 99; creates the vector i S = sum (i); obtains the sum S Example: Generate the discrete-time signal y(n) = n sin( n/2) in the interval 0 n 10. FOR loop approach: for n = 1:1: 11 n1(n) = n 1 y(n) = n1(n) * sin(pi*n1(n)/2) end y; gives the vector y stem(n1,y); plots the signal y(n) vs. n with impulses Vector approach: >> n = 0 : 10; creates the vector n >> y = n.*sin(pi*n/2); obtains the vector y >> stem(n,y); plots the signal y(n) vs. n with impulses Basic Signal Operations In MATLAB Example: Define the discrete-time signal x(n) = n u(n) in a vector in the range 0 n 10 and plot the signal. Solution: >> n = 0:10; defines the vector n of length 11 >> u (1:11) = ones(1,11); defines the unit step vector u of length 11 >> x = n. *u; defines the product n u(n) >> stem(n,x); plots the discrete signal x(n)