Overflow issues with ArduinoFFT real component - Need help

I'm at a loss on how to solve this issue :sweat_smile:. I'm processing samples from a microphone using the ArduinoFFT library functions, but I keep encountering an overflow of the real component (vReal). I've even replaced all real components in the code with a fixed number, but in every iteration, the vReal array still uncontrollably increases for some reason.

Here's the part of the code in the loop:

for (int i = 0; i < SAMPLES; i++) {
    vReal[i] = 0.5;
    vImag[i] = 0.0;
}

Serial.print(String(vReal[0]) + "\t");

FFT.dcRemoval();
Serial.print(String(vReal[0]) + "\t");

FFT.windowing(FFT_WIN_TYP_HAMMING, FFT_FORWARD);
Serial.print(String(vReal[0]) + "\t");

FFT.compute(FFT_FORWARD);
Serial.print(String(vReal[0]) + "\t");

FFT.complexToMagnitude();
Serial.print(String(vReal[0]));

And here is the output showing that the numbers are increasing, causing my microcontroller to crash:
|18:30:53.472 -> 0.50|0.50|0.04|12.99|19.68|19.68|
|---|---|---|---|---|---|
|18:30:54.030 -> 0.50|0.50|0.04|500.93|502.60|502.60|
|18:30:54.525 -> 0.50|0.50|0.04|10780.87|10820.21|10820.21|
|18:30:55.025 -> 0.50|0.50|0.04|173699.20|174900.83|190227.47|
|18:30:55.581 -> 0.50|0.50|0.04|3018009.00|3023591.75|3023591.75|
|18:30:56.092 -> 0.50|0.50|0.04|64106664.00|64131624.00|64131624.00|
|18:30:56.593 -> 0.50|0.50|0.04|1334393856.00|1344235264.00|1344235264.00|
|18:30:57.106 -> 0.50|0.50|0.04|20313952256.00|20314204160.00|ovf|
|18:30:57.620 -> 0.50|0.50|0.04|414506221568.00|414621466624.00|ovf|
|18:30:58.149 -> 0.50|0.50|0.04|6874642513920.00|6897976475648.00|ovf|
|18:30:58.635 -> 0.50|0.50|0.04|125691672657920.00|125765576294399.99|ovf|
|18:30:59.151 -> 0.50|0.50|0.04|2255658304929792.02|2262543405940736.10|ovf|
|18:30:59.705 -> 0.50|0.50|0.04|35762689435238401.71|36074366621974527.36|ovf|
|18:31:00.220 -> 0.50|0.50|0.04|806934263684399155.63|809745233880285120.16|ovf|
|18:31:00.699 -> 0.50|0.50|0.04|16730721982588388385.69|16747550008051499403.90|ovf|
|18:31:01.247 -> 0.50|0.50|0.04|303674802570709179150.29|inf|inf|
|18:31:01.760 -> 0.50|0.50|0.04|nan|nan|0.00|
|18:31:02.248 -> 0.50|0.50|0.04|nan|nan|0.00|
|18:31:02.803 -> 0.50|0.50|0.04|nan|nan|0.00|

Has functions to print large float in engineering / scientific format e.g. 5.46e23
for large numbers.

Thank you for the suggestion, but the issue isn't with printing large numbers. As the title suggests, the problem is an "Overflow issue with ArduinoFFT real component." The numbers shouldn't be this large, and that's the core problem I'm trying to solve.

Any insights on why the vReal values are uncontrollably increasing during each iteration would be greatly appreciated.

1 Like

Think most people need more code to be able to rerun the sketch to analyze the problem.

The problem is in the other part. Please post all the code.

By the way, the following line is just silly. Use of Strings is completely unnecessary, and often causes AVR-based Arduinos to crash.

Serial.print(String(vReal[0]) + "\t");

Instead, write

Serial.print(vReal[0]);
Serial.print("\t");

which is faster and uses significantly less program memory.

You are right, the problem was actually at the very beginning where I defined two SAMPLES (#define SAMPLES and #define SAMPLES_FFT) because I'm processing two different things within the same .ino file, and of course, by mistake I was using wrong one.

Regarding Strings, before I posted the code I want to reduce the number of lines for you whom I asked for help, so I used a single Serial.print statement before posting in order to keep the code concise and easier to read.

In any case, thank you and everyone for help!

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