1

我是在这里发布问题的新手,并试图遵循适当的礼仪,所以请帮助我从问题中的任何遗漏或错误中吸取教训。

我正在使用 Specgram 来分析声学信号,并且对结果的两个方面感到困惑。

  1. 绘图似乎将频谱图的末尾放置在 bin 数组中的最后一个值的时间,这应该是最后一个 bin 的中间。我希望情节以最后一个 bin 结尾的值结束

  2. 作为时间轴返回的 bin-center 值似乎与 Matlab 中的值不同,但在我的试验中可能存在不同的参数设置。

这些问题出现在 Matplotlib 示例之一中,来自: http ://matplotlib.org/1.4.0/examples/pylab_examples/specgram_demo.html

bin 值为:0.256、0.768、1.28…..19.2、19.712。

最明显的问题是频谱图以 19.712 结束,而不是预期值 20.0。

有人可以帮忙澄清一下吗?这些问题中的任何一个似乎都代表错误吗?还是我做错了什么?

这与这个问题有关:How to make specgram fill entire figure area with matplotlib?

提前感谢您提供的任何指导。

4

2 回答 2

0

是的,情节确实在最后一个箱子的中间结束。这可能是不正确的。

但是,无论如何,它不会是 2.0,原因有两个。

首先,端点很少会与最后一个样本完美匹配,因为它被划分为NFFT具有 重叠的 -length 段noverlap,这不太可能完全适合信号的长度,除非您非常仔细地选择信号长度,段长度, 和重叠。

即便如此,它也永远不会达到 20.0,因为 numpyarange与其他 python 范围一样,不包括最后一个值。也是,即 19.9995 t.max()20.0-dt同样,这只是与 MATLAB 使用的不同约定。

使用 MATLAB 2014b 和 spectogram 函数,我使用与 matplotlib 示例相同的参数运行它,确保考虑范围的终点。我得到与 matplotlib 相同的时间点。

于 2015-04-02T10:20:26.473 回答
0

请看一下:在specgram matplotlib中切割未使用的频率

这是上面的一个版本,带有不同的参数来说明它们的效果:

从 pylab 导入 *
    从 matplotlib 导入 *
    # 100、200 和 400 Hz 正弦“波”
    # 使用更多的样本点
    dt = 0.00005
    t = arange(0.0, 20.000, dt)
    s1 = sin(2*pi*100*t)
    s2 = 2*sin(2*pi*400*t)
    s3 = 2*sin(2*pi*200*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 + +s3 + nse # the signal
#x = s1 + s2 + nse # the signal
# Longer window
NFFT = 2048       # the length of the windowing segments
Fs = int(1.0/dt)  # the sampling frequency

# modified specgram()
def my_specgram(x, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
             window=mlab.window_hanning, noverlap=128,
             cmap=None, xextent=None, pad_to=None, sides='default',
             scale_by_freq=None, minfreq = None, maxfreq = None, **kwargs):
    """
    call signature::

      specgram(x, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
               window=mlab.window_hanning, noverlap=128,
               cmap=None, xextent=None, pad_to=None, sides='default',
               scale_by_freq=None, minfreq = None, maxfreq = None, **kwargs)

    Compute a spectrogram of data in *x*.  Data are split into
    *NFFT* length segments and the PSD of each section is
    computed.  The windowing function *window* is applied to each
    segment, and the amount of overlap of each segment is
    specified with *noverlap*.

    %(PSD)s

      *Fc*: integer
        The center frequency of *x* (defaults to 0), which offsets
        the y extents of the plot to reflect the frequency range used
        when a signal is acquired and then filtered and downsampled to
        baseband.

      *cmap*:
        A :class:`matplotlib.cm.Colormap` instance; if *None* use
        default determined by rc

      *xextent*:
        The image extent along the x-axis. xextent = (xmin,xmax)
        The default is (0,max(bins)), where bins is the return
        value from :func:`mlab.specgram`

      *minfreq, maxfreq*
        Limits y-axis. Both required

      *kwargs*:

        Additional kwargs are passed on to imshow which makes the
        specgram image

      Return value is (*Pxx*, *freqs*, *bins*, *im*):

      - *bins* are the time points the spectrogram is calculated over
      - *freqs* is an array of frequencies
      - *Pxx* is a len(times) x len(freqs) array of power
      - *im* is a :class:`matplotlib.image.AxesImage` instance

    Note: If *x* is real (i.e. non-complex), only the positive
    spectrum is shown.  If *x* is complex, both positive and
    negative parts of the spectrum are shown.  This can be
    overridden using the *sides* keyword argument.

    **Example:**

    .. plot:: mpl_examples/pylab_examples/specgram_demo.py

    """

    #####################################
    # modified  axes.specgram() to limit
    # the frequencies plotted
    #####################################

    # this will fail if there isn't a current axis in the global scope
    ax = gca()
    Pxx, freqs, bins = mlab.specgram(x, NFFT, Fs, detrend,
         window, noverlap, pad_to, sides, scale_by_freq)

    # modified here
    #####################################
    if minfreq is not None and maxfreq is not None:
        Pxx = Pxx[(freqs >= minfreq) & (freqs <= maxfreq)]
        freqs = freqs[(freqs >= minfreq) & (freqs <= maxfreq)]
    #####################################

    Z = 10. * np.log10(Pxx)
    Z = np.flipud(Z)

    if xextent is None: xextent = 0, np.amax(bins)
    xmin, xmax = xextent
    freqs += Fc
    extent = xmin, xmax, freqs[0], freqs[-1]
    im = ax.imshow(Z, cmap, extent=extent, **kwargs)
    ax.axis('auto')

    return Pxx, freqs, bins, im

# plot
ax1 = subplot(211)
plot(t, x)
subplot(212, sharex=ax1)
# Windowing+greater overlap + limiting bandwidth to plot:
# the minfreq and maxfreq args will limit the frequencies 
Pxx, freqs, bins, im = my_specgram(x, NFFT=NFFT, Fs=Fs, noverlap=2000, window=numpy.kaiser(NFFT,1.0), cmap=cm.gist_heat, minfreq = 0, maxfreq = 1000)
show()
close()

于 2015-04-02T17:07:26.870 回答