function lyngby_ui_view_fir(arg1, arg2, arg3, arg4, arg5, ...
    arg6, arg7, arg8, arg9, arg10, arg11, arg12) 

% lyngby_ui_view_fir   - Viewing FIR filter results
%
%	function lyngby_ui_view_fir(PrototypeName, PrototypeValue)
%
%       Input: PrototypeName:
%                  type       [ {Time} | Error | Error, abs | Filter |
%                             Histogram of data | Histogram of error |
%                             Histogram of error, bell ]
%                  voxel      Voxel to be plotted
%                  index      Time indices to be shown
%                  amplitude  [ {free} | keep ] axis control
%                  value      Used in the title
% 
%       This functions is automatically called from lyngby_ui_view
%
%       See also: lyngby_ui_view

% cvs:  $Id: lyngby_ui_view_fir.m,v 1.14 1999/07/09 13:30:33 fnielsen Exp $
%       $Revision: 1.14 $

    lyngby_global
    lyngby_ui_global
    
    plottype = 1;
    voxel = 1;
    indices = 1:size(RESULT_FIR,1);
    amplitude = 1;
    value = NaN;
    
    n = 1;
    while n < nargin
      eval(sprintf('arg = lower(arg%d);', n)); 
      if strcmp(arg, 'type')
	n = n + 1;
	eval(sprintf('arg = deblank(lower(arg%d));', n));
	if isstr(arg)
	  if strcmp(arg, 'time')
	    plottype = 1;
  	  elseif strcmp(arg, 'error')
	    plottype = 2;
	  elseif strcmp(arg, 'error, abs')
	    plottype = 3;
	  elseif strcmp(arg, 'filter')
	    plottype = 4;
	  elseif strcmp(arg, 'histogram of data')
	    plottype = 5;
	  elseif strcmp(arg, 'histogram of error')
	    plottype = 6;
	  elseif strcmp(arg, 'histogram of error, bell')
	    plottype = 7;
	  elseif strcmp(arg, 'cumperiodo')
	    plottype = 8;
	  else
	    error('Argument to ''type'' not recognized');
	  end
	else	  
	  error('''type'' PropertyName should be followed by a string');
	end
      elseif strcmp(arg, 'voxel')
	n = n + 1;
	eval(sprintf('arg = arg%d;', n));
	if length(voxel) == 1
	  voxel = arg;
	else
	  error(['''voxel'' PropertyName should be followed by a ' ...
	      'a single value']);
	end
      elseif strcmp(arg, 'index')
	n = n + 1;
	eval(sprintf('arg = arg%d;', n));
	if length(arg) == 2
	  indices = arg(1):arg(2);
	else
	  error('')
	end

      elseif strcmp(arg, 'amplitude')
	n = n + 1;
	eval(sprintf('arg = lower(arg%d);', n));
	if strcmp(arg, 'free')
	  amplitude = 1;
	elseif strcmp(arg, 'keep')
	  amplitude = 2;
	else
	  % disp('Warning: ''Amplitude'' PropertyName not recognized');
	end
      elseif strcmp(arg, 'value')
	n = n + 1;
	eval(sprintf('arg = arg%d;', n));
	value = arg;
      else
	error(sprintf('Invalid property: %s', arg));
      end
      n = n + 1;
    end

    
    % 'keep' only if same type as old plot
    previousPlottype = 0;
    h = findobj(gcf, 'tag', 'previousPlottype');
    if h
      previousPlottype = get(h, 'Userdata');
    end
    if plottype ~= previousPlottype
      amplitude = 1;
    end

    
    oldAxis = axis;
    
    if ~isempty(voxel)
      voxel3 = lyngby_mask2full(voxel);

      if plottype == 1
	xn = XN(:,voxel);
	
	model = conv(PN, RESULT_FIR(:,voxel));
	time = 1:length(PN);
      
        scal=max(abs(xn))/max(abs(PN));
	plot(time, xn, 'g-');
	hold on
	  plot(time, scal*PN, 'b:');
	  h = plot(time, model(1:length(PN)), 'r-.');
	  set(h, 'LineWidth', 2);
	  legend('Data', 'Paradigm', 'Model');
	hold off;
	sTitle = 'Time series';
	
      elseif plottype == 2 | plottype == 3
	% Histogram of error
	
	xn = XN(:,voxel);

	model = conv(PN, RESULT_FIR(:,voxel));
	error = xn - model(1:length(PN));
	time = 1:length(PN);
        scal = max(abs(error))/max(abs(PN));
	if plottype == 2
	  plot(time, error, 'g-');
	else 
	  plot(time, abs(error), 'g-');
	end
	hold on
	  plot(time, scal*PN, 'b:');
	  legend('Error', 'Paradigm');
	hold off;
	sTitle = 'Error time series';
	
      elseif plottype == 4
	plot(RESULT_FIR(:,voxel));
	legend off;
	sTitle = 'Filter Coefficients';
	
      elseif plottype == 5
	% Histogram of data
	
	xn = XN(:,voxel);

	hist(xn,25);
	sTitle = 'Histogram of data';
      	legend off;
	
      elseif plottype == 6
	% Histogram of error
	
	xn = XN(:,voxel);

	model = conv(PN, RESULT_FIR(:,voxel));

	error = xn - model(1:length(PN));
	hist(error,25);
	sTitle = 'Histogram of error';
	legend off;
	
      elseif plottype == 7
	% Histogram of error
	
	xn = XN(:,voxel);

	model = conv(PN, RESULT_FIR(:,voxel));
	error = xn - model(1:length(PN));
	[Hist, histCenters] = hist(error,25);
	hist(error, 25);                    % For Matlab4 combatibility!        
	% bar(histCenters, Hist, 'hist');   % 'hist' is internal (in Matlab5)!
	                                    % But otherwise there
	                                    % is aliasing in
	                                    % vertical lines.
	width = histCenters(2) - histCenters(1);
	if amplitude == 2
	  limits = oldAxis(1:2);
	else
	  ax = axis;
	  limits = ax(1:2);
	end
	x = (limits(1):(limits(2)-limits(1))/99:limits(2))';
	p = sum(Hist) * width * lyngby_pdf_gauss(x, 0, std(error)^2);
	hold on;
	  plot(x,p,'r');
	hold off;
	sTitle = 'Histogram of error';
	legend off;

      elseif plottype == 8
	% Cumulated periodogram of error
	
	xn = XN(:,voxel);

	
	model = conv(PN, RESULT_FIR(:,voxel));
	error = xn - model(1:length(PN));
	Periodo = lyngby_cumperiodo(error);
	
	plot(0:(length(Periodo)-1), Periodo);
	line([0 length(Periodo)-1],[0 1])
	sTitle = 'Normalized cumulated periodogram';

	xlabel('Frequency');
	ylabel('Normalized Cumulated Effect');
	legend off;

	
      else
	error('Wrong plottype')

      
      end
      
      if amplitude == 2
	ax = oldAxis;
      else
	ax = axis;
      end
      if any(plottype == [1 2 3])
	ax(1:2) = [min(indices) max(indices)];
      end
      axis(ax);

      
      sTitle = sprintf('%s, [%d, %d, %d]', sTitle, voxel3);
      sTitle = sprintf('%s (%d)', sTitle, voxel);
      if ~isnan(value)
	sTitle = sprintf('%s, %f', sTitle, value);
      end
      title(sTitle);
      
    end

    h = findobj(gcf, 'tag', 'previousPlottype');
    if isempty(h)
      uicontrol(gcf, 'Tag', 'previousPlottype', 'Visible', 'off');
    end
    set(findobj(gcf, 'Tag', 'previousPlottype'), 'UserData', plottype);


    










