Java如何将包含多个双精度的byte []转换为double []

时间:2014-02-05 16:42:06

标签: java arrays casting signal-processing fft

我有一个声音文件,我正在练习相位声码。我已经将文件的字节转换为double []并通过快速傅里叶变换和快速傅立叶逆变换操纵该文件的波形。问题是现在我需要将byte []转换回double。以下是一些有用的代码片段:

我如何首先转换数据:

/**
 * Converts bytes from a TargetDataLine into a double[] allowing the information to be read.
 * NOTE: One byte is lost in the conversion so don't expect the arrays to be the same length!
 * @param bufferData The buffer read in from the target Data Line
 * @return the double[] that the buffer has been converted into.
 */
   private static double[] bytesToDoubleArray(byte[] bufferData){
    final int bytesRecorded = bufferData.length;
    final int bytesPerSample = getAudioFormat().getSampleSizeInBits()/8; 
    final double amplification = 100.0; // choose a number as you like
    double[] micBufferData = new double[bytesRecorded - bytesPerSample + 1];
    for (int index = 0, floatIndex = 0; index < bytesRecorded - bytesPerSample + 1; index += bytesPerSample, floatIndex++) {
        double sample = 0;
        for (int b = 0; b < bytesPerSample; b++) {
            int v = bufferData[index + b];
            if (b < bytesPerSample - 1 || bytesPerSample == 1) {
                v &= 0xFF;
            }
            sample += v << (b * 8);
        }
        double sample32 = amplification * (sample / 32768.0);
        micBufferData[floatIndex] = sample32;

    }
    return micBufferData;
}

以及我对数据做了什么:

public static byte[] shift(byte[] data, int factor){
    double[] audioData = bytesToDoubleArray(data);
    audioData = Arrays.copyOf(audioData, roundToPowerOf2(audioData.length));
    Complex[] transformed = FFT.fft(doubleToComplex(audioData));
    transformed = shiftArray(transformed, 3);
    Complex[] reverted = FFT.ifft(transformed);
    for(int i = 0; i<reverted.length; i++){
        audioData[i] = reverted[i].re();
    }
    return null;//How do I convert audioData[] back into a byte[]????
}

有关如何解决此问题的任何想法?任何解决方案将不胜感激。此外,任何已经实现相位声码的Java库也都很棒。

3 个答案:

答案 0 :(得分:2)

大概在这里。我确定我搞砸了。 scaleFactor可能是327.68,以扭转上面的缩放。上面的代码似乎是大端。是否需要fullNormalize取决于你。

public byte[] doubleArrayToByteArray(double[] input, int bytesPerSample, double scaleFactor, boolean fullNormalize, boolean bigEndian) {
    byte[] result = new byte[input.length * bytesPerSample];
    performNormalization(input, scaleFactor, fullNormalize);
    for (int i = 0; i < input.length; i++) {
        long sourceVal = (long)(input[i] * scaleFactor);
        sourceVal = sourceVal >> 8 * (8 - bytesPerSample);
        for (int j = 0; j < bytesPerSample; j++) {
            int index = i * bytesPerSample;
            if (bigEndian) {
                index += (bytesPerSample - j);
            }
            else {
                index += j;
            }
            result[index] = (byte) sourceVal;
            sourceVal = sourceVal >> 8;
        }
    }
    return result;
}

public void performNormalization(double[] input, double scaleFactor, boolean fullNormalize) {
    double maxVal = 0.0;
    for (int i = 0; i < input.length; i++) {
        double val = Math.abs(input[i]) * scaleFactor;
        if (val > maxVal) {
            maxVal = val;
        }
    }
    if (fullNormalize || maxVal > Long.MAX_VALUE) {
        double normalizeFactor = (double)(Long.MAX_VALUE) / maxVal;
        for (int i = 0; i < input.length; i++) {
            input[i] *= normalizeFactor;
        }
    }
}

更新:意识到我需要在规范化中考虑scaleFactor。而且,您通常不会同时指定不是1.0的scaleFactorfullNormalize = true

答案 1 :(得分:1)

这取决于你将double []转换为byte []的含义。回想一下,通常double是8个字节长,而byte是1个字节长。

如果你可以将你的双重转换为无符号整数0-255(或签名-128-127),那么你只需要将每个double转换为byte并将其分配给一个新数组,否则你必须使用ByteBuffer:

int capacity = // calculate how much space you need for your byte array
ByteBuffer byteBuf = ByteBuffer(capacity);
for(double d : doubleArray) byteBuf.putDouble(d);
byte[] byteArray = byteBuf.array();

实际上上面的方法没有转换任何东西,只是你只是将字节流解释为字节序列而不是double的序列

答案 2 :(得分:0)

就像您从byte[]转换为double[]一样,您现在需要转换回byte[]。只需创建一个新的byte[]数组并手动复制double[]数组中的数据:

private static byte[] doublesToByteArray(double[] a) {
    int len = a.length;
    byte[] b = new byte[len];
    for (int i = 0; i < len; i++)
        b[i] = (byte) a[i];
    return b;
}

这当然假设double[]数组中的数据可以存储在byte[]数组中而无需进一步转换/缩放。否则,复制时需要注意这一点。