FFT in ARM Cortex-M0 returns NaN or infinite

807 Views Asked by At

I'm working with a 32-bit ARM Cortex-M0 RedBearLab nRF51822 and I am trying to develop a function to calculate the FFTs of a signal.

I can get a 256 points FFT of a signal with this function, but when I try the 512 points FFT (or more), it returns infinite values and NaN. However, when I use my function in a 32 bit ARM Cortex-M4 Teensy 3.1, it works perfectly.

Here I show my code:

void fourier_transform(double samples[], const int n, const int isign) {
    int nn,mmax,m,j,istep,i;
    float wtemp,wr,wpr,wpi,wi,theta,tempr,tempi;
    //if (n<2 || n&(n-1)) throw("n must be power of 2 in four1");
    nn = n << 1;
    j = 1;

    for (i=1;i<nn;i+=2) {
        if (j > i) {

            double a = samples[j-1];
            samples[j-1] = samples[i-1];
            samples[i-1] = a;
            double b = samples[j];
            samples[j] = samples[i];
            samples[i] = b;
            //SWAP(data[j-1],data[i-1]); //ver o que faz
            //SWAP(data[j],data[i]);
        }
        m=n;
        while (m >= 2 && j > m) {
            j -= m;
            m >>= 1;
        }
        j += m;
    }
    mmax=2;
    while (nn > mmax) {
        istep=mmax << 1;
        theta=isign*(6.28318530717959/mmax);
        wtemp=sin(0.5*theta);
        wpr = -2.0*wtemp*wtemp;
        wpi=sin(theta);
        wr=1.0;
        wi=0.0;
        for (m=1;m<mmax;m+=2) {
            for (i=m;i<=nn;i+=istep) {
                j=i+mmax;
                tempr=wr*samples[j-1]-wi*samples[j];
                tempi=wr*samples[j]+wi*samples[j-1];
                samples[j-1]=samples[i-1]-tempr;
                samples[j]=samples[i]-tempi;
                samples[i-1] += tempr;
                samples[i] += tempi;
            }
            wr=(wtemp=wr)*wpr-wi*wpi+wr;
            wi=wi*wpr+wtemp*wpi+wi;
        }
        mmax=istep;
    }
}

samples[] is the array with my signal and, in the end of FFT, the result is put here. n is the number of points I use to get my FFT.

0

There are 0 best solutions below