Tuesday 26 December 2017

Introduction To Complex Exponentials

Introduction: Sinusoidal signals such as
can be represented as complex exponential

The key is to use the appropriate complex amplitude together with the real part operator as follow

Manipulating the sinusoidal functions using complex exponentials turns the trigonometric problem into simple arithmetic and algebra. In this lab we will review the complex exponential signal and the phasor addition property for adding the cosine waves.
6. Complex numbers in Matlab
Matlab can be used to compute the complex valued formulas and also to display the results as vector or “phasor diagram”. Several functions are available in sp first toolbox for this purpose. See the help of M files zvect ,zcat, ucplot, zcoords, and zprint. Each of these functions can plot or print several complex numbers at once. For example, the following function call will plot five vectors all in one graph.
zvect( [ 1+j, j, 3-4*j, exp(j*pi), exp(2j*pi/3) ] )
Here are some of MATLAB’s built-in complex number operators:
conj Complex conjugate
abs Magnitude
angle Angle (or phase) in radians
real Real part
imag Imaginary part
i,j pre-defined as √−1
x = 3 + 4i i suffix defines imaginary constant (same for j suffix)
exp(j*theta) Function for the complex exponential π‘’π‘—πœƒ
Each of these functions take a vector or vector as its input argument and operates on each element of vector.
Exercise 1
Use z1=10𝑒−𝑗2πœ‹/3 and z2= -5 + j5 for all parts of this section
a. Enter the complex numbers z1 and z2 in Matlab. Plot them with zvect ( ) and print them zprint().
Whenever you make a plot with zvect () or zcat() it is helpful to provide axes for refrence. An x-y reference and unit circle can be superimposed on your zvect plot by doing the following:
hold on, zcoords, ucplot, hold off
b. The function zcat() can be used to plot vectors in a “head-to-tail” format. Execute the statement
zcat([j,-1,-2j,1]); to see how zcat() works when its input is a vector of complex numbers.
c. Compute z1 + z2 and plot the sum using zvect(). Then use zcat() to plot z1 and z2 as 2 vectors
head-to-tail, thus illustrating the vector sum. Use hold on to put all 3 vectors on the same plot.
If you want to see the numerical value of the sum, use zprint() to display it.
d. Compute z1z2 and plot the answer using zvect() to show how the angles of z1 and z2 determine the angle of the product. Use zprint() to display the result numerically.
e. Compute z2/z1 and plot the answers using zvect() to show how the angles of z1 and z2 determine the angle of the quotient. Use zprint() to display the result numerically.
f. Compute the conjugate z* for both z1 and z2 and plot the results. In MATLAB, see help conj.
Display the results numerically with zprint.
g. Compute the inverse 1/z for both z1 and z2 and plot the results. Display the results numerically with zprint.
h. Make a 2 × 2 subplot that displays the following four plots in one figure window: (i) z1 and z2 (ii) z1* and z2* on the same plot; (iii) 1/z1 and 1/z2 on the same plot; and (iv) z1z2. Add a unit circle and x-y axis to each plot for reference.
7. Vectorization
The power of MATLAB comes from its matrix-vector syntax. In most cases, loops can be replaced with vector operations because functions such as exp() and cos() are defined for vector inputs, e.g.,
cos(vv) = [cos(vv(1)), cos(vv(2)), cos(vv(3)), ... cos(vv(N))]
where vv is an N-element row vector. Vectorization can be used to simplify your code. If you have the
following code that plots a certain signal,
M = 200;
for k=1:M
x(k) = k;
y(k) = cos( 0.001*pi*x(k)*x(k) );
end
plot( x, y, ’ro-’ )
then you can replace the for loop and get the same result with 3 lines of code:
M = 200;
y = cos( 0.001*pi*(1:M).*(1:M) );
plot( 1:M, y, ’ro-’ )
Use this vectorization idea to write 2 or 3 lines of code that will perform the same task as the following MATLAB script without using a for loop. (Note: there is a difference between the two operations xx*xx and xx.*xx when xx is a vector.)
%--- make a plot of a weird signal
N = 200;
for k=1:N
xk(k) = k/50;
rk(k) = sqrt( xk(k)*xk(k) + 2.25 );
sig(k) = exp(j*2*pi*rk(k));
end
plot( xk, real(sig), ’mo-’ )
8. Functions
Functions are a special type of M-file that can accept inputs (matrices and vectors) and also return outputs. The keyword function must appear as the first word in the ASCII file that defines the function, and the first line of the M-file defines how the function will pass input and output arguments. The file extension must be lower case “m” as in my func.m. See Section B.5 in Appendix B for more discussion.
The following function has a few mistakes. Before looking at the correct one below, try to find these mistakes
(there are at least three):
matlab mfile [xx,tt] = badcos(ff,dur)
%BADCOS Function to generate a cosine wave
% usage:
% xx = badcos(ff,dur)
% ff = desired frequency in Hz
% dur = duration of the waveform in seconds
%
tt = 0:1/(100*ff):dur; %-- gives 100 samples per period
badcos = cos(2*pi*freeq*tt);
The corrected function should look something like:
function [xx,tt] = goodcos(ff,dur)
tt = 0:1/(100*ff):dur; %-- gives 100 samples per period
xx = cos(2*pi*ff*tt);
Notice the word “function” in the first line. Also, “freeq” has not been defined before being used. Finally, the function has “xx” as an output and hence “xx” should appear in the left-hand side of at least one assignment line within the function body. The function name is not used to hold values produced in the function.
Exercise 2
Write a function that will generate a single sinusoid x(t) = Acos(πœ”π‘‘+ πœ‘) by using four input arguments: Amplitude (A), frequency (πœ”) , phase (πœ‘) and duration (dur). The function should return two outputs: The values of sinusoidal signal (x) and the corresponding time t at which the sinusoidal values are known. Make sure that his function generates 20 values of sinusoid per period. Call this function one_cos.
Plot the output of function for following parameters A=95 , πœ”=200πœ‹rad/sec , πœ‘=pi/5 , and duration =0.025 second.
Exercise 3
Write an M-file called syn sin.m that will synthesize a waveform in the form of (7). Although for loops are rather inefficient in MATLAB, you must write the function with one loop in this lab. The first few statements of the M-file are the comment lines—they should look like:
function [xx,tt] = syn_sin(fk, Xk, fs, dur, tstart)
%SYN_SIN Function to synthesize a sum of cosine waves
% usage:
% [xx,tt] = syn_sin(fk, Xk, fs, dur, tstart)
% fk = vector of frequencies
% (these could be negative or positive)
% Xk = vector of complex amplitudes: Amp*eˆ(j*phase)
% fs = the number of samples per second for the time axis
% dur = total time duration of the signal
% tstart = starting time (default is zero, if you make this input optional)
% xx = vector of sinusoidal values
% tt = vector of times, for the time axis
%
% Note: fk and Xk must be the same length.
% Xk(1) corresponds to frequency fk(1),
% Xk(2) corresponds to frequency fk(2), etc.
The MATLAB syntax length(fk) returns the number of elements in the vector fk, so we do not need a separate input argument for the number of frequencies. On the other hand, the programmer (that’s you) should provide error checking to make sure that the lengths of fk and Xk are the same. See help error. Finally, notice that the input fs defines the number of samples per second for the cosine generation; in other words, we are no longer constrained to using 20 samples per period.
Include a copy of the MATLAB code with your lab report.
Default Inputs
You can make the last input argument(s) take on default values if you use the nargin operator in MATLAB. For example, tstart can be made optional by including the following line of code:
if nargin<5, tstart=0, end %--default value is zero
Testing
In order to use this M-file to synthesize harmonic waveforms, you must choose the entries in the frequency vector to be integer multiples of some desired fundamental frequency. Try the following test and plot the result. [xx0,tt0] = syn_sin([0,100,250],[10,14*exp(-j*pi/3),8*j],10000,0.1,0);
Measure the period of xx0 by hand. Then compare the period of xx0 to the periods of the three sinusoids that make up xx0, and write an explanation on the verification sheet of why the period of xx0 is longer.
Exercise 4
In MATLAB consult help on exp, real and imag. Be aware that you can also use the DSP First function zprint to print the polar and rectangular forms of any vector of complex numbers.
(a) Generate the signal x(t) =Re{2π‘’π‘—πœ‹π‘‘ +2π‘’π‘—πœ‹(𝑑−1.25)+(1−𝑗)π‘’π‘—πœ‹π‘‘ } and make a plot versus t. Use the syn sin function and take a range for t that will cover three periods starting at t = −1/2 secs. Include the MATLAB code with your report.
(b) From the plot of x(t) versus t, measure the frequency, phase and amplitude of the sinusoidal signal by hand. Show annotations on the plots to indicate how these measurements were made and what the values are. Compare to the calculation in part (c).
(c) Use the phasor addition theorem and MATLAB to determine the magnitude and phase of x(t).
5 | P a g e D i g i t a l S i g n a l P r o c e s s i n g
Summary: This lab gives a tutorial about generating different continuous and discrete time signals in MATLAB.

0 comments:

Post a Comment