Python ValueError:操作数无法与形状(1024,)(1024,2)一起广播

Python ValueError:操作数无法与形状(1024,)(1024,2)一起广播,python,matplotlib,spectrogram,Python,Matplotlib,Spectrogram,唉,我知道这与其他几个问题类似,但我这里的问题是我不太理解specgram函数在Matlab中是如何工作的。我的代码基于以下示例: 作为回报,我收到ValueError:操作数不能与形状(1024,)(1024,2)一起广播。我在这里不明白什么 编辑:完全回溯: ValueError Traceback (most recent call last) <ipython-input-13-4952b82b74f2> in &

唉,我知道这与其他几个问题类似,但我这里的问题是我不太理解specgram函数在Matlab中是如何工作的。我的代码基于以下示例:

作为回报,我收到
ValueError:操作数不能与形状(1024,)(1024,2)
一起广播。我在这里不明白什么

编辑:完全回溯:

ValueError                                Traceback (most recent call last)
<ipython-input-13-4952b82b74f2> in <module>()
     16 subplot(212, sharex=ax1)
     17 Pxx, freqs, bins, im = specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900,
---> 18                                 cmap=cm.gist_heat)
     19 show()
     20 

/Library/Python/2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.8-x86_64.egg/matplotlib/pyplot.pyc in specgram(x, NFFT, Fs, Fc, detrend, window, noverlap, cmap, xextent, pad_to, sides, scale_by_freq, hold, **kwargs)
   3145                           window=window, noverlap=noverlap, cmap=cmap,
   3146                           xextent=xextent, pad_to=pad_to, sides=sides,
-> 3147                           scale_by_freq=scale_by_freq, **kwargs)
   3148         draw_if_interactive()
   3149     finally:

/Library/Python/2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.8-x86_64.egg/matplotlib/axes.pyc in specgram(self, x, NFFT, Fs, Fc, detrend, window, noverlap, cmap, xextent, pad_to, sides, scale_by_freq, **kwargs)
   8932 
   8933         Pxx, freqs, bins = mlab.specgram(x, NFFT, Fs, detrend,
-> 8934              window, noverlap, pad_to, sides, scale_by_freq)
   8935 
   8936         Z = 10. * np.log10(Pxx)

/Library/Python/2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.8-x86_64.egg/matplotlib/mlab.pyc in specgram(x, NFFT, Fs, detrend, window, noverlap, pad_to, sides, scale_by_freq)
    467 
    468     Pxx, freqs, t = _spectral_helper(x, x, NFFT, Fs, detrend, window,
--> 469         noverlap, pad_to, sides, scale_by_freq)
    470     Pxx = Pxx.real #Needed since helper implements generically
    471 

/Library/Python/2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.8-x86_64.egg/matplotlib/mlab.pyc in _spectral_helper(x, y, NFFT, Fs, detrend, window, noverlap, pad_to, sides, scale_by_freq)
    262     for i in range(n):
    263         thisX = x[ind[i]:ind[i]+NFFT]
--> 264         thisX = windowVals * detrend(thisX)
    265         fx = np.fft.fft(thisX, n=pad_to)
    266 

ValueError: operands could not be broadcast together with shapes (1024,) (1024,2) 
ValueError回溯(最近一次调用)
在()
16子批次(212,sharex=ax1)
17 Pxx,频率,箱,im=specgram(x,NFFT=NFFT,Fs=Fs,noverlap=900,
--->18 cmap=厘米(热量)
19 show()
20
/specgram中的Library/Python/2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.8-x86_64.egg/matplotlib/pyplot.pyc
3145 window=window,noverlap=noverlap,cmap=cmap,
3146 xextent=xextent,pad_to=pad_to,sides=sides,
->3147按频率缩放=按频率缩放,**kwargs)
3148 draw_if_interactive()
3149最后:
/specgram中的Library/Python/2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.8-x86_64.egg/matplotlib/axes.pyc(self、x、NFFT、Fs、Fc、detrend、window、noverlap、cmap、xextent、pad_to、sides、scale_by_freq、**kwargs)
8932
8933 Pxx,频率,箱=mlab.specgram(x,NFFT,Fs,detrend,
->8934窗户,noverlap,墙板,侧面,按频率缩放)
8935
8936 Z=10。*np.log10(Pxx)
/specgram中的Library/Python/2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.8-x86_64.egg/matplotlib/mlab.pyc(x、NFFT、Fs、detrend、window、novelap、pad_to、sides、scale_by_freq)
467
468 Pxx,freqs,t=_spectrum_helper(x,x,NFFT,Fs,detrend,window,
-->469十一月地图,地图,侧面,按频率缩放)
470 Pxx=Pxx.real#需要,因为helper一般实现
471
/Library/Python/2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.8-x86_64.egg/matplotlib/mlab.pyc in_spectrum_helper(x、y、NFFT、Fs、detrend、window、novelap、pad_to、sides、scale_by_freq)
262对于范围内的i(n):
263 thisX=x[ind[i]:ind[i]+NFFT]
-->264 thisX=窗口VAL*detrend(thisX)
265 fx=np.fft.fft(thisX,n=pad_to)
266
ValueError:操作数无法与形状(1024,)(1024,2)一起广播

