2012-09-30 39 views
0

我需要用C#中的yin算法計算基本頻率。使用C#中的yin方法計算基本頻率

我有一個數組(數據[44100]),其中包含一個1秒長的250Hz正弦波的幅度。這裏是我的代碼:

int t = 0;     //starting point of the window 
int w = data.Length/25;  //the end of the window so the size of the window is 40msec 
int rmax = 0;     //the maximum of the correlation function 
int period = 0;    //the period of the sinus 

for (int tau = 0; tau < w; tau++) 
{ 
    int r = 0; 
    for (int j = t + 1; j < (t + w); j++) 
    { 
     r = r + (data[j] * data[j + tau]); 
    }  
    if (r > rmax) 
    { 
     rmax = r; 
     period = tau; 
    } 
} 

float time = (float)period/44100; 
float freq = 1.0f/time; 
System.Console.WriteLine(freq); 

我應該得到250的頻率,但出了點問題。數組中的值是好的,我在excel中檢查過,並且期限是應該的。任何人都可以幫忙嗎?

回答

1

您的代碼與您的評論之間存在不匹配。

1秒/ 25將是0.04秒而不是0.4秒。

1

您的自相關函數中包含零延遲,並且信號與零延遲完全相關。在達到一個週期的滯後之前,您也停止(tau < w),其中正弦波將具有其下一個自相關峯值。

嘗試從預期時間的一半延遲開始,步進到預期時間的1.5倍。

+0

我做到了,但它仍然是錯誤的。現在我計算了其他部分的時期,至少它們與第一時期相同。但結果取決於窗口的大小。這是正常的嗎? – user1707045

+0

矩形窗口包含任何不嚴格定期在該窗口寬度週期的信號將產生「頻譜泄漏」僞像。那是你所看到的嗎? – hotpaw2