写一个可以直接运行的java代码获取wav文件的频谱
时间: 2023-05-31 19:02:48 浏览: 106
一种解析wav格式文件的代码
以下是一个可以获取wav文件频谱的Java代码示例:
```
import javax.sound.sampled.*;
import java.io.*;
public class WavSpectrumAnalyzer {
public static void main(String[] args) {
String wavFileName = "test.wav"; // 替换为你的wav文件名
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(wavFileName));
byte[] audioBytes = new byte[(int) (audioInputStream.getFrameLength() * audioInputStream.getFormat().getFrameSize())];
audioInputStream.read(audioBytes);
int numChannels = audioInputStream.getFormat().getChannels();
int sampleSize = audioInputStream.getFormat().getSampleSizeInBits();
double sampleRate = audioInputStream.getFormat().getSampleRate();
// 将字节数组转换为double数组
double[] audioData = new double[audioBytes.length / (sampleSize / 8)];
int index = 0;
for (int i = 0; i < audioBytes.length; i += (sampleSize / 8)) {
double sample = 0.0;
if (sampleSize == 8) {
sample = audioBytes[i];
} else {
sample = ((audioBytes[i + 1] & 0xff) << 8) | (audioBytes[i] & 0xff);
}
if (numChannels == 2) {
i++;
}
audioData[index++] = sample / 32768.0;
}
// 计算FFT
int fftSize = 1024;
double[] fftData = new double[fftSize];
Complex[] fftComplex = new Complex[fftSize];
for (int i = 0; i < fftSize; i++) {
fftData[i] = audioData[i];
fftComplex[i] = new Complex(fftData[i], 0.0);
}
fftComplex = FFT.fft(fftComplex);
// 绘制频谱
int width = 800;
int height = 600;
StdDraw.setCanvasSize(width, height);
StdDraw.setXscale(0, sampleRate / 2);
StdDraw.setYscale(-100, 100);
StdDraw.setPenRadius(0.005);
for (int i = 0; i <= fftSize / 2; i++) {
double freq = i * sampleRate / fftSize;
double magnitude = 10 * Math.log10(fftComplex[i].abs() * fftComplex[i].abs() / (fftSize * fftSize));
StdDraw.point(freq, magnitude);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
class Complex {
public double re, im;
public Complex(double real, double imag) {
re = real;
im = imag;
}
public Complex plus(Complex b) {
double real = re + b.re;
double imag = im + b.im;
return new Complex(real, imag);
}
public Complex minus(Complex b) {
double real = re - b.re;
double imag = im - b.im;
return new Complex(real, imag);
}
public Complex times(Complex b) {
double real = re * b.re - im * b.im;
double imag = re * b.im + im * b.re;
return new Complex(real, imag);
}
public double abs() {
return Math.sqrt(re * re + im * im);
}
}
class FFT {
public static Complex[] fft(Complex[] x) {
int N = x.length;
// 偶数项和奇数项分别进行FFT
Complex[] even = new Complex[N / 2];
Complex[] odd = new Complex[N / 2];
for (int k = 0; k < N / 2; k++) {
even[k] = x[2 * k];
odd[k] = x[2 * k + 1];
}
Complex[] q = fft(even);
Complex[] r = fft(odd);
// 合并两个FFT结果
Complex[] y = new Complex[N];
for (int k = 0; k < N / 2; k++) {
double kth = -2 * k * Math.PI / N;
Complex wk = new Complex(Math.cos(kth), Math.sin(kth));
y[k] = q[k].plus(wk.times(r[k]));
y[k + N / 2] = q[k].minus(wk.times(r[k]));
}
return y;
}
}
```
注意,这个代码使用了一个名为StdDraw的类,用于绘制频谱图。如果你没有这个类,可以到官网下载:https://introcs.cs.princeton.edu/java/stdlib/StdDraw.java.html
此外,代码中还使用了一个名为FFT的类,用于实现快速傅里叶变换。如果你对FFT算法不熟悉,可以参考这篇文章:https://www.jianshu.com/p/5a5e5a8c5e5b
最后,如果你想要更好的频谱分析效果,可以尝试使用更大的FFT窗口大小,更高的采样率,以及其他的信号处理技术。
阅读全文