FFT, Spectrum analyzer

Hi, I’m making FFT frequency spectrum analyzer.

I’m writing code to take an input using DaisySeed (currently using A0) and FFT the raw signal so that I can see the dB magnitude at each frequency, but I am having two problem.

  1. No message is displayed when I open the serial monitor window and align the baud rate.

  2. As you can see from the code below, I’m using the Arduino IDE to develop, and the code is messy because I don’t know exactly how to use the classes and functions and remove duplicates.

please check my code and give me some advise.

#include "DaisyDuino.h"
#include "arduinoFFT.h"

#define SAMPLES 256        // 2^n
#define SAMPLE_RATE 96000  
#define FFT_SIZE 2048      // FFT size

DaisyHardware hw;
size_t num_channels;

unsigned int sampling_period_us;
double vReal[SAMPLES];  // input signal
double vImag[SAMPLES];  // set 0

float sample_rate = 96000;

//arduinoFFT FFT = arduinoFFT(vReal, vImag, SAMPLES, SAMPLING_FREQUENCY);
ArduinoFFT<double> FFT = ArduinoFFT<double>(vReal, vImag, SAMPLES, SAMPLE_RATE);

void setup() {
    /*
    hw = DAISY.init(DAISY_SEED, AUDIO_SR_96K);
    num_channels = hw.num_channels;
    sample_rate = DAISY.get_samplerate();
    */
    //patch = DAISY.init(DAISY_PATCH_SM);

    /** Set the samplerate to 96kHz */
    DAISY.SetAudioSampleRate(SaiHandle::Config:: SampleRate::SAI_96KHZ);
    sample_rate = DAISY.get_samplerate();

    /** Set the blocksize to 8 samples */
    DAISY.SetAudioBlockSize(8);

    /** Start the audio callback */
    //AISY.StartAudio(AudioCallback);

    sampling_period_us = round(1000000.0 / sample_rate); 

    Serial.begin(115200);

    //daisy::StartAudio(AudioCallback);
    delay(10); //forced delay
    Serial.println("Daisy FFT Analysis Start");
    delay(1000); // delay for next job

    //DAISY.begin(AudioCallBack);
}

/*
static void AudioCallback(float**  in, float** out, size_t size)
{
    for(size_t i = 0; i < size; i++)
    {
        OUT_L[i] = IN_L[i];
        OUT_R[i] = IN_R[i];
    }
}
*/

void loop() {
    /*
    for (int i = 0; i < FFT_SIZE; i++) {
        vReal[i] = in[0][i];         // audio input0
        vImag[i] = 0.0;
    }
    */
    for (int i = 0; i < FFT_SIZE; i++) {
        vReal[i] = analogRead(A0);     
        vImag[i] = 0.0;
    }

    // FFT
    FFT.windowing(FFTWindow::Hamming, FFTDirection::Forward);	/* Weigh data */
    FFT.compute(FFTDirection::Forward); /* Compute FFT */
    FFT.complexToMagnitude(); /* Compute magnitudes */

    // dB by Frequency
    int targetFrequencies[] = {50, 100, 150, 200, 250, 300, 400, 500, 750, 1000, 2000, 4000, 6000, 8000, 10000, 12000, 14000, 16000, 18000, 20000, 21000};
    int numFrequencies = sizeof(targetFrequencies) / sizeof(targetFrequencies[0]);

    Serial.println("Frequency Magnitudes:");
    for (int i = 0; i < numFrequencies; i++) {
        int index = (targetFrequencies[i] * FFT_SIZE) / sample_rate;
        double magnitude = vReal[index];  // FFT 크기값 가져오기
        double magnitudeDB = 20 * log10(magnitude);  // dB 변환
        Serial.print(targetFrequencies[i]);
        Serial.print(" Hz: ");
        Serial.print(magnitudeDB, 2);
        Serial.println(" dB)");
    }
    Serial.println();
    delay(500);
}

// Not Necessary
void AudioCallback(float *in, float *out, size_t size) {
    out[0] = in[0];
    }

I think it will be easier to debug the first problem once you address the second one. It’s hard to read your code snippet because you have a lot of stuff commented out. Are those the duplicates you are talking about?