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;
}
You are likely doing the zero-padding correctly. Any expectation that the results will be the same after zero-padding is incorrect. All the FFT bin frequencies will instead be related to the larger sized array.