% Apply Springer segmentation onto all training files.
% Parameters:
%   out_file    =   A path to the file where the segmented signals get written to.
%   label_file  =   A path to the file where the labels get written to.
%   limit       =   Process no more than 'limit' files. Zero means 'no limit'.
function do_full_segmentation(out_file, label_file, limit)

    tfs = 1000;     % Target sampling rate.
    
    data_path = 'training/';
    folderprefix = 'training-';
    folders = ['a' 'b' 'c' 'd' 'e' 'f'];
    csvfile = 'REFERENCE_withSQI.csv';
    ext = '.wav';
    
    %
    load('Springer_B_matrix.mat');
    load('Springer_pi_vector.mat');
    load('Springer_total_obs_distribution.mat');
    springer_options = default_Springer_HSMM_options;
    springer_options.audio_Fs = tfs;

    numfolders = length(folders);
    
    if(nargin == 0)
        fprintf('No output file specified!\n');
        exit(1);
    end
    
    
    % Get a list of all files in all folders.
    file_list = [];     % Will contain all training files.
    label_list = [];    % Contains all labels. Not yet used.
    for i = 1:numfolders
        curdir = strcat(data_path,folderprefix,folders(i));
        
        fid = fopen(strcat(curdir,filesep,csvfile));
        references = textscan(fid,'%s %f %f','delimiter',',');
        fclose(fid);
        
        files = references{1};
        labels = references{2};
        % merge unsure labels into normal/abnormal
        % -2: unsure normal; +2: unsure abnormal
        labels(references{3} == 0) = 2 * labels(references{3} == 0);
        
        files = strcat(curdir,filesep,files,ext); % Create file path.
        
        file_list = [file_list ; files];
        label_list = [label_list; labels];
    end
    label_list = label_list';
    
    % Save the labels to a file.
    save(label_file, 'label_list');
    
    num_files = size(file_list, 1); % Total number of files.
    
    if(nargin > 1 && limit > 0)
        % If limit is specified:
        fprintf('User defined limit: %d\n', limit);
        num_files = min(limit, num_files);
    end


    patientlist = cell(0);
    summary_features = cell(0);
    segment_lengths = cell(0);
    
    % Segment all files in the list.
    parallel = num_files > 40;
    
    % Use parfor only for large amount of data.
    if parallel
        parfor j = 1:num_files %loop over all files
            curfile = file_list{j};
            
            fprintf('%4.d/%4.d:  %s\n', j, num_files, curfile); % Print progress.
            % append to list
            [heartbeat_list, pat_segment_lengths, pat_summary_features] = do_single_segmentation(curfile);
            patientlist{j} = heartbeat_list;
            segment_lengths{j} = pat_segment_lengths;
            summary_features{j} = pat_summary_features;
        end
    else
        % Single thread.
        for j = 1:num_files %loop over all files
            curfile = file_list{j};
            
            fprintf('%4.d/%4.d:  %s\n', j, num_files, curfile); % Print progress.
            % append to list
            [heartbeat_list, pat_segment_lengths, pat_summary_features] = do_single_segmentation(curfile);
            patientlist{j} = heartbeat_list;
            segment_lengths{j} = pat_segment_lengths;
            summary_features{j} = pat_summary_features;
        end
    end

    % Save the segments.
    save(out_file,'patientlist','segment_lengths','summary_features');
    
    exit();
end