Ejemplo n.º 1
0
def spectrum(signal, Fs, N):

    import stft
    import windows

    F = stft.stft(signal, N, N / 2, win=windows.hann(N))
    stft.spectroplot(F.T, N, N / 2, Fs)
def comparePlot(signal1, signal2, Fs, fft_size=512, norm=False, equal=False, title1=None, title2=None):

    import matplotlib.pyplot as plt

    td_amp = np.maximum(np.abs(signal1).max(), np.abs(signal2).max())

    if norm:
        if equal:
            signal1 /= np.abs(signal1).max()
            signal2 /= np.abs(signal2).max()
        else:
            signal1 /= td_amp
            signal2 /= td_amp
        td_amp = 1.

    plt.subplot(2,2,1)
    plt.plot(np.arange(len(signal1))/float(Fs), signal1)
    plt.axis('tight')
    plt.ylim(-td_amp, td_amp)
    if title1 is not None:
        plt.title(title1)

    plt.subplot(2,2,2)
    plt.plot(np.arange(len(signal2))/float(Fs), signal2)
    plt.axis('tight')
    plt.ylim(-td_amp, td_amp)
    if title2 is not None:
        plt.title(title2)

    import stft
    import windows

    eps = constants.get('eps')

    F1 = stft.stft(signal1, fft_size, fft_size / 2, win=windows.hann(fft_size))
    F2 = stft.stft(signal2, fft_size, fft_size / 2, win=windows.hann(fft_size))

    # try a fancy way to set the scale to avoid having the spectrum
    # dominated by a few outliers
    p_min = 1
    p_max = 99.5
    all_vals = np.concatenate((dB(F1+eps), dB(F2+eps))).flatten()
    vmin, vmax = np.percentile(all_vals, [p_min, p_max])

    cmap = 'jet'
    interpolation='sinc'

    plt.subplot(2,2,3)
    stft.spectroplot(F1.T, fft_size, fft_size / 2, Fs, vmin=vmin, vmax=vmax,
            cmap=plt.get_cmap(cmap), interpolation=interpolation)

    plt.subplot(2,2,4)
    stft.spectroplot(F2.T, fft_size, fft_size / 2, Fs, vmin=vmin, vmax=vmax, 
            cmap=plt.get_cmap(cmap), interpolation=interpolation)
Ejemplo n.º 3
0
def comparePlot(signal1, signal2, Fs, fft_size=512, norm=False, equal=False, title1=None, title2=None):

    import matplotlib.pyplot as plt

    td_amp = np.maximum(np.abs(signal1).max(), np.abs(signal2).max())

    if norm:
        if equal:
            signal1 /= np.abs(signal1).max()
            signal2 /= np.abs(signal2).max()
        else:
            signal1 /= td_amp
            signal2 /= td_amp
        td_amp = 1.

    plt.subplot(2,2,1)
    plt.plot(np.arange(len(signal1))/float(Fs), signal1)
    plt.axis('tight')
    plt.ylim(-td_amp, td_amp)
    if title1 is not None:
        plt.title(title1)

    plt.subplot(2,2,2)
    plt.plot(np.arange(len(signal2))/float(Fs), signal2)
    plt.axis('tight')
    plt.ylim(-td_amp, td_amp)
    if title2 is not None:
        plt.title(title2)

    from constants import eps
    import stft
    import windows

    F1 = stft.stft(signal1, fft_size, fft_size / 2, win=windows.hann(fft_size))
    F2 = stft.stft(signal2, fft_size, fft_size / 2, win=windows.hann(fft_size))

    # try a fancy way to set the scale to avoid having the spectrum
    # dominated by a few outliers
    p_min = 1
    p_max = 99.5
    all_vals = np.concatenate((dB(F1+eps), dB(F2+eps))).flatten()
    vmin, vmax = np.percentile(all_vals, [p_min, p_max])

    cmap = 'jet'
    interpolation='sinc'

    plt.subplot(2,2,3)
    stft.spectroplot(F1.T, fft_size, fft_size / 2, Fs, vmin=vmin, vmax=vmax,
            cmap=plt.get_cmap(cmap), interpolation=interpolation)

    plt.subplot(2,2,4)
    stft.spectroplot(F2.T, fft_size, fft_size / 2, Fs, vmin=vmin, vmax=vmax, 
            cmap=plt.get_cmap(cmap), interpolation=interpolation)
x3 = np.floor(end*np.array([0.48, 0.64]))
y3 = np.floor(top*np.array([0.44, 0.56]))
box3 = [[x3[0],y3[0]],[x3[0],y3[1]],[x3[1],y3[1]],[x3[1],y3[0]],[x3[0],y3[0]]]

boxes = [Polygon(box1, True, fill=False, facecolor='none'),
         Polygon(box2, True, fill=False, facecolor='none'),
         Polygon(box3, True, fill=False, facecolor='none'),]
ec=np.array([0,0,0])
lw = 0.5

# Draw first the spectrograms with boxes on top
fig, ax = plt.subplots(figsize=figsize2, nrows=2, ncols=4)

ax = plt.subplot(2,4,1)
spectroplot(F0.T, fft_size+fft_zp, fft_hop, Fs, vmin=vmin, vmax=vmax,
        cmap=plt.get_cmap(cmap), interpolation=interpolation, colorbar=False)
ax.add_collection(PatchCollection(boxes, facecolor='none', edgecolor=ec, linewidth=lw))
ax.text(F0.shape[0]-300, F0.shape[1]/2-60, 'A', weight='bold')
ax.set_ylabel('')
ax.set_xlabel('')
aspect = ax.get_aspect()
ax.axis('off')

ax = plt.subplot(2,4,2)
spectroplot(F1.T, fft_size+fft_zp, fft_hop, Fs, vmin=vmin, vmax=vmax,
        cmap=plt.get_cmap(cmap), interpolation=interpolation, colorbar=False)
ax.add_collection(PatchCollection(boxes, facecolor='none', edgecolor=ec, linewidth=lw))
ax.text(F0.shape[0]-300, F0.shape[1]/2-60, 'B', weight='bold')
ax.set_ylabel('')
ax.set_xlabel('')
ax.axis('off')
    Polygon(box1, True, fill=False, facecolor='none'),
    Polygon(box2, True, fill=False, facecolor='none'),
    Polygon(box3, True, fill=False, facecolor='none'),
]
ec = np.array([0, 0, 0])
lw = 0.5

# Draw first the spectrograms with boxes on top
fig, ax = plt.subplots(figsize=figsize2, nrows=2, ncols=4)

ax = plt.subplot(2, 4, 1)
spectroplot(F0.T,
            fft_size + fft_zp,
            fft_hop,
            Fs,
            vmin=vmin,
            vmax=vmax,
            cmap=plt.get_cmap(cmap),
            interpolation=interpolation,
            colorbar=False)
ax.add_collection(
    PatchCollection(boxes, facecolor='none', edgecolor=ec, linewidth=lw))
ax.text(F0.shape[0] - 300, F0.shape[1] / 2 - 60, 'A', weight='bold')
ax.set_ylabel('')
ax.set_xlabel('')
aspect = ax.get_aspect()
ax.axis('off')

ax = plt.subplot(2, 4, 2)
spectroplot(F1.T,
            fft_size + fft_zp,