function timedom(action)
%TIMEDOM A simple demonstration of time-domain processing
% 
% To start, load a signal using the 'Load' menu.
%
%  Author: Stuart N Wrigley       
%  MAD - Matlab Auditory Demonstrations
%  (c) University of Sheffield 1998
%  Revision 0.05: 3 July 1998
%  updated July 1999 for v2.1 by Martin

if nargin < 1
  action='init';
end

switch action
case 'init'
  f=findobj('Tag','timedom_fig');
  if ~isempty(f)
    figure(f);
  else  	
    timedom_gui;
    ud=get(gcf,'UserData');
    ud.windows={'rectangular';'triangular';'hanning';'hamming'};

    % initialise signal data
    ud.sig=[];
    ud.fs=11025;
    ud.units='Samples';
    
    % initialise window data
    ud.window.small.size=128;
    ud.window.medium.size=256;
    ud.window.large.size=512;
    ud.window.small.shift=100;
    ud.window.medium.shift=100;
    ud.window.large.shift=100;
    ud.window.type=1;
    ud.radio='3size3shift';
    
    % initialise animation data
    ud.animate.size=256;
    ud.animate.shift=100;
    ud.animate.choice=0;  % off
    ud.animate.stop=0;
    
    % set initalised data in gui
    set(ud.windowtype,'String',ud.windows,'Value',1);
    set(findobj(gcf,'Tag','SmallWindow'),'String',ud.window.small.size);
    set(findobj(gcf,'Tag','MediumWindow'),'String',ud.window.medium.size);
    set(findobj(gcf,'Tag','LargeWindow'),'String',ud.window.large.size);
    set(findobj(gcf,'Tag','SmallShift'),'String',ud.window.small.shift);
    set(findobj(gcf,'Tag','MediumShift'),'String',ud.window.medium.shift);
    set(findobj(gcf,'Tag','LargeShift'),'String',ud.window.large.shift);
    set(findobj(gcf,'Tag','AnimateCheckBox'),'Value',ud.animate.choice);
    set(findobj(gcf,'Tag',ud.radio),'Value',1);
    set(findobj(gcf,'Tag','PauseButton'),'Enable','off');
    set(gcf,'UserData',ud);
    timedom animateclick;
    timedom newWindow;
  end

  case 'create'
  [sig,fs,name]=createsig;
  if length(sig)
    ud=get(gcf,'UserData');
    ud.sig=sig; ud.name=name; ud.fs=fs; 
    set(gcf,'UserData',ud);
    timedom 'newsig'
  end
  
case 'load'
  ud=get(gcf,'UserData');
  [name,sig,fs]=loadsig(fullfile(madroot,'data','sounds'));
  if length(sig) 
    ud.sig=sig; ud.name=name; ud.fs=fs; 
    set(gcf,'UserData',ud);
    timedom 'newsig'
  end
    
case 'newsig'
  timedom 'stop'
  ud=get(gcf,'UserData');
  set(gcf,'Name',['timedom [' ud.name ']'])
  ud.sig=normalise(ud.sig(:));  % ensure column vector
  ud.samples=length(ud.sig);
  ud.winleft=1;
  ud.winright=ud.samples;
  set(gcf,'UserData',ud);      
  timedom plotSignal;
  timedom redraw;

case 'plotSignal'
  ud=get(gcf,'UserData');
  if ~isempty(ud.sig)
    hold on;
    set(ud.signalAxes,'YLim',[-1.1 1.1],'XLim',[1 ud.samples]);
    set(ud.sigline,'XData',1:ud.samples,'YData',ud.sig,'Color','blue');
    set(gcf,'UserData',ud);
  end
  
