-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteChannels.m
70 lines (54 loc) · 1.61 KB
/
deleteChannels.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
function outputFilename = deleteChannels(filename, nChans, chansToDelete, delOriginal)
% The function deletes channels in the recording producing a dat file with
% a fewer number of channels.
%
% Input: filename should include the full path and the extension.
% nChans is the total number of channels in the recording.
% chansToDelete is the vector containing channels to be deleted.
% delOriginal is true if the original file is to be deleted.
% Otherwise it is false.
%
% Output: outputFilename is the name of the data file with fewer channels.
if nargin < 4
delOriginal = false;
end
chunkSize = 1000000;
fid = []; fidOut = [];
d = dir(filename);
nSampsTotal = d.bytes/nChans/2;
nChunksTotal = ceil(nSampsTotal/chunkSize);
chansToKeep = 1:nChans;
for i = 1:numel(chansToDelete)
chansToKeep = chansToKeep(chansToKeep ~= chansToDelete(i));
end
try
[pathstr, name, ext] = fileparts(filename);
fid = fopen(filename, 'r');
outputFilename = [pathstr filesep name '_reduced' ext];
fidOut = fopen(outputFilename, 'w');
chunkInd = 1;
while 1
fprintf(1, 'chunk %d/%d\n', chunkInd, nChunksTotal);
dat = fread(fid, [nChans chunkSize], '*int16');
if ~isempty(dat)
dat = dat(chansToKeep,:);
fwrite(fidOut, dat, 'int16');
else
break
end
chunkInd = chunkInd+1;
end
fclose(fid);
fclose(fidOut);
if delOriginal
delete(filename);
end
catch me
if ~isempty(fid)
fclose(fid);
end
if ~isempty(fidOut)
fclose(fidOut);
end
rethrow(me)
end