0

I am using Intel IPP's FFT (Fast Fourier Transform) function in my project. But my input data size is changeable. But IPP's FFT function allows only array with length of power of 2. So i tried to apply zerro padding for to make array power of 2. But it gives wrong results (i compare it with different programs, like MATLAB etc.). My code is;

#include <ipp.h>

// N is input array size

void IPP_fft(int N){

Ipp32fc* src;  //FFT input array
Ipp32fc* dst;  //FFT output array

int powerOf2Sz = pow(2,ceil(log2(N))); //Finding input's upper power of 2 value
                                      //if it's equal to power of 2 it's equal to N

int orderOfFFT = (int)(log((double)powerOf2Sz) / log(2.0));

src = ippsMalloc_32fc(powerOf2Sz);
dst = ippsMalloc_32fc(powerOf2Sz);

//set input with variable

for(int i = 0; i<N;i++){
   src[i].re = i+1;
   src[i].im = 0;
}

//do zero padding for rest of size

for(int i = N; i<powerOf2Sz;i++){
   src[i].re = 0;
   src[i].im = 0;
}

IppsFFTSpec_C_32fc* pSpec=0;

Ipp8u* pMemSpec   = 0;
Ipp8u* pMemInit   = 0;
Ipp8u* pMemBuffer = 0;

int sizeSpec   = 0;
int sizeInit   = 0;
int sizeBuffer = 0;

int flag = IPP_FFT_NODIV_BY_ANY;

//get sizes for required buffer
ippsFFTGetSize_C_32fc(FFTOrder, flag, ippAlgHintNone, &sizeSpec, &sizeInit, &sizeBuffer);

//allocate memory for buffer
pMemSpec = (Ipp8u*)ippMalloc(sizSpec);

if(sizeInit > 0){
pMemInit = (Ipp8u*)ippMalloc(sizInit);
}

if(sizeBuffer > 0){
pMemBuffer = (Ipp8u*)ippMalloc(sizBuffer);
}

//Initialize FFT Specification Structures
ippsFFTInit_C_32fc(&pSpec, FFTOrder, flag, ippAlgHintNone, pMemSpec, pMemInit);

//free initialization buffer
if(sizeInit > 0){
ippFree(pMemInit);
}

//perform Forward FFT
ippsFFTFwd_CToC_32fc(src,dst,pSpec,pMemBuffer);

//you can use dst output array

if(sizeBuffer > 0){
ippFree(pMemBuffer);
}

ippFree(pMemSpec);

}

Before i should say, when N is a power of 2 size, results are fine. (And i know after zero padding, results will be different and i look them with right way) Am i doing zero padding in the wrong way? (I tried to add 1 zero at the beginning but it also gives incorrect results) Or just Intel IPP's FFT function doesn't support array with size non-power of 2 even they zero padded? Do i must use another library like Intel MKL?

Hansov
  • 1
  • 5
  • Padding with zeroes essentially introduces a square wave into your signal, which is gonna mess up the frequency buckets. You could resample the data to fit the power-of-two array, and scale the frequencies accordingly, but that'll introduce artifacts from resampling. Probably the easiest thing you can do is use a less restrictive discrete FFT. – paddy Mar 15 '23 at 07:10
  • Thanks for your answer. Actually Intel IPP's DFT function supports non-power of 2 size arrays. But my main issue is speed. So i can't use it. Have you ever used Intel MKL? Is it fit for this problem? I am asking because finding an FFT example of this libraries like look for a needle in a haystack. – Hansov Mar 15 '23 at 07:22
  • Zero-padded DFTs are only equivalent if you eventually do an inverse DFT. You cannot directly compare in the frequency domain. You have to carry out all your computations on the power-of-2 array – Homer512 Mar 15 '23 at 07:27
  • Actually i didn't write here (because it's unrelated with question) but i am doing inverse FFT as well. With `ippsFFTInv_CToC_32fc()`. If input length N is power of 2 it's not a problem. But when i do zero padding and not even change anything then inverse it (As you said to me), it doesn't give input back. I tried it with simplest input. And nothing change. And also i can't change computations either. Thanks anyway. – Hansov Mar 15 '23 at 07:51
  • That doesn't make sense. ```inverse_FFT(FFT(signal))``` must return the original signal (factoring in the scaling factor common in iFFTs and of course numerical noise). It doesn't matter whether the signal was naturally its size or padded. If this condition doesn't hold for you, there is something wrong with your code or your tests – Homer512 Mar 16 '23 at 05:08
  • I know. But i didn't find it. And for this reason I posted my code and the problem :) – Hansov Mar 16 '23 at 06:41
  • Hansov, Could you please send us a working reproducer? – Shanmukh-Intel Apr 13 '23 at 11:44

0 Answers0