case 'animateclick'
  ud=get(gcf,'UserData');   
  ud.animate.choice=get(findobj('Tag','AnimateCheckBox'),'Value');
  if ud.animate.choice
    set(findobj(gcf,'Tag','MediumShift'),'Visible','off');
    set(findobj(gcf,'Tag','MediumWindow'),'Visible','off');
    set(findobj(gcf,'Tag','LargeShift'),'Visible','off');
    set(findobj(gcf,'Tag','LargeWindow'),'Visible','off');
    set(findobj(gcf,'Tag','SmallText'),'Visible','off');
    set(findobj(gcf,'Tag','MediumText'),'Visible','off');
    set(findobj(gcf,'Tag','LargeText'),'Visible','off');
    set(findobj(gcf,'Tag','SmallText2'),'Visible','off');
    set(findobj(gcf,'Tag','MediumText2'),'Visible','off');
    set(findobj(gcf,'Tag','LargeText2'),'Visible','off');
    set(findobj(gcf,'Tag','AnimateButton'),'Enable','on');
    set(findobj(gcf,'Tag','PauseText'),'Enable','on');
    set(findobj(gcf,'Tag','AnimatePause'),'Enable','on');
    set(findobj(gcf,'Style','radiobutton'),'Enable','off');
    if strcmp(ud.units,'Milliseconds')
      tempsize=samplestomilli(ud.animate.size,ud.fs);
      tempshift=samplestomilli(ud.animate.shift,ud.fs);
    else
      tempsize=ud.animate.size;
      tempshift=ud.animate.shift;
    end
    set(findobj(gcf,'Tag','SmallWindow'),'String',tempsize);
    set(findobj(gcf,'Tag','SmallShift'),'String',tempshift);
    set(findobj(gcf,'Tag','ShowLabel'),'Enable','off');
    set(findobj(gcf,'Tag','SmallCheckBox'),'Enable','off');
    set(findobj(gcf,'Tag','MediumCheckBox'),'Enable','off');
    set(findobj(gcf,'Tag','LargeCheckBox'),'Enable','off');
    set(gcf,'UserData',ud);
    timedom newSmallWindow;
    % Delete old plots
    timedom deletePlots;
  else
    timedom stop;  % ensure animation is stopped even if stop not pressed before changing mode.
    %set(ud.winline,'YData',[],'XData',[]);
    timedom deletePlots;
    set(findobj(gcf,'Style','radiobutton'),'Enable','on');
    set(findobj(gcf,'Tag','AnimateButton'),'Enable','off');
    if strcmp(ud.units,'Milliseconds')
      tempsize=samplestomilli(ud.window.small.size,ud.fs);
      tempshift=samplestomilli(ud.window.small.shift,ud.fs);
    else
      tempsize=ud.window.small.size;
      tempshift=ud.window.small.shift;
    end
  
    set(findobj(gcf,'Tag','SmallWindow'),'String',tempsize);
    set(findobj(gcf,'Tag','SmallShift'),'String',tempshift);
    set(findobj(gcf,'Tag','PauseText'),'Enable','off');
    set(findobj(gcf,'Tag','AnimatePause'),'Enable','off');
    set(findobj(gcf,'Tag','ShowLabel'),'Enable','on');
    set(findobj(gcf,'Tag','SmallCheckBox'),'Enable','on');
    set(findobj(gcf,'Tag','MediumCheckBox'),'Enable','on');
    set(findobj(gcf,'Tag','LargeCheckBox'),'Enable','on');
    set(gcf,'UserData',ud);
    timedom radio;
  end

case 'deletePlots'
  ud=get(gcf,'UserData');
  hold on;  
  set(ud.energyAxes,'NextPlot','replacechildren','Drawmode','fast','XLimMode','auto','YLimMode','auto');
  set(ud.magnitudeAxes,'NextPlot','replacechildren','Drawmode','fast','XLimMode','auto','YLimMode','auto');
  set(ud.zcAxes,'NextPlot','replacechildren','Drawmode','fast','XLimMode','auto','YLimMode','auto');
  set(ud.winline,'EraseMode','xor','XData',[],'YData',[]);
  set(ud.esline,'EraseMode','xor','XData',[],'YData',[]);
  set(ud.emline,'EraseMode','xor','XData',[],'YData',[]);
  set(ud.elline,'EraseMode','xor','XData',[],'YData',[]);
  set(ud.asline,'EraseMode','xor','XData',[],'YData',[]);
  set(ud.amline,'EraseMode','xor','XData',[],'YData',[]);
  set(ud.alline,'EraseMode','xor','XData',[],'YData',[]);
  set(ud.zsline,'EraseMode','xor','XData',[],'YData',[]);
  set(ud.zmline,'EraseMode','xor','XData',[],'YData',[]);
  set(ud.zlline,'EraseMode','xor','XData',[],'YData',[]);
  
