Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

FFT and FormatConverterStream #2089

Answered by pschatzmann
hdsjulian asked this question in Q&A
Discussion options

Full code below.

So my simple "take i2s input and detect main frequency" script sort of works fine, except that when i actually input stuff it gets me outputs like this

58.59 ovf => AS1 diff: 0.32
58.59 ovf => AS1 diff: 0.32
58.59 ovf => AS1 diff: 0.32

I was assuming this might have to do with the fact that AudioRealFFT can't handle 32bit samples?
So i decided to use FormatConverterStream (commented out in the code below) , but with that i don't get any output at all...
What might I be doing wrong?

#include <Arduino.h>
#include <AudioTools.h>
#include "AudioTools/AudioLibs/AudioRealFFT.h"
// I2S pins
#define I2S_BCK_PIN 14 // Bit Clock (BCK)
#define I2S_LRCK_PIN 15 // Left-Right Clock (LRCK)
#define I2S_DATA_PIN 12 // Data (DOUT)
#define CHANNELS 1
#define IN_SAMPLES_PER_SECOND 96000
#define IN_BITS_PER_SAMPLE 32
//Non Format Converter Stream Version
#define OUT_SAMPLES_PER_SECOND 96000
#define OUT_BITS_PER_SAMPLE 32\
//Format Converter Stream Version
//#define OUT_SAMPLES_PER_SECOND 44100
//#define OUT_BITS_PER_SAMPLE 16
I2SStream i2sStream;
AudioInfo from(96000, 1, 32);
AudioInfo to(44100, 1, 16);
AudioRealFFT afft;
//FormatConverterStream Version
// FormatConverterStream conv(afft);
//StreamCopy copier(conv, i2sStream);
//Non Format Converter Stream Version
StreamCopy copier(afft, i2sStream);
int bin_idx = 0;
// Buffer for audio samples
void fftResult(AudioFFTBase &fft){
 float diff;
 auto result = fft.result();
 if (result.magnitude>100){
 Serial.print(result.frequency);
 Serial.print(" ");
 Serial.print(result.magnitude); 
 Serial.print(" => ");
 Serial.print(result.frequencyAsNote(diff));
 Serial.print( " diff: ");
 Serial.println(diff);
 }
}
void setup() {
 Serial.begin(115200); // Initialize Serial for Serial Plotter
 AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
 // Configure I2S using AudioTools
 auto cfg = i2sStream.defaultConfig(RX_MODE);
 cfg.bits_per_sample = IN_BITS_PER_SAMPLE; // 16-bit samples
 cfg.channels = CHANNELS; // Mono
 cfg.sample_rate = IN_SAMPLES_PER_SECOND; // Sample rate
 cfg.is_master = false; // Slave mode
 cfg.i2s_format = I2S_STD_FORMAT;
 cfg.use_apll = true;
 cfg.pin_bck = I2S_BCK_PIN;
 cfg.pin_ws = I2S_LRCK_PIN;
 cfg.pin_data_rx = I2S_DATA_PIN;
 auto tcfg = afft.defaultConfig();
 tcfg.length = 8192;
 tcfg.channels = CHANNELS;
 tcfg.sample_rate = OUT_SAMPLES_PER_SECOND;
 tcfg.bits_per_sample = OUT_BITS_PER_SAMPLE;
 tcfg.callback = &fftResult;
 afft.begin(tcfg);
 i2sStream.begin(cfg); // Start I2S
 //Format Converter Stream Version
 //conv.begin(from, to);
}
void loop() {
 copier.copy(); 
 }
You must be logged in to vote

I am usually working with 16 bits, so this has never been tested with 32 bits.
I committed a correction to process the scaled float values

Replies: 1 comment 1 reply

Comment options

I am usually working with 16 bits, so this has never been tested with 32 bits.
I committed a correction to process the scaled float values

You must be logged in to vote
1 reply
Comment options

Thanks! Works like a charm

Answer selected by pschatzmann
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /