我对音频处理很陌生,我正在尝试从 wav 文件中提取频率。正如研究论文所述,我将 wav 音频放入字节数组并分段并尝试应用 FFT 从音频中获取频率。然而问题是,即使我输入一个只播放一个音符的音轨(没有噪音),我也会得到很多频率,结果我应该得到一个。
我的猜测是 FFT 代码有问题,因为音频被正确分割。
提前谢谢大家,
我的fft代码
public void getFFT(double[] x, int a) {
double amp[] = new double[512];
double firstSum;
double secSum;
double twoPInjk;
double[][] s = new double[512][2];
for (int j = 0; j < 512; j++) {
firstSum = 0;
secSum = 0;
FreqArr[j] = (j * sampleRate) / (512);
for (int k = 0; k < 512; k++) {
twoPInjk = ((2 * Math.PI) / 512) * (j * k);
firstSum += x[k] * Math.cos(twoPInjk);
secSum += x[k] * Math.sin(twoPInjk);
}
amp[j] = Math.abs(Math.sqrt(Math.pow(firstSum, 2) + Math.pow(secSum, 2)));
}
highest(amp, a);
}
public void highest(double[] amp, int a) {
String note;
double all = 0;
double max_magnitude = -1;
int max_index = -1;
for (int i = 0; i < (512); i++) {
all = all + amp[i];
if (amp[i] > max_magnitude) {
max_magnitude = amp[i];
max_index = i;
}
}
double freq = 0;
int cnt = 0;
freq = max_index * (sampleRate / 2) / (512);
System.out.println("Maximum_index: " + max_index);
System.out.println("Sample rate: " + sampleRate);
System.out.println(freq + " is the frequnecy");
}
我的结果(对于只播放一个音符的 1 秒音频)
Sample rate 44100 My data size 70376 My myBitsPerSample 16 My channels 2 My myByteRate 176400 My Length 0.7983219954648526 0.7983219954648526 44100.0 Max_index: 56 Sample rate: 44100.0 2411.71875 is the frequnecy Max_index: 256 Sample rate: 44100.0 11025.0 is the frequnecy Max_index: 256 Sample rate: 44100.0 11025.0 is the frequnecy Max_index: 129 Sample rate: 44100.0 5555.56640625 is the frequnecy Max_index: 511 Sample rate: 44100.0 22006.93359375 is the frequnecy Max_index: 256 Sample rate: 44100.0 11025.0 is the frequnecy Max_index: 152 Sample rate: 44100.0 6546.09375 is the frequnecy Max_index: 422 采样率:44100.0 18174.0234375 是频率 Max_index:26 采样率:44100.0 1119.7265625 是频率 Max_index:462 采样率:44100.0 19896。6796875 is the frequnecy Max_index: 450 Sample rate: 44100.0 19379.8828125 is the frequnecy Max_index: 201 Sample rate: 44100.0 8656.34765625 is the frequnecy Max_index: 383 Sample rate: 44100.0 16494.43359375 is the frequnecy Max_index: 6 Sample rate: 44100.0 258.3984375 is the frequnecy Max_index: 385 Sample rate: 44100.0 16580.56640625 is the frequnecy Max_index: 100 Sample rate: 44100.0 4306.640625 is the frequnecy Max_index: 15 Sample rate: 44100.0 645.99609375 is the frequnecy Max_index: 421 Sample rate: 44100.0 18130.95703125 is the frequnecy Max_index: 256 Sample rate: 44100.0 11025.0是频率 Max_index:0 采样率:44100.0 0.0 是频率 Max_index:483 采样率:44100.0 20801.07421875 是频率 Max_index:0 采样率:44100.0 0。0 is the frequnecy Max_index: 134 Sample rate: 44100.0 5770.8984375 is the frequnecy Max_index: 123 Sample rate: 44100.0 5297.16796875 is the frequnecy Max_index: 2 Sample rate: 44100.0 86.1328125 is the frequnecy Max_index: 378 Sample rate: 44100.0 16279.1015625 is the frequnecy Max_index: 491 Sample rate: 44100.0 21145.60546875 is the frequnecy Max_index: 125 Sample rate: 44100.0 5383.30078125 is the frequnecy Max_index: 242 Sample rate: 44100.0 10422.0703125 is the frequnecy Max_index: 267 Sample rate: 44100.0 11498.73046875 is the frequnecy Max_index: 257 Sample rate: 44100.0 11068.06640625是频率 Max_index:128 采样率:44100.0 5512.5 是频率 Max_index:496 采样率:44100.0 21360.9375 是频率 Max_index:350 采样率:44100.0 15073。2421875 is the frequnecy Max_index: 75 Sample rate: 44100.0 3229.98046875 is the frequnecy Max_index: 127 Sample rate: 44100.0 5469.43359375 is the frequnecy Max_index: 473 Sample rate: 44100.0 20370.41015625 is the frequnecy Max_index: 35 Sample rate: 44100.0 1507.32421875 is the frequnecy Max_index: 420 Sample rate: 44100.0 18087.890625 is the frequnecy Max_index: 413 Sample rate: 44100.0 17786.42578125 is the frequnecy Max_index: 136 Sample rate: 44100.0 5857.03125 is the frequnecy Max_index: 93 Sample rate: 44100.0 4005.17578125 is the frequnecy Max_index: 251 Sample rate: 44100.0 10809.66796875是频率 Max_index:431 采样率:44100.0 18561.62109375 是频率 Max_index:490 采样率:44100.0 21102.5390625 是频率 Max_index:221 采样率:44100.0 9517.67578125 is the frequnecy Max_index: 364 Sample rate: 44100.0 15676.171875 is the frequnecy Max_index: 19 Sample rate: 44100.0 818.26171875 is the frequnecy Max_index: 487 Sample rate: 44100.0 20973.33984375 is the frequnecy Max_index: 350 Sample rate: 44100.0 15073.2421875 is the frequnecy Max_index : 243 Sample rate: 44100.0 10465.13671875 is the frequnecy Max_index: 384 Sample rate: 44100.0 16537.5 is the frequnecy Max_index: 403 Sample rate: 44100.0 17355.76171875 is the frequnecy Max_index: 56 Sample rate: 44100.0 2411.71875 is the frequnecy Max_index: 151 Sample rate: 44100.0 6503.02734375 是频率 Max_index:447 采样率:44100.0 19250.68359375 是频率 Max_index:501 采样率:44100.0 21576.26953125 是频率 Max_index:377 Sample rate: 44100.0 16236.03515625 is the frequnecy Max_index: 465 Sample rate: 44100.0 20025.87890625 is the frequnecy Max_index: 417 Sample rate: 44100.0 17958.69140625 is the frequnecy Max_index: 259 Sample rate: 44100.0 11154.19921875 is the frequnecy Max_index: 34 Sample rate: 44100.0 1464.2578125 is the frequnecy Max_index: 327 Sample rate: 44100.0 14082.71484375 is the frequnecy Max_index: 280 Sample rate: 44100.0 12058.59375 is the frequnecy Max_index: 271 Sample rate: 44100.0 11670.99609375 is the frequnecy Max_index: 92 Sample rate: 44100.0 3962.109375 is the frequnecy Max_index: 141采样率:44100.0 6072.36328125 是频率 Max_index:467 采样率:44100.0 20112.01171875 是频率 Max_index:123 采样率:44100.0 5297。16796875 is the frequnecy Max_index: 388 Sample rate: 44100.0 16709.765625 is the frequnecy Max_index: 122 Sample rate: 44100.0 5254.1015625 is the frequnecy Max_index: 386 Sample rate: 44100.0 16623.6328125 is the frequnecy Max_index: 464 Sample rate: 44100.0 19982.8125 is the frequnecy Max_index: 231 Sample rate: 44100.0 9948.33984375 is the frequnecy Max_index: 95 Sample rate: 44100.0 4091.30859375 is the frequnecy Max_index: 112 Sample rate: 44100.0 4823.4375 is the frequnecy Max_index: 260 Sample rate: 44100.0 11197.265625 is the frequnecy Max_index: 383 Sample rate: 44100.0 16494.43359375是频率 Max_index:458 采样率:44100.0 19724.4140625 是频率 Max_index:17 采样率:44100.0 732.12890625 是频率 Max_index:129 采样率:44100。0 5555.56640625 is the frequnecy Max_index: 262 Sample rate: 44100.0 11283.3984375 is the frequnecy Max_index: 259 Sample rate: 44100.0 11154.19921875 is the frequnecy Max_index: 0 Sample rate: 44100.0 0.0 is the frequnecy Max_index: 404 Sample rate: 44100.0 17398.828125 is the frequnecy Max_index : 471 Sample rate: 44100.0 20284.27734375 is the frequnecy Max_index: 119 Sample rate: 44100.0 5124.90234375 is the frequnecy Max_index: 239 Sample rate: 44100.0 10292.87109375 is the frequnecy Max_index: 385 Sample rate: 44100.0 16580.56640625 is the frequnecy Max_index: 485 Sample rate: 44100.0 20887.20703125 是频率 Max_index:14 采样率:44100.0 602.9296875 是频率 Max_index:384 采样率:44100.0 16537.5 是频率 Max_index:12 采样率:44100。0 516.796875 is the frequnecy Max_index: 342 Sample rate: 44100.0 14728.7109375 is the frequnecy Max_index: 369 Sample rate: 44100.0 15891.50390625 is the frequnecy Max_index: 15 Sample rate: 44100.0 645.99609375 is the frequnecy Max_index: 22 Sample rate: 44100.0 947.4609375 is the frequnecy Max_index :395 采样率:44100.0 17011.23046875 是频率 Max_index:26 采样率:44100.0 1119.7265625 是频率 Max_index:386 采样率:44100.0 16623.6328125 是频率4609375 是频率 Max_index:395 采样率:44100.0 17011.23046875 是频率 Max_index:26 采样率:44100.0 1119.7265625 是频率 Max_index:386 采样率:44100.0 16623.6328125 是频率4609375 是频率 Max_index:395 采样率:44100.0 17011.23046875 是频率 Max_index:26 采样率:44100.0 1119.7265625 是频率 Max_index:386 采样率:44100.0 16623.6328125 是频率