0

I'm reading from a wav audio the bytes for each frame. The format that i have a sample Rate = 44100,channels = 2.

     byte[] buffer = new byte[(int) 1024];
     while (running)                                    {
     n++;
     if (n > 400000){break;}                  
     int count = 0;
     count = outDinSound.read(buffer, 0, 1024);
     if (count > 0) {out.write(buffer, 0, count);}      }
     byte b[] = out.toByteArray();
     final int totalSize = b.length;
     int amountPossible = totalSize / 1024;
                                                      *so far all is good*

The next step is to create a complex number from every value that i have in my array and then i call Fast Fourier transfer for these complex numbers

          Complex[][] results = new Complex[amountPossible][];
          for (int times = 0; times < amountPossible; times++) {
                    Complex[] complex = new Complex[1024]; 
                    for (int i = 0; i < 1024; i++)      {
                    complex[i] = new Complex(b[(times * 1024) + i], 0); }
          results[times] = FFT.fft(complex);
                                                                     }

Now i can find out the magnitude for each signal like that

            for (int t = 0; t < results.length; t++) {    
                   for (int freq = 1; freq < 1024; freq++)  {
                       magnitude =results[t][freq].abs();   }
                                                       }

but my final goal is to find out the frequency(or the frequencies_ that exist for that signal).

The complex class that i use is that

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

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

   public String toString() {   // return a string representation of the invoking Complex object
    if (im == 0)    return re + "";
    if (re == 0)    return im + "i";
    if (im < 0) return re + " - " + (-im) + "i";
    return re + " + " + im + "i";   }
           //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     public double abs() {   // return abs/modulus/magnitude and angle/phase/argument
     return Math.hypot(re, im); } // Math.sqrt(re*re + im*im)
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public double phase() {
       return Math.atan2(im, re);   } // between -pi and pi
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       public Complex plus(Complex b) {// return a new Complex object whose value is (this + b)
       Complex a = this; // invoking object
       double real = a.re + b.re;
       double imag = a.im + b.im;
       return new Complex(real, imag);  }
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       public Complex minus(Complex b) {// return a new Complex object whose value is (this - b)
       Complex a = this;
       double real = a.re - b.re;
       double imag = a.im - b.im;
       return new Complex(real, imag);  }
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       public Complex times(Complex b) {// return a new Complex object whose value is (this * 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);  }

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

       public Complex divides(Complex b) {// return a / b
       Complex a = this;
       return a.times(b.reciprocal());  }
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      public Complex exp() {//return a new Complex object whose value is the complex exponential of
return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re)* Math.sin(im));    }
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       public Complex sin() {// return a new Complex object whose value is the complex sine of this
       return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re)* Math.sinh(im));   }
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       public Complex cos() {// return a new Complex object whose value is the complex cosine of this
       return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re)* Math.sinh(im)); }
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       public Complex tan() {// return a new Complex object whose value is the complex tangent of this
       return sin().divides(cos()); }
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       public static Complex plus(Complex a, Complex b) {// a static version of plus
double real = a.re + b.re;
double imag = a.im + b.im;
Complex sum = new Complex(real, imag);
return sum; }

   }

Any idea? i try to take the sin of the signal but steel i cant go to the frequency part.

Aegean
  • 9
  • 5
  • 1
    Many duplicates, e.g. https://stackoverflow.com/questions/16060134/calculate-frequency-from-sound-input-using-fft/16060490#16060490 and https://stackoverflow.com/questions/7674877/how-to-get-frequency-from-fft-result/7675171#7675171 – Paul R Feb 24 '18 at 14:47

1 Answers1

0

FFT_SIZE = Length of your FFT, in your case complex.length or results[x].length or 1024

Number of FFT freqency bins, FFT_BIN_SIZE = FFT_SIZE / 2

FFT_BIN_WIDTH = Sample Rate / FFT_SIZE or Nyquist Frequency / FFT_BIN_SIZE

for (int i = 0; i < results.length; i++) {
    for (int j = 0; j < results[i].length / 2; j++) {
          Frequency = j * FFT_BIN_WIDTH;
          Magnitude = results[i][j].abs();
    }
}

Note that the maximum frequency you can get is the Nyquist Frequency which is the half of your sample rate, and second-half of the FFT output is the complex conjugate of the first-half.

Mokarrom Hossain
  • 357
  • 1
  • 11