HI,
All this is based on the FFT tutorial in the audio tutorial of Teensy with its audio shield.
I'm trying a simple code in order to identifie in real time the predominant frequency in a complexe sound. In a FOR loop, I compare each FFT value at each 43Hz to get at the end of the loop the highest value and then print it in the monitor.
So if I simply ask it to print the value each time it gets a new higher value while being in one step of the loop, the code works and I get in the monitor in each line all the highest frequencies in the sample with the highest one at the end of the line. So for each sample I will get 0 (initiale value in HZ) then 43 (first test) and then some other frequencies that the code founds in the FFT higher than the 43HZ value. I compare the value of the FFT but I print the frequency that correspond to the value.
But I would like only to get the highest value of the sample and print it at the end of the FOR loop. So I tried the program below and it doesn't work (I've put in red my modifications). The monitor only gives "FFT : -" on each line as if the "c" value I test in the code always got to the initial value "0"... I don't understand why. Could someone help me please?
thx!
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioInputI2S i2s2; //xy=280,253
AudioSynthWaveform waveform1; //xy=452,228
AudioMixer4 mixer1; //xy=459,307
AudioAnalyzeFFT1024 fft1024_1; //xy=658,324
AudioOutputI2S i2s1; //xy=659,254
AudioConnection patchCord1(i2s2, 0, mixer1, 0);
AudioConnection patchCord2(i2s2, 1, mixer1, 1);
AudioConnection patchCord3(waveform1, 0, i2s1, 0);
AudioConnection patchCord4(waveform1, 0, i2s1, 1);
AudioConnection patchCord5(mixer1, fft1024_1);
AudioControlSGTL5000 sgtl5000_1; //xy=321,382
// GUItool: end automatically generated code
void setup() {
Serial.begin(9600);
AudioMemory(10);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
sgtl5000_1.micGain(20);
mixer1.gain(0, 0.5);
mixer1.gain(1, 0.5);
mixer1.gain(2, 0.5);
waveform1.begin(0.5,440,WAVEFORM_PULSE);
delay(1000);
}
void loop() {
float c = 0 ;
float a = 0 ;
if (fft1024_1.available()) {
Serial.print("FFT: ");
for (int i=0; i<50; i++) { // 0-25 --> DC to 1.25 kHz
float n = fft1024_1.read(i);
if ( n > a ) {
a = n ;
c = i*43 ;
if (i=50){
printNumber(c);
waveform1.begin(0.5,c ,WAVEFORM_PULSE);
}
}
else{
}
}
Serial.println();
}
else{
waveform1.begin(0,440,WAVEFORM_PULSE);
}
}
void printNumber(float n) {
if (n >= 0.004) {
Serial.print(n, 3);
Serial.print(" ");
} else {
Serial.print(" - "); // don't print "0.00"
}
}
All this is based on the FFT tutorial in the audio tutorial of Teensy with its audio shield.
I'm trying a simple code in order to identifie in real time the predominant frequency in a complexe sound. In a FOR loop, I compare each FFT value at each 43Hz to get at the end of the loop the highest value and then print it in the monitor.
So if I simply ask it to print the value each time it gets a new higher value while being in one step of the loop, the code works and I get in the monitor in each line all the highest frequencies in the sample with the highest one at the end of the line. So for each sample I will get 0 (initiale value in HZ) then 43 (first test) and then some other frequencies that the code founds in the FFT higher than the 43HZ value. I compare the value of the FFT but I print the frequency that correspond to the value.
But I would like only to get the highest value of the sample and print it at the end of the FOR loop. So I tried the program below and it doesn't work (I've put in red my modifications). The monitor only gives "FFT : -" on each line as if the "c" value I test in the code always got to the initial value "0"... I don't understand why. Could someone help me please?
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioInputI2S i2s2; //xy=280,253
AudioSynthWaveform waveform1; //xy=452,228
AudioMixer4 mixer1; //xy=459,307
AudioAnalyzeFFT1024 fft1024_1; //xy=658,324
AudioOutputI2S i2s1; //xy=659,254
AudioConnection patchCord1(i2s2, 0, mixer1, 0);
AudioConnection patchCord2(i2s2, 1, mixer1, 1);
AudioConnection patchCord3(waveform1, 0, i2s1, 0);
AudioConnection patchCord4(waveform1, 0, i2s1, 1);
AudioConnection patchCord5(mixer1, fft1024_1);
AudioControlSGTL5000 sgtl5000_1; //xy=321,382
// GUItool: end automatically generated code
void setup() {
Serial.begin(9600);
AudioMemory(10);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
sgtl5000_1.micGain(20);
mixer1.gain(0, 0.5);
mixer1.gain(1, 0.5);
mixer1.gain(2, 0.5);
waveform1.begin(0.5,440,WAVEFORM_PULSE);
delay(1000);
}
void loop() {
float c = 0 ;
float a = 0 ;
if (fft1024_1.available()) {
Serial.print("FFT: ");
for (int i=0; i<50; i++) { // 0-25 --> DC to 1.25 kHz
float n = fft1024_1.read(i);
if ( n > a ) {
a = n ;
c = i*43 ;
if (i=50){
printNumber(c);
waveform1.begin(0.5,c ,WAVEFORM_PULSE);
}
}
else{
}
}
Serial.println();
}
else{
waveform1.begin(0,440,WAVEFORM_PULSE);
}
}
void printNumber(float n) {
if (n >= 0.004) {
Serial.print(n, 3);
Serial.print(" ");
} else {
Serial.print(" - "); // don't print "0.00"
}
}