值错误:操作数形状无法广播 (1024,) (1024,2)

0 投票
1 回答
2412 浏览
提问于 2025-04-18 13:19

唉,我知道这个问题和其他几个问题很相似,但我在这里遇到的麻烦是,我不太明白Matlab中的specgram函数是怎么工作的。我是根据这个例子写代码的,链接在这里:http://matplotlib.org/examples/pylab_examples/specgram_demo.html

#!/usr/bin/env python
from pylab import *

dt = 0.0005
t = arange(0.0, 20.0, dt)
s1 = sin(2*pi*100*t)
s2 = 2*sin(2*pi*400*t)

# create a transient "chirp"
mask = where(logical_and(t>10, t<12), 1.0, 0.0)
s2 = s2 * mask

# add some noise into the mix
nse = 0.01*randn(len(t))

x = s1 + s2 + nse # 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(t, x)
subplot(212, sharex=ax1)
Pxx, freqs, bins, im = specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900,
                                cmap=cm.gist_heat)
show()

不过,我需要读取一个wav文件,所以我把代码改成了:

#!/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: operands could not be broadcast together with shapes (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) 

1 个回答

1

这不是代码的问题,而是wav文件的问题。这个文件有两个音频通道,所以数据中有第二行。

撰写回答