Skip to content

Instantly share code, notes, and snippets.

@tanepiper
Created February 28, 2014 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanepiper/9269203 to your computer and use it in GitHub Desktop.
Save tanepiper/9269203 to your computer and use it in GitHub Desktop.
Any ideas?
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;
};
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;
};
@schmerg
Copy link

schmerg commented Feb 28, 2014

My JS is a bit rusty, but would do something like

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 > maxindex[0]) ? [freq,index] : acc;
  }, [-Infinity, -1]);

  // Only care about sufficiently tall peaks.
  return maxindex[0] > this.options.peakThreshold ? this.indexToFreq(maxindex[1]) : null;
};

@schmerg
Copy link

schmerg commented Feb 28, 2014

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