case 'radio'
  ud=get(gcf,'UserData');
  if strcmp(get(gcbo,'Style'),'radiobutton')==1
    ud.radio=get(gcbo,'Tag');
    set(findobj('Style','radiobutton'),'Value',0);
    set(gcbo,'Value',1);
  end
  switch ud.radio
  case '1size3shift'
    set(findobj(gcf,'Tag','MediumShift'),'Visible','on');
    set(findobj(gcf,'Tag','MediumWindow'),'Visible','off');
    set(findobj(gcf,'Tag','LargeShift'),'Visible','on');
    set(findobj(gcf,'Tag','LargeWindow'),'Visible','off');
    set(findobj(gcf,'Tag','SmallText'),'Visible','off');
    set(findobj(gcf,'Tag','MediumText'),'Visible','off');
    set(findobj(gcf,'Tag','LargeText'),'Visible','off');
    set(findobj(gcf,'Tag','SmallText2'),'Visible','on');
    set(findobj(gcf,'Tag','MediumText2'),'Visible','on');
    set(findobj(gcf,'Tag','LargeText2'),'Visible','on');
  case '3size1shift'
    set(findobj(gcf,'Tag','MediumShift'),'Visible','off');
    set(findobj(gcf,'Tag','MediumWindow'),'Visible','on');
    set(findobj(gcf,'Tag','LargeShift'),'Visible','off');
    set(findobj(gcf,'Tag','LargeWindow'),'Visible','on');
    set(findobj(gcf,'Tag','SmallText'),'Visible','on');
    set(findobj(gcf,'Tag','MediumText'),'Visible','on');
    set(findobj(gcf,'Tag','LargeText'),'Visible','on');
    set(findobj(gcf,'Tag','SmallText2'),'Visible','off');
    set(findobj(gcf,'Tag','MediumText2'),'Visible','off');
    set(findobj(gcf,'Tag','LargeText2'),'Visible','off');
  case '3size3shift'
    set(findobj(gcf,'Tag','MediumShift'),'Visible','on');
    set(findobj(gcf,'Tag','MediumWindow'),'Visible','on');
    set(findobj(gcf,'Tag','LargeShift'),'Visible','on');
    set(findobj(gcf,'Tag','LargeWindow'),'Visible','on');
    set(findobj(gcf,'Tag','SmallText'),'Visible','on');
    set(findobj(gcf,'Tag','MediumText'),'Visible','on');
    set(findobj(gcf,'Tag','LargeText'),'Visible','on');
    set(findobj(gcf,'Tag','SmallText2'),'Visible','on');
    set(findobj(gcf,'Tag','MediumText2'),'Visible','on');
    set(findobj(gcf,'Tag','LargeText2'),'Visible','on');
  end
  set(gcf,'UserData',ud);
  timedom redraw;
  
case 'newSmallWindow'
  ud=get(gcf,'UserData');
  if ud.animate.choice
    tempsize=ud.animate.size;
  else
    tempsize=ud.window.small.size;
  end
  tempwin=window(tempsize,getuicontrolvalue(ud.windowtype));
  if ud.animate.choice
    ud.animate.window=tempwin;
  else
    ud.window.small.vector=tempwin;
  end
  set(gcf,'UserData',ud);
  
  
case 'newMediumWindow'
  ud=get(gcf,'UserData');
  ud.window.medium.vector=window(ud.window.medium.size,getuicontrolvalue(ud.windowtype));
  set(gcf,'UserData',ud);
  
  
case 'newLargeWindow'
  ud=get(gcf,'UserData');
  ud.window.large.vector=window(ud.window.large.size,getuicontrolvalue(ud.windowtype));
  set(gcf,'UserData',ud);

case 'newWindow'
  timedom newSmallWindow;
  timedom newMediumWindow;
  timedom newLargeWindow;
  timedom redraw
  
case 'newSmallSize'
  ud=get(gcf,'UserData');
  tempsize=str2num(get(findobj('Tag','SmallWindow'),'String'));
  if strcmp(ud.units,'Milliseconds')
    tempsize=millitosamples(tempsize,ud.fs);
  end
  if ud.animate.choice
    ud.animate.size=tempsize;
  else
    ud.window.small.size=tempsize;
  end
  set(gcf,'UserData',ud);
  timedom newSmallWindow;
  timedom redraw;
  
