% DEMO          LISSAJOUS FIGURE DEMO
%
% Program:      lissajous.m
% Written by:   
% For:          EE 311
%               Module For Computer Lab 2
% Date:         6-19-2001
% Purpose:      Program to plot Lissajous figures according to the
%               equation    z(t)=x(t)+j*y(t),
%               where       x(t)=Ax*cos(w*t+phix),
%               and         y(t)=Ay*cos(w*t+phiy).
%               The plots are timed such that the Lissajous figure is
%               drawn like a movie

% ************** Initialization ********************************
clear all, hold off, clf reset, close all
j=sqrt(-1);
disp('Lissajous figure demonstration')
disp('Frequency is fixed at 1000 Hz for this example')
disp(' ')

% ************** User input ************************************
Ax=input('Enter amplitude of x(t), Ax: ');
px=input('Enter phase phix in degrees: ');
Ay=input('Enter amplitude of y(t), Ay: ');
py=input('Enter phase phiy in degrees: ');

phix=(2*pi/360)*px;
phiy=(2*pi/360)*py;
f=1000;                     % Frequency in [Hz]
w=2*pi*f;                   % Frequency in [rad/sec]
period=1/f;                 % Period [sec]
res=40;                     % Resolution is 40 points per period
t=(period/res)*[0:2*res];   % Range for t is [0:2*period]
one=ones(1,length(t));
lt=length(t);
x=Ax*cos(w*t+phix*one);     % Waveform x(t)
y=Ay*cos(w*t+phiy*one);     % Waveform y(t)
z=x+j*y;                    % Complex Waveform z(t)

% ******** Graph Results **********************************
axis('square')
zm=max([abs(x) abs(y)]);     % Calculate limits for axis boundaries
ti=min(t);                   
ta=max(t);
set(gcf,'DoubleBuffer','on') % Keep the output screen from blinking

% Initialize subplots
subplot(221)
axis([ti ta -zm zm])
plot([ti ta],[0 0],'-k')     % Graph x(t)=0 for reference 
grid
title('Waveform x(t)')
xlabel('t [sec]')
ylabel('x(t)')
hold on

subplot(223)
axis([ti ta -zm zm])
plot([ti ta],[0 0],'-k')     % Graph y(t)=0 for reference
grid
title('Waveform y(t)')
xlabel('t [sec]')
ylabel('y(t)')
hold on

subplot(122)
axis([-zm zm -zm zm])
plot([-zm zm],[0 0],'-m',[0 0],[-zm zm],'-m')
grid
title('Lissajous Figure z(t)')
xlabel('Real (x(t))')
ylabel('Imag (y(t))')
hold on

% Plot starting points for x(t), y(t), and z(t)
subplot(221)
axis([ti ta -zm zm])
plot(t(1),x(1),'.g')

subplot(223)
plot(t(1),y(1),'.r')

subplot(122)
axis('square')
axis([-zm zm -zm zm])
plot([0 x(1)],[y(1) y(1)],':g',[x(1) x(1)],[0 y(1)],':r')
plot(x(1),y(1),'ob',[0 x(1)],[0 y(1)],'--m')

% Plot the Lissajous figure as a movie
for i=2:lt
    pause(.01)               % Simulate movie command
    subplot(221)             % Plot next segment of x(t)
    axis([ti ta -zm zm])
    plot([t(i-1),t(i)],[x(i-1),x(i)],'-g')
    
    subplot(223)             % Plot next segment of y(t)
    plot([t(i-1),t(i)],[y(i-1),y(i)],'-r')
    
    subplot(122)             % Plot next segment of z(t)
    axis([-zm zm -zm zm])
    plot([0 x(i-1)],[y(i-1) y(i-1)],':w',[x(i-1) x(i-1)],[0 y(i-1)],':w')
                             % Colors white over the old labels of the real and imag parts
    plot([0 x(i)],[y(i) y(i)],':g',[x(i) x(i)],[0 y(i)],':r')
                             % Labels new real and imag parts.
    plot(x(i-1),y(i-1),'ok',x(i),y(i),'ob')
                             % Replaces previous point with a black circle and labels 
                             % the current point with a blue circle.
end

subplot(122)                 % Puts finishing touches on the z(t) subplot
plot(x,y,'--b')              % Plots z(t) in a dotted blue over the black circles. 
figure(gcf)
