1
\$\begingroup\$

I'm trying to use rfft function in STM32F407G and I am getting wrong results when I compare it with an online fft calculator . I give a sample input as {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32}. My results using the online calculator are as follows -

1)   528 + 0j 
2)  -16 + 162.450726j
3)  -16 + 80.437432j
4)  -16 + 52.744931j
5)  -16 + 38.627417j
6)  -16 + 29.933895j
7)  -16 + 23.945692j
8)  -16 + 19.496056j
9)  -16 + 16j
10) -16 + 13.130861j
11) -16 + 10.690858j
12) -16 + 8.552178j
13) -16 + 6.627417j
14) -16 + 4.853547j
15) -16 + 3.182598j
16) -16 + 1.575862j
17) -16 + 0j
18) -16 - 1.575862j
19) -16 - 3.182598j
20) -16 - 4.853547j
21) -16 - 6.627417j
22) -16 - 8.552178j
23) -16 - 10.690858j
24) -16 - 13.130861j
25) -16 - 16j
26) -16 - 19.496056j
27) -16 - 23.945692j
28) -16 - 29.933895j
29) -16 - 38.627417j
30) -16 - 52.744931j
31) -16 - 80.437432j
32) -16 - 162.450726j

But the results using my code is completely different. I get the following result -

ffttestop[0]    float32_t   528 
ffttestop[1]    float32_t   -16 
ffttestop[2]    float32_t   -19.6766739 
ffttestop[3]    float32_t   125.12075   
ffttestop[4]    float32_t   -22.83638   
ffttestop[5]    float32_t   46.0686264  
ffttestop[6]    float32_t   -24.1503487 
ffttestop[7]    float32_t   25.8768311  
ffttestop[8]    float32_t   -25.7224121 
ffttestop[9]    float32_t   15.1554413  
ffttestop[10]   float32_t   -28.2019958 
ffttestop[11]   float32_t   7.10556698  
ffttestop[12]   float32_t   -32.8137932 
ffttestop[13]   float32_t   -1.21792603 
ffttestop[14]   float32_t   -43.8981056 
ffttestop[15]   float32_t   -14.4978828 
ffttestop[16]   float32_t   -96.4374313 
ffttestop[17]   float32_t   -64.4374313 
ffttestop[18]   float32_t   26.5798321  
ffttestop[19]   float32_t   48.0752258  
ffttestop[20]   float32_t   6.12293482  
ffttestop[21]   float32_t   25.4729309  
ffttestop[22]   float32_t   -0.353181839    
ffttestop[23]   float32_t   16.9155769  
ffttestop[24]   float32_t   -3.09499454 
ffttestop[25]   float32_t   11.9728451  
ffttestop[26]   float32_t   -3.78621292 
ffttestop[27]   float32_t   8.55855846  
ffttestop[28]   float32_t   -1.21792793 
ffttestop[29]   float32_t   6.12293386  
ffttestop[30]   float32_t   29.4866867  
ffttestop[31]   float32_t   6.05591106  

Here is my code -

#include "stm32f4xx.h"
#include "arm_math.h"
#include "arm_const_structs.h"
#include "core_cm4.h"


#define TEST_LENGTH_SAMPLES 32

/* -------------------------------------------------------------------
*  Input and Output buffer Declarations for FFT
* ------------------------------------------------------------------- */
float32_t ffttestip[TEST_LENGTH_SAMPLES]=
{
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
};
static float32_t ffttestop[TEST_LENGTH_SAMPLES];

/* ------------------------------------------------------------------
* Global variables
* ------------------------------------------------------------------- */
uint16_t fftSize = 32;
uint8_t ifftFlag = 0;
uint32_t doBitReverse = 0;


int main(void)
{
    //arm_cfft_instance_f32 * S;
    //arm_cfft_f32(&arm_cfft_sR_f32_len16, ffttestip, ifftFlag, doBitReverse);
    arm_rfft_fast_instance_f32 S;
    arm_rfft_fast_init_f32  (&S,fftSize);
    arm_rfft_fast_f32(&S,ffttestip,ffttestop,ifftFlag);
    //arm_cmplx_mag_f32(ffttestop,ffttestip,fftSize);


  while(1);
 return 0;
}

 /** \endlink */

What is the problem? I am unable to figure it out so it would be great if someone could help.

\$\endgroup\$
  • \$\begingroup\$ I'm seeing float and complex number here. \$\endgroup\$ – Long Pham May 5 '18 at 14:35
  • \$\begingroup\$ yes..I read somewhere that the real part and imaginary part will be stored in alternate indices. \$\endgroup\$ – user3147192 May 5 '18 at 14:39
  • \$\begingroup\$ You should reverse-engineer the function to find out what it do. \$\endgroup\$ – Long Pham May 5 '18 at 14:53
  • \$\begingroup\$ yeah..I tried doing that but the algorithm for fft is really complex..I also went through the documentation here..keil.com/pack/doc/CMSIS/DSP/html/group__RealFFT.html \$\endgroup\$ – user3147192 May 5 '18 at 14:55
  • 1
    \$\begingroup\$ No, if you go through the document, it says the output will return complex numbers. The FFT of real numbers can be complex. This is a quote from the document - "Looking at the data, we see that we can uniquely represent the FFT using only N/2 complex numbers. These are packed into the output array in alternating real and imaginary components: X = { real[0], imag[0], real[1], imag[1], real[2], imag[2] ... real[(N/2)-1], imag[(N/2)-1 }" \$\endgroup\$ – user3147192 May 5 '18 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.