System.err.println("Invalid window function. I know: hanning");
System.exit(1);
}
}
Wave wave;
try {
wave = Wave.readFile(args[0]);
} catch (IOException e) {
throw new RuntimeException(e);
}
short channel = Short.parseShort(args[1]);
if (channel >= wave.channels()) {
System.err.println("Cannot request channel not present in file");
System.exit(1);
}
// Find the highest power of two smaller than wave.samples(),
// the stupid way. Assumes that there *are* samples.
int bits = 0;
for (bits = 0; bits < 32; bits++) {
if (1 << (bits + 1) > wave.samples()) {
break;
}
}
int nSamples = 1 << bits;
double[] samples = new double[nSamples];
int data[] = wave.sampleData(channel);
for (int i = 0; i < nSamples; i++) {
samples[i] = (double)data[i];
}
FFT fft = new FFT(nSamples, window);
fft.transform(samples);
// Output the spectrum
double[] magnitude = fft.magnitudes();
final double stepHz = 2.0d * nSamples / (double)wave.sampleRate();
for (int i = 0; i < nSamples / 2; i++) {
System.out.println("" + (stepHz * i) + " " + magnitude[i]);
}
}