gpt4 book ai didi

java - 在 Java 中应用 Hamming,然后对 wav 文件进行 FFT

转载 作者:太空宇宙 更新时间:2023-11-04 10:49:22 24 4
gpt4 key购买 nike

我正在尝试执行汉明运算,然后对 wav 文件执行 FFT。我已经在 python 中实现了相同的功能。如何在 Java 中执行此操作。我已经在给定的波形文件上应用了汉明,它返回一个 bytearrayOutputStream。现在如何对该 byteArrayOutputStream 执行 FFT?我是音频处理新手。我当前的代码是:

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;


import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

public class AudioFFT {

public static void main(String[] args) throws UnsupportedAudioFileException, IOException {


String wavFilePath="C:\\abc.wav";
ByteArrayOutputStream byteArrayOutputStream=applyHamming(wavFilePath);
byte[] bytesOut=byteArrayOutputStream.toByteArray();
System.out.println(Arrays.toString(bytesOut));
}

public static ByteArrayOutputStream applyHamming(String filePath)

{
// TODO Auto-generated method stub

ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
File fileIn = new File(filePath);
AudioInputStream audioInputStream;
try {
audioInputStream = AudioSystem.getAudioInputStream(fileIn);
int bytesPerFrame = audioInputStream.getFormat().getFrameSize();
if (bytesPerFrame == AudioSystem.NOT_SPECIFIED) {
bytesPerFrame = 1;
}
int numBytes = 1024 * bytesPerFrame;
byte[] audioBytes = new byte[numBytes];
int numBytesRead = 0;

while ((numBytesRead = audioInputStream.read(audioBytes, 0, audioBytes.length)) != -1) {

outputStream.write(audioBytes, 0, numBytesRead);
}

} catch (UnsupportedAudioFileException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return outputStream;
}


private static int BITS_IN_BYTE = 8;

private static AudioInputStream audioInputStream;
private static AudioFormat format;
final static int W = 1024;

public static void getFFT() {
String wavFilePath="C:\\abc.wav";;
File AudioFile = new File(wavFilePath);

ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedInputStream in;

try {
audioInputStream = AudioSystem.getAudioInputStream(AudioFile);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
format = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

if (!AudioSystem.isLineSupported(info)) {
System.out.println("Error");
}

TargetDataLine line = null;
try {

line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);

} catch (LineUnavailableException ex) {
System.out.println("Error");
}

line.start();
byte[] data = new byte[W * format.getSampleSizeInBits() / BITS_IN_BYTE];
double[] inbuf = new double[W];
double[] fftbuf = new double[W];

try {
in = new BufferedInputStream(new FileInputStream(AudioFile));
int read;
while ((read = in.read(data)) > 0) {
out.write(data, 0, read);
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

data = out.toByteArray();

decode(data, inbuf);

fft(inbuf, fftbuf);

}

public static void decode(byte[] input, double[] output) {
assert input.length == 2 * output.length;
for (int i = 0; i < output.length; i++) {
output[i] = (short) (((0xFF & input[2 * i + 1]) << 8) | (0xFF & input[2 * i]));
output[i] /= Short.MAX_VALUE;
}
}

public static void fft(final double[] inputReal, double[] inputImag) {
assert inputReal.length == 2 * inputImag.length;
int n = inputReal.length;
double ld = Math.log(n) / Math.log(2.0);

if (((int) ld) - ld != 0) {
System.out.println("The number of elements is not a power of 2.");
}
int nu = (int) ld;
int n2 = n / 2;
int nu1 = nu - 1;
double[] xReal = new double[n];
double[] xImag = new double[n];
double tReal, tImag, p, arg, c, s;

double constant;
if (true){
constant = -2 * Math.PI;
}

for (int i = 0; i < n; i++) {
xReal[i] = inputReal[i];
xImag[i] = inputImag[i];
}

int k = 0;
for (int l = 1; l <= nu; l++) {
while (k < n) {
for (int i = 1; i <= n2; i++) {
p = bitreverseReference(k >> nu1, nu);
arg = constant * p / n;
c = Math.cos(arg);
s = Math.sin(arg);
tReal = xReal[k + n2] * c + xImag[k + n2] * s;
tImag = xImag[k + n2] * c - xReal[k + n2] * s;
xReal[k + n2] = xReal[k] - tReal;
xImag[k + n2] = xImag[k] - tImag;
xReal[k] += tReal;
xImag[k] += tImag;
k++;
}
k += n2;
}
k = 0;
nu1--;
n2 /= 2;
}

k = 0;
int r;
while (k < n) {
r = bitreverseReference(k, nu);
if (r > k) {
tReal = xReal[k];
tImag = xImag[k];
xReal[k] = xReal[r];
xImag[k] = xImag[r];
xReal[r] = tReal;
xImag[r] = tImag;
}
k++;
}

double[] newArray = new double[xReal.length * 2];
double radice = 1 / Math.sqrt(n);
for (int i = 0; i < newArray.length; i += 2) {
int i2 = i / 2;
newArray[i] = xReal[i2] * radice;
newArray[i + 1] = xImag[i2] * radice;
}

for (int i = 0; i < newArray.length; i++) {
System.out.println("Array: " + newArray[i]);
}

}

private static int bitreverseReference(int j, int nu) {
int j2;
int j1 = j;
int k = 0;
for (int i = 1; i <= nu; i++) {
j2 = j1 / 2;
k = 2 * k + j1 - 2 * j2;
j1 = j2;
}
return k;
}
}

最佳答案

bytesOut 包含您的 wav 文件数据(经过Hamming window 函数修改后)。这些数据表示您应发送到 fft 方法的实数部分 (inputReal)。对于虚部,创建一个与 inputReal 大小相同的数组并用零填充它

//为虚部创建一个数组(我假设长度为1024)

double[] imgBytesOut = new double[1024]; //imgBytesOut is not a good name for this
for(int i=0; i<1024;i++)
imgBytesOut[i] = 0;

现在您已拥有调用 fft 所需的一切

fft(bytesOut, imgBytesOut);

您的 fft 方法填充 xRealxImg 数组,但由于您在本地声明了它们,因此您不会能够在 fft 完成后使用它们(将它们声明为静态全局变量)。

此外,如果您的文件包含 10000 个样本,并且您的 fft 大小 的长度为 1024 个样本(outBytesimgBytesOut 的长度为 1024 个样本)您必须重复调用 fft 才能处理整个文件。为了获得最佳结果,您仍然需要应用重叠(例如,对于 50% 重叠和 1024 的 fft 大小,您需要处理样本 1-1024,然后是 512-1536,然后是 1024-2048 等等)。

关于java - 在 Java 中应用 Hamming,然后对 wav 文件进行 FFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48004859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com