Skip to content

Instantly share code, notes, and snippets.

@jukworks
Created November 8, 2012 06:34
Show Gist options
  • Save jukworks/4037243 to your computer and use it in GitHub Desktop.
Save jukworks/4037243 to your computer and use it in GitHub Desktop.
A Sample Code: Getting a Real FFT of 1D array using jTransform library.
import edu.emory.mathcs.jtransforms.fft.DoubleFFT_1D;
public class FftTest {
public static void main(String[] args) {
double[] input = new double[]{
0.0176,
-0.0620,
0.2467,
0.4599,
-0.0582,
0.4694,
0.0001,
-0.2873};
DoubleFFT_1D fftDo = new DoubleFFT_1D(input.length);
double[] fft = new double[input.length * 2];
System.arraycopy(input, 0, fft, 0, input.length);
fftDo.realForwardFull(fft);
for(double d: fft) {
System.out.println(d);
}
}
}
@lobsteroh
Copy link

thanks, for me this was a critical bit of info to get this working. just a few notes ...

  1. import edu.emory.mathcs.jtransforms.fft.DoubleFFT_1D; is now at ...
    import org.jtransforms.fft.DoubleFFT_1D;
  2. to compile the source you also need ...
    https://github.com/IcmVis/JLargeArrays
  3. for me the resulting output for comparison is ...
    0.7862
    0.0
    -0.8283067304251197
    -0.3991936433800569
    -0.2874
    -0.2348
    0.9799067304251197
    0.09400635661994317
    -0.37379999999999997
    0.0
    0.9799067304251197
    -0.09400635661994317
    -0.2874
    0.2348
    -0.8283067304251197
    0.3991936433800569
  4. to get back to the original data add...
    System.out.println("get back");
    fftDo.complexInverse(fft,true);
    for (double d: fft)
    System.out.println(d);

@bulbatross
Copy link

You are great!! Thank you for this sample!!

@EvgenyYankovskiy
Copy link

This is not entirely correct if you set a signal with a period T = 1 s, that is, the frequency should be 1 Hz, then with this code you will see 2 Hz. More correct would be DoubleFFT_1D fftDo = new DoubleFFT_1D(input.length / 2);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment