【Urgent Question】  Codewarrior Bug or Code error ?  ---About The FFT function dfr16RFFT

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

【Urgent Question】  Codewarrior Bug or Code error ?  ---About The FFT function dfr16RFFT

4,577 Views
vincent2046
Contributor I
         When  i  use  the  function-dfr16RFFT  in  PE  to  execute  a  FFT.  It  doesn't  work  well,Although  i  strictly  follow  the  instructions  of  PE's  manual  ,It does not work well . My code goes following . i'm wondering what's wrong with it ?  does Anyone can give me an example of the function dfr16RFFT which give the same result of the answer from Matlab  "fft([0.1 0.2 0.3 0.4],8)"
ans =
  1.0000         
 -0.0414 + 0.7243i
 -0.2000 - 0.2000i
  0.2414 + 0.1243i
 -0.2000         
  0.2414 - 0.1243i
 -0.2000 + 0.2000i
 -0.0414 - 0.7243i
 
 
Thanks A lot!!!!!!!
==============
void main(void)
{
 /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
 
 /*** End of Processor Expert internal initialization.                    ***/
 /* Write your code here */
  dfr16_tRFFTStruct *pRFFT;
 Result             res;
 Frac16            pX[]={FRAC16(0.1),FRAC16(0.2),FRAC16(0.3),FRAC16(0.4)};
 UInt16      options = FFT_SCALE_RESULTS_BY_N;
 dfr16_sInplaceCRFFT *pZ=malloc(sizeof(dfr16_sInplaceCRFFT));
 
 PE_low_level_init();
 pRFFT = dfr16RFFTCreate (8, options); /* N = 8 point RFFT */
 res = dfr16RFFT (pRFFT, pX, pZ);
 
 
 dfr16RFFTDestroy (pRFFT);
 
 for(;:smileywink: {}
}
=====================
Labels (1)
Tags (1)
0 Kudos
3 Replies

849 Views
vincent2046
Contributor I
 rocoo,  thanks for  ur reply. But  It's  not a problem of speed or accurate.
i could not get any result from this function [ dfr16RFFT].
 
The function [dfr16RFFT]  always returns -1 which indicate an error.
or it jump to an interrupt vector when i step by step run this program.
 
even more ,i'm confused by the result struct.

typedef struct {

  Frac16   z0;        /* z[0]               (real 16-bit fractional) */

  Frac16   zNDiv2;    /* z[n/2]             (real 16-bit fractional) */

  CFrac16  cz[1];     /* z[1] .. z[n/2 - 1] (complex 16-bit fractional)*/

} dfr16_sInplaceCRFFT;

how could it place z[1] .. z[n/2 - 1] to 2 short CFrac16 variable?

i want to print the result with the command "printf".

for (i=0;i<n/2;i++)

  printf("z[%d]= %f",i,????);

0 Kudos

849 Views
ProcessorExpert
Senior Contributor III

Hello,

it seems to be a problem of memory allocation.

On the line:
dfr16_sInplaceCRFFT *pZ=malloc(sizeof(dfr16_sInplaceCRFFT));

It is not possible to use the sizeof operator, since the structure dfr16_sInplaceCRFFT represents only a beginning of the array of the values (if contains only a first and last element - both real valus(z0 and zNDIv2) and only a beginning of the complex results array CZ that is array of CFrac16 types.
The actual size of the cz[] array differs according to the length of the input.

It should be done the following way: 

dfr16_sInplaceCRFFT *pZ=malloc(2*sizeof(Frac16) + (N/2-1) * sizeof(CFrac16));

The second thing is that the malloc function won't work placed before the
PE_low_level_init(); call, because it uses the DSP_MEM bean.
Do you have DSP_MEM bean correctly configured?
The DSP_MEM bean has to be correctly configured for the proper function of
the dynamic memory allocation. There has to be at least one memory area with
the quialifier INTERNAL_DYNAMIC or EXTERNAL_DYNAMIC.
Please see the applicaiton notes page in the help pages for the DSP_MEM bean.
This may be the reason why the statement

pRFFT = dfr16RFFTCreate (8, options); /* N = 8 point RFFT */

doesn't properly allocate the memory and consequently the dfr16RFFT function
results with -1. Please see the pRFFT value if it is not null.

Regarding the displaying of the values, you can use the following way:


/* first */
printf("z[0]= %f", ((short) (pZ->z0)) / (float)32768);
/* complex values */
for (i=0;i<N/2;i++) {

 printf("z[%d] real = %f",i,((short) (pZ->z[i].real)) / (float)32768));
 printf("z[%d] imag = %f",i,((short) (pZ->z[i].imag)) / (float)32768));
  
}
/* last */
printf("z[N/2]= %f", ((short) (pZ->zNDiv2)) / (float)32768 );

I hope that this will help.

best regards
Petr Hradsky
Processor Expert Support Team
UNIS

0 Kudos

851 Views
rocco
Senior Contributor II
Hi, Vincent:

What do you mean by "it does not work well"? Do you mean it is not as accurate as Matlab? Or do you mean that it takes a lot of coffee breaks and complains a lot? (I do when I have to do an FFT).

Matlab is going to give you a very accurate answer, but a 16-bit fraction is not. The resolution of a 16-bit fraction is approximately 0.00003, ranging from -1.0 to almost 1.0 (actually, 0.99997). When you consider what happens to the coefficients and with all of the multiplications, you can't expect much accuracy. But what you are getting is speed.

If your problem is not with the accuracy, then never mind . . .

Hope that helps.
0 Kudos