【问题标题】:ValueError: x and y must have same first dimension, but have shapes (2140699,) and (4281398,)ValueError:x 和 y 必须具有相同的第一维,但具有形状 (2140699,) 和 (4281398,)
【发布时间】:2021-06-18 07:59:05
【问题描述】:

我使用 miniconda jupyter notebook python,我正在尝试实现一台机器(音频过滤)。我收到了这个错误,我真的不知道如何解决它。

这里我用文件路径导入了我需要的库:

import wave as we
import numpy as np
import matplotlib.pyplot as plt

dir = r'/home/pc/Downloads/Bubble audios'

这里应该绘制图表的功能:

def read_wav(wavfile, plots=True, normal=False):
    f = wavfile
    params = f.getparams()
    # print(params)
    nchannels, sampwidth, framerate, nframes = params[:4]
    strData = f.readframes(nframes)  # , string format
    waveData = np.frombuffer(strData, dtype=np.int16) # Convert a string to an int
    # wave amplitude normalization
    if normal == True:
        waveData = waveData*1.0/(max(abs(waveData)))
    # 
    if plots == True:
        time = np.arange(0, nframes ,dtype=np.int16) *(1.0 / framerate)
        plt.figure(dpi=100)
        plt.plot(time, waveData)
        plt.xlabel("Time")
        plt.ylabel("Amplitude")
        plt.title("Single channel wavedata")
        plt.show()
        
    return (Wave, time)

def fft_wav(waveData, plots=True):
    f_array = np.fft.fft(waveData)  # Fourier transform, the result is a complex array
    f_abs = f_array
    axis_f = np.linspace(0, 250, np.int(len(f_array)/2))  # map to 250
    # axis_f = np.linspace(0, 250, np.int(len(f_array))) # map to 250
    if plots == True:
        plt.figure(dpi=100)
        plt.plot(axis_f, np.abs(f_abs[0:len(axis_f)]))
        # plt.plot(axis_f, np.abs(f_abs))
        plt.xlabel("Frequency")
        plt.ylabel("Amplitude spectrum")
        plt.title("Tile map")
        plt.show()
    return f_abs

在这里,我用我想要读取和绘制的文件调用函数。

f = we.open(dir+r'/Ars1_Aufnahme.wav', 'rb')
Wave, time = read_wav(f)

我得到的错误:

ValueError: x and y must have same first dimension, but have shapes (2140699,) and (4281398,)

我尝试使用 np.reshape 但它不起作用,或者我可能用错了。那么,有什么建议吗?

【问题讨论】:

  • 你确定你的 .wav 文件是单声道的吗?我尝试使用单声道文件,您的函数正确返回了绘图。

标签: python-3.x numpy jupyter-notebook anaconda miniconda


【解决方案1】:

看来你的时间是你的波浪大小的 1/2。也许你的nframe 太短了。如果你这样做 nframses = 2*nframes 是什么错误?

【讨论】:

  • 我编辑到 2*nframes 并且它有效。谢谢
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-01
  • 2021-06-30
  • 2016-11-10
  • 1970-01-01
相关资源
最近更新 更多