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

% PURPOSE: This function, by applying transformations to each curve, provides the multivariate data.

% INPUT VARIABLES:
%    numberFDtoMVmethod: Number of the transformation to be applied.
%    infoFDtoMVmethod: Cell array with the necessary information for the method.
%    groupFD1: Matrix with the training functions of group 1 as rows.
%    groupFD1test: Matrix with the testing functions of group 1 as rows.
%    groupFD2: Matrix with the training functions of group 2 as rows.
%    groupFD2test: Matrix with the testing functions of group 2 as rows.

% OUTPUT VARIABLES:
%    groupMV1: Matrix with the values of the multivariate variable for the training sample of group 1.
%    groupMV1test: Matrix with the values of the multivariate variable for the testing sample of group 1.
%    groupMV2: Matrix with the values of the multivariate variable for the training sample of group 2.
%    groupMV2test: Matrix with the values of the multivariate variable for the testing sample of group 2.

% COMMENTS:
%   (1) By default, our method is implemented, but the user can easily add others.
%   (2) This function can be used outside the framework of this paper.

function [groupMV1, groupMV1test, groupMV2, groupMV2test] =...
    fromFDtoMV(numberFDtoMVmethod, infoFDtoMVmethod, groupFD1, groupFD1test, groupFD2, groupFD2test)

switch numberFDtoMVmethod
  case 1  % Our proposed discriminant variables, based on the differentials or derivatives

    diffOrder = infoFDtoMVmethod{1};
    diffsMode = infoFDtoMVmethod{2};
    numberFDdistance = infoFDtoMVmethod{3};
    numberFDreference = infoFDtoMVmethod{4};
    infoFDreference = infoFDtoMVmethod{5};
    
    n1 = size(groupFD1,1);
    n2 = size(groupFD2,1);
    m1 = size(groupFD1test,1);
    m2 = size(groupFD2test,1);
    T = size(groupFD1,2);
    
    groupMV1 = zeros(n1,1);
    groupMV2 = zeros(n2,1);
    groupMV1test = zeros(m1,1);
    groupMV2test = zeros(m2,1);
    
    % Generating the p-th derivative or differential of the functions
    if ~isequal(diffOrder,0)
      % Generating the p-th derivative or differential of the functions
      switch diffsMode
        case 'derivatives'
          groupFD1 = ((T-1)^diffOrder)*diff(groupFD1,diffOrder,2);
          groupFD1test = ((T-1)^diffOrder)*diff(groupFD1test,diffOrder,2);
          groupFD2 = ((T-1)^diffOrder)*diff(groupFD2,diffOrder,2);
          groupFD2test = ((T-1)^diffOrder)*diff(groupFD2test,diffOrder,2);
        case 'differentials'
          groupFD1 = diff(groupFD1,diffOrder,2);
          groupFD1test = diff(groupFD1test,diffOrder,2);
          groupFD2 = diff(groupFD2,diffOrder,2);
          groupFD2test = diff(groupFD2test,diffOrder,2);
      end
    end
    % Calculating the functional group representative of the two training samples
    refGroupFD1 = referenceElement(numberFDreference,infoFDreference,groupFD1);
    refGroupFD2 = referenceElement(numberFDreference,infoFDreference,groupFD2);
    % Computing the distances for the training data
    groupMV1(:,1) = distancesFD(numberFDdistance,groupFD1,refGroupFD1) - distancesFD(numberFDdistance,groupFD1,refGroupFD2);
    groupMV2(:,1) = distancesFD(numberFDdistance,groupFD2,refGroupFD1) - distancesFD(numberFDdistance,groupFD2,refGroupFD2);
    % Computing the distances for the testing data
    groupMV1test(:,1) = distancesFD(numberFDdistance,groupFD1test,refGroupFD1) - distancesFD(numberFDdistance,groupFD1test,refGroupFD2);
    groupMV2test(:,1) = distancesFD(numberFDdistance,groupFD2test,refGroupFD1) - distancesFD(numberFDdistance,groupFD2test,refGroupFD2);
  
end

end
