Fourier transform of a byte matrix

I'm not that good at Java, so please keep it pretty simple. I will, however, try to understand everything that you publish. Here is my problem.

I wrote code to record sound from an external microphone and saved it in .wav. The storage of this file is important for archiving purposes. I need to make FFT saved sound.

My approach to this is loading a wav file as a byte array and transforming it with a problem: 1. There the heading is how I need to get rid, but I have to do it 2. I got an array of bytes, but most, if not all the FFT algorithms that I found on the Internet and tried to patch in my design work with complex / two double arrays.

I tried to work around both of these problems and finally was able to build my FFT array in a graph when I found out that it just returns me "0". The .wav file is fine, I can play it without a problem. I thought that maybe converting bytes to doubles was a problem for me, so here is my approach to this (I know this is not very)

byte ByteArray[] = Files.readAllBytes(wav_path);
String s = new String(ByteArray);
double[] DoubleArray = toDouble(ByteArray);
// build 2^n array, fill up with zeroes
boolean exp = false;
int i = 0;
int pow = 0;
while (!exp) {
    pow = (int) Math.pow(2, i);
    if (pow > ByteArray.length) {
        exp = true;
    } else {
        i++;
    }
}
System.out.println(pow);
double[] Filledup = new double[pow];
for (int j = 0; j < DoubleArray.length; j++) {
    Filledup[j] = DoubleArray[j];
    System.out.println(DoubleArray[j]);
}
for (int k = DoubleArray.length; k < Filledup.length; k++) {
    Filledup[k] = 0;
}

This is the function I use to convert a byte array to a double array:

public static double[] toDouble(byte[] byteArray) {
    ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray);
    double[] doubles = new double[byteArray.length / 8];
    for (int i = 0; i < doubles.length; i++) {
        doubles[i] = byteBuffer.getDouble(i * 8);
    }
    return doubles;
}

, , . , 2 , ( 2 ^ n ). , , - . , ( , ).

: 44100 , 16 .

FFT, .

, :

...
-2.0311904060823147E236
-1.3309975624948503E241
1.630738286366793E-260
1.0682002560745842E-255
-5.961832069690704E197
-1.1476447092561027E164
-1.1008407401197794E217
-8.109566204271759E298
-1.6104556241572942E265
-2.2081172620352248E130
NaN
3.643749694745671E-217
-3.9085815506127892E202
-4.0747557114875874E149
...

, - , - , , . , : ?

+4
2

, [& hellip;]

javax.sound.sampled.AudioInputStream, , "" . , , .

44100 , 16 .

, , 16- (short Java).

ByteBuffer , 64- . , short, double.

, short, double.

, , ( 8-, 16-, 32- 64- PCM):

import javax.sound.sampled.*;
import javax.sound.sampled.AudioFormat.Encoding;
import java.io.*;
import java.nio.*;

static double[] readFully(File file)
throws UnsupportedAudioFileException, IOException {
    AudioInputStream in = AudioSystem.getAudioInputStream(file);
    AudioFormat     fmt = in.getFormat();

    byte[] bytes;
    try {
        if(fmt.getEncoding() != Encoding.PCM_SIGNED) {
            throw new UnsupportedAudioFileException();
        }

        // read the data fully
        bytes = new byte[in.available()];
        in.read(bytes);
    } finally {
        in.close();
    }

    int   bits = fmt.getSampleSizeInBits();
    double max = Math.pow(2, bits - 1);

    ByteBuffer bb = ByteBuffer.wrap(bytes);
    bb.order(fmt.isBigEndian() ?
        ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);

    double[] samples = new double[bytes.length * 8 / bits];
    // convert sample-by-sample to a scale of
    // -1.0 <= samples[i] < 1.0
    for(int i = 0; i < samples.length; ++i) {
        switch(bits) {
            case 8:  samples[i] = ( bb.get()      / max );
                     break;
            case 16: samples[i] = ( bb.getShort() / max );
                     break;
            case 32: samples[i] = ( bb.getInt()   / max );
                     break;
            case 64: samples[i] = ( bb.getLong()  / max );
                     break;
            default: throw new UnsupportedAudioFileException();
        }
    }

    return samples;
}

FFT, , , , - . , ( , ).

. , , 0, :

double[] realPart = mySamples;
double[] imagPart = new double[realPart.length];
myFft(realPart, imagPart);

... " Java Sound?"

+4

8- , .

( WAVE RIFF) , , .

2- .

+1

All Articles