Reputation: 4306
I have been using some code, and I switched the casting to double instead of bytes in the Complex number conversion, and now all the arrays are returning zeros when there used to be numbers. Ideas?
Code:
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;
}
}
Upvotes: 0
Views: 159
Reputation: 53694
you have a major problem in your stream reading code. you seem to assume that the entire byte[] will be filled on each read. most likely, however, it won't. you need to fully fill each byte[]
before moving to the next. also, your last byte[]
will most likely only be partially full, which you should also take into account.
other than that, no one could give you more details without knowing what's happening inside your Complex class and your FFT class.
Upvotes: 3