繁体   English   中英

ValueError: x 和 y 必须具有相同的第一维,但具有形状 (2140699,) 和 (4281398,)

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

我使用 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 但它没有用,或者我可能用错了。 那么,有什么建议吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM