-
Notifications
You must be signed in to change notification settings - Fork 0
/
swapCARreverse.m
95 lines (84 loc) · 2.9 KB
/
swapCARreverse.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function filenameReversed = swapCARreverse(filename, medianFile, nChans, addMedian)
% Reverses channel reordering and adds median to each channel (if
% previously subtracted). The reversed dat file is saved under the old name
% but appended with _reversed.
%
% Input: filename should include the extension.
% medianFile is a mat file containing the median trace and the swap
% order.
% nChans is optional. It should be specified only when there are
% extra input channels in the recording file. This helps to
% disambiguate electrode recordings from other input data.
% addMedian - true or false for adding subtracted median. Default is
% true.
%
% Output: filenameReversed is the name of the data file with reversed
% channels.
%
% Should make chunk size as big as possible so that the medians of the
% channels differ little from chunk to chunk.
load(medianFile); %#ok<LOAD>
if nargin < 3
nChans = numel(probe2headstageConf.probeConf.chanMap);
end
if nargin < 4
addMedian = true;
end
swapOrder_temp = swapOrder;
for i = 1:numel(swapOrder_temp)
swapOrder(i) = find(swapOrder_temp == i); %#ok<AGROW>
end
if numel(swapOrder) < nChans
swapOrder = [swapOrder numel(swapOrder)+1:nChans];
end
chunkSize = 1000000;
fid = []; fidOut = [];
d = dir(filename);
nSampsTotal = d.bytes/nChans/2;
nChunksTotal = ceil(nSampsTotal/chunkSize);
try
fid = fopen(filename, 'r');
[pathstr, name, ext] = fileparts(filename);
filenameReversed = [pathstr filesep name '_reversed' ext];
fidOut = fopen(filenameReversed, 'w');
chunkInd = 1;
sampleCount = 0;
while 1
fprintf(1, 'chunk %d/%d\n', chunkInd, nChunksTotal);
dat = fread(fid, [nChans chunkSize], '*int16');
if ~isempty(dat)
datSamples = size(dat,2);
if addMedian && (exist('medianTrace','var') && ~isempty(medianTrace))
median2Add = int16(repmat(medianTrace(sampleCount+1:sampleCount+datSamples),numel(swapOrder_temp),1));
else
median2Add = int16(repmat(zeros(size(sampleCount+1:sampleCount+datSamples)),numel(swapOrder_temp),1));
end
if addMedian && (exist('channelMedian','var') && ~isempty(channelMedian)) %#ok<*USENS,*NODEF>
median2Add = median2Add + repmat(channelMedian(:,chunkInd),1,size(median2Add,2));
end
if numel(swapOrder_temp) < nChans
median2Add = [median2Add; int16(zeros(1,size(median2Add,2)))]; %#ok<AGROW>
end
swappedDat = dat(swapOrder,:)+median2Add;
fwrite(fidOut, swappedDat, 'int16');
chunkInd = chunkInd+1;
sampleCount = sampleCount + datSamples;
else
break
end
end
if ~isempty(fid)
fclose(fid);
end
if ~isempty(fidOut)
fclose(fidOut);
end
catch me
if ~isempty(fid)
fclose(fid);
end
if ~isempty(fidOut)
fclose(fidOut);
end
rethrow(me)
end