Anda di halaman 1dari 11

# Kate Gleason College of Engineering

## 0306-451 Digital Signal Processing Homework 1

NOTE: This homework is designed to help you not only get familiarized with Matlab but also to help you in learning to independently use Matlab. Because of this, you will find that you will need to use some functions that were not explicitly explained in class. Throughout the homework, remember to use the help and lookfor commands and feel free to consult Matlabs online documentation (http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk /help/techdoc/) both to learn more about the functions you are using and to find out what functions may help you in solving the exercises.

1) Show the instructions in Matlab to create a 5x5 matrix where the elements of the main diagonal are equal to 2 and the elements of every odd column AND row (except those on the main diagonal) are equal to 5. The rest of the elements of the matrix must be equal to 0. >> >> >> >> A=eye(5,5); M=zeros(5,5); M([1:2:end],[1:2:end])=5; M(find(A==1))= 2

2) Show the instructions in Matlab to do the following: 1. Create a vector with 5 elements, where the elements go from 5 to 25 in steps of 5. 2. Create a second vector with 5 elements, where the elements go from 10 to 6 in steps of -1. 3. Calculate the inner product of the two vectors without using any for loop or Matlab function (such as dot or sum). Hint: you may use the transpose of a vector. Recall that for a vector and a vector , their inner product is .

4. Verify that your result is correct by using the Matlab function dot.

>> a=[5:5:25]; >> b=[10:-1:6]; >> p1=a*b' p1 = 550 >> p2=dot(a,b) p2 = 550

3) Create a function in Matlab that takes as input a vector of numbers and returns another vector containing the indices of those elements in the input that are prime numbers (hint: use the Matlab function find). Also show how would you call the function from the command window. Function: function out=point2primes(input) out= find(isprime(input)); Call: >> invec=[3 7 8 5 4 12]; >> primesindx=point2primes(invec)

4) Given any 3x4 matrix, show the Matlab commands to calculate a matrix made from the square of each element in the original matrix Two possible solutions: >> B=A.^2 >> B=A.*A

5) Show the Matlab command to plot the function ( ( ( The time variable should be between 0 and 8. The plot should include a grid, labels for both axis and a legend. The line tracing the function should be blue, continuous and with x markers. Include the figure you obtained. Hint: you will need to use the element-by-element division, ./ to calculate your function. >> >> >> >> >> t=linspace(0,8,50); plot(t,sin(3*(t-4))./(3*(t-4)),'-bx');grid xlabel('time'); ylabel('function y') legend('Result');

1 Result 0.8

0.6

function y

0.4

0.2

-0.2

-0.4

4 time

6) Show the Matlab command to plot, using the stem function, the function {

The plot should include a grid, labels for both axis. Include the figure you obtained.

function y

## 0.5 0.4 0.3 0.2 0.1 0

5 n

10

7) Create a function in Matlab that takes in a complex number expressed in Cartesian coordinates and returns the magnitude and phase of the number (warning: be careful to consider that the arctan used to calculate de phase returns the correct value considering the quadrant where the complex number is).

- The easy solution: function [m,p]=cart2pol(zc) % Calculate magnitude m=abs(zc); % Calculate phase p=angle(zc);

## A more complicated solution:

function [m,p]=cart2pol(zc) % Split the real from the imaginary part x=real(zc); y=imag(zc); % Calculate magnitude m=sqrt(x^2+y^2); % Calculate phase p=atan(y/x); if( (x<0) && (y>0) ) p=p+pi; end if( (x<0) && (y<0) ) p=p-pi; end

8) In this exercise use the function plotcmplx.m (which is provided separately in myCourses) to plot complex numbers as vectors (phasors) in the following questions. (a) For the following complex numbers, calculate magnitude and phase, write down the number as a complex exponential and plot the number: a1: magnitude = 0.75 phase = 0 (a positive real number)

-0.6

-0.4

-0.2

0.2

0.4

0.6

0.8

## a2: magnitude = 0.5 phase = (a negative real number)

0.5 0.4 0.3 0.2 0.1 0 -0.1 -0.2 -0.3 -0.4 -0.5 -0.5

0.5

-0.5

0.5

## 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 -1

-0.5

0.5

a5: magnitude = (using Matlab) abs(2+2j) magnitude = 2.8284 phase = (using Matlab) angle(2+2j) phase =

-1

-2

-3 -3

-2

-1

a6: magnitude = (using Matlab) abs(-2+2j) magnitude = 2.8284 phase = (using Matlab) angle(-2+2j) phase =

-1

-2

-3 -3

-2

-1

9) What is the result of multiplying the complex number with the number ? Plot the two complex numbers z and v, as well as the result using the function plotcmplx.m (which is provided separately in myCourses). Explain how multiplying by v changes the magnitude and phase of z. With Matlab plotcmplx(.25*exp(-j*pi/4),'b'):
z 0.25 0.2 0.15 0.1 0.05 0 -0.05 -0.1 -0.15 -0.2 -0.25 -0.25

-0.2

-0.15

-0.1

-0.05

0.05

0.1

0.15

0.2

0.25

## With Matlab plotcmplx(.4*exp(j*pi/2),'b'):

0.4

0.3

0.2

0.1

-0.1

-0.2

-0.3

-0.4 -0.4

-0.3

-0.2

-0.1

0.1

0.2

0.3

0.4

the magnitudes of z and v are multiplied and the phases are added.
1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 -1

-0.8

-0.6

-0.4

-0.2

0.2

0.4

0.6

0.8

10) Consider the following complex-valued function of the variable : ( ( . Use Matlab to plot the magnitude and the phase of y as a function of in the range .

-3

-2

-1

-1

-2

-3

-4 -4

-3

-2

-1