使用互相关的声音文件的时间延迟

时间:2014-05-12 13:29:43

标签: java signal-processing audio-processing cross-correlation

我试图加快我在Java中两个声音文件之间的时间延迟估算算法。我的想法是使用互相关并搜索最高值,这会给我延迟的样本量。

我已经完成了这项工作并且工作正常。我创建了一些示例文件,然后计算了时间延迟。结果非常好。问题是,由于操作量大,算法需要花费大量时间。

有什么方法可以加快速度吗?

/**
 * Input vector for signal x1 (reference).
 */
private double[] x1;

/**
 * Input vector for signal x2 (test).
 */
private double[] x2;


/**
 * Execute the cross correlation between signal x1 and x2 and calculate the time delay.
 */
public void execCorrelation()
{
    // define the size of the resulting correlation field
    int corrSize = 2*x1.length;
    // create correlation vector
    out = new double[corrSize];
    // shift variable
    int shift = x1.length;
    double val;
    int maxIndex = 0;
    double maxVal = 0;

    // we have push the signal from the left to the right
    for(int i=0;i<corrSize;i++)
    {
        val = 0;
        // multiply sample by sample and sum up
        for(int k=0;k<x1.length;k++)
        {
            // x2 has reached his end - abort
            if((k+shift) > (x2.length -1))
            {
                break;
            }

            // x2 has not started yet - continue
            if((k+shift) < 0)
            {
                continue;
            }

            // multiply sample with sample and sum up
            val += x1[k] * x2[k+shift];
            //System.out.print("x1["+k+"] * x2["+(k+tmp_tau)+"] + ");
        }
        //System.out.println();
        // save the sample
        out[i] = val;
        shift--;
        // save highest correlation index
        if(out[i] > maxVal)
        {
            maxVal = out[i];
            maxIndex = i;
        }
    }

    // set the delay
    this.delay = maxIndex - x1.length;
}

1 个答案:

答案 0 :(得分:2)

如果我没记错的话,互相关与其中一个时间反转信号的卷积相同。通过将两个信号的频谱相乘来有效地计算卷积。即,将每个信号的FFT至少填充到两个信号的大小之和,乘以FFT变换的光谱,进行逆IFFT,并搜索您的峰值。

对于Java,您可以使用JTransforms进行FFT / IFFT。

如果您想在实际实施之前使用此方法,可以尝试我的应用程序FScape;它有一个卷积模块,可以获取两个声音文件(你标记了“音频处理”这个问题,所以我假设你可以生成声音文件)。