Home > matlab code, Uncategorized, youtube demo code > matlab complex exponential visualisation

matlab complex exponential visualisation

% Used to help visualise complex exponentials as shown in my youtube video (http://youtu.be/K_C7htSXORY)
% by david.dorran@gmail.com

function complex_exponential_gui(varargin)

global x;global t;global w;global real_circle_pos; global imag_circle_pos;
global argand_circle_pos; global T; global freq;

diameter = 0.15; %diameter of argand circle
border = 0.1; %gap between objects

action = '';
if(nargin > 0)
    action = varargin{1}
end
if(~freq)
    freq = 'positive'
end
if(strcmp(action, 'toggle'))
    if(strcmp(freq,'positive'))
        freq = 'negative';
    else
        freq = 'positive';
    end
end
if(nargin ==0 || strcmp(action, 'toggle'))
    close all
    w = pi/3;
    
    T = 0.1;
    t = 0:T:20.3;
    
    if(~strcmp(freq, 'negative'))
    x = exp(j*w*t);
    else
        x = exp(-j*w*t);
    end
    
    
    uicontrol('style','pushbutton',...
        'string','start',...
        'fontsize',8,...
        'units','normalized',...
        'position',[.15 .46 .18 .04],...
        'callback','complex_exponential_gui(''play'')');
    uicontrol('style','pushbutton',...
        'string',' ',...
        'fontsize',8,...
        'units','normalized',...
        'position',[.1 .1 .01 .04],...
        'callback','complex_exponential_gui(''toggle'')');

    if(~strcmp(freq, 'negative'))
        annotation('textbox',[0.3 0.75 0.1 0.1],'string','e^{j{\omega}t} = cos({\omega}t)+jsin({\omega}t)','linestyle','none','fontsize',14)
        
    annotation('textbox',[0.7 0.3 0.1 0.1],'string','jsin({\omega}t)','linestyle','none','fontsize',10)
    else
        annotation('textbox',[0.3 0.75 0.1 0.1],'string','e^{j-{\omega}t} = cos(-{\omega}t)+jsin(-{\omega}t)','linestyle','none','fontsize',14)
        annotation('textbox',[0.3 0.68 0.1 0.1],'string','e^{-j{\omega}t} = cos({\omega}t)-jsin({\omega}t)','linestyle','none','fontsize',14)
    annotation('textbox',[0.7 0.3 0.1 0.1],'string','-jsin({\omega}t)','linestyle','none','fontsize',10)
    end
    annotation('textbox',[0.3 0.1 0.1 0.1],'string','cos({\omega}t)','linestyle','none','fontsize',10)
    annotation('textbox',[0.64 0.35 0.1 0.1],'string','-j','linestyle','none','fontsize',10)
    annotation('textbox',[0.64 0.5 0.1 0.1],'string','j','linestyle','none','fontsize',10)
    annotation('textbox',[0.4 0.25 0.1 0.1],'string','-1','linestyle','none','fontsize',10)
    annotation('textbox',[0.56 0.25 0.1 0.1],'string','1','linestyle','none','fontsize',10)
    subplot(3,1,3)
    plot(real(x));
    hold on
    real_circle_pos = plot(1, real(x(1)),'r.', 'markersize',20);
    plot([0 length(x)*1.3], [ 0 0 ] ,'k','linewidth',2)
    %plot([0 1], [0 1.5] ,'k','linewidth',2)
    
    plot([length(x)*1.2 length(x)*1.3], [ 0.1 0 ] ,'k','linewidth',2)
    plot([length(x)*1.2 length(x)*1.3], [ -0.1 0 ] ,'k','linewidth',2)
    set(gca, 'visible','off', 'position',[0.5-diameter/2 border-.1 diameter 0.5-diameter-border+0.05],'CameraUpVector',[-1 0 0])
    hold off
    
    subplot(3,1,2)
    plot(imag(x));
    hold on
    imag_circle_pos = plot(1, imag(x(1)),'r.', 'markersize',20);
    plot([0 length(x)*1.3], [ 0 0 ] ,'k','linewidth',2)
    plot([length(x)*1.2 length(x)*1.3], [ 0.1 0 ] ,'k','linewidth',2)
    plot([length(x)*1.2 length(x)*1.3], [ -0.1 0 ] ,'k','linewidth',2)
    set(gca, 'visible','off', 'position',[0.5+diameter/2+border 0.5-diameter/2  0.5-diameter-border diameter])
    
    
    subplot(3,1,1)
    plot3(t, -real(x), imag(x));
    hold on
    plot3([0 T], [1.4 -1.4], [0 0],'k','linewidth',2)
    plot3([0 T], [1.4 1.2], [0 0.1],'k','linewidth',2)
    plot3([0 T], [1.4 1.2], [0 -0.1],'k','linewidth',2)
    plot3([0 T], [-1.4 -1.2], [0 0.1],'k','linewidth',2)
    plot3([0 T], [-1.4 -1.2], [0 -0.1],'k','linewidth',2)
    
    
    plot3([0 T], [0 0] , [1.4 -1.4],'k','linewidth',2)
    plot3([0 T], [0 0.1],[1.4 1.2], 'k','linewidth',2)
    plot3([0 T], [0 -0.1],[1.4 1.2], 'k','linewidth',2)
    plot3([0 T], [0 0.1],[-1.4 -1.2], 'k','linewidth',2)
    plot3([0 T],  [0 -0.1],[-1.4 -1.2],'k','linewidth',2)
    
    plot3([0 t(end)*1.4],  [0 0],[0 0 ],'k','linewidth',2)
    plot3([t(end)*1.3 t(end)*1.4],  [0 0],[0.1 0 ],'k','linewidth',2)
    plot3([t(end)*1.3 t(end)*1.4],  [0 0],[-0.1 0 ],'k','linewidth',2)
    
    argand_circle_pos = plot3(0, -real(x(1)), imag(x(1)),'g.', 'markersize',20);
    
    set(gca,'visible','off',  'position',[0.5-diameter 0.5-diameter  diameter*2 diameter*2],'cameraposition',[1 0 0])
    
    set(gca,'ytick', [-1 0 1])
    set(gca,'yticklabel', [-1 0 1])
    set(gca,'xtick', [-1 0 1])
    set(gcf, 'ToolBar', 'figure');
end
if(strcmp(action,'play') || strcmp(action,'pause'))
    offset = 0;
    one_pause = 0;
    two_pause = 0;
    for k = 1:length(t)-1
        set(real_circle_pos, 'Xdata', k,'Ydata', real(x(k)));
        set(imag_circle_pos, 'Xdata', k,'Ydata', imag(x(k)));
        set(argand_circle_pos, 'Xdata',t(k) ,'Ydata', -real(x(k)), 'Zdata',imag(x(k)));
        
        if(imag(x(k)) > imag(x(k+1)) && one_pause == 0 && strcmp(freq,'positive'))
            pause(10)
            offset = offset+ 10;
            one_pause = 1;
        end        
        if(imag(x(k)) > -1/sqrt(2) && imag(x(k)) < 0 && real(x(k)) > 0 && two_pause == 0 && strcmp(freq,'positive'))
            pause(7)
            offset = offset+ 7;
            two_pause = 1;
        end
        drawnow;
        pause(T)
    end
end
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: