%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (c) Copyright 2014 Hexoskin
% Permission to use, copy, modify, and distribute this software for any 
% purpose with or without fee is hereby granted, provided that the above 
% copyright notice and this permission notice appear in all copies. The 
% software is provided "as is" and the author disclaims all warranties with
% regard to this software including all implied warranties of
% merchantability and fitness. In no event shall the author be liable for
% any special, direct, indirect, or consequential damages or any damages
% whatsoever resulting from loss of use, data or profits, whether in an 
% action of contract, negligence or other tortious action, arising out of
% or in connection with the use or performance of this software.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% convert_source_file    
% This function demonstrates and implements the decoding of binary data
% as downloaded grom the my.hexoskin.com dashboard. Datas are downloaded in
% binary (wav of csv) format from the dashboard. The code converts them to
% a more "human-friendly" format, then saves it as a CSV in the same
% folder.


function convert_source_file(path)
% convert_source_file(path) converts all raw wav or hxd contained in a folder
% to a more "human-friendly" format and saves it as a CSV in the same
% folder
%
%   path is the path to the folder where the wav and hxd are stored
if exist(path, 'dir')
    files = dir(path);
    for i = 1:length(files)
        if length(files(i).name)>3 &&...
            (strcmp(files(i).name(end-2:end), 'wav') || strcmp(files(i).name(end-2:end), 'csv')) &&...
            ~exist(fullfile(path,files(i).name ),'dir') && ...
            isempty(strfind(files(i).name, 'CHAR')) && ...
            isempty(strfind(files(i).name, 'statistics.csv')) 
            if ~(exist(fullfile(path,[files(i).name(1:end-3), 'wav'] ),'file')  && ...
                    exist(fullfile(path,[files(i).name(1:end-3), 'csv'] ),'file'))
                data = load_hx_data(path, files(i).name);
                if ~isempty(data)
                    disp(strcat('Converting file: ',files(i).name))
                    saveCSV(data, path, files(i).name)
                end
            end
        end
    end
    disp('Done')
end


function saveCSV(data, path, filename)
% saveTXT(path, filename, data) receives some data and saves it in CSV
% format. The first column contains the timestamps, and the second column
% contains values, if applicable.
%
%   data is the array output of the decoded data
%   path is the file to the path
%   filename is the name of the file

fileout = strrep(strrep(filename, 'hxd', 'csv'), 'wav', 'csv');
filename = fullfile(path, fileout);
dlmwrite(filename, data, 'delimiter', ',','precision', '%10.8f')


%% Example functions
% The functions below are not used above, they are presented here as
% examples of how to integrate hexoskin data with other programs.

function saveKubiosRR(path, rr)
% example to save the rr interval in a file format that is compatible
% with kubios rr ASCII file format
% Note it may be necessary to use the Kubios "custom ascii file import command",
% otherwise the rr file can be treated as a ECG file
%
%   path is the name of the path where to save the created file
%   rr is the rr intervals array, as decoded by the loadData function

filename = fullfile(path,'nn_kubios.txt');
dlmwrite(filename, rr,'delimiter', ' ')


function saveKubiosECG(path, ECG)
% example to save the ECG  in a file format that is compatible with kubios
% EKG ASCII file format
%
%   path is the name of the path where to save the created file
%   ECG is the ECG array, as decoded by the loadData function

filename = fullfile(path,'ECG_kubios.txt');
dlmwrite(filename, ECG, 'delimiter', ' ')
