Skip to content

Instantly share code, notes, and snippets.

@tryggvigy
Last active July 25, 2019 19:40
Show Gist options
  • Save tryggvigy/02fc7c3187165ca448ff5c246eb8174a to your computer and use it in GitHub Desktop.
Save tryggvigy/02fc7c3187165ca448ff5c246eb8174a to your computer and use it in GitHub Desktop.
autoCorrelation optimization
function autoCorrelation(arr) {
var ac = new Float32Array(2048);
for (var lag = 0; lag < arr.length; lag++) {
var value = 0;
for (var index = 0; index < arr.length - lag; index++) {
let a = arr[index];
- let b = arr[index-lag];
+ let otherindex = index - lag;
+ let b = otherindex >= 0 ? arr[index-lag] : 0;
value = value + a * b;
}
ac[lag] = value;
}
return ac;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment