Package edu.purdue.wind.util

Source Code of edu.purdue.wind.util.Spectrum

package edu.purdue.wind.util;

import java.io.IOException;

import edu.purdue.wind.Wave;
import edu.purdue.wind.FFT;
import edu.purdue.wind.WindowFunction;
import edu.purdue.wind.HanningWindow;

public class Spectrum {
    public static void main(String[] args) {
  if (args.length != 2 && args.length != 3) {
      System.err.println("usage: Spectrum <file.wav> <channel> [window]");
      System.exit(1);
  }

  WindowFunction window = null;
  if (args.length == 3) {
      if (args[2].equals("hanning")) {
    window = new HanningWindow();
      } else {
    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]);
  }
    }
}
TOP

Related Classes of edu.purdue.wind.util.Spectrum

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.