Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9138239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:13:50+00:00 2026-06-17T09:13:50+00:00

I have been using some code, and I switched the casting to double instead

  • 0

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;
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:13:51+00:00Added an answer on June 17, 2026 at 9:13 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not all that familiar with unmanaged code but have been using some
I have been using the following code for quite some testing now and havent
I have been using this code, but have been running into some memory issues:
I have been given some matlab code compiled using the .net compiler. I can
I have been using InstallJammer for some time, but it's development is now discontinued.
I have been using the AddressBook api of the iPhone for some time now.
Building my Django app, I have been using some code from django-basic-app , in
I have been using m2eclipse for 2 years or so and have now switched
I have been using C# for some time now to make a small game,
I have been using some code to create MTOM by using code from MSDN

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.