 # How to normalize a vector resulted from cufft

Hi.

I’m using cufft in a project I’m working on. What I need, is to get the result from cufft and normalize it, the same way MATLAB normalizes it’s fft’s. Does anyone have an idea on how to do this? I’m really quite clueless of how to do it. Tried a normal, complex-vector normalization, but it didn’t give the same result.

The normalization algorithm in C.

``````void normalize(cufftComplex *vec, cufftComplex *result, int N)

{

cufftComplex norm;

norm.x = norm.y = 0.0f;

for(int i = 0; i < N; ++i) {

norm.x += (vec[i].x*vec[i].x - vec[i].y*vec[i].y);

norm.y += (vec[i].x*vec[i].y + vec[i].x*vec[i].y);

}

float r = sqrt(norm.x*norm.x + norm.y*norm.y);

float x = norm.x;

float y = norm.y;

norm.x = sqrt((r + x)*0.5);

norm.y = y/sqrt(2*(r+x));

float div = (norm.x*norm.x + norm.y*norm.y);

if(div != 0) {

div = 1.0 / div;

for(int i = 0; i < N; ++i) {

result[i].x = (vec[i].x*norm.x + vec[i].y*norm.y) / div;

result[i].y = (vec[i].x*norm.y - vec[i].y*norm.x) / div;

}

}

}
``````

AFAIK, MATLAB doesn’t perform any normalization on direct FFT and performs (1/N)-normalization (where N is number of elements) on inverse FFT. Check it using some very simple vector, like [1 2 3 4 5].

I normalize with 1/N at the inverse and it works for me.

N in this setting is the length of the vector, right? I do this, but it doesn’t look exactly the same as in MATLAB. So I’m wondering if it could be becouse of the single precision.

Most likely, AFAIK matlab always uses doubles by default :)

N.