【问题标题】:Effect of window shifting in spectrogram?频谱图中窗口移动的影响?
【发布时间】:2020-09-25 23:03:27
【问题描述】:

我在 DSP 和生成音频文件的频谱图方面相当陌生。我的频谱图不平滑,它显示的是带有像素值的非常原始的图像,像这样

虽然我正在寻找这样的平滑频谱图

我在哪里做错了?是因为窗口大小的大小吗?我生成梅尔谱图的代码是

def readData(file):
    origData,origSampFreq = librosa.load(file, sr=None)
    return origData, origSampFreq


def resample(originalData, origSampFreq, targetSampFreq):
    resampledData = librosa.resample(originalData, origSampFreq, targetSampFreq)
    return resampledData


def normalizeSound(resampledData, axis):
    """ Axis is 0 for row-wise and 1 
    for column wise"""
    normalizedData = normalize(resampledData, axis)
    return normalizedData

def calculateMelSpectogram(normalizedData, hop_length, win_length, sr):
    #newSamplingFreq = 16000
    S=librosa.feature.melspectrogram(normalizedData, sr=sr, hop_length=hop_length, win_length=win_length)
    return S

#Plot melspectogram

def plotMelSpectogram(S, sr, name, ref=np.max):
    plt.figure(figsize=(10,3))
    S_dB = librosa.power_to_db(S, ref=np.max)
    librosa.display.specshow(S_dB, x_axis='time',y_axis='mel', sr=16000,)
    plt.colorbar(format='%+2.0f dB')
    plt.title('Mel-frequency spectrogram')
    plt.savefig('./chunk_images/' + name + "mel.png",dpi=(300), bbox_inches='tight')
    plt.tight_layout()
    plt.show()
def featureExtraction(audioFile, name, targetSampFreq = 16000, 
                      axis =0 , 
                      hop_length= 256,
                      win_length=512):
    
    y, y_sr = readData(file=audioFile)
    print(y, y_sr)
    resampledData = resample(originalData=y, origSampFreq=y_sr, targetSampFreq=targetSampFreq)
    normalizedData = normalizeSound(resampledData, axis=axis)
    S = calculateMelSpectogram(normalizedData=normalizedData, hop_length=hop_length, win_length=win_length, sr=targetSampFreq)
    plotSound(soundData=normalizedData, sr=targetSampFreq,x_axis_string='time' , name = name)
    plotMelSpectogram(S, sr=targetSampFreq, name = name, ref=np.max)
    return S

# plot orginal time domain data

def plotSound(soundData, sr, x_axis_string, name):
    plt.figure(figsize=(10,3))
    waveplot(soundData, sr, x_axis=x_axis_string)
    plt.savefig('./chunk_images/' + name + "sound.png",dpi=(300), bbox_inches='tight')

【问题讨论】:

  • 我真的很熟悉DSP,但我不知道问题是什么。顶部图像是否显示了未经过任何平滑处理的下部图像的放大版本?你能用我们可以处理的一小部分数据(或随机值)重现这个问题吗?在任何情况下,平滑梅尔谱图都会改变它的外观。
  • 你的两张图片的时间尺度完全不同,所以很难比较它们的平滑度。

标签: python matplotlib signal-processing librosa spectrogram


【解决方案1】:

梅尔谱图的时间分辨率由hop_length 指定。 16kHz 的 256 个样本是 16 毫秒,这是一个相当高的分辨率。较低的值意味着较高的分辨率。 您可以使用大于 hop_length 的n_fft 来实现一些平滑。默认值为 n_fft = 4x hop_length,而您只有 2x。

频率分辨率由n_mels 给出,您没有指定。越高,分辨率越高。它通常在 32-256 波段范围内,典型值为 128(也是 librosa 中的默认值)。 如果你想要比频率轴更高,你最好只使用 STFT - 不应用 Mel 滤波器组。

顺便说一句,您没有将hop_length 传递给librosa.display.specshow,因此情节的时间轴很可能是错误的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-28
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 2021-03-25
    • 2017-09-08
    • 1970-01-01
    相关资源
    最近更新 更多