in speech-commands/src/dataset.ts [875:944]
export function getValidWindows(
snippetLength: number, focusIndex: number, windowLength: number,
windowHop: number): Array<[number, number]> {
tf.util.assert(
Number.isInteger(snippetLength) && snippetLength > 0,
() =>
`snippetLength must be a positive integer, but got ${snippetLength}`);
if (focusIndex != null) {
tf.util.assert(
Number.isInteger(focusIndex) && focusIndex >= 0,
() =>
`focusIndex must be a non-negative integer, but got ${focusIndex}`);
}
tf.util.assert(
Number.isInteger(windowLength) && windowLength > 0,
() => `windowLength must be a positive integer, but got ${windowLength}`);
tf.util.assert(
Number.isInteger(windowHop) && windowHop > 0,
() => `windowHop must be a positive integer, but got ${windowHop}`);
tf.util.assert(
windowLength <= snippetLength,
() => `windowLength (${windowLength}) exceeds snippetLength ` +
`(${snippetLength})`);
tf.util.assert(
focusIndex < snippetLength,
() => `focusIndex (${focusIndex}) equals or exceeds snippetLength ` +
`(${snippetLength})`);
if (windowLength === snippetLength) {
return [[0, snippetLength]];
}
const windows: Array<[number, number]> = [];
if (focusIndex == null) {
// Deal with the special case of no focus frame:
// Output an array of evenly-spaced windows, starting from
// the first possible location.
let begin = 0;
while (begin + windowLength <= snippetLength) {
windows.push([begin, begin + windowLength]);
begin += windowHop;
}
return windows;
}
const leftHalf = Math.floor(windowLength / 2);
let left = focusIndex - leftHalf;
if (left < 0) {
left = 0;
} else if (left + windowLength > snippetLength) {
left = snippetLength - windowLength;
}
while (true) {
if (left - windowHop < 0 || focusIndex >= left - windowHop + windowLength) {
break;
}
left -= windowHop;
}
while (left + windowLength <= snippetLength) {
if (focusIndex < left) {
break;
}
windows.push([left, left + windowLength]);
left += windowHop;
}
return windows;
}