我正在尝试在 IOS 8 中实现吉他调音器,并且我从已经实现它的人那里得到了一些代码:它处理 Goertzel 算法,该算法在短期内比较固定频率的幅度 - 正如为 srings EADGBE 定义的那样。- 这里放置在CoreAudio的回调方法中的例程:
int currentString(SInt16 *samples, int N) {
int note0 = 82;
int note1 = 110;
int note2 = 147;
int note3 = 196;
int note4 = 247;
int note5 = 330;
int offset = 0.5;
double results[6];
// filter for the six strings
results[0] = (goertzelFilter(samples, note0+offset, N) +goertzelFilter(samples, note0, N) + goertzelFilter(samples, note0-offset, N))/3.0;
results[1] = (goertzelFilter(samples, note1+offset, N) +goertzelFilter(samples, note1, N) + goertzelFilter(samples, note1-offset, N))/3.0;
results[2] = (goertzelFilter(samples, note2+offset, N) +goertzelFilter(samples, note2, N) + goertzelFilter(samples, note2-offset, N))/3.0;
results[3] = (goertzelFilter(samples, note3+offset, N) +goertzelFilter(samples, note3, N) + goertzelFilter(samples, note3-offset, N))/3.0;
results[4] = (goertzelFilter(samples, note4+offset, N) +goertzelFilter(samples, note4, N) + goertzelFilter(samples, note4-offset, N))/3.0;
results[5] = (goertzelFilter(samples, note5+offset, N) +goertzelFilter(samples, note5, NN) + goertzelFilter(samples, note5-offset, N))/3.0;
int maxInd = -1;
double maxVal = 0.0;
for (int i=0; i<6; i++) {
if (results[i] > maxVal) {
if (i==0)
NSLog(@"String %d - value: %f", i+1, results[i]);
maxVal = results[i];
maxInd = i;
}
}
// if all levels are quite low, return -1
if (maxVal < 1) {
maxInd = -1;
}
return maxInd;
}
然而,这样的例程只适用于较低的字符串“DGBE”。对于 E 弦和 D 弦,我得到错误的结果,我相信这种行为与泛音有关,因为它们似乎比搜索的更强烈——也许低“E”有“A”或“D”作为泛音,幅度更大。
我的问题:有没有遇到过类似的问题?并解决了吗?Goertzel 是正确的算法,还是 FFT 或卷积是更好的解决方案?
最后是我使用的 Goertzel 算法:
double goertzelFilter(SInt16* samples, double freq, int N) {
double s_prev = 0.0;
double s_prev2 = 0.0;
double coeff,normalizedfreq,power,s;
int i;
normalizedfreq = freq / 44100;
coeff = 2*cos(2*M_PI*normalizedfreq);
for (i=0; i<N; i++) {
s = samples[i] + coeff * s_prev - s_prev2;
s_prev2 = s_prev;
s_prev = s;
}
power = s_prev2*s_prev2+s_prev*s_prev-coeff*s_prev*s_prev2;
return power;
}