0
votes

I am trying to compute the Inverse FFT, given that I want only select frequencies. Here is how I am taking the FFT:

final double[] points = reader.readPoints();
final DoubleFFT_1D analyzer = new DoubleFFT_1D(points.length);
final double[] fft = new double[points.length * 2];

for (int i = 0; i < points.length; i++) {
    fft[2 * i] = points[i];
    fft[2 * i + 1] = 0;
}

analyzer.complexForward(fft);

As seen above, I am making a complex number from each point.

I am then calculating my power levels, for a frequency:

final double[] magnitude = new double[fft.length];
for (int i = 0; i < fft.length / 2; i++) {
    magnitude[2 * i] = Math.sqrt(Math.pow(fft[2 * i], 2) + Math.pow(fft[2 * i + 1], 2));
    magnitude[2 * i + 1] = 0;
}

From the generated graph, I can see a spike of interest, at an approximated frequency of 0.2717391304347826 . Let's infer that this Double value is stored in a variable called frequencyOfInterest;

I am attempting to take the iFFT as follows:

public void performIfft() {
    int ifftIdx = 0;
    for (int idx = 0; idx < magnitude.length; idx += 2) {
        final Double currentBinFreq = getFrequency(magnitude.length, idx);
        final boolean freqMatch = currentBinFreq.compareTo(frequencyOfInterest) == 0;
        ifft[ifftIdx] = Double.valueOf(freqMatch ? fft[ifftIdx] : 0);
        ifft[ifftIdx + 1] = Double.valueOf(freqMatch ? fft[ifftIdx + 1] : 0);
        ifftIdx += 2;
    }
    analyzer.complexInverse(ifft, true);
}

private static Double getFrequency(final int pointsLength, final int idx) {
    final Double sampleCount = Double.valueOf(idx * 30);
    final Double n = Double.valueOf(pointsLength);
    return Double.valueOf(sampleCount / n);
}

After performing this, my complex array is as follows:

0.0,0.0
0.0,0.0
-6126.10186299952,-6126.10186299952
-3385.1822332667743,-3385.1822332667743
0.0,0.0
[ 0.0,0.0 continues until the end ]

And my result looks as follows on a graph: enter image description here

While the frequency output seems to be correct ( 5 waves in my graph ), I do not understand why there are two ( Img and real ). Alike when I made the FFT entryset, I'd expect the imaginary counterparts to be zeros; i.e 2 * i + 1 = 0.

Did I do something wrong in taking out the iFFT using this library?

1

1 Answers

2
votes

To get a strictly real result (all imaginary components equal to zero), the input to an IFFT must be conjugate symmetric. e.g. the upper half of the array have to be complex conjugates of the lower half, mirrored around the center of the array, except for the zero-eth element (the DC or 0 Hz bin).