Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years ago.
mbed-dsp FFT question
Can someone help me understand why when I do a FFT of a 60Hz test signal, my Maximum BIN is always 2X what it should be? See the code below, I have changed the SAMPLE_RATE to change the BIN resolution and the FFT always comes out as double. In the code below I set the SAMPLE_RATE to 2048 and the FFT size to 2048. That should give me a BIN resolution of 1Hz. So my max BIN location should be 60, but it comes out as 120. I have also tried a 10kHz SAMPLE_RATE that should have resulted in a Max bin of 12 or 13, but it comes out as 25. Am I missing something?
#include "mbed.h" #include "dsp.h" #include "arm_const_structs.h" #define SAMPLE_RATE 2048 #define SAMPLE_SIZE 2048 #define FFT_SIZE SAMPLE_SIZE #define INPUT_FREQ 60 #define INPUT1_PHASE 0 float32_t input1[SAMPLE_SIZE]; float32_t complex1[SAMPLE_SIZE * 2]; float32_t input1_mag[FFT_SIZE]; float32_t input1_maxValue; uint32_t input1_maxIndex = 0; const static arm_cfft_instance_f32 *S1; Serial debug_pc(USBTX, USBRX); int main() { float32_t input1_RMS; int i; debug_pc.baud(115200); /* --- Generate Test Signals --- */ Sine_f32 sine_input1( INPUT_FREQ, SAMPLE_RATE, 1.0, INPUT1_PHASE, SAMPLE_SIZE); sine_input1.generate(input1); for(i=0;i<(SAMPLE_SIZE * 2) + 1;i=i+2) { /* --- Add IMAG value to complex sample --- */ complex1[i] = input1[i]; complex1[i+1] = 0.0; } /* --- Calculate RMS of signal --- */ arm_rms_f32(input1, SAMPLE_SIZE, &input1_RMS); printf("Input1 RMS: %1.4f\r\n", input1_RMS); /* --- Calculate Complex FFT --- */ S1 = & arm_cfft_sR_f32_len2048; arm_cfft_f32(S1, complex1, 0, 1); /* --- Get Max BIN --- */ arm_cmplx_mag_f32(complex1, input1_mag, FFT_SIZE); /* Calculates maxValue and returns corresponding BIN value */ arm_max_f32(input1_mag, FFT_SIZE, &input1_maxValue, &input1_maxIndex); printf("Input1 Max Bin: %d\r\n", input1_maxIndex); printf("Input1 Max Value: %1.4f\r\n", input1_maxValue); for(i=0;i<(SAMPLE_SIZE) + 1;i=i+2) { //Don't display the top half of the FFT BINS printf("BIN %d, RE:%1.4f, IM: %1.4f\r\n", i / 2, complex1[i], complex1[i+1]); }
My problem is on line 40, I was skipping every other input sample. It should be like: