Skip to content

Commit

Permalink
decompression function - cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
Julie-Fabre committed Sep 18, 2023
1 parent 7a96957 commit 5c80e06
Showing 1 changed file with 7 additions and 47 deletions.
54 changes: 7 additions & 47 deletions decompressData/bc_extractCbinData.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
% using parfor regularly crashed on my computer, so I have
% disabled by default.
% saveFileFolder - where to save your data
% onlySaveSyncChannel - if true, only save the sync channel
% onlySaveSyncChannel - if true, only save the sync channel. This speeds up
% the function
% verbose - if true, dispply information about progress
% ------
% Outputs
Expand Down Expand Up @@ -76,7 +77,7 @@
startEnd(1) = cbinMeta.chunk_bounds(1);
end

if sStartEnd(2) < cbinMeta.chunk_bounds(2)
if sStartEnd(2) > cbinMeta.chunk_bounds(2)
warning(sprintf('samples to read outside of file range, changing end sample from %s to %s',num2str(sStartEnd(2)), num2str(cbinMeta.chunk_bounds(end))))
startEnd(2) = cbinMeta.chunk_bounds(end);
end
Expand All @@ -102,15 +103,6 @@
nSamplesPerChunk = diff(cbinMeta.chunk_bounds(iChunkStart:iChunkEnd+1));
iSampleStart = max(sampleStart - cbinMeta.chunk_bounds(iChunkStart:iChunkEnd), 1);
iSampleEnd = min(sampleEnd - cbinMeta.chunk_bounds(iChunkStart:iChunkEnd), nSamplesPerChunk);
% nSamples we will actually need from each chunk
% nSamplesToExtract = iSampleEnd - iSampleStart + 1;
% these are start and end indices for extracted samples in the output data array
% startIdx = cumsum([1; nSamplesToExtract(1:end-1)]);
% endIdx = cumsum(nSamplesToExtract);

% nSamplesOut = sampleEnd - sampleStart + 1;
% nChannelsOut = numel(chIdx);
% data = zeros(nSamplesOut, nChannelsOut, cbinMeta.dtype);

nChunks = iChunkEnd - iChunkStart + 1;

Expand Down Expand Up @@ -139,7 +131,6 @@
if doParfor
data = cell(nChunks, 1);
parfor iChunk = 1:nChunks
% chunkInd = iChunk + iChunkStart - 1;
% size of expected decompressed data for that chunk
zmatLocalInfo = zmatInfo;
zmatLocalInfo.size = [nSamples(iChunk)*nChannels, 1];
Expand All @@ -153,18 +144,12 @@
decompData = zmat(compData, zmatLocalInfo);
decompData = reshape(decompData, nSamples(iChunk), nChannels);
chunkData = cumsum(decompData(:, allChannelIndices), 1);
% data(startIdx(iChunk):endIdx(iChunk), :) = chunkData(iSampleStart(iChunk):iSampleEnd(iChunk), :);
data{iChunk} = chunkData(iSampleStart(iChunk):iSampleEnd(iChunk), :);
end
dataOut = cell2mat(data);
% if onlySaveSync
% fwrite(fidOut, dataOut, 'int16');
% else
% fwrite(fidOut, dataOut, 'int16');
% end

if onlySaveSyncChannel
error('saving only sync channel not yet implemented in parfor')
%save(decompDataFile, 'syncdata')
else
fwrite(fidOut, dataOut, 'int16');
end
Expand All @@ -175,7 +160,6 @@

zmatLocalInfo = zmatInfo;
zmatLocalInfo.size = [nSamples(iChunk)*nChannels, 1];
% only for one channel : new offset and chunkSizeBytes


% read a chunk from the compressed data
Expand All @@ -190,9 +174,7 @@
data = chunkData(iSampleStart(iChunk):iSampleEnd(iChunk), :);
reshaped_data = reshape(permute(data, [2,1]), [nSamples(iChunk)*nChannels, 1]);

%plot(reshaped_data(end-30000:end))
%plot(alldata(385:385:end))
%

if ~onlySaveSyncChannel
fwrite(fidOut, reshaped_data, 'int16');
else
Expand All @@ -206,31 +188,9 @@
if onlySaveSyncChannel
save(decompDataFile, 'syncdata')
end
% dataOut = cell2mat(data);
% fwrite(fidOut, dataOut, 'int16');
% save in particular locations. open file size
% for iChannel = 1:nChannels % can't decompress channel-wise
%
% zmatLocalInfo = zmatInfo;
% zmatLocalInfo.size = [nSamples(iChunk)*nChannels, 1];
%
%
% % read a chunk from the compressed data
% fid = fopen(fileName, 'r');
% fseek(fid, offset(iChunk), 'bof');
% compData = fread(fid, chunkSizeBytes(iChunk), '*uint8');
% fclose(fid);
% decompData = zmat(compData, zmatLocalInfo);
% decompData = reshape(decompData, nSamples(iChunk), nChannels);
% chunkData = cumsum(decompData(:, chIdx), 1);
% data{iChunk} = chunkData(iSampleStart(iChunk):iSampleEnd(iChunk), :);
% fwrite(fidOut, dataOut, 'int16');
%
%
% end


end
if ~onlySaveSyncChannel
fclose(fidOut);
end
%fidOut_read = fopen(decompDataFile, 'r');

0 comments on commit 5c80e06

Please sign in to comment.