-
Notifications
You must be signed in to change notification settings - Fork 12
/
dST_choose_segments.m
51 lines (42 loc) · 2.06 KB
/
dST_choose_segments.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
function [startsSec,stopsSec] = dST_choose_segments(hdr,ftype)
% dt_choose_segments.m
%
% Divide acoustic data into smaller chunks to prevent over-committing memory
% Find a reasonable length of data to handle taking into account that the
% interleaved channels will also be read. To ensure that analysis
% is continuous across a segment, we make sure that the time split
% time is some multiple of the frame rate.
switch ftype
case 1 % wav files
% Figure out how long the file is by summing up all the bytes
starts = 0;
stops = sum(hdr.xhd.byte_length)/hdr.xhd.ByteRate;
fftSize = 2^ceil(log2(hdr.fs * .01));
frameAdvanceSec = fftSize / hdr.fs; % this should now be used instead of
% p.frameLengthSec because it has more precision
chooseMB = 45; % Based upon empirical performance on a 1 GB machine.
chooseSamples = chooseMB * 1024 * 1024 / 8; % change into samples
chooseSec = chooseSamples / hdr.fs; % translate into time
maxTimeSec = frameAdvanceSec * floor(chooseSec/hdr.nch/frameAdvanceSec);
labLength = stops-starts;
segmentsRequired = ceil(labLength/maxTimeSec); % # segments per interval
startsSec = zeros(sum(segmentsRequired), 1);
stopsSec = zeros(sum(segmentsRequired), 1);
newIdx = 1;
% Step through starts, and if multiple segments are required to handle an
% interval, add intermediate starts/stops, so you end up with smaller
% chunks.
for oldIdx = 1:length(starts)
for k=1:segmentsRequired(oldIdx)
startsSec(newIdx) = starts(oldIdx) + ((k-1)*maxTimeSec);
stopsSec(newIdx) = min(startsSec(newIdx)+ maxTimeSec-1/hdr.fs, stops(oldIdx));
newIdx = newIdx+1;
end
end
case 2 % xwav files
dnum2sec = 60*60*24;
starts = hdr.raw.dnumStart;
stops = hdr.raw.dnumEnd;
startsSec = (starts - starts(1)).*dnum2sec;
stopsSec = (stops - starts(1)).*dnum2sec;
end