data = bz_LoadBinary([basename '.dat'], 'channels', 1, 'nChannels', 4);
load('digitalchannels.mat') % this will load the intan digital inputs
timestamps = digital_on{1,15};
channel1 = data(timestamps);
plot(channel1);

basename_sinaps = basenameFromBasepath
data_sinaps = bz_LoadBinary([basename_sinaps '.dat'], 'channels', 384, 'nChannels', 1024);


 lfp_group{1} = int16(channel1);
 lfp_group{2} = int16(channel2);
  lfp_group{3} = int16(channel3);

  %% 2. Creating .lfp for all signals
samplesCount_lfp = size(lfp_group{1,1},1);
%samplesCount_lfp = min(samplesCount_lfp);
% matching sampling number
% for ii = 1:length(lfp_group)
%     lfp_group{ii} = lfp_group{ii}(1:samplesCount_lfp,:);
% end


% creating lfp file in batches
srLFP = 1250;
disp('Writing lfp file...');
fileID = fopen(strcat(basenameFromBasepath,'merged.lfp'),'a');
samples_win = 10 * srLFP;
cursor = 1;
while cursor + samples_win < samplesCount_lfp
    data_lfp = [];
    for ii = 1:length(lfp_group)
        data_lfp = cat(2,data_lfp,lfp_group{ii}(cursor: cursor + samples_win,:));
    end
    fwrite(fileID,data_lfp','int16');
    cursor = cursor + samples_win + 1;
end
data_lfp = [];
for ii = 1:length(lfp_group)
    data_lfp = cat(2,data_lfp,lfp_group{ii}(cursor: end,:));
end
fwrite(fileID,data_lfp','int16');
fclose(fileID);

disp('Completed lfp file...');

% clear data_lfp lfp_group
% foldername = [filename, '_withMacro'];
% mkdir(foldername);
% movefile(strcat(filename,'_withMacro.lfp'),[foldername filesep strcat(filename,'_withMacro.lfp')])