case 'newMediumSize'
  ud=get(gcf,'UserData');
  ud.window.medium.size=str2num(get(findobj('Tag','MediumWindow'),'String'));
  if strcmp(ud.units,'Milliseconds')
    ud.window.medium.size=millitosamples(ud.window.medium.size,ud.fs);
  end
  set(gcf,'UserData',ud);
  timedom newMediumWindow;
  timedom redraw;
  
case 'newLargeSize'
  ud=get(gcf,'UserData');
  ud.window.large.size=str2num(get(findobj('Tag','LargeWindow'),'String'));
  if strcmp(ud.units,'Milliseconds')
    ud.window.large.size=millitosamples(ud.window.large.size,ud.fs);
  end
  set(gcf,'UserData',ud);
  timedom newLargeWindow;
  timedom redraw;   
  
case 'newSmallShift'
  ud=get(gcf,'UserData');
  tempshift=str2num(get(findobj('Tag','SmallShift'),'String'));
  if strcmp(ud.units,'Milliseconds')
    tempshift=millitosamples(tempshift,ud.fs);
  end
  if ud.animate.choice
    ud.animate.shift=tempshift;
  else
    ud.window.small.shift=tempshift;
  end

  set(gcf,'UserData',ud);
  timedom redraw;
  
case 'newMediumShift'
  ud=get(gcf,'UserData');
  ud.window.medium.shift=str2num(get(findobj('Tag','MediumShift'),'String'));
  if strcmp(ud.units,'Milliseconds')
    ud.window.medium.shift=millitosamples(ud.window.medium.shift,ud.fs);
  end
  set(gcf,'UserData',ud);
  timedom redraw;
  
case 'newLargeShift'
  ud=get(gcf,'UserData');
  ud.window.large.shift=str2num(get(findobj('Tag','LargeShift'),'String'));
  if strcmp(ud.units,'Milliseconds')
    ud.window.large.shift=millitosamples(ud.window.large.shift,ud.fs);
  end
  set(gcf,'UserData',ud);
  timedom redraw;
  
case 'unitsChanged'   
  ud=get(gcf,'UserData');
  if ~strcmp(ud.units,get(gcbo,'Label'))
    ud.units=get(gcbo,'Label');
    
    set(findobj('Tag','units'),'Checked','off');
    set(gcbo,'Checked','on');
    fs=ud.fs;
    if strcmp(ud.units,'Milliseconds')
      if ud.animate.choice
        set(findobj(gcf,'Tag','SmallWindow'),'String',samplestomilli(ud.animate.size,fs));
        set(findobj(gcf,'Tag','MediumWindow'),'String',samplestomilli(ud.window.medium.size,fs));
        set(findobj(gcf,'Tag','LargeWindow'),'String',samplestomilli(ud.window.large.size,fs));
        set(findobj(gcf,'Tag','SmallShift'),'String',samplestomilli(ud.animate.shift,fs));
        set(findobj(gcf,'Tag','MediumShift'),'String',samplestomilli(ud.window.medium.shift,fs));
        set(findobj(gcf,'Tag','LargeShift'),'String',samplestomilli(ud.window.large.shift,fs));
      else
        set(findobj(gcf,'Tag','SmallWindow'),'String',samplestomilli(ud.window.small.size,fs));
        set(findobj(gcf,'Tag','MediumWindow'),'String',samplestomilli(ud.window.medium.size,fs));
        set(findobj(gcf,'Tag','LargeWindow'),'String',samplestomilli(ud.window.large.size,fs));
        set(findobj(gcf,'Tag','SmallShift'),'String',samplestomilli(ud.window.small.shift,fs));
        set(findobj(gcf,'Tag','MediumShift'),'String',samplestomilli(ud.window.medium.shift,fs));
        set(findobj(gcf,'Tag','LargeShift'),'String',samplestomilli(ud.window.large.shift,fs));
      end
      if ~isempty(ud.sig)
        NewXLim=(samplestomilli(ud.samples,fs));
        set(ud.signalAxes,'YLim',[-1.1 1.1],'XLim',[1 NewXLim]);
        set(ud.sigline,'XData',linspace(1,NewXLim,ud.samples),'YData',ud.sig,'Color','blue');
      end
    else % samples (default)
      set(findobj(gcf,'Tag','SmallWindow'),'String',millitosamples(str2num(get(findobj('Tag','SmallWindow'),'String')),fs));
      set(findobj(gcf,'Tag','MediumWindow'),'String',millitosamples(str2num(get(findobj('Tag','MediumWindow'),'String')),fs));
      set(findobj(gcf,'Tag','LargeWindow'),'String',millitosamples(str2num(get(findobj('Tag','LargeWindow'),'String')),fs));
      set(findobj(gcf,'Tag','SmallShift'),'String',millitosamples(str2num(get(findobj('Tag','SmallShift'),'String')),fs));
      set(findobj(gcf,'Tag','MediumShift'),'String',millitosamples(str2num(get(findobj('Tag','MediumShift'),'String')),fs));
      set(findobj(gcf,'Tag','LargeShift'),'String',millitosamples(str2num(get(findobj('Tag','LargeShift'),'String')),fs));                 
      timedom plotSignal;
      timedom redraw; % only redraw after ms to samp conversion as this conversion is the only potentially lossy convertion.
    end
    set(gcf,'UserData',ud);
  end

