gpt4 book ai didi

python-3.x - Librosa Constant Q Transform (CQT) 在频谱图的开头和结尾包含缺陷

转载 作者:行者123 更新时间:2023-12-03 16:38:08 26 4
gpt4 key购买 nike

考虑以下代码

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() got an unexpected keyword argument 'center'



我做错了什么吗?如何制作 center=Falsecqt ?

最佳答案

我想你可能想试试 pad_modecqt 中得到支持.如果您查看 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>

关于python-3.x - Librosa Constant Q Transform (CQT) 在频谱图的开头和结尾包含缺陷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57532917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com