How do I set up a buffer when doing an FFT using the Accelerate framework?

3.9k views Asked by At

I'm using the Accelerate framework to perform a Fast Fourier Transform (FFT), and am trying to find a way to create a buffer for use with it that has a length of 1024. I have access to the average peak and peak of a signal on which I want to do the FFT.

Can somebody help me or give me some hints to do this?

1

There are 1 answers

3
Brad Larson On BEST ANSWER

Apple has some examples of how to set up FFTs in their vDSP Programming Guide. You should also check out the vDSP Examples sample application. While for the Mac, this code should translate directly across to iOS as well.

I recently needed to do a simple FFT of an 64 integer input waveform, for which I used the following code:

static FFTSetupD fft_weights;
static DSPDoubleSplitComplex input;
static double *magnitudes;

+ (void)initialize
{
    /* Setup weights (twiddle factors) */
    fft_weights = vDSP_create_fftsetupD(6, kFFTRadix2);

    /* Allocate memory to store split-complex input and output data */
    input.realp = (double *)malloc(64 * sizeof(double));
    input.imagp = (double *)malloc(64 * sizeof(double));
    magnitudes = (double *)malloc(64 * sizeof(double));
}

- (CGFloat)performAcceleratedFastFourierTransformAndReturnMaximumAmplitudeForArray:(NSUInteger *)waveformArray;
{   
    for (NSUInteger currentInputSampleIndex = 0; currentInputSampleIndex < 64; currentInputSampleIndex++)
    {
        input.realp[currentInputSampleIndex] = (double)waveformArray[currentInputSampleIndex];
        input.imagp[currentInputSampleIndex] = 0.0f;
    }

    /* 1D in-place complex FFT */
    vDSP_fft_zipD(fft_weights, &input, 1, 6, FFT_FORWARD);  

    input.realp[0] = 0.0;
    input.imagp[0] = 0.0;

    // Get magnitudes
    vDSP_zvmagsD(&input, 1, magnitudes, 1, 64);

    // Extract the maximum value and its index
    double fftMax = 0.0;
    vDSP_maxmgvD(magnitudes, 1, &fftMax, 64);

    return sqrt(fftMax);
}

As you can see, I only used the real values in this FFT to set up the input buffers, performed the FFT, and then read out the magnitudes.