我能够使用来自https://code.google.com/p/musicg/的musicg库来可视化频谱图,但我发现了一些我不太懂的奇怪事物。 我尝试使用采样率为22050的wav文件,并使用blackmann窗口使用1024个样本执行fft,重叠率为50%。 计算结果是二维阵列(谱图[时间] [频率] =强度)。 我的问题是如果第二维称为频率,为什么它的大小只有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];
}
}
}
谢谢
答案 0 :(得分:1)
每个FFT结果区之间的间距是采样率除以FFT长度。对于以22050 sps的速率采样的数据,其馈送到1024的FFT长度,得到的频率仓间隔将约为21.5Hz。如果将FFT长度减小到512,那么较大的bin间距会导致光谱图垂直轴中的总二进制位数减少,然后才能达到采样率的一半以上。使用Blackman窗口(实际上是任何窗口),每个bin的带宽都会有一些重叠。