为什么我的 sample.shape 总是不同于 sample_rate

Why my sample.shape always different than sample_rate

我想生成一个频谱图,但它总是显示

   ValueError: x and y must have same first dimension, but have shapes (22050,) and (40925,) enter code here

此外,当我使用自己的 .wav 文件和这些代码时:

    audio_path = 'data/'
    filename = 'riceuniversity.wav'
    # sample_rate, samples = wavfile.read(str(audio_path) + filename)
    samples, sample_rate = librosa.load(str(audio_path)+filename)
    print(samples.shape)
    print("sample rate: ", sample_rate)

我总是得到两个不同的值,当我使用我教授的所有其他 .wav 文件时不会发生这种情况,这两个值都是 22050。

    (40925,)
    sample rate:  22050

所以当我用这些代码生成频谱图时

#def log_specgram(audio, sample_rate, window_size=20,
#                 step_size=10, eps=1e-10):
def log_specgram(audio, sample_rate=16000, window_size=20,
                 step_size=10, eps=1e-10):
    if audio.ndim > 1 : #ignore channels  2+
        audio = audio[:,0]
    nperseg = int(round(window_size * sample_rate / 1e3))
    noverlap = int(round(step_size * sample_rate / 1e3)) 
    freqs, times, spec = signal.spectrogram(audio,
                                    fs=sample_rate,
                                    window='hann',
                                    nperseg=nperseg,
                                    noverlap=noverlap,
                                    detrend=False)
    return freqs, times, np.log(spec.T.astype(np.float32) + eps)
#return freqs, times, np.log(spec.T.astype(np.float32) + eps)
freqs, times, spectrogram = log_specgram(samples, sample_rate)

fig = plt.figure(figsize=(14, 8))
ax1 = fig.add_subplot(211)
ax1.set_title('Raw wave of ' + filename)
ax1.set_ylabel('Amplitude')
ax1.plot(np.linspace(0, sample_rate/len(samples), sample_rate), samples)

ax2 = fig.add_subplot(212)
ax2.imshow(spectrogram.T, aspect='auto', origin='lower', 
           extent=[times.min(), times.max(), freqs.min(), freqs.max()])
ax2.set_yticks(freqs[::16])
ax2.set_xticks(times[::16])
ax2.set_title('Spectrogram of ' + filename)
ax2.set_ylabel('Freqs in Hz')
ax2.set_xlabel('Seconds')

我总是得到 ValueError

ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_15432/4131919736.py in <module>
      5 ax1.set_title('Raw wave of ' + filename)
      6 ax1.set_ylabel('Amplitude')
----> 7 ax1.plot(np.linspace(0, sample_rate/len(samples), sample_rate), samples)
      8 
      9 ax2 = fig.add_subplot(212)

D:\anaconda\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
   1603         """
   1604         kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1605         lines = [*self._get_lines(*args, data=data, **kwargs)]
   1606         for line in lines:
   1607             self.add_line(line)

D:\anaconda\lib\site-packages\matplotlib\axes\_base.py in __call__(self, data, *args, **kwargs)
    313                 this += args[0],
    314                 args = args[1:]
--> 315             yield from self._plot_args(this, kwargs)
    316 
    317     def get_next_color(self):

D:\anaconda\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs, return_kwargs)
    499 
    500         if x.shape[0] != y.shape[0]:
--> 501             raise ValueError(f"x and y must have same first dimension, but "
    502                              f"have shapes {x.shape} and {y.shape}")
    503         if x.ndim > 2 or y.ndim > 2:

ValueError: x and y must have same first dimension, but have shapes (22050,) and (40925,)

这是因为我的 .wav 文件有问题吗?我该如何解决?非常感谢!

文件似乎不是 1 秒。 (与其他文件不同。)首先检查“不是 sec 文件”是否可以接受,然后修复您的文件。或者更改绘图代码以接收任意长度的文件。

ax1.plot(np.linspace(0, sample_rate/len(samples), len(samples)), samples)