我一直在使用一些代码,并且在复数转换中将转换转换为双精度而不是字节,现在所有数组在过去都是数字时都返回零。有想法吗?

码:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class Decoder implements Runnable {
public AudioInputStream din;
public File decoding;
BufferedWriter write = new BufferedWriter(new FileWriter("STDOUT.txt"));

public Decoder(File f) throws Exception {
    AudioInputStream in = AudioSystem.getAudioInputStream(f);
    AudioFormat baseFormat = in.getFormat();
    AudioFormat decodedFormat = new AudioFormat(
            AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(),
            16, baseFormat.getChannels(), baseFormat.getChannels() * 2,
            baseFormat.getSampleRate(), false);
    din = AudioSystem.getAudioInputStream(decodedFormat, in);
    decoding = f;
}

@Override
public void run() {
    byte[] buf = new byte[2048];
    ArrayList<byte[]> bytes = new ArrayList<byte[]>();
    int numBytesRead;
    int total = 0;
    try {

        while ((numBytesRead = din.read(buf)) != -1) {
            if (Converter.abort)
                break;
            System.out.println("Read " + numBytesRead);
            total += numBytesRead;
            bytes.add(buf);
            buf = new byte[2048];
        }
        for (byte b : buf)
            System.out.print(b + "-"); //No matter how I choose the array, all the bytes are zeros.
        System.out.println("Total read: " + total + ". Amt of arrays: "
                + bytes.size());
        ArrayList<double[]> fft_out = doFFT(bytes);
        System.out.println("Writing bytes to FFTOut.txt");
        BufferedWriter write = new BufferedWriter(new FileWriter(
                "FFTOut.txt"));
        for (double[] ba : fft_out) {
            String bout = "";
            for (double b : ba) {
                bout += b + "";
            }
            bout += "\n";
            write.write(bout);
        }
        write.close();
        System.out.println("Done writing");
        // TODO do more
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            din.close();
        } catch (IOException e) {
            System.exit(1);
        }
        Converter.done(decoding.getName());
    }
}

private ArrayList<double[]> doFFT(List<byte[]> bytes) throws Exception {
    for (byte b : bytes.get(6))
        System.out.print(b + "-");
    ArrayList<double[]> dout = new ArrayList<double[]>();
    System.out.println("Amt of arrays: " + bytes.size());
    for (int j = 0; j < bytes.size(); j++) {
        byte[] arr = bytes.get(j);
        Complex[] in = new Complex[arr.length];
        for (int i = 0; i < arr.length; i++) {
            in[i] = new Complex(arr[i], 0);
        }
        Complex[] out = FFT.fft(in);
        double[] rep = new double[out.length];
        for (int i = 0; i < out.length; i++) {
            rep[i] = out[i].re();
        }
        dout.add(rep);
    }
    write.write("Processed " + bytes.size() + " arrays of bytes.");
    return dout;
}
}


Complex.class:

public class Complex {
private final double re; // the real part
private final double im; // the imaginary part

// create a new object with the given real and imaginary parts
public Complex(double real, double imag) {
    re = real;
    im = imag;
}

// return a string representation of the invoking Complex object
public String toString() {
    if (im == 0)
        return re + "";
    if (re == 0)
        return im + "i";
    if (im < 0)
        return re + " - " + (-im) + "i";
    return re + " + " + im + "i";
}

// return abs/modulus/magnitude and angle/phase/argument
public double abs() {
    return Math.hypot(re, im);
} // Math.sqrt(re*re + im*im)

public double phase() {
    return Math.atan2(im, re);
} // between -pi and pi

// return a new Complex object whose value is (this + b)
public Complex plus(Complex b) {
    Complex a = this; // invoking object
    double real = a.re + b.re;
    double imag = a.im + b.im;
    return new Complex(real, imag);
}

// return a new Complex object whose value is (this - b)
public Complex minus(Complex b) {
    Complex a = this;
    double real = a.re - b.re;
    double imag = a.im - b.im;
    return new Complex(real, imag);
}

// return a new Complex object whose value is (this * b)
public Complex times(Complex b) {
    Complex a = this;
    double real = a.re * b.re - a.im * b.im;
    double imag = a.re * b.im + a.im * b.re;
    return new Complex(real, imag);
}

// scalar multiplication
// return a new object whose value is (this * alpha)
public Complex times(double alpha) {
    return new Complex(alpha * re, alpha * im);
}

// return a new Complex object whose value is the conjugate of this
public Complex conjugate() {
    return new Complex(re, -im);
}

// return a new Complex object whose value is the reciprocal of this
public Complex reciprocal() {
    double scale = re * re + im * im;
    return new Complex(re / scale, -im / scale);
}

// return the real or imaginary part
public double re() {
    return re;
}

public double im() {
    return im;
}

// return a / b
public Complex divides(Complex b) {
    Complex a = this;
    return a.times(b.reciprocal());
}

// return a new Complex object whose value is the complex exponential of
// this
public Complex exp() {
    return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re)
            * Math.sin(im));
}

// return a new Complex object whose value is the complex sine of this
public Complex sin() {
    return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re)
            * Math.sinh(im));
}

// return a new Complex object whose value is the complex cosine of this
public Complex cos() {
    return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re)
            * Math.sinh(im));
}

// return a new Complex object whose value is the complex tangent of this
public Complex tan() {
    return sin().divides(cos());
}

// a static version of plus
public static Complex plus(Complex a, Complex b) {
    double real = a.re + b.re;
    double imag = a.im + b.im;
    Complex sum = new Complex(real, imag);
    return sum;
}
}

最佳答案

您在流阅读代码中遇到了主要问题。您似乎假设每次读取都会填充整个byte []。但是,极有可能不会。您需要完全填充每个byte[],然后再移动到下一个。同样,您的最后一个byte[]很可能只会部分充满,这也应考虑在内。

除此之外,没有人可以在不知道您的Complex类和FFT类内部发生什么情况的情况下为您提供更多详细信息。

09-16 07:16