帮助实现这种节拍检测算法?

dsp*_*boy 5 c# algorithm audio-processing

我最近试图实现这里找到的节拍检测代码,即Derivation和Combfilter算法#1 :: http://archive.gamedev.net/reference/programming/features/beatdetection/page2.asp

我不太确定我是否成功实施,因为我没有取得好成绩.我想知道是否有人成功地实现了这一点,或者仅仅是想要帮助一般的好人.这是我的实现:

//Cycle through Tempo's (60 to 200) incrementing each time by 10
for (int i = (int)mintempo; i <= maxtempo; i += 10)
{
    //Clear variables to be used
    curtempo = i;
    fftPulse.Clear();
    offset = 0;
    energy = 0;
    short[] prevBuffer = null;

    //Calculate ti
    ti = (60 / curtempo) * 44100;
    ti = Math.Round(ti, 0);

    //Generate pulse train
    for (int j = 0; j < pulseTrain.Length; j++)
    {
        if ((j % ti) == 0)
            pulseTrain[j] = short.MaxValue;
        else
            pulseTrain[j] = 0;
    }

    //Compute FFT of the pulseTrain array
    while (offset < pulseTrain.Length)
    {
        //Generate block samples (1024 is my blocksize)
        short[] fftPulseBuffer = new short[po.blocksize / 2];

        //Store samples from pulseTrain in a 1024 block buffer for passing to the FFT algorithm
        index = 0;
        for (int j = offset; j < (offset + (po.blocksize / 2)) && j < pulseTrain.Length; j++)
        {
            fftPulseBuffer[index] = pulseTrain[j];
            index++;
        }

        //Initialize prevBuffer, which contains samples from the previous block, used in conjunction with the current block for the FFT
        if (prevBuffer == null)
            prevBuffer = new short[po.blocksize / 2];

        //Calculate the FFT using the current and previous blocks
        fftPulse.Add(CalculateFFT(fftPulseBuffer,prevBuffer));

        //Set prevBuffer and increment to next block start position
        prevBuffer = fftPulseBuffer;
        offset += (po.blocksize / 2);
    }

//Calculate energy
    for (int j = 0; j < intendomainarr.Count; j++)
    {
        double[] signalarr = intendomainarr[j];
        double[] pulsearr = fftPulse[j];
        for (int x = 0; x < signalarr.Length; x++)
        {
            energy += Math.Abs(signalarr[x] * pulsearr[x]);
        }
    }

    //Get current best tempo match
    if (energy > maxenergy)
    {
        chosentempo = curtempo;
        maxenergy = energy;
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到的结果总是非常高,通常在190和200BPM左右,不应该是这种情况,因为我的.wav文件只有60-120BPM之间的节奏.

请注意,我使用的是.WAV文件(44.1Khz,16位,Mono),因此某些公式有点修改(即计算能量),只能使用一个通道.我想确认我的实施是否有任何差异?我不担心FFT部分,因为我正在使用一个库.

非常感谢你!

Ben*_*igt 3

绘制能量与频率的关系图。

我认为您会发现谐波具有与基本信号几乎相同的能量,并且如果实际频率落在频率箱之间的中间,则对二次谐波的峰值进行采样,并轻松地将两个样本击败到真实频率的任一侧。

您需要稍微惩罚更高的频率来克服这种影响。


请注意,虽然 C# 对于实时实现此类算法或批量处理来说并不是一个不合理的选择,但它对于算法开发和调整来说是可怕的。我建议使用 MatLab(或免费克隆 Octave)来获得正确的算法,并且只有在它处理某些测试用例时,才将代码转换为 C#(或 C++)。