Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: fast peak detection

  1. #1

    fast peak detection

    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.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    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.

  3. #3
    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.)

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    If you want to get access to the actual 44.1 kHz data, use the queue objects.

  5. #5
    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.
    Last edited by swarfrat; 02-09-2016 at 11:56 AM.

  6. #6
    Senior Member
    Join Date
    Oct 2013
    Rogersville MO
    AudioRecordQueue returns 128 sample block, doing the math 345*128=44160 which is aprox the sample rate of the audio library.

  7. #7
    Doh. and thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts