javacudafftjcuda

real-to-complex FFT with JCufft


I'm doing a real-to-complex FFT with the org.apache.commons.math3.transform library as following:

private Complex[] fft(double[] values) {
   FastFourierTransformer ffTransformer = new FastFourierTransformer(DftNormalization.STANDARD);
   Complex[] result = ffTransformer.transform(values, TransformType.FORWARD); 
   return result;  
}

This gives me a org.apache.commons.math3.complex array with the result. This works fine.

Now I want to perform exactly the same with the JCufft library. I tried to do to it as following:

private Complex[] fft(double[] values) {
   double inputJCufft[] = values.clone();
   double outputJCufft[] = new double[values.length * 2];
   cufftHandle plan = new cufftHandle();
   JCufft.cufftPlan1d(plan, values.length, cufftType.CUFFT_D2Z, 1);
   JCufft.cufftExecD2Z(plan, inputJCufft, outputJCufft);
   JCufft.cufftDestroy(plan);
   Complex[] result = BaseHelper.getComplexArray(outputJCufft);
   return result;
}

public static Complex[] getComplexArray(double[] input) {
    List<Complex> result = new ArrayList<Complex>();
    for (int i = 0; i < input.length - 1; i = i + 2) {
        result.add(new Complex(input[i], input[i + 1]));
    }
    return result.toArray(new Complex[result.size()]);
}

However, when I'm comparing the results, they differ from each other. What I have not taken into account, what am I doing wrong?

Thanks for your help.


Solution

  • Ok, it was my lack of understanding the FFT...

    I changed the getComplexArray method to the following and now it works fine:

    public static Complex[] getComplexArray(double[] input) {
        Deque<Complex> deque = new LinkedList<Complex>();
        int size = (input.length / 4 + 1) * 2;
        for (int i = 0; i < size; i = i + 2) {
            deque.add(new Complex(input[i], input[i + 1]));
        }
        List<Complex> result = new ArrayList<Complex>(deque);
        deque.removeLast();
        while (deque.size() > 1) {
            result.add(deque.removeLast().conjugate());
        }
        return result.toArray(new Complex[result.size()]);
    }