0

I am trying to take data from an accelerometer and apply Kiss FFT to the samples. I'm using a Freescale Kinetis FRDM-K22F board. I want to use 64 samples, but when I run the program I get an error saying "kiss fft usage error: improper alloc" I started turning down the sample size and saw that the FFT does work with 32 samples, but giving it 33 samples the program just stops and returns no errors. Giving it any more samples gives similar results.

I played around with how I set up the FFT and followed a few websites and forum posts:

KissFFT output of kiss_fftr

http://digiphd.com/programming-reconstruction-fast-fourier-transform-real-signal-kiss-fft-libraries/

Kiss FFT on a dsPIC33

From what I can see, I haven't done anything different from what the above websites and forums have done. I've included my code below. Any help or advice is greatly appreciated.

void Sample_RUN()
{
    int size = 64;
    kiss_fft_scalar zero;
    memset(&zero,0,sizeof(zero));
    kiss_fft_cpx fft_in[size];
    kiss_fft_cpx fft_out[size];

    kiss_fftr_cfg fft = kiss_fftr_alloc(size*2 ,0 ,NULL,NULL);

    signed short samples[size];

    for (int i = 0; i < size; i++) {
        fft_in[i].r = zero;
        fft_in[i].i = zero;
        fft_out[i].r = zero;
        fft_out[i].i = zero;
    }

    printf("Data Collection Begins \r\n");

for(int j = 0; j < size; j++)
{
    for(;;)
    {
        dr_status = My_I2C_ReadByte(STATUS_REG);
        dr_status = (dr_status & 0x04);
        if (dr_status == 0x04)
        {
            //READING FROM ACCEL OUTPUT DATA REGISTERS
            AccelData[0] = My_I2C_ReadByte(OUT_X_MSB_REG);
            AccelData[1] = My_I2C_ReadByte(OUT_X_LSB_REG);
            AccelData[2] = My_I2C_ReadByte(OUT_Y_MSB_REG);
            AccelData[3] = My_I2C_ReadByte(OUT_Y_LSB_REG);
            AccelData[4] = My_I2C_ReadByte(OUT_Z_MSB_REG);
            AccelData[5] = My_I2C_ReadByte(OUT_Z_LSB_REG);

            // 14-bit accelerometer data
            Xout_Accel_14_bit = ((signed short) (AccelData[0]<<8 | AccelData[1])) >> 2;     // Compute 16-bit X-axis acceleration output value
            Yout_Accel_14_bit = ((signed short) (AccelData[2]<<8 | AccelData[3])) >> 2;     // Compute 16-bit Y-axis acceleration output value
            Zout_Accel_14_bit = ((signed short) (AccelData[4]<<8 | AccelData[5])) >> 2;     // Compute 16-bit Z-axis acceleration output value

            mag_accel = sqrt(pow(Xout_Accel_14_bit, 2) + pow(Yout_Accel_14_bit, 2) + pow(Zout_Accel_14_bit, 2) );

            printf("%d \r\n", mag_accel);

            samples[j] = mag_accel;

            break;

        } // end if
    } // end infinite for
} // end for


for (int j = 0; j < size; j++)
{
     fft_in[j].r = samples[j];
     fft_in[j].i = zero;
     fft_out[j].r = zero;
     fft_out[j].i = zero;
}

printf("Executing FFT\r\n");
kiss_fftr(fft, (kiss_fft_scalar*) fft_in,  fft_out);

printf("Printing FFT Outputs\r\n");
for(int j = 0; j < size; j++)
{
    printf("%d \r\n", fft_out[j].r);
}

kiss_fft_cleanup();
free(fft);
} // end Sample_RUN
Community
  • 1
  • 1
Reggie I
  • 1
  • 1

1 Answers1

1

Sounds like you are running out of memory. I am not familiar with that chip, but perhaps you should be using the last arguments of kiss_fft_alloc so you can skip heap allocation.

Mark Borgerding
  • 8,117
  • 4
  • 30
  • 51
  • Thanks for the tip! I've been trying this but have had no luck. I don't think I'm using the kiss_fft_alloc function correctly. Could you possibly give me an example of how I would use the last two arguments of this function? – Reggie I Mar 20 '15 at 21:58
  • I've sort of fixed this problem. I realized that I'm using the real fft, and that only requires a real component of fft_in to be input to the fft. And also, I declared fft_out to be half of the sample size, because I only need to see up to nyquist frequency. The fft does work, but when it receives very slight movements from the accelerometer, it only outputs 0's or 1's from the fft. I've put these inputs through matlab's fft and the results are as expected; I'm able to see the frequency domain signal I expect. Is there any way I can get around this problem. Thank you. – Reggie I Mar 20 '15 at 23:11