function data_scope(new_data, DT)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% version 0.3
% Feb 25, 2004
%
% Displays a scolling "chart recorder" graph of multichannel data.
% Automatically creates the display window if it does not exist.
% To be called on repeated time steps from within a simulation loop.
%
% NOTES:
% Currently hardcoded to use figure 2. 
% The data is available from the workspace as a global variable:
% global DS_channel_data
%
% Example usage:
% for t = 0:DT:maxTime
%   <SIMULATION COMMANDS>
%   data(1) = variable1;
%   data(2) = variable2;
%   data(3) = variable3;
%   data_scope(data, DT);
% end
% 
% Mark Nelson, Feb 2004
%
% Programming and development support: 
% Eugene Kang (mandoo@uiuc.edu)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
persistent NCHAN hchan hminLabel hmaxLabel maxVal 
global DS_channel_data

NPTS = 300;
cstr = 'rgbm';

% default value for DT
if(nargin < 2)
    DT = 1/30;
end

if(isempty(NCHAN))
    NCHAN = length(new_data);
    maxVal = ones(1,NCHAN);
end

% if number of channels has changed, 
% delete the old scope are create a new one
if(length(new_data) ~= NCHAN)
    try, delete(2), end
    disp('deleting old scope');
end

if (~ishandle(2)) % if data_scope plot hasn't been generated
    disp('making new scope');
    NCHAN = length(new_data);
    
    % create a new figure window in upper right of screen
    ss = get(0,'screenSize');
    width = 400;
    height = min(100*NCHAN, ss(4)-100);
    hscope=figure(2);
    set(2,'name', 'Scope',...
        'NumberTitle','off', ...
        'position',[ss(3)-width-100 ss(4)-height-50 width height],...
        'menu','none',...
        'Color',[1 1 0.9], ...
        'doublebuffer','on');
    
    DS_channel_data = zeros(NPTS, NCHAN);
    maxVal = ones(1,NCHAN);
   
    % initialize plot handles
    for i = 1:NCHAN
        cidx = mod(i-1,4)+1;
        hchan(i)=plot((1:NPTS)*DT, i+DS_channel_data(:,i),cstr(cidx),'markersize',4); 
        hold on;
    end
    
    % channel labels
    axis([0 NPTS*DT 0 NCHAN]);
    set(gca,'ytick',(1:NCHAN)-0.5);
    for i=1:NCHAN
        str(i,:) = sprintf('Ch %1d',i);
    end
    set(gca,'yticklabel',str);
    xlabel('time');
    
    % channel yaxis limits
    for i=1:NCHAN
        cidx = mod(i-1,4)+1;
        hminLabel(i) = ...
            text(-5*DT, (i-1)+0.1, num2str(-maxVal(i)),'horiz','right','color',cstr(cidx));
        hmaxLabel(i) = ...
            text(-5*DT, (i-1)+0.9, num2str(maxVal(i)),'horiz','right','color',cstr(cidx));
        grid on;
    end
end

% update channels
figure(2);
for i = 1:NCHAN
    
    % scale factor
    if(abs(new_data(i)) > maxVal(i))
        maxVal(i) = abs(new_data(i));
        set(hminLabel(i), 'string', num2str(-maxVal(i)));
        set(hmaxLabel(i), 'string', num2str(maxVal(i)));
    end
    
    scale_factor=0.5/(maxVal(i));
    
    % shift data to left, apply scale factors, append new data at the end
    DS_channel_data(1:NPTS-1,i) = DS_channel_data(2:NPTS,i);
    DS_channel_data(NPTS,i) = new_data(i);
    
    % update plot
    set(hchan(i), 'ydata', (i-1) + scale_factor*(DS_channel_data(:,i)+maxVal(i)));
end

