title: Spectogram of a .wav file returns wrong results
body: <p>I am currently trying to plot the frequency domain of a .wav file which then i can use the values of, to train a neural network to predict which sound is playing.</p>
  <p>I already read the bytes of the .wav file using this bit of code, starting from the 44th byte , which is the start of the data chunk:</p>
  <pre><code>entireFileData= Files.readAllBytes(Paths.get(filepath));
  byte[] data_raw= Arrays.copyOfRange(entireFileData,44,entireFileData.length);
  
  double[] dataNew=new double[(int)myDataSize/2];
  int new_length=data_raw.length/2;
  double val;
  
   myData = new byte[(int)myDataSize];
  for(int i=0;i&lt;new_length;i+=2) {
        val =((data_raw[i]*0xff)&lt;&lt;8)| (data_raw[i+1]*0xff);
        dataNew[i]=(double) val;
  }
  
  
  </code></pre>
  <p>Then i used fourier transform on the byte array</p>
  <pre><code>ApacheFFT apacheFFT=new ApacheFFT();
  apacheFFT.forward(dataNew,(float)44100,w);
  
  Spectrum s = apacheFFT.getMagnitudeSpectrum(); //I used the getMagnitudeSpectrum method to be able to plot 
  double[] freq=s.array();                       //the graph
  double[] array=new double[freq.length]
  
  </code></pre>
  <p>Here is the createSpectrum method:</p>
  <pre><code>public static Spectrum createMagnitudeSpectrum(Complex[] data, float samplingrate, WindowFunction windowFunction) {
          int bins = data.length / 2;
          double normalizationFactor = data.length * windowFunction.normalization(data.length);
  
          double[] magnitudeSpectrum = new double[bins];
          magnitudeSpectrum[0] = Math.sqrt(Math.pow(data[0].getReal(), 2) + Math.pow(data[0].getImaginary(), 2)) / normalizationFactor;
          for (int i = 1; i &lt; bins; i++) {
              magnitudeSpectrum[i] = (2 * data[i].abs()) / normalizationFactor;
          }
  
          return new Spectrum(Type.MAGNITUDE, magnitudeSpectrum, FFTUtil.binCenterFrequencies(data.length, samplingrate));
      }
  </code></pre>
  <p>This is the Forward Fourier Transform i used, from the AppacheFFT package:</p>
  <pre><code>public void forward(double[] data, float samplingrate, WindowFunction window) {
          this.windowFunction = window;
          this.samplingrate = samplingrate;
  
          int actual_length = 8192;
          int valid_length = FFTUtil.nextPowerOf2(actual_length);
          double[] reshaped = new double[valid_length];
          for (int i = 0; i &lt; reshaped.length; i++) {
              if (i &lt; actual_length) {
                  reshaped[i] = data[i] * this.windowFunction.value(i, valid_length);
              } else {
                  reshaped[i] = 0;
              }
          }
  
          /* Perform FFT using FastFourierTransformer library. */
          FastFourierTransformer transformer = new FastFourierTransformer(DftNormalization.STANDARD);
          this.data = transformer.transform(reshaped, TransformType.FORWARD);
  
          /* Reset the calculated properties. */
          this.powerSpectrum = null;
          this.magnitudeSpectrum = null;
      }
  </code></pre>
  <p>I then plotted the values to a graph using LineGraphSeries like so:</p>
  <pre><code>for(int i=0;i&lt;freq.length;i++)
      {
     dataPoints[i]=new DataPoint(i,freq[i]);
          
      }
  LineGraphSeries&lt;DataPoint&gt; series = new LineGraphSeries&lt;DataPoint&gt;(dataPoints);
  graph.addSeries(series);
  </code></pre>
  <p>So to test myself i used audacity's plot spectrum tool to see the difference, and it is quite a difference between my result and audacity's:</p>
  <p>I am not allowed to post pictures so <a href="https://i.stack.imgur.com/BLfBT.png" rel="nofollow noreferrer">here's</a> a link to the comparison.</p>
  <p>As you can see it's not even near the one in audacity. I have actually observed that my signal is mirrored from the half.</p>
  <p>I tried multiple ways of reading the .wav file, multiple ways of plotting the file to the graph.
  I can't really fathom what could have went wrong here.</p>
  <p>Also i'm sorry if i made any formatting mistakes.</p>
  <p>Any opinions on this?</p>
  <p>Edit: Okay, i actually did it , it was the way i read the bytes, i didnt mask it, and bytes are signed so it would give me wrong values, but now it only works on one frequency, for example if i have a 3Khz sine wave file, it shows tehe 3Khz freq, if i have 10Khz it shows 10, but if i have both of the frequencies in one file, it only shows the 3Khz one</p>