2

I have data regarding the redness of the user's finger that is currently quite noisy, so I'd like to run it through an FFT to reduce the noise. The data on the left side of this image is similar to my data currently. I've familiarized myself with the Apple documentation regarding vDSP, but there doesn't seem to be a clear or concise guide on how to implement a Fast Fourier Transform using Apple's vDSP and the Accelerate framework. How can I do this?

I have already referred to this question, which is on a similar topic, but is significantly outdated and doesn't involve vDSP.

Community
  • 1
  • 1
fi12
  • 495
  • 11
  • 30

1 Answers1

3

Using vDSP for FFT calculations is pretty easy. I'm assuming you have real values on input. The only thing you need to keep in mind you need to convert your real valued array to a packed complex array that FFT algo from vDSP uses internally.

You can see a good overview in the documentation:

https://developer.apple.com/library/content/documentation/Performance/Conceptual/vDSP_Programming_Guide/UsingFourierTransforms/UsingFourierTransforms.html

Here is the smallest example of calculating real valued FFT:

const int n = 1024;
const int log2n = 10; // 2^10 = 1024

DSPSplitComplex a;
a.realp = new float[n/2];
a.imagp = new float[n/2];

// prepare the fft algo (you want to reuse the setup across fft calculations)
FFTSetup setup = vDSP_create_fftsetup(log2n, kFFTRadix2);

// copy the input to the packed complex array that the fft algo uses
vDSP_ctoz((DSPComplex *) input, 2, &a, 1, n/2);

// calculate the fft
vDSP_fft_zrip(setup, &a, 1, log2n, FFT_FORWARD);

// do something with the complex spectrum
for (size_t i = 0; i < n/2; ++i) {
    a.realp[i];
    a.imagp[i];
}

One trick is that a.realp[0] is the DC offset and a.imagp[0] is the real valued magnitude at the Nyquist frequency.

Lukáš Lalinský
  • 40,587
  • 6
  • 104
  • 126
  • Quick question - does this example assume that `input` is `n/2` length or `n` length? I'm confused about the size of the arrays in the vDSP_ctoz step? And if it's `n/2` elements, why is it a 1024-sized FFT? – joerick May 08 '19 at 10:30
  • 1
    @joerick The code is calculating real FFT analysis, so `input` is an array with `n` real numbers. vDSP requires you to take the `n` real numbers and pretend they are `n/2` complex numbers. – Lukáš Lalinský May 09 '19 at 19:17
  • so it's a data packing trick! thank you. (those vDSP docs could really use some attention!) – joerick May 10 '19 at 07:54