Sergio
Sergio

Reputation: 33

Fast Fourier Transform in Objective-C doesn't work fine

I have a method in Objective-C that receives an array of doubles and then it uses the Fast Fourier Transform, however the exit of the FFT doesn't match to what I want.

Can someone help me, I don't know what I'm doing wrong?

This is my method where fftLength is 4096:

-(double(*)) doFFT:(double(*))data{
    double (*fft) = malloc((fftLength * 2) * sizeof(double));

    FFTSetupD fft_weights = vDSP_create_fftsetupD((log2((double)(fftLength))), kFFTRadix2);

    DSPDoubleSplitComplex fftData;
    //fftData.imagp = fftMagnitudes;

    fftData.imagp = (double*)malloc(fftLength * sizeof(double));
    fftData.realp = (double*)malloc(fftLength * sizeof(double));

    for (int i=0; i< fftLength; i++) {
        fftData.realp[i] = (double)data[i];
        fftData.imagp[i] = (double)0.0;

    }

    vDSP_fft_zipD(fft_weights, &fftData, 1, log2((double)(fftLength)), FFT_FORWARD);

    for(int i = 0;i<fftLength * 2;i++){
        fft[i] = fftData.realp[i];
        fft[i+1] = fftData.imagp[i];
    }

    return fft;
}

And this is part of my input data:

[0.0,2.092731423889438E-4, 8.858534436404497E-4,0.0013714427743574675,0.0012678166431137061,-9.650789019044481E-4,-0.002852548808273958,-0.005176802258252122,-0.007281581949909022,-0.00575977878132905,…]

And the result should be:

[21478.183372382526,0.0,-10190.412374839314,…]

But I'm not getting this.

Upvotes: 0

Views: 839

Answers (1)

Paul R
Paul R

Reputation: 212929

This loop is wrong:

for(int i = 0;i<fftLength * 2;i++){
    fft[i] = fftData.realp[i];
    fft[i+1] = fftData.imagp[i];
}

Assuming you want interleaved real/complex output data then it should be:

for(int i = 0; i < fftLength; i++) {
    fft[i * 2] = fftData.realp[i];
    fft[i * 2 + 1] = fftData.imagp[i];
}

Upvotes: 1

Related Questions