gpt4 book ai didi

python - python librosa.core.stft() 和 matlab spectrogram(x) 的输出之间的区别

转载 作者:行者123 更新时间:2023-11-28 22:11:45 28 4
gpt4 key购买 nike

我正在将 Python 代码转换为 MATLAB。 Python 代码,使用以下命令:

stft_ch = librosa.core.stft(audio_input[:, ch_cnt], n_fft=self._nfft, 
hop_length=self._hop_len, win_length=self._win_len,
window='hann')

其中 audio_input.shape=(2880000, 4)self._nfft=2048self._hop_len=960 self._win_len=1920

在转换为 MATLAB 时我使用了:

stft_ch = spectrogram(audio_input(:, ch_cnt), hann(win_len), win_len-hop_len, nfft);

我在其中验证了 size(audio_input)=2880000, 4win_len=1920win_len-hop_len=960nfft =2048

我从 MATLAB 获得了 size(stft_ch)=1025, 2999 的输出,其中 Python 显示 stft_ch.shape=(1025, 3001)。 MATLAB 输出中的大小 2999 很清楚并且超过了 documentation其中 k = ⌊(Nx – noverlap)/(length(window) – noverlap)⌋ 如果 window 是一个向量。

但是,我在 Python documentation 中找不到t 的长度是如何设置的。

为什么尺寸之间存在差异?我的转换好吗?

是否有一个 Python 函数可以产生更类似于 MATLAB 的 spectrogram() 的输出,以便我可以获得具有相同大小的复数输出?

最佳答案

我自己找到了答案。

MATLAB 函数 spectrogram()输出一个时间向量,它对应于每个窗口的中间,同时省略最后一个窗口。例如,具有 3 个样本窗口和 1 个样本重叠的 10 个样本长度信号将产生以下 4 个窗口:

1:3 , 3:5 , 5:7 , 7:9 , 其中m:n表示一个窗口,其中包含来自 m 的样本至 n包括 n第个样本。

因此, window 的中心将是:2,4,6,8 .请注意,第 10 个样本不包括在内。

似乎 MATLAB 需要最大 number_of_windows隶属于(number_of_windows-1)*hop_length+window_size<=number_of_samples .

关于python版本liberosa.core.stft()另一方面,t 是每个帧的第一个样本的时间,帧覆盖的范围比输入信号多。例如,具有 3 个样本窗口和 2 个样本跳跃(跳跃且不重叠)的 10 个样本长度信号将产生以下 4 个窗口:

1:3 , 3:5 , 5:7 , 7:9 , 9:11 , 其中m:n表示一个窗口,其中包含来自 m 的样本至 n包括 n第个样本。

因此,窗口的开头是:1,3,5,7,9 .请注意,包括第 11 个不存在的样本。

似乎 liberosa 需要最少的 number_of_windows隶属于number_of_windows*hop_length>number_of_samples .

以我为例:

(2999-1)960+1920=2880000<=2880000 对于 MATLAB。3001960=2880960>2880000 而 30000*960=2880000 !> 2880000 在 python 中。

请注意,可以通过设置 center=True 在 Python 中将时间居中旗帜。

这是我能找到的最好的解释。

关于python - python librosa.core.stft() 和 matlab spectrogram(x) 的输出之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55474581/

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