代码之家  ›  专栏  ›  技术社区  ›  Raven Cheuk

Librosa常数Q变换(CQT)在谱图的开始和结束处都存在缺陷

  •  1
  • Raven Cheuk  · 技术社区  · 5 年前

    考虑以下代码

    import numpy as np
    import matplotlib.pyplot as plt
    from librosa import cqt
    
    s = np.linspace(0,1,44100)
    x = np.sin(2*np.pi*1000*s)
    fmin=500
    
    cq_lib = cqt(x,sr=44100, fmin=fmin, n_bins=40)
    
    plt.imshow(abs(cq_lib),aspect='auto', origin='lower')
    plt.xlabel('Time Steps')
    plt.ylabel('Freq bins')
    

    enter image description here

    当你仔细观察光谱图的开头和结尾时,你会发现其中有一些缺陷。

    当只绘制第一个和最后一个时间步长时,可以看到频率不正确。

    plt.plot(abs(cq_lib)[:,0])
    plt.ylabel('Amplitude')
    plt.xlabel('Freq bins')
    plt.tick_params(labelsize=16)
    

    enter image description here

    最后一帧和第二帧比较

    plt.plot(abs(cq_lib)[:,-1])
    plt.plot(abs(cq_lib)[:,-2])
    plt.legend(['last step', '2nd last step'], fontsize=16)
    plt.ylabel('Amplitude')
    plt.xlabel('Freq bins')
    plt.tick_params(labelsize=16)
    

    enter image description here

    据我所知,这应该是由于填充和放置 stft cqt 不支持这个论点 center=False

    cq_lib = cqt(x,sr=44100, fmin=fmin, n_bins=40,center=False)
    

    TypeError:cqt()获得意外的关键字参数“center”

    我做错什么了吗?如何制作 在里面 cqt ?

    1 回复  |  直到 5 年前
        1
  •  3
  •   user11563547 user11563547    5 年前

    我想你应该试试 pad_mode 在中支持 cqt . 如果您签出np.pad documentation ,您可以看到可用的选项(或查看本文末尾)。和 wrap 选项,你会得到这样的结果,虽然我怀疑这个阶段是一团糟,所以你应该确保这符合你的需要。如果您总是生成自己的信号,可以尝试使用 <function> 而不是一个可用的选项。

    import numpy as np
    import matplotlib.pyplot as plt
    from librosa import cqt
    
    s = np.linspace(0,1,44100)
    x = np.sin(2*np.pi*1000*s)
    fmin=500
    
    cq_lib = cqt(x,sr=44100, fmin=fmin, n_bins=40, pad_mode='wrap')
    
    plt.imshow(abs(cq_lib),aspect='auto', origin='lower')
    plt.xlabel('Time Steps')
    plt.ylabel('Freq bins')
    

    enter image description here

    如果你看第一帧和最后两帧,你可以看到它现在看起来好多了。我用librosa 0.6.3和0.7.0进行了测试,结果是一样的。

    enter image description here

    enter image description here

    尝试一些选项,希望您能找到其中一个填充选项来实现此功能: np.pad options ‘constant’, ‘edge’, ‘linear_ramp’, ‘maximum’, ‘mean’,‘median’,‘minimum’, ‘reflect’, ‘symmetric’, ‘wrap’, ‘empty’, <function>