1

私はここに質問を投稿するのは初めてで、適切なエチケットに従おうとしているので、質問の省略や間違いから学ぶのを手伝ってください.

スペクグラムを使用して音響信号を分析していますが、結果の 2 つの側面について混乱しています。

  1. プロットは、ビン配列の最後の値の時点に配置されたスペクトログラムの終わりを持っているように見えます。これは、最後のビンの中央であると想定されています。最後のビンの最後値でプロットが終了することを期待しています。

  2. 時間軸として返される bin-center 値は、Matlab の値と同じではないように見えますが、私の試行ではパラメーター設定が異なる可能性があります。

これらの問題は、http: //matplotlib.org/1.4.0/examples/pylab_examples/specgram_demo.htmlの Matplotlib の例の 1 つに表示されます 。

ビンの値は、0.256、0.768、1.28…..19.2、19.712 です。

最も明白な問題は、スペクトログラムのプロットが 20.0 の期待値ではなく、19.712 で終了することです。

誰でも明確にするのを手伝ってもらえますか? これらの問題のいずれかがバグを表しているように見えますか? それとも私は何か間違ったことをしていますか?

これは、次の質問に関連しています:スペックグラムを matplotlib で図領域全体に埋める方法は?

あなたが提供できるガイダンスを前もって感謝します。

4

2 に答える 2

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 に答える