这不是代码的问题:而是WAV文件的问题。该文件有两个音频通道,因此位于数据的第二行。

您能发布完整的异常回溯吗?我假设这里的错误在specgram中得到提升?添加了完整的回溯。啊!有趣。这似乎是我的WAV文件的问题?奇怪,因为我刚刚让iTunes为我转换了…嗯。。我认为问题在于立体声文件。我有第二个,现在,由大胆创造。。。
#!/usr/bin/env python

from pylab import *
import scipy.io.wavfile

dt = 0.0005

sr, x = scipy.io.wavfile.read(fname) # the signal
NFFT = 1024       # the length of the windowing segments
Fs = int(1.0/dt)  # the sampling frequency

# Pxx is the segments x freqs array of instantaneous power, freqs is
# the frequency vector, bins are the centers of the time bins in which
# the power is computed, and im is the matplotlib.image.AxesImage
# instance

ax1 = subplot(211)
plot(x)
subplot(212, sharex=ax1)
Pxx, freqs, bins, im = specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900,
                                cmap=cm.gist_heat)
show()
ValueError                                Traceback (most recent call last)
<ipython-input-13-4952b82b74f2> in <module>()
     16 subplot(212, sharex=ax1)
     17 Pxx, freqs, bins, im = specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900,
---> 18                                 cmap=cm.gist_heat)
     19 show()
     20 

/Library/Python/2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.8-x86_64.egg/matplotlib/pyplot.pyc in specgram(x, NFFT, Fs, Fc, detrend, window, noverlap, cmap, xextent, pad_to, sides, scale_by_freq, hold, **kwargs)
   3145                           window=window, noverlap=noverlap, cmap=cmap,
   3146                           xextent=xextent, pad_to=pad_to, sides=sides,
-> 3147                           scale_by_freq=scale_by_freq, **kwargs)
   3148         draw_if_interactive()
   3149     finally:

/Library/Python/2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.8-x86_64.egg/matplotlib/axes.pyc in specgram(self, x, NFFT, Fs, Fc, detrend, window, noverlap, cmap, xextent, pad_to, sides, scale_by_freq, **kwargs)
   8932 
   8933         Pxx, freqs, bins = mlab.specgram(x, NFFT, Fs, detrend,
-> 8934              window, noverlap, pad_to, sides, scale_by_freq)
   8935 
   8936         Z = 10. * np.log10(Pxx)

/Library/Python/2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.8-x86_64.egg/matplotlib/mlab.pyc in specgram(x, NFFT, Fs, detrend, window, noverlap, pad_to, sides, scale_by_freq)
    467 
    468     Pxx, freqs, t = _spectral_helper(x, x, NFFT, Fs, detrend, window,
--> 469         noverlap, pad_to, sides, scale_by_freq)
    470     Pxx = Pxx.real #Needed since helper implements generically
    471 

/Library/Python/2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.8-x86_64.egg/matplotlib/mlab.pyc in _spectral_helper(x, y, NFFT, Fs, detrend, window, noverlap, pad_to, sides, scale_by_freq)
    262     for i in range(n):
    263         thisX = x[ind[i]:ind[i]+NFFT]
--> 264         thisX = windowVals * detrend(thisX)
    265         fx = np.fft.fft(thisX, n=pad_to)
    266 

ValueError: operands could not be broadcast together with shapes (1024,) (1024,2)