Teensy or Arduino; FFT or YIN? frequency detection project

Hi,
finally I had time to test the 13 tuba files I mentioned some posts before :
Below you can download the wav-files I have tested, the wav2sketch files (renamed), a screenshot of serial monitor of any note and the Arduino sketch for Teensy.

Everyone can use these files ad lib.

In general the note detection works well. Sometimes there are some exceptions like on "Tuba_98_G2":
G2 has exactly 97,998 Hz
If I measure with smartphone app "gStrings" I get 97,5Hz.
With Teensy the frequency switches between 96,78Hz and 99,15Hz (With 3 exceptions: 32.71Hz, 110.56Hz and -10.94Hz).
Maybe later I will filter the values through probability and/or I will average them…

Yes Paul it`s clearly evident that you put a lot of work in it. Now I´m only at beginning and I will go on with studying your documentations and postings.

I'm not at all keen on FFT with arduino, as by its nature it is rather a fat algorithm which works best with lots of sample points and several times as much memory as holds them all at once. I rather think that a counter-timer would better suit the arduino, and that you might want to read a bit about interrupts. I rather think that the low pass filter which you might need to avoid triple-blipping waveforms and comparable nastiness, particularly violin, should be changed according to the initial estimate from the counter timer.

Audacity on a pc with many MB of RAM can do FFT quite well

I see that you might have 262kB, and have 44kB for local variables. Whilst that is rather a lot to a 1980's bloke, I think that it still looks small for a heavily parallel algorithm such as FFT.

ad2049q:
Audacity on a pc with many MB of RAM can do FFT quite well

No matter how much processing power and memory you have, FFT is a poor fit for this application. The YIN algorithm is the way to go.

Hello,
as I show in my last post the “notefreq”-app works on my Teensy.
In the meantime I increase my equipment with the AudioAdaptor for Teensy.
https://www.pjrc.com/store/teensy3_audio.html
The goal was to get “notefreq” work in real, through the mic input from AudioAdaptor and not via transformed wav to sketch files.

First step was to attach the microphone and to test it.
A simple task with video tutorial and documentation:

See Part 2-4: Using the Microphone
https://github.com/PaulStoffregen/AudioWorkshop2015/raw/master/workshop.pdf

Code:

// Advanced Microcontroller-based Audio Workshop
//
// https://github.com/PaulStoffregen/AudioWorkshop2015/raw/master/workshop.pdf
// https://hackaday.io/project/8292-microcontroller-audio-workshop-had-supercon-2015
// 
// Part 2-4: Using The Microphone
///////////////////////////////////
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioInputI2S            i2s2;           //xy=64,105
AudioOutputI2S           i2s3;           //xy=301,212
AudioConnection          patchCord1(i2s2, 0, i2s3, 0);
AudioConnection          patchCord2(i2s2, 0, i2s3, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=203,275
// GUItool: end automatically generated code

///////////////////////////////////
void setup() {
  Serial.begin(9600);
  AudioMemory(8);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.6);
  sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
  sgtl5000_1.micGain(30);
  delay(1000);
}

void loop() {
  // do nothing
}

Until this point everything ok. Mic and output to my stereo amp works fine! :slight_smile:

Next step:
Design of note frequency detection via mic.
I used this simply design...Image: Note_Freq_mic1

Code:

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioInputI2S            i2s1;           //xy=246,135
AudioAnalyzeNoteFrequency notefreq1;      //xy=623,182
AudioConnection          patchCord1(i2s1, 0, notefreq1, 0);
// GUItool: end automatically generated code



void setup() {
    AudioMemory(30);
    /*
     *  Initialize the yin algorithm's absolute
     *  threshold, this is good number.
     */
    notefreq1.begin(.15);
   // pinMode(LED_BUILTIN, OUTPUT);

}

void loop() {
    // read back fundamental frequency
    if (notefreq1.available()) {
        float note = notefreq1.read();
        float prob = notefreq1.probability();
        Serial.printf("Note: %3.2f | Probability: %.2f\n", note, prob);
    }
}

Unfortunately with completely silence it prints out a frequency of ca. 83Hz.
Image: Note_Freq_mic1_80Hz Dropbox - Note_Freq_mic1_80Hz.JPG - Simplify your life

To understand the problem better I connect also the output to hear what happens.
Image: Note_Freq_mic_output Dropbox - Note_Freq_mic_output.JPG - Simplify your life

Code:

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioInputI2S            i2s1;           //xy=352,135
AudioAnalyzeNoteFrequency notefreq1;      //xy=600,228
AudioOutputI2S           i2s2;           //xy=626,144
AudioConnection          patchCord1(i2s1, 0, notefreq1, 0);
AudioConnection          patchCord2(i2s1, 0, i2s2, 0);
AudioConnection          patchCord3(i2s1, 0, i2s2, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=525,305
// GUItool: end automatically generated code

//---------------------------------------------------------------------------------------
void setup() {
   AudioMemory(30);
   Serial.begin(9600);

  sgtl5000_1.enable();
  sgtl5000_1.volume(0.6);
  sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
  sgtl5000_1.micGain(30);
  delay(1000);
    /*
     *  Initialize the yin algorithm's absolute
     *  threshold, this is good number.
     */
    notefreq1.begin(.15);
    //pinMode(LED_BUILTIN, OUTPUT);
  
}

void loop() {
    // read back fundamental frequency
    if (notefreq1.available()) {
       float note = notefreq1.read();
       float prob = notefreq1.probability();
        Serial.printf("Note: %3.2f | Probability: %.2f\n", note, prob);
    }
}

On every new line that prints on serial monitor I hear a noise. If I halve the CPU speed from 48Mhz to 24Mhz the detected frequency duplicates to 170Hz and the noise I hear at my loudspeakers is also half as fast.
Why I have this issue? Any elementary error in programming? :frowning:

BR

Hi I repost my question on teensy forum...