View Full Version : fast peak detection

02-01-2016, 03:16 PM
I'm doing an SPL meter stoplight, and using analogRead() with a Teensy 3.0, I'm only getting about 390Hz sample rate. I've got analogReadResolution(6) and analogReadAveraging(0), running open loop for 50ms.
I'm not trying to do FFT or anything super fancy - I just need to make sure I don't miss high frequency peaks due to sample rate. I could rectify and filter, but I was hoping to be able to work directly off the mic preamp signal (using an Adafruit MAX4466 electret board). I really only need about 20db dynamic range (85 to 105db SPL). Can I run lower than 8 bits for faster conversion? Does analogReadAveraging(0) actually disable it?

I thought open loop was more like 10khz sample rate - which should be enough for my application.

02-01-2016, 03:34 PM
analogRead should be able to run much faster. If you want help with specific code, please see the "Forum Rule" which appears at the top of every page on this forum!

The other approach would be to use the Teensy Audio library.


I highly recommend watching the tutorial video. If you don't have 48 minutes for the whole thing, perhaps skip to 31:16 where peak detection is demonstrated.

All the audio library examples use the audio shield, but it's pretty easy to modify them to get input from the ADC object instead or I2S. Watch the stuff about the design tool, and use the design tool yourself to see what your other choices are.

02-02-2016, 04:04 PM
Thanks - that's sufficient for the audio analysis portion of my project. But .. I looked at the AudioAnalyzePrint class, and besides verbage saying not to use it - I couldn't seem to get it to tell how many samples had been read. (I'd still like to have a sanity check of the resulting sampling rate.)

02-02-2016, 06:21 PM
If you want to get access to the actual 44.1 kHz data, use the queue objects.

02-09-2016, 02:31 AM
Ok, still getting poor sample rates (about 345 Hz on a 96 Mhz Teensy 3.0) Here's my code:

#include <Audio.h>
#include <Wire.h>

AudioInputAnalog microphone(A9);
AudioRecordQueue rq;
AudioConnection patchCord2(microphone, rq);

void setup()

void loop()
unsigned int samples = 0;
elapsedMillis elapsed = 0;
unsigned long duration = 0;


while (samples < 1000)
int thisread = rq.available();
if (thisread)
samples += thisread;
duration = elapsed;
Serial.print(" samples in ");
Serial.print(" ms = ");
Serial.print((float)samples * 1000 / duration);
Serial.println(" Hz");

Something's not quite right still.

02-09-2016, 12:14 PM
AudioRecordQueue returns 128 sample block, doing the math 345*128=44160 which is aprox the sample rate of the audio library.

02-09-2016, 12:25 PM
Doh. and thank you.