% EXERCISE      ROTATING PHASORS
%
% Program:      dampened_phasor1.m 
% Written by:   
% For:          EE 311
%               Exercise For Computer Lab 2
% Date:         5-31-2001
% Purpose:      Program to create snapshots of a rotating phasor in polar
%               form and its real part as a waveform with respect to time.
%               The rotating phasor is z(t)=A*exp(j*phi)*exp(j*omega*t).
%               Its real part is Re[z(t)]=A*cos(omega*t + phi).
%               The amplitude is A (range 0..2), the frequency f (range 0.5..5 kHz).
%               and phase phi (range -180..180 deg) are entered manually.
clear all, hold off, clf reset, close all

disp('Rotating phasor demonstration')
disp(' ')
j = sqrt(-1);

% User Inputs
A=input('Enter Amplitude A (range 0..2):              ');
f=input('Enter Frequency f (range 500..5000) in [Hz]: ');
p=input('Enter phase phi in degrees:                  ');
w=2*pi*f;                   % Frequency in [rad/sec]
phi=(2*pi/360)*p;           % Phase phi in [rad]

% Parameter
m=2;                        % Number of rows of snapshots
n=3;                        % Number of columns of snapshots
pt=20;                      % Number of points per snapshot
k=2;                        % Number of 'o' per snapshot
Ax=2;                       % Maximum amplitude
tx=1e-3;                    % Maximum time

% Initialization
kx=ones(1,m*n*k)+round((pt/2)*[0:m*n*k-1]);
                            % Indexes for 'o' markers
pts=m*n*pt;                 % Total number of points
t=(tx/pts)*[0:pts-1];       % Time axis
c=(Ax/2)*exp(j*(2*pi/180)*[0:180]);
                            % Circle

% Computation
z=A*exp(j*phi)*exp(j*w*t);  % (Complex) phasor z(t)
Rez=real(z);                % Real part of z(t)
Imz=imag(z);                % Imaginary part of z(t)
Reo=Rez(kx);                % Real part for 'o' marks
Imo=Imz(kx);                % Imaginary part for 'o' marks
to=t(kx);                   % Time points for 'o' marks

% Display
for u=0:m-1
    for v=0:n-1
        ix=u*n+v;           % Index for plot command
        subplot(2*m,n,2*u*n+v+1)
        plot(Rez(1:(ix+1)*pt),Imz(1:(ix+1)*pt),'-b')
                            % plot z(t) path
        hold on
        plot(Reo(1:(ix+1)*k),Imo(1:(ix+1)*k),'om')
                            % Plot z(t) 'o' marks
        plot([0 Rez(1)],[0 Imz(1)],'-c')
                            % Mark Initial Phase phi
        plot(real(c),imag(c),':w',real(2*c),imag(2*c),':w')
                            % Circle grid
        hold off
        title('Phasor z(t)')
        grid
        axis('square')
        axis([-Ax Ax -Ax Ax])
        subplot(2*m,n,2*u*n+n+v+1)
        plot(t(1:(ix+1)*pt)/tx,Rez(1:(ix+1)*pt),'-b')
                            % Plot waveform Re[z(t)]
        hold on
        plot(to(1:(ix+1)*k)/tx,Reo(1:(ix+1)*k),'om')
                            % Plot Re[z(t)] 'o' marks
        hold off
        title('Waveform Re[z(t)]')
        grid
        axis([0 1 -Ax Ax])
    end
end
figure(gcf)

%************************************************************************
% Exercise:                                                             *
%                                                                       *
% 1.) Change the program so the lower graphs display the Imaginary part *
%     of z(t). Make sure the graphs have the proper labels.             *
%                                                                       *
% 2.) Go to the link 'Dampened Phasor' on the web page. Alter this      *
%     so that the output looks like a dampened phasor.                  *
%     HINT: One parameter that was time independent in this program     *
%     must now be time dependent.                                       *
%                                                                       *
%************************************************************************


