I have a question about fft

I've succeeded in converting sound to data using FFT, but I don't know how to convert this data into reverse phase and make sound again. Can I know the code for this?

#include <arduinoFFT.h>

arduinoFFT FFT = arduinoFFT();

const uint16_t samples = 128;
double signalFrequency = 1000;
double samplingFrequency = 5000;
uint8_t amplitude = 100;

double vReal[samples];
double vImag[samples];

#define SCL_INDEX 0x00
#define SCL_TIME 0x01
#define SCL_FREQUENCY 0x02

#define Theta 6.2831 //2*Pi

void setup()
{
  Serial.begin(9600);

}

void loop()
{
  for (uint8_t i = 0; i < samples; i++)
  {
    vReal[i] = analogRead(A0);
    delayMicroseconds(100);
    vImag[i] = 0;
  }
  FFT.Windowing(vReal, samples, FFT_WIN_TYP_HAMMING, FFT_FORWARD); 
  FFT.Compute(vReal, vImag, samples, FFT_FORWARD);
  FFT.ComplexToMagnitude(vReal, vImag, samples); 
  PrintVector(vReal, (samples >> 1), SCL_FREQUENCY);
}

void PrintVector(double *vData, uint8_t bufferSize, uint8_t scaleType)
{
  for (uint16_t i = 2; i < bufferSize; i++)
  {
    uint8_t val_temp = map(vData[i],0,1000,0,255);
    Serial.println(val_temp);
  }
  Serial.println('\n');
}

This is the code for converting sound into data.

Compute the phase shifted sine of each frequency and add up all these amplitudes and output. Do that at the sample rate for the original sound.

I don't know Arduino very well, so can you explain it in more detail? :pray: :pray:

Inverse FFT is a matter of math. Once you understand the math you can start with Arduino basics.

From helicopter view, The theory says that any signal is the sum of multiple sinusoids at different frequency / amplitudes.

The DFT/FFT process helps you divide a number of sample into its components, those single sinusoidal oscillations at distinct frequencies each with their own amplitude and phase.

In a nutshell, to reconstruct the signal (or something close to the signal using the dominant frequencies) you need to sum back all the sinusoids. This is known as ifft (inverse FFT). Because of the sampling process there will be artifacts that you need to take into account

It’s discussed in many places - here is a first Google hit

Why would you want to do fft followed by ifft?
Just to prove it can be done? That is OK!
Usually one would use fft to make spectrum analyzer or something like that. Or maybe as a start of a compression algorithm.
But most arduinos can only hold a few seconds of audio in RAM... also arduino is not likely to be fast enough for fft ifft in real time...

have you tried running it thru the fft again?

After computing the inverse FFT, to make sound, you will need a digital to analog converter of some sort (most Arduinos do no have one), an audio amplifier and speaker.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.