
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%  Title: StatisCLAS: A Package of Classification Methods   %%%%
%%%%  Author: David Casado de Lucas                            %%%%
%%%%  Version: 29th May 2013                                   %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% PURPOSE: This function plots some time series of two samples.

% INPUT VARIABLES:
%    t: Vector with the values of the independent variable (time).
%    groupTS1: Matrix with the series of group 1 as rows.
%    groupTS2: Matrix with the series of group 2 as rows.
%    figureTitle: Optional string variable with the title of the figure.
%    percentMargin: Optional variable (in percent) to control de distance from
%                   the more external data to the axes.
%    seriesJointly: Optional logic variable to indicate whether or not the two
%                   samples will be represented jointly.
%    refGroupTS1: Optional vector with a series of population 1 (usually the
%                 reference series, e.g. the mean).
%    refGroupTS2: Optional vector with a series of population 2 (usually the
%                 reference series, e.g. the mean).

% COMMENTS:
%   (1) The reference series can have been calculated from samples different to 'groupTS1' and 'groupTS2'.

function plotTimeSeries(t, groupTS1, groupTS2, figureTitle, percentMargin, seriesJointly, refGroupTS1, refGroupTS2)

if ~exist('figureTitle','var')
  figureTitle = 'Series';
end
if ~exist('percentMargin','var')
  percentMargin = 5;
end
if ~exist('seriesJointly','var')
  seriesJointly = true;
end

xMin = t(1);
xMax = t(end);
% xRange = xMax - xMin;
yMin = min(min([groupTS1; groupTS2; refGroupTS1; refGroupTS2]));
yMax = max(max([groupTS1; groupTS2; refGroupTS1; refGroupTS2]));
yRange = yMax - yMin;
if isequal(yRange,0)
  yRange = 0.01;
end

if seriesJointly
  plot(t,groupTS1','b')
  hold on
  plot(t,groupTS2','r')
  if exist('refGroupTS1','var') && ~isempty(refGroupTS1)
    plot(t,refGroupTS1,'b-','LineWidth',2)
  end
  if exist('refGroupTS2','var') && ~isempty(refGroupTS2)
    plot(t,refGroupTS2,'r-','LineWidth',2)
  end
  hold off
  xlabel('Independent variable')
  ylabel('Dependent variable')
  title(figureTitle)
  set(gca,'XLim',[xMin xMax])
  set(gca,'YLim',[yMin-yRange*percentMargin/100 yMax+yRange*percentMargin/100])
else
  subplot(2,1,1)
    plot(t,groupTS1','b')
    hold on
    if exist('refGroupTS1','var') && ~isempty(refGroupTS1)
      plot(t,refGroupTS1,'b-','LineWidth',2)
    end
    hold off
    xlabel('Independent variable')
    ylabel('Dependent variable')
    title([figureTitle ' (group 1)'])
    set(gca,'XLim',[xMin xMax])
    set(gca,'YLim',[yMin-yRange*percentMargin/100 yMax+yRange*percentMargin/100])
  subplot(2,1,2)      
    plot(t,groupTS2','r')
    hold on
    if exist('refGroupTS2','var') && ~isempty(refGroupTS2)
      plot(t,refGroupTS2,'r-','LineWidth',2)
    end
    hold off
    xlabel('Independent variable')
    ylabel('Dependent variable')
    title([figureTitle ' (group 2)'])
    set(gca,'XLim',[xMin xMax])
    set(gca,'YLim',[yMin-yRange*percentMargin/100 yMax+yRange*percentMargin/100])
end
  
end
