I am using iOS Accelerate framework for finding FFT of a 2D array. The code below works correctly only for power of 2 images. We have to pad input arrays with zeros for non power of 2 images. But I am not able to do the padding correctly. Currently I pad arrays like below
float inputImg2D[3][3] = { 1,1,1, 1,1,1, 1,1,1 };
float paddedImg2D[4][4] = { 1,1,1,0, 1,1,1,0, 1,1,1,0, 0,0,0,0 };
float expectedOutput[6]6] = { 9,0,0,0,0,0
0,0,0,0,0,0
0,0,0,0,0,0
0,0,0,0,0,0
0,0,0,0,0,0
0,0,0,0,0,0 };
For a 4*4 array, I am correctly get output as 8*8 array with value 16 at (0,0).
Accelerate FFT Code.
/*
* 2D fft sample working only for power of 2 images.
* expected output for below 3*3 array is a 6*6 array with value 9 at ( 0,0) - all other values will be zero
* expected output for below 4*4 array is a 8*8 array with value 16 at (0,0) - all other values will be zero
*/
#include <stdio.h>
#include "Accelerate/Accelerate.h"
#define NON_POWER_OF_2_TEST_WILL_FAIL
int main(int argc, const char * argv[]) {
#ifdef NON_POWER_OF_2_TEST_WILL_FAIL
const int IMG_ROWS = 3;
const int IMG_COLS = 3;
float img2D[3][3] = { 1,1,1, 1,1,1, 1,1,1 };
#else
const int IMG_ROWS = 4;
const int IMG_COLS = 4;
float img2D[4][4] = { 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1 };
#endif
/* build necessary values for fft setup */
int maxDimension = ( IMG_ROWS > IMG_COLS ) ? IMG_ROWS : IMG_COLS;
int optimalDftSize = ceil( log2( maxDimension) );
/* fft setup */
FFTSetup fftSetup = vDSP_create_fftsetup( optimalDftSize, FFT_RADIX2 );
/* expand images to power of two size with zero values*/
COMPLEX_SPLIT in_fft;
int optimalDftWidth = 1 << optimalDftSize;
int optimalDftHeight = 1 << optimalDftSize;
int numElements = optimalDftWidth * optimalDftHeight;
in_fft.realp = ( float* ) calloc ( numElements, sizeof(float) );
in_fft.imagp = ( float* ) calloc ( numElements, sizeof(float) );
/* assign image pixels if only in range */
for ( int i = 0; i < optimalDftWidth; i++ ) {
for ( int j = 0; j < optimalDftHeight; j++ ) {
if (i < IMG_ROWS && j < IMG_COLS) {
in_fft.realp[i * optimalDftHeight + j] = img2D[i][j];
//in_fft.imagp[i] = 0.0;
}
}
}
/* do fft in place */
int rowStride = 1;
int columnStride = 0;
vDSP_fft2d_zip(fftSetup, &in_fft, rowStride, columnStride, optimalDftSize, optimalDftSize, FFT_FORWARD);
/* print results */
for(int i=0; i < optimalDftWidth; ++i) {
for(int j=0; j < optimalDftHeight; ++j) {
printf (" %.2f, %.2f, ", in_fft.realp[i*optimalDftHeight+j], in_fft.imagp[i*optimalDftHeight+j] );
}
printf("\n");
}
/* TODO: free resources */
return 0;
}
expectedOutput( as in question). For a 3*3 input array of all 1's, I am expecting a result of all zeros except a value of 9 at (0,0) in result array. Is my expectation wrong ? I assume the functionvDSP_fft2d_zipexpects padded zeros. - kiranpradeepvDSP_fft2d_zipdoesn't know that you're giving it padded data - it just blindly processes the data as a 4x4 FFT, so you get an interpolated 4x4 output. You'll need to use an FFT that supports non-power-of-2 sizes such as 3x3 (e.g. FFTW) or re-think whatever it is that you are trying to achieve with this operation. - Paul Rn*ndft with Accelerate framework, wherencannot be expressed as power of 2,3 or 5. The second part about rethinking also helped, since I was planning to do correlations with Accelerate which didn't really need the exact n*m dft. I will follow theconvolveDFTalgorithm as in docs.opencv.org/modules/core/doc/operations_on_arrays.html#dft. This comment has helped and let me know if could write it as an answer to be accepted. Or else I could do the same. - kiranpradeep