Created
February 28, 2014 11:02
-
-
Save tanepiper/9269203 to your computer and use it in GitHub Desktop.
Any ideas?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Listener.prototype.getPeakFrequency = function() { | |
// Find where to start. | |
var start = this.freqToIndex(this.options.codec.options.minFreq); | |
// TODO: use first derivative to find the peaks, and then find the largest peak. | |
// Just do a max over the set. However this doesn't seem to be reliable | |
var max = _.max(this.frequencies); | |
if (max > this.options.peakThreshold) { | |
return this.indexToFreq(_.indexOf(this.frequencies, max)); | |
} | |
return null; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Listener.prototype.getPeakFrequency = function() { | |
var start = this.freqToIndex(this.options.codec.options.minFreq); | |
var max = -Infinity; | |
var index = -1; | |
for (var i = start; i < this.frequencies.length; i++) { | |
if (this.frequencies[i] > max) { | |
max = this.frequencies[i]; | |
index = i; | |
} | |
} | |
// Only care about sufficiently tall peaks. | |
if (max > this.options.peakThreshold) { | |
return this.indexToFreq(index); | |
} | |
return null; | |
}; |
Ooops, try again (bug in the if() inside _.reduce
Listener.prototype.getPeakFrequency = function() {
var start = this.freqToIndex(this.options.codec.options.minFreq);
var maxindex = _.reduce(this.frequencies, function(acc,freq,index) {
return (index >= start && freq > acc[0]) ? [freq,index] : acc;
}, [-Infinity, -1]);
// Only care about sufficiently tall peaks.
return maxindex[0] > this.options.peakThreshold ? this.indexToFreq(maxindex[1]) : null;
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My JS is a bit rusty, but would do something like