case 'redraw'
  ud=get(gcf,'UserData');
  if ~isempty(ud.sig)
    figID=findobj(gcf,'Tag','Fig1');
    set(figID,'Name','Simple Time-Domain Processing - Calculating...');
    if ~ud.animate.choice
      timedom deletePlots;
      timedom rePlot;
    else
      timedom animate;
    end
    set(figID,'Name','Simple Time-Domain Processing');
  end


case 'rePlot'
  ud=get(gcf,'UserData');
  
  switch ud.radio
  case '3size1shift'
    paddedSignal=[zeros(ud.window.small.size,1);ud.sig;zeros(ud.window.small.size,1)];
    signedSignal=sgn(paddedSignal);
    halfsmall=round(ud.window.small.size/2);
    j=1;
    for i=1:ud.window.small.shift:ud.samples
      se(j)=(sum((paddedSignal(i+1+halfsmall:ud.window.small.size+halfsmall+i).^2).*ud.window.small.vector))/ud.window.small.size;
      sa(j)=(sum((abs(paddedSignal(i+1+halfsmall:ud.window.small.size+halfsmall+i))).*ud.window.small.vector))/ud.window.small.size;
      sz(j)=(sum((abs(signedSignal(i+1+halfsmall:ud.window.small.size+halfsmall+i)-signedSignal(i+halfsmall:ud.window.small.size+i-1+halfsmall))/2).*ud.window.small.vector))*(1/ud.window.small.size);
      j=j+1;
    end
    se=sqrt(se);
    paddedSignal=[zeros(ud.window.medium.size,1);ud.sig;zeros(ud.window.medium.size,1)];
    signedSignal=sgn(paddedSignal);
    halfmedium=round(ud.window.medium.size/2);
    j=1;
    for i=1:ud.window.small.shift:ud.samples
      me(j)=(sum((paddedSignal(i+1+halfmedium:ud.window.medium.size+halfmedium+i).^2).*ud.window.medium.vector))/ud.window.medium.size;
      ma(j)=(sum((abs(paddedSignal(i+1+halfmedium:ud.window.medium.size+halfmedium+i))).*ud.window.medium.vector))/ud.window.medium.size;
      mz(j)=(sum((abs(signedSignal(i+1+halfmedium:ud.window.medium.size+halfmedium+i)-signedSignal(i+halfmedium:ud.window.medium.size+i-1+halfmedium))/2).*ud.window.medium.vector))*(1/ud.window.medium.size);
      j=j+1;
    end
    me=sqrt(me);
    paddedSignal=[zeros(ud.window.large.size,1);ud.sig;zeros(ud.window.large.size,1)];
    signedSignal=sgn(paddedSignal);
    halflarge=round(ud.window.large.size/2);
    j=1;
    for i=1:ud.window.small.shift:ud.samples
      le(j)=(sum((paddedSignal(i+1+halflarge:ud.window.large.size+halflarge+i).^2).*ud.window.large.vector))/ud.window.large.size;
      la(j)=(sum((abs(paddedSignal(i+1+halflarge:ud.window.large.size+halflarge+i))).*ud.window.large.vector))/ud.window.large.size;
      lz(j)=(sum((abs(signedSignal(i+1+halflarge:ud.window.large.size+halflarge+i)-signedSignal(i+halflarge:ud.window.large.size+i-1+halflarge))/2).*ud.window.large.vector))*(1/ud.window.large.size);
      j=j+1;
    end
    le=sqrt(le);
    
  case '1size3shift'
    paddedSignal=[zeros(ud.window.small.size,1);ud.sig;zeros(ud.window.small.size,1)];
    signedSignal=sgn(paddedSignal);
    halfsmall=round(ud.window.small.size/2);
    j=1;
    for i=1:ud.window.small.shift:ud.samples
      se(j)=(sum((paddedSignal(i+1+halfsmall:ud.window.small.size+i+halfsmall).^2).*ud.window.small.vector))/ud.window.small.size;
      sa(j)=(sum((abs(paddedSignal(i+1+halfsmall:ud.window.small.size+i+halfsmall))).*ud.window.small.vector))/ud.window.small.size;
      sz(j)=(sum((abs(signedSignal(i+1+halfsmall:ud.window.small.size+i+halfsmall)-signedSignal(i+halfsmall:ud.window.small.size+i-1+halfsmall))/2).*ud.window.small.vector))*(1/ud.window.small.size);
      j=j+1;
    end
    se=sqrt(se);
    j=1;
    for i=1:ud.window.medium.shift:ud.samples
      me(j)=(sum((paddedSignal(i+1+halfsmall:ud.window.small.size+i+halfsmall).^2).*ud.window.small.vector))/ud.window.small.size;
      ma(j)=(sum((abs(paddedSignal(i+1+halfsmall:ud.window.small.size+i+halfsmall))).*ud.window.small.vector))/ud.window.small.size;
      mz(j)=(sum((abs(signedSignal(i+1+halfsmall:ud.window.small.size+i+halfsmall)-signedSignal(i+halfsmall:ud.window.small.size+i-1+halfsmall))/2).*ud.window.small.vector))*(1/ud.window.small.size);
      j=j+1;
    end
    me=sqrt(me);
    j=1;
    for i=1:ud.window.large.shift:ud.samples
      le(j)=(sum((paddedSignal(i+1+halfsmall:ud.window.small.size+i+halfsmall).^2).*ud.window.small.vector))/ud.window.small.size;
      la(j)=(sum((abs(paddedSignal(i+1+halfsmall:ud.window.small.size+i+halfsmall))).*ud.window.small.vector))/ud.window.small.size;
      lz(j)=(sum((abs(signedSignal(i+1+halfsmall:ud.window.small.size+i+halfsmall)-signedSignal(i+halfsmall:ud.window.small.size+i-1+halfsmall))/2).*ud.window.small.vector))*(1/ud.window.small.size);
      j=j+1;
    end
    le=sqrt(le);
    
    
  case '3size3shift'
    %small window
    %pad beginning with zeros length of window
    paddedSignal=[zeros(ud.window.small.size,1);ud.sig;zeros(ud.window.small.size,1)];
    signedSignal=sgn(paddedSignal);
    halfsmall=round(ud.window.small.size/2);
    j=1;
    for i=1:ud.window.small.shift:ud.samples
      se(j)=(sum((paddedSignal(i+1+halfsmall:ud.window.small.size+i+halfsmall).^2).*ud.window.small.vector))/ud.window.small.size;
      sa(j)=(sum((abs(paddedSignal(i+1+halfsmall:ud.window.small.size+i+halfsmall))).*ud.window.small.vector))/ud.window.small.size;
      sz(j)=(sum((abs(signedSignal(i+1+halfsmall:ud.window.small.size+i+halfsmall)-signedSignal(i+halfsmall:ud.window.small.size+i-1+halfsmall))/2).*ud.window.small.vector))*(1/ud.window.small.size);
      j=j+1;
    end
    se=sqrt(se);
    
    
    %medium window
    %pad beginning with zeros length of window
    paddedSignal=[zeros(ud.window.medium.size,1);ud.sig;zeros(ud.window.medium.size,1)];
    signedSignal=sgn(paddedSignal);
    halfmedium=round(ud.window.medium.size/2);
    j=1;
    for i=1:ud.window.medium.shift:ud.samples
      me(j)=(sum((paddedSignal(i+1+halfmedium:ud.window.medium.size+i+halfmedium).^2).*ud.window.medium.vector))/ud.window.medium.size;
      ma(j)=(sum((abs(paddedSignal(i+1+halfmedium:ud.window.medium.size+i+halfmedium))).*ud.window.medium.vector))/ud.window.medium.size;
      mz(j)=(sum((abs(signedSignal(i+1+halfmedium:ud.window.medium.size+i+halfmedium)-signedSignal(i+halfmedium:ud.window.medium.size+i-1+halfmedium))/2).*ud.window.medium.vector))*(1/ud.window.medium.size);
      j=j+1;   
    end
    me=sqrt(me);
    
    %large window
    %pad beginning with zeros length of window
    paddedSignal=[zeros(ud.window.large.size,1);ud.sig;zeros(ud.window.large.size,1)];
    signedSignal=sgn(paddedSignal);
    halflarge=round(ud.window.large.size/2);
    j=1;
    for i=1:ud.window.large.shift:ud.samples
      le(j)=(sum((paddedSignal(i+1+halflarge:ud.window.large.size+i+halflarge).^2).*ud.window.large.vector))/ud.window.large.size;
      la(j)=(sum((abs(paddedSignal(i+1+halflarge:ud.window.large.size+i+halflarge))).*ud.window.large.vector))/ud.window.large.size;
      lz(j)=(sum((abs(signedSignal(i+1+halflarge:ud.window.large.size+i+halflarge)-signedSignal(i+halflarge:ud.window.large.size+i-1+halflarge))/2).*ud.window.large.vector))*(1/ud.window.large.size);
      j=j+1;   
    end
    le=sqrt(le);
    j=[];
  end
  longeste=max(length(se),max(length(me),length(le)));
  longesta=max(length(sa),max(length(ma),length(la)));
  longestz=max(length(sz),max(length(mz),length(lz)));
  
  hold on;  
  set(ud.energyAxes,   'NextPlot','replacechildren','Drawmode','fast','XLim',[1 longeste],'YLimMode','auto');
  set(ud.magnitudeAxes,'NextPlot','replacechildren','Drawmode','fast','XLim',[1 longesta],'YLimMode','auto');
  set(ud.zcAxes,       'NextPlot','replacechildren','Drawmode','fast','XLim',[1 longestz],'YLimMode','auto');
  
  if get(findobj('Tag','SmallCheckBox'),'Value')
    set(ud.esline,'EraseMode','normal','XData',linspace(1,longeste,length(se)),'YData',se,'Color','blue');
    set(ud.asline,'EraseMode','normal','XData',linspace(1,longesta,length(sa)),'YData',sa,'Color','blue');
    set(ud.zsline,'EraseMode','normal','XData',linspace(1,longestz,length(sz)),'YData',sz,'Color','blue');
  end

  if get(findobj('Tag','MediumCheckBox'),'Value')
    set(ud.emline,'EraseMode','normal','XData',linspace(1,longeste,length(me)),'YData',me,'Color','red');
    set(ud.amline,'EraseMode','normal','XData',linspace(1,longesta,length(ma)),'YData',ma,'Color','red');
    set(ud.zmline,'EraseMode','normal','XData',linspace(1,longestz,length(mz)),'YData',mz,'Color','red');
  end   

  if get(findobj('Tag','LargeCheckBox'),'Value')
    set(ud.elline,'EraseMode','normal','XData',linspace(1,longeste,length(le)),'YData',le,'Color','green');   
    set(ud.alline,'EraseMode','normal','XData',linspace(1,longesta,length(la)),'YData',la,'Color','green');   
    set(ud.zlline,'EraseMode','normal','XData',linspace(1,longestz,length(lz)),'YData',lz,'Color','green');
  end   

  set(gcf,'UserData',ud);      
  
  
case 'animate'
  set(findobj(gcf,'Tag','SmallWindow'),'Enable','off');
  set(findobj(gcf,'Tag','SmallShift'),'Enable','off');
  set(findobj(gcf,'Tag','WindowPopup'),'Enable','off');
  set(findobj(gcf,'Tag','AnimatePause'),'Enable','off');
  set(findobj(gcf,'Tag','PauseButton'),'Enable','on');
  timedom deletePlots;
  ud=get(gcf,'UserData');
  ud.animate.stop=0;
  paddedSignal=[zeros(ud.animate.size,1);ud.sig;zeros(ud.animate.size,1)];
  signedSignal=sgn(paddedSignal)+1;
  halfanimate=round(ud.animate.size/2);
  
  j=1;
  for i=1:ud.animate.shift:ud.samples
    range=i+1+halfanimate:ud.animate.size+i+halfanimate;
    ae(j)=sqrt((sum((paddedSignal(range).^2).*ud.animate.window))/ud.animate.size);
    aa(j)=(sum((abs(paddedSignal(range))).*ud.animate.window))/ud.animate.size;
    az(j)=(sum((abs(signedSignal(range)-signedSignal(i+halfanimate:ud.animate.size+i-1+halfanimate))/2).*ud.animate.window))*(1/ud.animate.size);
    j=j+1;
  end
  AnimateXLim=length(ae);
  maxE=max(ae); eYLim=maxE+(.3*maxE);
  maxA=max(aa); aYLim=maxA+(.3*maxA);
  maxZ=max(az); zYLim=maxZ+(.3*maxZ);
  
  set(ud.signalAxes,   'NextPlot','replacechildren','Drawmode','fast');
  set(ud.energyAxes,   'NextPlot','replacechildren','Drawmode','fast','XLim',[1 AnimateXLim],'YLim',[0 eYLim]);
  set(ud.magnitudeAxes,'NextPlot','replacechildren','Drawmode','fast','XLim',[1 AnimateXLim],'YLim',[0 aYLim]);
  set(ud.zcAxes,       'NextPlot','replacechildren','Drawmode','fast','XLim',[1 AnimateXLim],'YLim',[0 zYLim]);
  
  set(ud.winline,'EraseMode','xor');
  set(ud.emline,'EraseMode','xor');
  set(ud.amline,'EraseMode','xor');
  set(ud.zmline,'EraseMode','xor');
  hold on;
  set(findobj(gcf,'Tag','AnimateButton'),'String','Stop','Callback','timedom stop');
  set(gcf,'UserData',ud);
  
  i=0;
  while and(strcmp('Stop',get(findobj('Tag','AnimateButton'),'String')),(i<AnimateXLim))
    if strcmp('Pause',get(findobj('Tag','PauseButton'),'String'))
      i=i+1;
      wind=zeros(ud.samples,1);
      WinStartIndex=1;
      WinEndIndex=ud.animate.size;
      StartIndex=((i-1)*ud.animate.shift)+1+halfanimate-ud.animate.size;
      EndIndex=((i-1)*ud.animate.shift)+halfanimate;
      if StartIndex<1
        WinStartIndex=(1-StartIndex)+1;
        StartIndex=1;
      end
      if EndIndex>ud.samples
        WinEndIndex=ud.animate.size-(EndIndex-ud.samples);
        EndIndex=ud.samples;
      end
      wind(StartIndex:EndIndex)=ud.animate.window(WinStartIndex:WinEndIndex);
      sigXLim=get(ud.signalAxes,'XLim');
      %sigXLim(2)
      set(ud.winline,'YData',wind,'XData',linspace(1,sigXLim(2),ud.samples))
      set(ud.emline,'YData',ae(1:i),'XData',1:i);
      set(ud.amline,'YData',aa(1:i),'XData',1:i);
      set(ud.zmline,'YData',az(1:i),'XData',1:i);
      pause(str2num(get(findobj('Tag','AnimatePause'),'String')));
    else
      pause(1);
    end
  end
  set(ud.winline,'YData',[],'XData',[]);
  timedom stop;
  
case 'stop'
  ud=get(gcf,'UserData');
  set(findobj(gcf,'Tag','SmallWindow'),'Enable','on');
  set(findobj(gcf,'Tag','SmallShift'),'Enable','on');
  set(findobj(gcf,'Tag','WindowPopup'),'Enable','on');
  set(findobj(gcf,'Tag','AnimatePause'),'Enable','on');
  set(findobj(gcf,'Tag','PauseButton'),'Enable','off');
  %if ~isempty(ud.sig)
  set(findobj(gcf,'Tag','AnimateButton'),'String','Run','Callback','timedom redraw');
  ud.animate.stop=1;
  set(gcf,'UserData',ud);
  timedom pauseoff;
  %end
  
case 'pauseon'
  set(findobj(gcf,'Tag','PauseButton'),'String','Run','Callback','timedom pauseoff');
  
case 'pauseoff'
  set(findobj(gcf,'Tag','PauseButton'),'String','Pause','Callback','timedom pauseon');  

case 'playSignal'
  ud=get(gcf,'UserData');
  if ~isempty(ud.sig)
    soundsc(ud.sig,ud.fs);
  end

otherwise
  maduievent('timedom',action);
 
end


