Librosa Constant Q Transform (CQT) 在频谱图的开始和结束处包含缺陷
Librosa Constant Q Transform (CQT) contains defects at the beginning and ending of the spectrogram
考虑以下代码
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')
它会给出这样的频谱图
仔细观察频谱图的开头和结尾,您会发现那里存在一些缺陷。
当只绘制出第一个和最后一个时间步长时,您可以看到频率不正确。
第一帧
plt.plot(abs(cq_lib)[:,0])
plt.ylabel('Amplitude')
plt.xlabel('Freq bins')
plt.tick_params(labelsize=16)
最后一帧和第二帧比较
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)
我尝试解决它
据我所知,应该是把stft
window 填充到中间的原因。但似乎 cqt
不支持 center=False
.
的论点
cq_lib = cqt(x,sr=44100, fmin=fmin, n_bins=40,center=False)
TypeError: cqt() got an unexpected keyword argument 'center'
我做错了什么吗?如何在 cqt
中制作 center=False
?
我想你可能想试试 cqt. If you checkout the np.pad documentation 支持的 pad_mode
,你可以看到可用的选项(或看到这个 post 的结尾)。使用 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')
如果您查看第一帧和最后两帧,您会发现它现在看起来好多了。我用 librosa 0.6.3 和 0.7.0 试过了,结果是一样的。
尝试一些选项,希望您能找到可以解决问题的填充选项之一:
np.pad
options:
‘constant’, ‘edge’, ‘linear_ramp’, ‘maximum’, ‘mean’,‘median’,‘minimum’, ‘reflect’, ‘symmetric’, ‘wrap’, ‘empty’, <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)
plt.imshow(abs(cq_lib),aspect='auto', origin='lower')
plt.xlabel('Time Steps')
plt.ylabel('Freq bins')
它会给出这样的频谱图
仔细观察频谱图的开头和结尾,您会发现那里存在一些缺陷。
当只绘制出第一个和最后一个时间步长时,您可以看到频率不正确。
第一帧
plt.plot(abs(cq_lib)[:,0])
plt.ylabel('Amplitude')
plt.xlabel('Freq bins')
plt.tick_params(labelsize=16)
最后一帧和第二帧比较
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)
我尝试解决它
据我所知,应该是把stft
window 填充到中间的原因。但似乎 cqt
不支持 center=False
.
cq_lib = cqt(x,sr=44100, fmin=fmin, n_bins=40,center=False)
TypeError: cqt() got an unexpected keyword argument 'center'
我做错了什么吗?如何在 cqt
中制作 center=False
?
我想你可能想试试 cqt. If you checkout the np.pad documentation 支持的 pad_mode
,你可以看到可用的选项(或看到这个 post 的结尾)。使用 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')
如果您查看第一帧和最后两帧,您会发现它现在看起来好多了。我用 librosa 0.6.3 和 0.7.0 试过了,结果是一样的。
尝试一些选项,希望您能找到可以解决问题的填充选项之一:
np.pad
options:
‘constant’, ‘edge’, ‘linear_ramp’, ‘maximum’, ‘mean’,‘median’,‘minimum’, ‘reflect’, ‘symmetric’, ‘wrap’, ‘empty’, <function>