function matrix = segmentation_to_matrix(segmentation,parameter)
% Map Ground Truth to Frames/Samples
%  Input:
%       - parameter.gt
%       - parameter.numChords
%       - parameter.featureRate
%  Output:
%        - matrix


if nargin<2
   parameter=[]; 
end

if isfield(parameter,'numSamples')==0
   parameter.numSamples = -1;
end

if isfield(parameter,'numChords')==0
   parameter.numChords = 24;
end

if isfield(parameter,'featureRate')==0
   parameter.featureRate = 1;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


numSamples = parameter.numSamples;
numChords = parameter.numChords;
featureRate = parameter.featureRate;
if numSamples == -1
    time_max = max([segmentation.end]);
    numSamples = ceil(time_max*featureRate+1);
end


matrix = zeros(numChords,numSamples);

num = size(segmentation,2);
for n=1:num
    index_start = min(ceil(segmentation(n).start*featureRate + 1),numSamples);
    time_end = segmentation(n).end;
    if floor(time_end) == time_end
        index_stop = min(floor(time_end*featureRate),numSamples);
    else
        index_stop = min(floor(time_end*featureRate+1),numSamples);
     end
    label = segmentation(n).label;
    if label > 0
        matrix(label,index_start:index_stop) = 1;
    end
end

end

