0

私はいくつかのコードを使用しており、キャストを複素数変換でバイトではなく double に切り替えましたが、以前は数値があった場合にすべての配列がゼロを返すようになりました。アイデア?

コード:

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;
}
}

複雑なクラス:

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;
}
}
4

1 に答える 1

3

ストリーム読み取りコードに重大な問題があります。読み取りごとに byte[] 全体が埋められると想定しているようです。ただし、ほとんどの場合、そうはなりません。byte[]次に移動する前に、それぞれを完全に埋める必要があります。また、あなたの最後のものbyte[]はおそらく部分的にしか満たされていない可能性が高いため、これも考慮する必要があります.

それ以外は、Complex クラスと FFT クラスの内部で何が起こっているかを知らずに、詳細を説明することはできません。

于 2013-01-17T01:11:18.603 に答える