CFFT_Q15 validation

cancel
Showing results for 
Search instead for 
Did you mean: 

CFFT_Q15 validation

819 Views
michelgaeta
Contributor I

Hi 

i am trying to use arm_cfft_q15.c from cmsis dsp. 

I could not match Matlab result. 

matalb resul : 

real_fft_result = 40 21 129 259 170 -17 30 136 100 136 30 -17 170 259 129 21
imag_fft_result = 0 53 136 17 -150 -92 76 23 0 -23 -76 92 150 -17 -136 -53

CMSIS result : 

TEST FFT Q15 using dsp_cmsis.
fft q15 result, real part : 1 0 7 15 11 -1 1 8 6 8 2 -1 11 16 7 1 
fft q15 result, Imag part : 0 3 7 0 -10 -6 5 2 0 -2 -5 5 9 -2 -8 -4

Can some one could have a look to the following code 

 

 

/*
* test_fft_q15.c
*
* Created on: 28 déc. 2016
* Author: gaeta
*/
/* ----------------------------------------------------------------------
* Test fft q15 function in comparison to matlab result
*
// reference matlab code
R=[100, 10,-40,-100, 20,50,-10,10, 0,0,0,0,0,0,0,0];
I=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
signal=R+1i*I;
fft_result=fft(signal);
real_fft_result=fix( real(fft_result) )
imag_fft_result=fix( imag(fft_result) )
//Matlab display :
real_fft_result = 40 21 129 259 170 -17 30 136 100 136 30 -17 170 259 129 21
imag_fft_result = 0 53 136 17 -150 -92 76 23 0 -23 -76 92 150 -17 -136 -53

* ------------------------------------------------------------------- */

#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <stdlib.h>
#include "fsl_debug_console.h"
#include "fsl_device_registers.h"
#include "Frdm.h"
#include "arm_math.h"
#include "arm_const_structs.h"


/* ----------------------------------------------------------------------
* Defines each of the tests performed
* ------------------------------------------------------------------- */
#define MAX_BLOCKSIZE 64
/* ----------------------------------------------------------------------
* Declare I/O buffers
* ------------------------------------------------------------------- */
q15_t Ak[MAX_BLOCKSIZE]; /* Input A */
q15_t testInputA [32] =
{100, 0,10,0,-40, 0,-100, 0,20,0,50,0,-10, 00,10, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};


int32_t test_fft_q15(void)
{
uint8_t fftSize = 16;
uint8_t ifftFlag = 0;
uint8_t bitReverseFlag =1;
uint8_t ii;

/* Initialise the fft input buffers with all zeros */
arm_fill_q15(0, Ak, MAX_BLOCKSIZE);

/* Copy the input values to the fft input buffers */
for ( ii=0;ii<16;ii++) Ak[ii]=(q15_t) testInputA[ii];

/* Transform input a[n] from time domain to frequency domain A[k] */
arm_cfft_q15(&arm_cfft_sR_q15_len16, Ak,ifftFlag,bitReverseFlag);

/* Display result */
PRINTF(" fft q15 result, real part :" );
for ( ii=0;ii<32;ii++) PRINTF(" %d",Ak[ii*2] );
PRINTF(" \r\n" );
PRINTF(" fft q15 result, Imag part :" );
for ( ii=0;ii<32;ii++) PRINTF(" %d",Ak[ii*2+1] );
PRINTF(" \r\n" );

return 1;
}

Labels (1)
1 Reply

365 Views
pablocottens
Contributor II

Hello, let me see if I can help with anything. 

First of all ... the matlab code is the input buffer in q15 format as well?? Are you using an algorithm to do the FFT in q15 in there as well?? It just may be using the input data as floating point ... or double ... 32bit integer or who knows. This is something you need to check or else the results will never be the same because since the input buffer is not the same.

I suggest you try doing what I did, create a sine wave with amplitude 1 in float and the use the arm_cfft_f32 function .. create the same wave in matlab and in the C code and then compare their outputs, if you are as lucky as I am, the result will be the same for both ... after that convert the float sine wave from float to q31 and to q15 and then use their respective FFT functions ... and then convert the output to float ... if you have my luck you will notice that the q31 output is the same as the float output, the only thing is that it is dampened by a factor I don't remember now (I think it was 16, 32, 64 or 128 ... as you can see all of them are 2^x so this telles me that I can trust its output) ... but if you are unlucky like me you will notice that the resulting FFT for the q15 algorithm does not match the float nor the q31.

Try doing this by comparing their RMS value so you know that you are doing it right, since the RMS value of a sine wave of amplitude 1 is 0.707 (just divide the amplitude by sqrt(2)), it's much easier that comparing each member.

Also ... if you get the q15 FFT's output and you notice it works ... please tell me your configuration to see if there is something wrong with mine. 

Good luck and I hope I was helpful