将FFT应用于java中的录音

时间:2014-01-30 23:54:15

标签: java audio signal-processing fft

我在这个网站上看到过与此相似的类似问题,但我的问题有点不同。我用来捕获音频的代码是this。我想简单地获取捕获的音频,然后用256点应用FFT。

我意识到这个line count = line.read(buffer, 0, buffer.length);将音频分解为“块”。

我可以找到我正在使用的FFT here

我的问题是:

  1. 我想知道是否有办法将FFT应用于整个录音而不仅仅是缓冲量。
  2. 我看到FFT的代码需要一个实部和虚部,我如何通过音频文件从代码中获取实部和虚部。

1 个答案:

答案 0 :(得分:6)

所有javax.sound.sampled包都读取文件中的原始字节并将它们写入输出。因此,您需要做的是“介于两者之间”,即自行转换样本。

以下显示了如何对PCM执行此操作(带注释),取自我的代码示例WaveformDemo

public static float[] unpack(
    byte[] bytes,
    long[] transfer,
    float[] samples,
    int bvalid,
    AudioFormat fmt
) {
    if(fmt.getEncoding() != AudioFormat.Encoding.PCM_SIGNED
            && fmt.getEncoding() != AudioFormat.Encoding.PCM_UNSIGNED) {

        return samples;
    }

    final int bitsPerSample = fmt.getSampleSizeInBits();
    final int bytesPerSample = bitsPerSample / 8;
    final int normalBytes = normalBytesFromBits(bitsPerSample);

    /*
     * not the most DRY way to do this but it's a bit more efficient.
     * otherwise there would either have to be 4 separate methods for
     * each combination of endianness/signedness or do it all in one
     * loop and check the format for each sample.
     * 
     * a helper array (transfer) allows the logic to be split up
     * but without being too repetetive.
     * 
     * here there are two loops converting bytes to raw long samples.
     * integral primitives in Java get sign extended when they are
     * promoted to a larger type so the & 0xffL mask keeps them intact.
     * 
     */

    if(fmt.isBigEndian()) {
        for(int i = 0, k = 0, b; i < bvalid; i += normalBytes, k++) {
            transfer[k] = 0L;

            int least = i + normalBytes - 1;
            for(b = 0; b < normalBytes; b++) {
                transfer[k] |= (bytes[least - b] & 0xffL) << (8 * b);
            }
        }
    } else {
        for(int i = 0, k = 0, b; i < bvalid; i += normalBytes, k++) {
            transfer[k] = 0L;

            for(b = 0; b < normalBytes; b++) {
                transfer[k] |= (bytes[i + b] & 0xffL) << (8 * b);
            }
        }
    }

    final long fullScale = (long)Math.pow(2.0, bitsPerSample - 1);

    /*
     * the OR is not quite enough to convert,
     * the signage needs to be corrected.
     * 
     */

    if(fmt.getEncoding() == AudioFormat.Encoding.PCM_SIGNED) {

        /*
         * if the samples were signed, they must be
         * extended to the 64-bit long.
         * 
         * so first check if the sign bit was set
         * and if so, extend it.
         * 
         * as an example, imagining these were 4-bit samples originally
         * and the destination is 8-bit, a mask can be constructed
         * with -1 (all bits 1) and a left shift:
         * 
         *     11111111
         *  <<  (4 - 1)
         *  ===========
         *     11111000
         * 
         * (except the destination is 64-bit and the original
         * bit depth from the file could be anything.)
         * 
         * then supposing we have a hypothetical sample -5
         * that ought to be negative, an AND can be used to check it:
         * 
         *    00001011
         *  & 11111000
         *  ==========
         *    00001000
         * 
         * and an OR can be used to extend it:
         * 
         *    00001011
         *  | 11111000
         *  ==========
         *    11111011
         * 
         */

        final long signMask = -1L << bitsPerSample - 1L;

        for(int i = 0; i < transfer.length; i++) {
            if((transfer[i] & signMask) != 0L) {
                transfer[i] |= signMask;
            }
        }
    } else {

        /*
         * unsigned samples are easier since they
         * will be read correctly in to the long.
         * 
         * so just sign them:
         * subtract 2^(bits - 1) so the center is 0.
         * 
         */

        for(int i = 0; i < transfer.length; i++) {
            transfer[i] -= fullScale;
        }
    }

    /* finally normalize to range of -1.0f to 1.0f */

    for(int i = 0; i < transfer.length; i++) {
        samples[i] = (float)transfer[i] / (float)fullScale;
    }

    return samples;
}

public static int normalBytesFromBits(int bitsPerSample) {

    /*
     * some formats allow for bit depths in non-multiples of 8.
     * they will, however, typically pad so the samples are stored
     * that way. AIFF is one of these formats.
     * 
     * so the expression:
     * 
     *  bitsPerSample + 7 >> 3
     * 
     * computes a division of 8 rounding up (for positive numbers).
     * 
     * this is basically equivalent to:
     * 
     *  (int)Math.ceil(bitsPerSample / 8.0)
     * 
     */

    return bitsPerSample + 7 >> 3;
}

这段代码假定为float[]而您的FFT需要double[],但这是一个相当简单的更改。 transfersamples是长度等于bytes.length * normalBytes的数组,bvalidread的返回值。我的代码示例假设AudioInputStream,但相同的转换应适用于TargetDataLine。我不确定你是否可以直接复制和粘贴它,但这只是一个例子。

关于你的两个问题:

  1. 您可以对整个录制进行非常长的FFT或平均每个缓冲区的FFT。
  2. 您链接的FFT计算到位。所以真正的部分是音频样本,虚部是一个长度等于实部的空数组(用零填充)。
  3. 但是当完成FFT时,还有一些事情要做,我没有看到链接的类在做什么:

    • 转换为极坐标。
    • 通常会丢弃负频率(频谱的整个上半部分,即下半部分的镜像)。
    • 通过将它们除以变换的长度,可以对得到的幅度(实部)进行放大。

    编辑,相关: