我能够使用来自https://code.google.com/p/musicg/的音乐库来可视化频谱图,但我发现了一些我不太了解的奇怪事情。我尝试使用采样率为 22050 的 wav 文件,并使用 blackmann 窗口使用 1024 个具有 50% 重叠的样本执行 fft。计算的结果是二维数组(spectrogram[time][frequency]=intensity)。我的问题是,如果第二个维度称为频率,为什么它的大小只有 256?它与频率宽度bin有关吗?那么我如何确定频率?当我尝试使用 512 个样本时,大小减少到一半(128)。
那么我们应该对频谱图进行归一化吗?
这是我从 musicg 得到的代码
short[] amplitudes=wave.getSampleAmplitudes();
int numSamples = amplitudes.length;
int pointer=0;
// overlapping
if (overlapFactor>1){
int numOverlappedSamples=numSamples*overlapFactor;
int backSamples=fftSampleSize*(overlapFactor-1)/overlapFactor;
int fftSampleSize_1=fftSampleSize-1;
short[] overlapAmp= new short[numOverlappedSamples];
pointer=0;
for (int i=0; i<amplitudes.length; i++){
overlapAmp[pointer++]=amplitudes[i];
if (pointer%fftSampleSize==fftSampleSize_1){
// overlap
i-=backSamples;
}
}
numSamples=numOverlappedSamples;
amplitudes=overlapAmp;
}
// end overlapping
numFrames=numSamples/fftSampleSize;
framesPerSecond=(int)(numFrames/wave.length());
// set signals for fft (windowing)
WindowFunction window = new WindowFunction();
window.setWindowType("BLACKMAN");
double[] win=window.generate(fftSampleSize);
double[][] signals=new double[numFrames][];
for(int f=0; f<numFrames; f++) {
signals[f]=new double[fftSampleSize];
int startSample=f*fftSampleSize;
for (int n=0; n<fftSampleSize; n++){
signals[f][n]=amplitudes[startSample+n]*win[n];
}
}
// end set signals for fft
absoluteSpectrogram=new double[numFrames][];
// for each frame in signals, do fft on it
FastFourierTransform fft = new FastFourierTransform();
for (int i=0; i<numFrames; i++){
absoluteSpectrogram[i]=fft.getMagnitudes(signals[i]);
}
if (absoluteSpectrogram.length>0){
numFrequencyUnit=absoluteSpectrogram[0].length;
unitFrequency=(double)wave.getWaveHeader().getSampleRate()/2/numFrequencyUnit; // frequency could be caught within the half of nSamples according to Nyquist theory
// normalization of absoluteSpectrogram
spectrogram=new double[numFrames][numFrequencyUnit];
// set max and min amplitudes
double maxAmp=Double.MIN_VALUE;
double minAmp=Double.MAX_VALUE;
for (int i=0; i<numFrames; i++){
for (int j=0; j<numFrequencyUnit; j++){
if (absoluteSpectrogram[i][j]>maxAmp){
maxAmp=absoluteSpectrogram[i][j];
}
else if(absoluteSpectrogram[i][j]<minAmp){
minAmp=absoluteSpectrogram[i][j];
}
}
}
谢谢你