예제 #1
0
def compare_timefreqs(s, sample_rate, win_sizes=[0.050, 0.100, 0.250, 0.500, 1.25]):
    """
        Compare the time frequency representation of a signal using different window sizes and estimators.
    """

    #construct different types of estimators
    gaussian_est = GaussianSpectrumEstimator(nstd=6)
    mt_est_lowbw = MultiTaperSpectrumEstimator(bandwidth=10.0, adaptive=False)
    mt_est_lowbw_adapt = MultiTaperSpectrumEstimator(bandwidth=10.0, adaptive=True, max_adaptive_iter=150)
    mt_est_lowbw_jn = MultiTaperSpectrumEstimator(bandwidth=10.0, adaptive=False, jackknife=True)
    mt_est_highbw = MultiTaperSpectrumEstimator(bandwidth=30.0, adaptive=False)
    mt_est_highbw_adapt = MultiTaperSpectrumEstimator(bandwidth=30.0, adaptive=True, max_adaptive_iter=150)
    mt_est_highbw_jn = MultiTaperSpectrumEstimator(bandwidth=30.0, adaptive=False, jackknife=True)
    wavelet = WaveletSpectrumEstimator(num_cycles_per_window=10, min_freq=1, max_freq=sample_rate/2, num_freqs=50, nstd=6)
    #estimators = [gaussian_est, mt_est_lowbw, mt_est_lowbw_adapt, mt_est_highbw, mt_est_highbw_adapt]
    estimators = [wavelet]
    #enames = ['gauss', 'lowbw', 'lowbw_a', 'highbw', 'highbw_a']
    enames = ['wavelet']

    #run each estimator for each window size and plot the amplitude of the time frequency representation
    plt.figure()
    spnum = 1
    for k,win_size in enumerate(win_sizes):
        increment = 1.0 / sample_rate
        for j,est in enumerate(estimators):
            t,freq,tf = timefreq(s, sample_rate, win_size, increment, est)
            print 'freq=',freq
            ax = plt.subplot(len(win_sizes), len(estimators), spnum)
            plot_spectrogram(t, freq, np.abs(tf), ax=ax, colorbar=True, ticks=True)
            if k == 0:
                plt.title(enames[j])
            #if j == 0:
                #plt.ylabel('%d ms' % (win_size*1000))
            spnum += 1
예제 #2
0
def plotSoundSeg(fig, seg):
    # fig pointer to figure
    # seg is the segment
    # returns the filtered sound from the loudest of the two signals.

    # clear figure
    fig.clear()

    # The sound signal
    soundSnip = seg.analogsignals[1]
    fs = soundSnip.sampling_rate
    tvals = soundSnip.times

    # Calculate the rms of each mic
    rms0 = np.std(soundSnip[:, 0])
    rms1 = np.std(soundSnip[:, 1])

    # Choose the loudest to plot
    if rms1 > rms0:
        sound = np.asarray(soundSnip[:, 1]).squeeze()
    else:
        sound = np.asarray(soundSnip[:, 0]).squeeze()

    # Calculate envelope and spectrogram
    sound = sound - sound.mean()
    sound_env = lowpass_filter(np.abs(sound), float(fs),
                               250.0)  # Sound Enveloppe
    to, fo, spect, rms = spectrogram(sound, float(fs), 1000, 50)

    # Plot sonogram and spectrogram
    gs = gridspec.GridSpec(100, 1)

    ax = fig.add_subplot(gs[0:20, 0])

    ax.plot(tvals - tvals[0], sound / sound.max())
    ax.plot(tvals - tvals[0],
            sound_env / sound_env.max(),
            color="red",
            linewidth=2)
    plt.title('%s %d' % (seg.name, seg.index))
    plt.xlim(0.0, to[-1])

    ax = fig.add_subplot(gs[21:, 0])
    plot_spectrogram(to,
                     fo,
                     spect,
                     ax=ax,
                     ticks=True,
                     fmin=250,
                     fmax=8000,
                     colormap=None,
                     colorbar=False,
                     log=True,
                     dBNoise=50)
    plt.ylabel('Frequency')
    plt.tick_params(labelbottom='off')
    plt.xlim(0.0, to[-1])
    plt.show()
    return sound, fs
예제 #3
0
    def test_delta(self):

        dur = 30.
        sample_rate = 1e3
        nt = int(dur * sample_rate)
        t = np.arange(nt) / sample_rate
        freqs = np.linspace(0.5, 1.5, nt)
        # freqs = np.ones_like(t)*2.
        s = np.sin(2 * np.pi * freqs * t)

        center_freqs = np.arange(0.5, 4.5, 0.5)

        psi = lambda _t, _f, _bw: (np.pi * _bw**2)**(-0.5) * np.exp(
            2 * np.pi * complex(0, 1) * _f * _t) * np.exp(-_t**2 / _bw**2)
        """
        scalogram = np.zeros([len(center_freqs), nt])
        bandwidth = 1.
        nstd = 6
        nwt = int(bandwidth*nstd*sample_rate)
        wt = np.arange(nwt) / sample_rate

        for k,f in enumerate(center_freqs):
            w = psi(wt, f, bandwidth)
            scalogram[k, :] = convolve1d(s, w)
        """

        win_len = 2.
        spec_t, spec_freq, spec, spec_rms = gaussian_stft(
            s, sample_rate, win_len, 100e-3)

        fi = (spec_freq < 10) & (spec_freq > 0)

        plt.figure()
        gs = plt.GridSpec(100, 1)
        ax = plt.subplot(gs[:30, 0])
        plt.plot(t, s, 'k-', linewidth=4.0, alpha=0.7)

        wa = WaveletAnalysis(s, dt=1. / sample_rate, frequency=True)

        ax = plt.subplot(gs[35:, 0])
        power = wa.wavelet_power
        scales = wa.scales
        t = wa.time
        T, S = np.meshgrid(t, scales)
        # ax.contourf(T, S, power, 100)
        # ax.set_yscale('log')
        # plt.imshow(np.abs(scalogram)**2, interpolation='nearest', aspect='auto', cmap=plt.cm.afmhot_r, origin='lower',
        #            extent=[t.min(), t.max(), min(center_freqs), max(center_freqs)])
        plot_spectrogram(spec_t,
                         spec_freq[fi],
                         np.abs(spec[fi, :])**2,
                         ax=ax,
                         colorbar=False,
                         colormap=plt.cm.afmhot_r)
        plt.plot(t, freqs, 'k-', alpha=0.7, linewidth=4.0)
        plt.axis('tight')
        plt.show()
def get_temporary_template(i = 17):
    """
        Returns a spectrogram and a template, if i = 17 it's some sort of song
        Really just a placeholder for a better template algorithm
        TODO replace this with real code, like user defined sections of spectrograms
    """
    template = mic[sound_onset[i]:sound_offset[i]]
    template = template[4800:18000] #shift it, particular to this template
    templ_t, templ_freq, templ_timefreq, templ_rms = spectrogram(template, fs_mic, spec_sample_rate = 1000, freq_spacing = 50)
    figure()    
    plot_spectrogram(templ_t, templ_freq, templ_timefreq, dBNoise=80, colorbar = False)
    return template, templ_t, templ_freq, templ_timefreq
예제 #5
0
def compare_timefreqs(s,
                      sample_rate,
                      win_sizes=[0.050, 0.100, 0.250, 0.500, 1.25]):
    """
        Compare the time frequency representation of a signal using different window sizes and estimators.
    """

    #construct different types of estimators
    gaussian_est = GaussianSpectrumEstimator(nstd=6)
    mt_est_lowbw = MultiTaperSpectrumEstimator(bandwidth=10.0, adaptive=False)
    mt_est_lowbw_adapt = MultiTaperSpectrumEstimator(bandwidth=10.0,
                                                     adaptive=True,
                                                     max_adaptive_iter=150)
    mt_est_lowbw_jn = MultiTaperSpectrumEstimator(bandwidth=10.0,
                                                  adaptive=False,
                                                  jackknife=True)
    mt_est_highbw = MultiTaperSpectrumEstimator(bandwidth=30.0, adaptive=False)
    mt_est_highbw_adapt = MultiTaperSpectrumEstimator(bandwidth=30.0,
                                                      adaptive=True,
                                                      max_adaptive_iter=150)
    mt_est_highbw_jn = MultiTaperSpectrumEstimator(bandwidth=30.0,
                                                   adaptive=False,
                                                   jackknife=True)
    wavelet = WaveletSpectrumEstimator(num_cycles_per_window=10,
                                       min_freq=1,
                                       max_freq=sample_rate / 2,
                                       num_freqs=50,
                                       nstd=6)
    #estimators = [gaussian_est, mt_est_lowbw, mt_est_lowbw_adapt, mt_est_highbw, mt_est_highbw_adapt]
    estimators = [wavelet]
    #enames = ['gauss', 'lowbw', 'lowbw_a', 'highbw', 'highbw_a']
    enames = ['wavelet']

    #run each estimator for each window size and plot the amplitude of the time frequency representation
    plt.figure()
    spnum = 1
    for k, win_size in enumerate(win_sizes):
        increment = 1.0 / sample_rate
        for j, est in enumerate(estimators):
            t, freq, tf = timefreq(s, sample_rate, win_size, increment, est)
            print 'freq=', freq
            ax = plt.subplot(len(win_sizes), len(estimators), spnum)
            plot_spectrogram(t,
                             freq,
                             np.abs(tf),
                             ax=ax,
                             colorbar=True,
                             ticks=True)
            if k == 0:
                plt.title(enames[j])
            #if j == 0:
            #plt.ylabel('%d ms' % (win_size*1000))
            spnum += 1
def plot_high_corrs(sound_onset,
                    high_corrs,
                    x,
                    y,
                    mic,
                    fs_mic,
                    template_correlations,
                    amplitude_correlations,
                    corr_ind,
                    amp_ind,
                    plot_figures=0):
    """
        Aligns and plots a given template against the microphone channel. Returns alignment points.
        Requires high_corrs, an array of which sounds you want to align (index of sound_onsets), 
        and template (x, z, int), the template you are matching to.
        
    """
    alignments = list()
    amp_alignments = list()
    figure(1)
    title('template')
    plot_zscore_spectrogram(template_t[x], template_freq[x],
                            template_timefreq[x])
    #    plot_zscore_spectrogram(template_t[y], template_freq[y], template_timefreq[y])

    for sound in high_corrs:
        align = corr_ind[x][sound]
        a_align = amp_ind[x][sound]
        good_align = np.int(sound_onset[sounds[sound]]) + align - np.int(
            np.round(.5 * len(template_correlations[x][sound])))
        amp_good_align = np.int(sound_onset[sounds[sound]]) + a_align - np.int(
            np.round(.5 * len(amplitude_correlations[x][sound])))
        sound_align_wav = mic[good_align - fs_mic:good_align + 2 * fs_mic]
        amp_align_wav = mic[amp_good_align - fs_mic:amp_good_align +
                            2 * fs_mic]
        if plot_figures:
            sound_t, sound_freq, sound_timefreq, sound_rms = spectrogram(
                np.squeeze(sound_align_wav),
                fs_mic,
                spec_sample_rate=1000,
                freq_spacing=50)

            figure(2)
            plot_spectrogram(sound_t, sound_freq, sound_timefreq)
            pause(.1)
            close(2)
        alignments.append(good_align)
        amp_alignments.append(amp_good_align)
    return alignments, amp_alignments
예제 #7
0
def plot_specs_with_env(ax, rp, stim_id, trial=3):

    # plot distance call w/ amp envelope
    i = (rp.event_df.stim_id == stim_id) & (rp.event_df.trial == trial)
    assert i.sum() == 1

    stime = rp.event_df[i].start_time.values[0]
    etime = rp.event_df[i].end_time.values[0]
    si = int(stime*rp.sample_rate)
    ei = int(etime * rp.sample_rate)

    spec = rp.U[si:ei, :].T
    spec_freq = rp.spec_freq
    spec_env = spec.sum(axis=0)
    spec_env /= spec_env.max()
    spec_t = np.arange(spec.shape[1]) / rp.sample_rate

    spec_env *= (spec_freq.max() - spec_freq.min())
    spec_env += spec_freq.min()
    plot_spectrogram(spec_t, spec_freq, spec, ax=ax, ticks=True, fmax=8000., colormap='SpectroColorMap', colorbar=False)
    plt.plot(spec_t, spec_env, 'k-', linewidth=5.0, alpha=0.7)
    plt.axis('tight')
 high_amp_clean = np.zeros(high_amp.shape)
 high_amp_suppressed = np.zeros(high_amp.shape)
 neural_signal_env = np.zeros(high_amp.shape)
 neural_noise_env = np.zeros(high_amp.shape)
 neural_clean_env = np.zeros(high_amp.shape)
 GS = 6  # Gain for the sigmoid
 if i == 0:
     # update
     ax = fig_clean.add_subplot(gs[0:29, 0])
     to, fo, spect, rms = spectrogram(mic_slice, sample_rate, 1000, 50)
     plot_spectrogram(to,
                      fo,
                      spect,
                      ax=ax,
                      ticks=False,
                      fmin=250,
                      fmax=8000,
                      colormap=None,
                      colorbar=False,
                      log=True,
                      dBNoise=50)
     ax = fig_raw.add_subplot(gs[0:29, 0])
     to, fo, spect, rms = spectrogram(mic_slice, sample_rate, 1000, 50)
     plot_spectrogram(to,
                      fo,
                      spect,
                      ax=ax,
                      ticks=False,
                      fmin=250,
                      fmax=8000,
                      colormap=None,
freq_index = []
iter = 0
for i in range(len(templ_freq[0])):
    if (templ_freq[0][i] > low_freq_corr) & (templ_freq[0][i] < high_freq_corr):
        freq_index.append(i)
        iter += iter
for i in range(len(templ_timefreq)):
    templ_freq[i] = templ_freq[i][freq_index]
    templ_timefreq[i] = templ_timefreq[i][freq_index,:]
    templ_timefreq[i] = zscore(templ_timefreq[i], axis = None)


if plot_templates:
    for i in range(len(templ_timefreq)):
        figure()
        plot_spectrogram(templ_t[i], templ_freq[i], templ_timefreq[i], dBNoise=80, colorbar = False)
#==============================================================================
# loop through every sound_onset and calculate correlations with templates 
template_corr = list()
template_corr_peak = list()
for jj in range(len(templ_freq)):   
    corr_list = list()
    peak_list = list()
    longest_corr = 0
    for i in range(len(sound_onset)):
        sound_wav = mic[sound_onset[i]:sound_offset[i]]
        spect_corr = get_spect_corr(sound_wav, templ_timefreq[jj], freq_index, fs_mic)
        corr_list.append(spect_corr)
        peaks = argrelextrema(spect_corr, np.greater, order = 100) # seems to work ok / except now it maybe doesn't
        peak_list = np.append(peak_list, peaks)
        if len(spect_corr) > longest_corr: # just keeps track of the longest corr so we can pad the others with NAN later
예제 #10
0
def draw_figures(stim_event, stim_ids, syllable_indices, e1=5, e2=2):

    assert isinstance(stim_event, StimEventTransform)

    sample_rate = stim_event.lfp_sample_rate
    lags_ms = get_lags_ms(sample_rate)

    cross_functions_total = dict()
    cross_functions_locked = dict()
    cross_functions_nonlocked = dict()
    specs = dict()
    syllable_times = dict()
    stim_end_time = dict()

    hemi = ','.join(stim_event.rcg_names)

    # compute all cross and auto-correlations
    if hemi == 'L':
        electrode_order = ROSTRAL_CAUDAL_ELECTRODES_LEFT
    else:
        electrode_order = ROSTRAL_CAUDAL_ELECTRODES_RIGHT

    freqs = None
    nelectrodes = None
    index2electrode = stim_event.index2electrode

    seg_uname = stim_event.seg_uname
    bird,block,seg = seg_uname.split('_')
    psd_stats = get_psd_stats(bird, stim_event.block_name, stim_event.segment_name, hemi)

    for stim_id,syllable_index in zip(stim_ids, syllable_indices):
        lfp = stim_event.lfp_reps_by_stim['raw'][stim_id]
        ntrials,nelectrodes,nt = lfp.shape

        # get the start and end time of the syllable
        i = (stim_event.segment_df['stim_id'] == stim_id) & (stim_event.segment_df['order'] == syllable_index)
        assert i.sum() > 0, "No syllable for stim_id=%d, order=%d" % (stim_id, syllable_index)
        assert i.sum() == 1, "More than one syllable for stim_id=%d, order=%d" % (stim_id, syllable_index)
        start_time = stim_event.segment_df[i]['start_time'].values[0]
        end_time = stim_event.segment_df[i]['end_time'].values[0]
        syllable_times[stim_id] = (start_time, end_time)

        # get the end time of the last syllable
        i = (stim_event.segment_df['stim_id'] == stim_id)
        stim_end_time[stim_id] = stim_event.segment_df[i]['end_time'].max()

        si = int((stim_event.pre_stim_time + start_time)*sample_rate)
        ei = int((stim_event.pre_stim_time + end_time)*sample_rate)

        # restrict the lfp to just the syllable time
        lfp = lfp[:, :, si:ei]
        specs[stim_id] = stim_event.spec_by_stim[stim_id]

        i1 = index2electrode.index(e1)
        lfp1 = lfp[:, i1, :]

        i2 = index2electrode.index(e2)
        lfp2 = lfp[:, i2, :]

        # compute the covariance functions
        pfreq, psd1, psd2, psd1_ms, psd2_ms, c12, c12_nonlocked, c12_total = compute_spectra_and_coherence_single_electrode(lfp1, lfp2,
                                                                                                          sample_rate,
                                                                                                          e1, e2,
                                                                                                          psd_stats=psd_stats)
        cross_functions_total[stim_id] = (psd1, psd2, c12_total)
        cross_functions_locked[stim_id] = (psd1, psd2, c12)
        cross_functions_nonlocked[stim_id] = (psd1, psd2, c12_nonlocked)

    # plot the cross covariance functions to overlap for each stim
    plot_cross_pair(stim_ids, cross_functions_total, electrode_order, freqs, lags_ms)
    plt.suptitle('Total Covariance')
    fname = os.path.join(get_this_dir(), 'cross_total.svg')
    plt.savefig(fname, facecolor='w', edgecolor='none')

    # plot_cross_mat(stim_ids, cross_functions_locked, electrode_order, freqs)
    plot_cross_pair(stim_ids, cross_functions_locked, electrode_order, freqs, lags_ms)
    plt.suptitle('Stim-locked Covariance')
    fname = os.path.join(get_this_dir(), 'cross_locked.svg')
    plt.savefig(fname, facecolor='w', edgecolor='none')

    # plot_cross_mat(stim_ids, cross_functions_nonlocked, electrode_order, freqs)
    plot_cross_pair(stim_ids, cross_functions_nonlocked, electrode_order, freqs, lags_ms)
    plt.suptitle('Non-locked Covariance')
    fname = os.path.join(get_this_dir(), 'cross_nonlocked.svg')
    plt.savefig(fname, facecolor='w', edgecolor='none')

    # plot the spectrograms
    fig_height = 2
    fig_max_width = 6
    spec_lens = np.array([stim_end_time[stim_id] for stim_id in stim_ids])
    spec_ratios = spec_lens / spec_lens.max()
    spec_sample_rate = sample_rate

    for k,stim_id in enumerate(stim_ids):
        spec = specs[stim_id]
        syllable_start,syllable_end = syllable_times[stim_id]
        print 'stim_id=%d, syllable_start=%0.3f, syllable_end=%0.3f' % (stim_id, syllable_start, syllable_end)
        spec_t = np.arange(spec.shape[1]) / spec_sample_rate
        stim_end = stim_end_time[stim_id]

        figsize = (spec_ratios[k]*fig_max_width, fig_height)
        fig = plt.figure(figsize=figsize)
        ax = plt.gca()
        plot_spectrogram(spec_t, stim_event.spec_freq, spec, ax=ax, ticks=True, fmin=300., fmax=8000.,
                         colormap='SpectroColorMap', colorbar=False)
        plt.axvline(syllable_start, c='k', linestyle='dashed', linewidth=3.0)
        plt.axvline(syllable_end, c='k', linestyle='dashed', linewidth=3.0)
        plt.xlim(0, stim_end+0.005)
        fname = os.path.join(get_this_dir(), 'stim_spec_%d.svg' % stim_id)
예제 #11
0
import matplotlib.pyplot as plt
from scipy.io.wavfile import read
import numpy as np
from lasp.timefreq import gaussian_stft
from lasp.sound import plot_spectrogram
import pdb

fname = 'DCCalls16x16/fbird8call1.wav'
wf = read(fname, 'r')[1]

# Frames: 30870.00d, Rate: 44100.00

wl = 0.007  # 7ms
ic = 0.001  # 1ms

t, freq, timefreq, rms = gaussian_stft(wf, 44100, wl, ic)

spec = np.abs(timefreq)
spec = spec / spec.max()
nz = spec > 0
spec[nz] = 20 * np.log10(spec[nz]) + 50
spec[spec < 0] = 0

plot_spectrogram(t, freq, spec)
plt.show()
예제 #12
0
def draw_figures(bird, block, segment, hemi, e1, e2, stim_id, syllable_index, data_dir='/auto/tdrive/mschachter/data', exp=None):

    # load up the experiment
    if exp is None:
        bird_dir = os.path.join(data_dir, bird)
        exp_file = os.path.join(bird_dir, '%s.h5' % bird)
        stim_file = os.path.join(bird_dir, 'stims.h5')
        exp = Experiment.load(exp_file, stim_file)
    seg = exp.get_segment(block, segment)

    # get the start and end times of the stimulus
    etable = exp.get_epoch_table(seg)

    i = etable['id'] == stim_id
    stim_times = zip(etable[i]['start_time'].values, etable[i]['end_time'].values)
    stim_times.sort(key=operator.itemgetter(0))
    stim_times = np.array(stim_times)

    stim_durs = stim_times[:, 1] - stim_times[:, 0]
    stim_dur = stim_durs.min()

    # aggregate the LFPs across trials
    lfps = list()
    sample_rate = None
    electrode_indices = None
    for start_time,end_time in stim_times:

        # get a slice of the LFP
        lfp_data = exp.get_lfp_slice(seg, start_time, end_time)
        electrode_indices,the_lfps,sample_rate = lfp_data[hemi]

        stim_dur_i = int(stim_dur*sample_rate)
        lfps.append(the_lfps[:, :stim_dur_i])
    lfps = np.array(lfps)

    # rescale the LFPs to they are in uV
    lfps *= 1e6

    # get the log spectrogram of the stimulus
    start_time_0 = stim_times[0][0]
    end_time_0 = stim_times[0][1]
    stim_spec_t,stim_spec_freq,stim_spec = exp.get_spectrogram_slice(seg, start_time_0, end_time_0)
    stim_spec_t = np.linspace(0, stim_dur, len(stim_spec_t))
    stim_spec_dt = np.diff(stim_spec_t)[0]
    nz = stim_spec > 0
    stim_spec[nz] = 20*np.log10(stim_spec[nz]) + 100
    stim_spec[stim_spec < 0] = 0

    # get the amplitude envelope
    amp_env = stim_spec.std(axis=0, ddof=1)
    amp_env -= amp_env.min()
    amp_env /= amp_env.max()

    # segment the amplitude envelope into syllables
    merge_thresh = int(0.002*sample_rate)
    events = break_envelope_into_events(amp_env, threshold=0.05, merge_thresh=merge_thresh)

    # translate the event indices into actual times
    events *= stim_spec_dt

    syllable_start,syllable_end,syllable_max_amp = events[syllable_index]
    syllable_start -= 0.005
    syllable_end += 0.010
    amp_env_rs = amp_env*(stim_spec_freq.max() - stim_spec_freq.min()) + stim_spec_freq.min()
    last_syllable_end = events[-1, 1] + 0.025

    i1 = electrode_indices.index(e1)
    i2 = electrode_indices.index(e2)

    lfp1 = lfps[:, i1, :]
    lfp2 = lfps[:, i2, :]

    # zscore the LFP
    lfp1 -= lfp1.mean()
    lfp1 /= lfp1.std(ddof=1)
    lfp2 -= lfp2.mean()
    lfp2 /= lfp2.std(ddof=1)

    ntrials,nelectrodes,nt = lfps.shape
    ntrials_to_plot = 5

    t = np.arange(nt) / sample_rate

    # plot the stimulus and raw LFP for two electrodes
    figsize = (24.0, 10)
    fig = plt.figure(figsize=figsize)
    plt.subplots_adjust(top=0.95, bottom=0.05, left=0.03, right=0.99, hspace=0.10)
    gs = plt.GridSpec(3, 100)

    ax = plt.subplot(gs[0, :60])
    plot_spectrogram(stim_spec_t, stim_spec_freq, stim_spec, ax=ax, ticks=True, fmin=300, fmax=8000,
                     colormap='SpectroColorMap', colorbar=False)
    # plt.plot(stim_spec_t, amp_env_rs, 'k-', linewidth=2.0, alpha=0.50)
    plt.axvline(syllable_start, c='k', linestyle='dashed', linewidth=3.0)
    plt.axvline(syllable_end, c='k', linestyle='dashed', linewidth=3.0)
    plt.axis('tight')
    plt.xlim(0, last_syllable_end)

    # plot the first LFP (all trials)
    ax = plt.subplot(gs[1, :60])
    for k in range(ntrials_to_plot):
        plt.plot(t, lfp1[k, :], '-', linewidth=2.0, alpha=0.75)
    plt.plot(t, lfp1.mean(axis=0), 'k-', linewidth=3.0)
    plt.axvline(syllable_start, c='k', linestyle='dashed', linewidth=3.0)
    plt.axvline(syllable_end, c='k', linestyle='dashed', linewidth=3.0)
    plt.xlabel('Time (ms)')
    plt.ylabel('E%d (z-scored)' % e1)
    plt.axis('tight')
    plt.xlim(0, last_syllable_end)

    ax = plt.subplot(gs[2, :60])
    for k in range(ntrials_to_plot):
        plt.plot(t, lfp2[k, :], '-', linewidth=2.0, alpha=0.75)
    plt.plot(t, lfp2.mean(axis=0), 'k-', linewidth=3.0)
    plt.axvline(syllable_start, c='k', linestyle='dashed', linewidth=3.0)
    plt.axvline(syllable_end, c='k', linestyle='dashed', linewidth=3.0)
    plt.xlabel('Time (ms)')
    plt.ylabel('E%d (z-scored)' % e2)
    plt.axis('tight')
    plt.xlim(0, last_syllable_end)

    # restrict the lfps to a single syllable
    print 'syllable_start=%f, syllable_end=%f' % (syllable_start, syllable_end)
    syllable_si = int(syllable_start*sample_rate)
    syllable_ei = int(syllable_end*sample_rate)
    lfp1 = lfp1[:, syllable_si:syllable_ei]
    lfp2 = lfp2[:, syllable_si:syllable_ei]

    # compute the trial averaged and mean subtracted lfps
    lfp1_mean,lfp1_ms = compute_avg_and_ms(lfp1)
    lfp2_mean,lfp2_ms = compute_avg_and_ms(lfp2)

    psd_stats = get_psd_stats(bird, block, segment, hemi)
    freqs, psd1, psd2, psd1_ms, psd2_ms, c12, c12_nonlocked, c12_total = compute_spectra_and_coherence_single_electrode(lfp1, lfp2, sample_rate, e1, e2, psd_stats=psd_stats)
    lags_ms = get_lags_ms(sample_rate)

    lfp_absmax = max(np.abs(lfp1_mean).max(), np.abs(lfp2_mean).max())
    lfp_ms_absmax = max(np.abs(lfp1_ms).max(), np.abs(lfp2_ms).max())

    ax = plt.subplot(gs[0, 65:80])
    plt.axhline(0, c='k')
    plt.plot(t[syllable_si:syllable_ei], lfp1_mean, 'k-', linewidth=3.0, alpha=1.)
    plt.plot(t[syllable_si:syllable_ei], lfp2_mean, '-', c='#c0c0c0', linewidth=3.0)
    # plt.xlabel('Time (s)')
    plt.ylabel('Trial-avg LFP')
    leg = custom_legend(['k', '#c0c0c0'], ['E%d' % e1, 'E%d' % e2])
    plt.legend(handles=leg, fontsize='x-small')
    plt.axis('tight')
    plt.ylim(-lfp_absmax, lfp_absmax)

    ax = plt.subplot(gs[0, 85:])
    plt.axhline(0, c='k')
    plt.plot(t[syllable_si:syllable_ei], lfp1_ms, 'k-', linewidth=3.0, alpha=1.)
    plt.plot(t[syllable_si:syllable_ei], lfp2_ms, '-', c='#c0c0c0', linewidth=3.0)
    # plt.xlabel('Time (s)')
    plt.ylabel('Mean-sub LFP')
    plt.legend(handles=leg, fontsize='x-small')
    plt.axis('tight')
    plt.ylim(-lfp_ms_absmax, lfp_ms_absmax)

    psd_max = max(psd1.max(), psd2.max(), psd1_ms.max(), psd2_ms.max())

    ax = plt.subplot(gs[1, 65:80])
    plt.axhline(0, c='k')
    plt.plot(freqs, psd1, 'k-', linewidth=3.0)
    plt.plot(freqs, psd2, '-', c='#c0c0c0', linewidth=3.0)
    plt.xlabel('Time (s)')
    plt.ylabel('Trial-avg Power')
    plt.legend(handles=leg, fontsize='x-small', loc=2)
    plt.axis('tight')
    # plt.ylim(0, psd_max)

    ax = plt.subplot(gs[1, 85:])
    plt.axhline(0, c='k')
    plt.plot(freqs, psd1_ms, 'k-', linewidth=3.0)
    plt.plot(freqs, psd2_ms, '-', c='#c0c0c0', linewidth=3.0)
    plt.xlabel('Time (s)')
    plt.ylabel('Mean-sub Power')
    plt.legend(handles=leg, fontsize='x-small', loc=2)
    plt.axis('tight')
    # plt.ylim(0, psd_max)

    ax = plt.subplot(gs[2, 65:80])
    plt.axhline(0, c='k')
    plt.axvline(0, c='k')
    plt.plot(lags_ms, c12_total, 'k-', linewidth=3.0, alpha=0.75)
    plt.plot(lags_ms, c12, '-', c='r', linewidth=3.0, alpha=0.75)
    plt.plot(lags_ms, c12_nonlocked, '-', c='b', linewidth=3.0, alpha=0.75)
    plt.xlabel('Lags (ms)')
    plt.ylabel('Coherency')
    leg = custom_legend(['k', 'r', 'b'], ['Raw', 'Trial-avg', 'Mean-sub'])
    plt.legend(handles=leg, fontsize='x-small')
    plt.axis('tight')
    plt.ylim(-0.2, 0.3)

    fname = os.path.join(get_this_dir(), 'raw+coherency.svg')
    plt.savefig(fname, facecolor='w', edgecolor='none')
        sound_onsets[0] = -1 # the case where sound onset happens on the first data point
    if sound_onsets[i] == 1:
        break 


# TODO Next is to iterate through vocal_density and check spectrograms, but I'm running out of time for now
# the idea will be to calculate xcorrs of both amplitude waveform and the spectrogram (row by row). Both will be zscored.
# this should give us 
#for i in range(vocal_density.shape[0]):
#    if vocal_density[i] > density_thresh:
#        t, freq, timefreq, rms = spectrogram(mic[i*(window-overlap):i*(window-overlap)+window], fs_mic, 1000, 50)
#        plot_spectrogram(t, freq, timefreq, dBNoise=80)

# xcorr for envelope and spectrogram, then make it slide, zscore, correlate


plot(vocal_density[0:200])
i = 0
t, freq, timefreq, rms = spectrogram(mic[i*(window-overlap):50*(window-overlap)+window], fs_mic, 1000, 50)
plot_spectrogram(t, freq, timefreq, dBNoise=80)

i = 75
t, freq, timefreq, rms = spectrogram(mic[i*(window-overlap):(i+25)*(window-overlap)+window], fs_mic, 1000, 50)
plot_spectrogram(t, freq, timefreq, dBNoise=80)


# mic[36600000:36750000] has a song
# t, freq, timefreq, rms = spectrogram(mic[36600000:36750000], fs_mic, 1000, 50)
# plot_spectrogram(t, freq, timefreq, dBNoise=80)

예제 #14
0
def draw_figures(bird, block, segment, hemi, e1, e2, stim_id, trial, syllable_index, data_dir='/auto/tdrive/mschachter/data', exp=None):

    # load up the experiment
    if exp is None:
        bird_dir = os.path.join(data_dir, bird)
        exp_file = os.path.join(bird_dir, '%s.h5' % bird)
        stim_file = os.path.join(bird_dir, 'stims.h5')
        exp = Experiment.load(exp_file, stim_file)
    seg = exp.get_segment(block, segment)

    # get the start and end times of the stimulus
    etable = exp.get_epoch_table(seg)

    i = etable['id'] == stim_id
    stim_times = zip(etable[i]['start_time'].values, etable[i]['end_time'].values)
    stim_times.sort(key=operator.itemgetter(0))

    start_time,end_time = stim_times[trial]
    stim_dur = float(end_time - start_time)

    # get a slice of the LFP
    lfp_data = exp.get_lfp_slice(seg, start_time, end_time, zscore=True)
    electrode_indices,lfps,sample_rate = lfp_data[hemi]

    # get the log spectrogram of the stimulus
    stim_spec_t,stim_spec_freq,stim_spec = exp.get_spectrogram_slice(seg, start_time, end_time)
    stim_spec_t = np.linspace(0, stim_dur, len(stim_spec_t))
    stim_spec_dt = np.diff(stim_spec_t)[0]
    nz = stim_spec > 0
    stim_spec[nz] = 20*np.log10(stim_spec[nz]) + 100
    stim_spec[stim_spec < 0] = 0

    # get the amplitude envelope
    amp_env = stim_spec.std(axis=0, ddof=1)
    amp_env -= amp_env.min()
    amp_env /= amp_env.max()

    # segment the amplitude envelope into syllables
    merge_thresh = int(0.002*sample_rate)
    events = break_envelope_into_events(amp_env, threshold=0.05, merge_thresh=merge_thresh)

    # translate the event indices into actual times
    events *= stim_spec_dt

    syllable_start,syllable_end,syllable_max_amp = events[syllable_index]
    amp_env_rs = amp_env*(stim_spec_freq.max() - stim_spec_freq.min()) + stim_spec_freq.min()
    last_syllable_end = events[-1, 1] + 0.025

    i1 = electrode_indices.index(e1)
    i2 = electrode_indices.index(e2)

    lfp1 = lfps[i1, :]
    lfp2 = lfps[i2, :]

    t = np.arange(len(lfp1)) / sample_rate
    legend = ['E%d' % e1, 'E%d' % e2]

    if hemi == 'L':
        electrode_order = ROSTRAL_CAUDAL_ELECTRODES_LEFT
    else:
        electrode_order = ROSTRAL_CAUDAL_ELECTRODES_RIGHT

    # get the power spectrum stats for this site
    psd_stats = get_psd_stats(bird, block, segment, hemi)

    # compute the power spectra and cross coherence for all electrodes
    lags_ms = get_lags_ms(sample_rate)
    spectra,cross_mat = compute_spectra_and_coherence_multi_electrode_single_trial(lfps, sample_rate, electrode_indices, electrode_order, psd_stats=psd_stats)

    # plot the stimulus and raw LFP for two electrodes
    figsize = (24.0, 10)
    fig = plt.figure(figsize=figsize)
    plt.subplots_adjust(top=0.95, bottom=0.05, left=0.03, right=0.99, hspace=0.10)

    ax = plt.subplot(2, 1, 1)
    plot_spectrogram(stim_spec_t, stim_spec_freq, stim_spec, ax=ax, ticks=True, fmin=300, fmax=8000,
                     colormap='SpectroColorMap', colorbar=False)
    # plt.plot(stim_spec_t, amp_env_rs, 'k-', linewidth=2.0, alpha=0.50)
    plt.axvline(syllable_start, c='k', linestyle='dashed', linewidth=3.0)
    plt.axvline(syllable_end, c='k', linestyle='dashed', linewidth=3.0)
    plt.axis('tight')
    plt.xlim(0, last_syllable_end)

    ax = plt.subplot(2, 1, 2)
    plt.plot(t, lfp1, 'b-', linewidth=3.0)
    plt.plot(t, lfp2, 'r-', linewidth=3.0, alpha=0.7)
    plt.axvline(syllable_start, c='k', linestyle='dashed', linewidth=3.0)
    plt.axvline(syllable_end, c='k', linestyle='dashed', linewidth=3.0)
    plt.xlabel('Time (ms)')
    plt.ylabel('LFP (z-scored)')
    plt.legend(legend)
    plt.axis('tight')
    plt.xlim(0, last_syllable_end)

    fname = os.path.join(get_this_dir(), 'raw.svg')
    plt.savefig(fname, facecolor='w', edgecolor='none')

    # restrict the lfps to a single syllable
    syllable_si = int(syllable_start*sample_rate)
    syllable_ei = int(syllable_end*sample_rate)
    lfp1 = lfp1[syllable_si:syllable_ei]
    lfp2 = lfp2[syllable_si:syllable_ei]

    # plot the two power spectra
    psd_ub = 6
    psd_lb = 0
    i1 = electrode_order.index(e1)
    i2 = electrode_order.index(e2)

    a1 = spectra[i1, :]
    a2 = spectra[i2, :]
    freqs = get_freqs(sample_rate)

    figsize = (10.0, 4.0)
    fig = plt.figure(figsize=figsize)
    plt.subplots_adjust(top=0.90, bottom=0.10, left=0.10, right=0.99, hspace=0.10)

    ax = plt.subplot(1, 2, 1)
    plt.plot(freqs, a1, 'b-', linewidth=3.0)
    plt.plot(freqs, a2, 'r-', linewidth=3.0)
    plt.xlabel('Frequency (Hz)')
    plt.ylabel('Power (z-scored)')
    plt.title('Power Spectrum')
    handles = custom_legend(['b', 'r'], legend)
    plt.legend(handles=handles, fontsize='small')
    plt.axis('tight')
    plt.ylim(psd_lb, psd_ub)

    # plot the coherency
    cf_lb = -0.1
    cf_ub = 0.3
    coh = cross_mat[i1, i2, :]
    ax = plt.subplot(1, 2, 2)
    plt.axhline(0, c='k')
    plt.axvline(0, c='k')
    plt.plot(lags_ms, coh, 'g-', linewidth=3.0)
    plt.xlabel('Frequency (Hz)')
    plt.title('Coherency')
    plt.axis('tight')
    plt.ylim(cf_lb, cf_ub)

    fname = os.path.join(get_this_dir(), 'auto+cross.svg')
    plt.savefig(fname, facecolor='w', edgecolor='none')

    # compute all cross and auto-correlations
    nelectrodes = len(electrode_order)

    # make a plot
    figsize = (24.0, 13.5)
    fig = plt.figure(figsize=figsize)
    plt.subplots_adjust(top=0.95, bottom=0.05, left=0.03, right=0.99, hspace=0.10)

    gs = plt.GridSpec(nelectrodes, nelectrodes)
    for i in range(nelectrodes):
        for j in range(i+1):
            ax = plt.subplot(gs[i, j])
            plt.axhline(0, c='k')
            plt.axvline(0, c='k')

            _e1 = electrode_order[i]
            _e2 = electrode_order[j]

            clr = 'k'
            if i == j:
                if _e1 == e1:
                    clr = 'b'
                elif _e1 == e2:
                    clr = 'r'
            else:
                if _e1 == e1 and _e2 == e2:
                    clr = 'g'

            if i == j:
                plt.plot(freqs, spectra[i, :], '-', c=clr, linewidth=2.0)
            else:
                plt.plot(lags_ms, cross_mat[i, j, :], '-', c=clr, linewidth=2.0)
            plt.xticks([])
            plt.yticks([])
            plt.axis('tight')
            if i != j:
                plt.ylim(cf_lb, cf_ub)
            else:
                plt.axhline(0, c='k')
                plt.ylim(psd_lb, psd_ub)

            if j == 0:
                plt.ylabel('E%d' % electrode_order[i])
            if i == nelectrodes-1:
                plt.xlabel('E%d' % electrode_order[j])

    fname = os.path.join(get_this_dir(), 'cross_all.svg')
    plt.savefig(fname, facecolor='w', edgecolor='none')
예제 #15
0
def plot_nofund(agg, data_dir='/auto/tdrive/mschachter/data'):

    spec_colormap()

    aprops = ['fund', 'maxfund', 'minfund', 'cvfund', 'fund2', 'voice2percent',
               'stdspect', 'skewspect', 'kurtosisspect', 'entropyspect', 'sal',
               'meanspect', 'q1', 'q2', 'q3', 'skewtime', 'kurtosistime', 'entropytime', 'maxAmp']
    Xz, good_indices = agg.remove_duplicates(acoustic_props=aprops)

    stim_durs = agg.df.end_time.values - agg.df.start_time.values
    good_indices = [gi for gi in good_indices if stim_durs[gi] > 0.040 and stim_durs[gi] < 0.450]

    stims = [(stim_id, order, stim_type) for stim_id, order, stim_type in zip(agg.df.stim_id[good_indices],
                                                                              agg.df.syllable_order[good_indices],
                                                                              agg.df.stim_type[good_indices])]

    plt.figure()
    plt.hist(stim_durs[good_indices], bins=25)
    plt.show()

    fund_i = agg.acoustic_props.index('fund')
    funds = np.array([agg.Xraw[xindex, fund_i] for xindex in agg.df.xindex[good_indices]])

    print 'funds.min=%f, q1=%f, q2=%f' % (funds.min(), np.percentile(funds, 1), np.percentile(funds, 2))

    no_fund = np.where(funds <= 0)[0]
    yes_fund = np.where(funds > 0)[0]
    print 'no_fund=', no_fund

    plt.figure()
    plt.hist(funds, bins=20)
    plt.title('funds')
    # plt.show()

    # np.random.seed(1234567)
    # np.random.shuffle(no_fund)
    # np.random.shuffle(yes_fund)

    # get syllable properties for some examples of syllables with and without fundamental frequencies
    set_font(10)
    figsize = (23, 10)
    fig = plt.figure(figsize=figsize)
    fig.subplots_adjust(wspace=0.35, hspace=0.35, left=0.05, right=0.95)

    num_syllables = 5
    no_fund_i = no_fund[:num_syllables]
    yes_fund_i = yes_fund[:num_syllables]

    gs = plt.GridSpec(2, num_syllables)
    for k in range(num_syllables):

        fi = no_fund_i[k]
        f = funds[fi]
        stim_id,stim_order,stim_type = stims[fi]
        sprops = get_syllable_props(agg, stim_id, stim_order, data_dir)

        ax = plt.subplot(gs[0, k])
        si = sprops['spec_si']
        ei = sprops['spec_ei']
        plot_spectrogram(sprops['spec_t'][si:ei], sprops['spec_freq'], sprops['spec'][:, si:ei], ax=ax,
                         colormap='SpectroColorMap', colorbar=False)
        plt.title('%d_%d (%s), fund=%0.2f' % (stim_id, stim_order, stim_type, f))

        fi = yes_fund_i[k]
        f = funds[fi]
        stim_id, stim_order, stim_type = stims[fi]
        sprops = get_syllable_props(agg, stim_id, stim_order, data_dir)

        ax = plt.subplot(gs[1, k])
        si = sprops['spec_si']
        ei = sprops['spec_ei']
        plot_spectrogram(sprops['spec_t'][si:ei], sprops['spec_freq'], sprops['spec'][:, si:ei], ax=ax,
                         colormap='SpectroColorMap', colorbar=False)
        plt.title('%d_%d (%s), fund=%0.2f' % (stim_id, stim_order, stim_type, f))

    plt.show()
예제 #16
0
def plot_syllable_comps(agg, stim_id=43, syllable_order=1, data_dir='/auto/tdrive/mschachter/data'):

    sprops = get_syllable_props(agg, stim_id, syllable_order, data_dir)
    wave = sprops['wave']
    wave_t = sprops['wave_t']
    wave_si = sprops['wave_si']
    wave_ei = sprops['wave_ei']
    ps_freq = sprops['ps_freq']
    ps = sprops['ps']
    amp_env = sprops['amp_env']
    aprops = sprops['aprops']
    start_time = aprops['start_time']
    spec = sprops['spec']
    spec_t = sprops['spec_t']
    spec_freq = sprops['spec_freq']
    spec_si = sprops['spec_si']
    spec_ei = sprops['spec_ei']

    aprop_specs = dict()
    aprop_spec_props = ['maxAmp', 'meanspect', 'sal']
    for aprop in aprop_spec_props:
        aprop_specs[aprop] = get_syllable_examples(agg, aprop, data_dir, bird='GreBlu9508M')

    figsize = (23, 13)
    fig = plt.figure(figsize=figsize, facecolor='w')
    fig.subplots_adjust(top=0.95, bottom=0.08, right=0.97, left=0.06, hspace=0.30, wspace=0.30)
    gs = plt.GridSpec(3, 100)

    sp_width = 20

    ax = plt.subplot(gs[0, :sp_width])
    plt.plot((wave_t[wave_si:wave_ei] - start_time)*1e3, wave[wave_si:wave_ei], 'k-', linewidth=2.)
    plt.plot((wave_t[wave_si:wave_ei] - start_time)*1e3, amp_env[wave_si:wave_ei], 'r-', linewidth=4., alpha=0.7)
    plt.xlabel('Time (ms)')
    plt.ylabel('Waveform')
    plt.axis('tight')

    aprops_to_get = ['skewtime', 'kurtosistime', 'entropytime', 'maxAmp']
    units = ['s', '', '', 'bits', '']
    ax = plt.subplot(gs[0, sp_width:(sp_width+18)])
    txt_space = 0.1
    for k,aprop in enumerate(aprops_to_get):
        aval = aprops[aprop]
        if aval > 10:
            txt = '%s: %d' % (ACOUSTIC_PROP_NAMES[aprop], aval)
        else:
            txt = '%s: %0.2f' % (ACOUSTIC_PROP_NAMES[aprop], aval)
        txt += ' %s' % units[k]
        plt.text(0.1, 1-((k+1)*txt_space), txt, fontsize=18)
    ax.set_axis_off()
    plt.xlim(0, 1)
    plt.ylim(0, 1)
    plt.xticks([])
    plt.yticks([])

    ax = plt.subplot(gs[1, :sp_width])
    fi = (ps_freq > 0) & (ps_freq <= 8000.)
    plt.plot(ps_freq[fi]*1e-3, ps[fi], 'k-', linewidth=3., alpha=1.)
    for aprop in ['q1', 'q2', 'q3']:
        plt.axvline(aprops[aprop]*1e-3, color='#606060', linestyle='--', linewidth=3.0, alpha=0.9)
        # plt.text(aprops[aprop]*1e-3 - 0.6, 3000., aprop, fontsize=14)
    plt.ylabel("Power")
    plt.yticks([])
    plt.xlabel('Frequency (kHz)')
    plt.axis('tight')

    aprops_to_get = ['meanspect', 'stdspect', 'skewspect', 'kurtosisspect', 'entropyspect', 'q1', 'q2', 'q3']
    units = ['Hz', 'Hz', '', '', 'bits', 'Hz', 'Hz', 'Hz']
    ax = plt.subplot(gs[1, sp_width:(sp_width+18)])
    for k,aprop in enumerate(aprops_to_get):
        aval = aprops[aprop]
        if aval > 10:
            txt = '%s: %d' % (ACOUSTIC_PROP_NAMES[aprop], aval)
        else:
            txt = '%s: %0.2f' % (ACOUSTIC_PROP_NAMES[aprop], aval)
        txt += ' %s' % units[k]
        plt.text(0.1, 1-((k+1)*txt_space), txt, fontsize=18)
    ax.set_axis_off()
    plt.xlim(0, 1)
    plt.ylim(0, 1)
    plt.xticks([])
    plt.yticks([])

    spec_colormap()
    ax = plt.subplot(gs[2, :sp_width])
    plot_spectrogram((spec_t[spec_si:spec_ei]-start_time)*1e3, spec_freq*1e-3, spec[:, spec_si:spec_ei], ax, colormap='SpectroColorMap', colorbar=False)
    plt.axhline(aprops['fund']*1e-3, color='k', linestyle='--', linewidth=3.0)
    plt.ylabel("Frequency (kHz)")
    plt.xlabel('Time (ms)')

    for k,aprop in enumerate(aprop_spec_props):

        full_spec_freq, full_spec, centers, propvals, stypes = aprop_specs[aprop]

        ax = plt.subplot(gs[k, (sp_width+20):])
        full_spec_t = np.arange(full_spec.shape[1]) / sprops['spec_sample_rate']
        plot_spectrogram(full_spec_t, full_spec_freq*1e-3, full_spec, ax, colormap='SpectroColorMap', colorbar=False)
        for c,st in zip(centers,stypes):
            plt.text(c/sprops['spec_sample_rate'], 7., CALL_TYPE_NAMES[st], fontsize=20)
        pstrs = list()
        for p in propvals:
            if p > 10:
                if aprop == 'meanspect':
                    pstrs.append('%d Hz' % p)
                else:
                    pstrs.append('%d' % p)
            else:
                pstrs.append('%0.3f' % p)
        for c,p in zip(centers,pstrs):
            plt.text(c, 6, p, fontsize=20)
        # plt.xticks(centers, pstrs)
        plt.xticks([])
        plt.ylabel('Frequency (kHz)')

        scale_t = (full_spec_t.max()*0.95) - np.linspace(0, 0.050, 20)
        scale_y = np.zeros_like(scale_t) + 3
        plt.plot(scale_t, scale_y, 'k-', linewidth=4.0)
        plt.text(scale_t.min(), 2.0, '50ms', fontsize=20, fontweight='bold')

        upper_right_x = np.percentile(full_spec_t, 80)
        upper_right_y = 7
        plt.title(ACOUSTIC_PROP_FULLNAMES[aprop], fontsize=22)

    aprops_to_get = ['fund', 'fund2', 'sal', 'voice2percent', 'maxfund', 'minfund', 'cvfund']
    units = ['Hz', 'Hz', '', '', 'Hz', 'Hz', 'Hz']
    ax = plt.subplot(gs[2, sp_width:(sp_width + 18)])
    for k, aprop in enumerate(aprops_to_get):
        aval = aprops[aprop]
        if aval > 10:
            txt = '%s: %d' % (ACOUSTIC_PROP_NAMES[aprop], aval)
        else:
            txt = '%s: %0.2f' % (ACOUSTIC_PROP_NAMES[aprop], aval)
        txt += ' %s' % units[k]
        plt.text(0.1, 1 - ((k + 1) * txt_space), txt, fontsize=18)
    ax.set_axis_off()
    plt.xlim(0, 1)
    plt.ylim(0, 1)
    plt.xticks([])
    plt.yticks([])

    fname = os.path.join(get_this_dir(), 'figure.svg')
    plt.savefig(fname, facecolor='w', edgecolor='none')

    plt.show()
# for i in range(len(sound_onset)):
#     t, freq, timefreq, rms = spectrogram(mic[sound_onset[i]:sound_offset[i]], fs_mic, 1000, 50)
#     plot_spectrogram(t, freq, timefreq, dBNoise=80, colorbar = False)
#     pause(.05)
#==============================================================================

# just a temporary template, ~ 1 motif, maybe even a playback but who cares
# TODO make a template finding function
i = 17
template = mic[sound_onset[i]:sound_offset[i]]
template = template[4800:18000]  #shift it, particular to this template
templ_t, templ_freq, templ_timefreq, templ_rms = spectrogram(
    template, fs_mic, spec_sample_rate=1000, freq_spacing=50)
plot_spectrogram(templ_t,
                 templ_freq,
                 templ_timefreq,
                 dBNoise=80,
                 colorbar=False)
# Make xcorr of spectrogram - if i = 17 template is present in sound
# TODO this is the part that will have to loop through for every vocal chunk
# for now it is just a bout that has three motifs in it
t, freq, timefreq, rms = spectrogram(mic[sound_onset[i]:sound_offset[i]],
                                     fs_mic,
                                     spec_sample_rate=1000,
                                     freq_spacing=50)

# find the frequencies of interest
freq_index = [0]
iter = 0
for i in range(len(freq)):
    if (freq[i] > low_freq_corr) & (freq[i] < high_freq_corr):
예제 #18
0
파일: convert.py 프로젝트: ShariqM/nnbird
import matplotlib.pyplot as plt
from scipy.io.wavfile import read
import numpy as np
from lasp.timefreq import gaussian_stft
from lasp.sound import plot_spectrogram
import pdb

fname = 'DCCalls16x16/fbird8call1.wav'
wf = read(fname,'r')[1]

# Frames: 30870.00d, Rate: 44100.00

wl = 0.007 # 7ms
ic = 0.001 # 1ms

t,freq,timefreq,rms = gaussian_stft(wf, 44100, wl, ic)

spec = np.abs(timefreq)
spec = spec/spec.max()
nz = spec > 0
spec[nz] = 20*np.log10(spec[nz]) + 50
spec[spec < 0] = 0

plot_spectrogram(t, freq, spec)
plt.show()
예제 #19
0
def plot_full_data(d, syllable_index):

    syllable_start = d['syllable_props'][syllable_index]['start_time'] - 0.020
    syllable_end = d['syllable_props'][syllable_index]['end_time'] + 0.030

    figsize = (24.0, 10)
    fig = plt.figure(figsize=figsize, facecolor='w')
    fig.subplots_adjust(top=0.95, bottom=0.02, right=0.97, left=0.03, hspace=0.20, wspace=0.20)

    gs = plt.GridSpec(100, 100)
    left_width = 55
    top_height = 30
    middle_height = 40
    # bottom_height = 40
    top_bottom_sep = 20

    # plot the spectrogram
    ax = plt.subplot(gs[:top_height+1, :left_width])
    spec = d['spec']
    spec[spec < np.percentile(spec, 15)] = 0
    plot_spectrogram(d['spec_t'], d['spec_freq']*1e-3, spec, ax=ax, colormap='SpectroColorMap', colorbar=False, ticks=True)
    plt.axvline(syllable_start, c='k', linestyle='--', linewidth=3.0, alpha=0.7)
    plt.axvline(syllable_end, c='k', linestyle='--', linewidth=3.0, alpha=0.7)
    plt.ylabel('Frequency (kHz)')
    plt.xlabel('Time (s)')

    # plot the LFPs
    sr = d['lfp_sample_rate']
    # lfp_mean = d['lfp'].mean(axis=0)
    lfp_mean = d['lfp'][2, :, :]
    lfp_t = np.arange(lfp_mean.shape[1]) / sr
    nelectrodes,nt = lfp_mean.shape
    gs_i = top_height + top_bottom_sep
    gs_e = gs_i + middle_height + 1

    ax = plt.subplot(gs[gs_i:gs_e, :left_width])

    voffset = 5
    for n in range(nelectrodes):
        plt.plot(lfp_t, lfp_mean[nelectrodes-n-1, :] + voffset*n, 'k-', linewidth=3.0, alpha=0.75)
    plt.axis('tight')
    ytick_locs = np.arange(nelectrodes) * voffset
    plt.yticks(ytick_locs, list(reversed(d['electrode_order'])))
    plt.ylabel('Electrode')
    plt.axvline(syllable_start, c='k', linestyle='--', linewidth=3.0, alpha=0.7)
    plt.axvline(syllable_end, c='k', linestyle='--', linewidth=3.0, alpha=0.7)
    plt.xlabel('Time (s)')

    # plot the PSTH
    """
    gs_i = gs_e + 5
    gs_e = gs_i + bottom_height + 1
    ax = plt.subplot(gs[gs_i:gs_e, :left_width])
    ncells = d['psth'].shape[0]
    plt.imshow(d['psth'], interpolation='nearest', aspect='auto', origin='upper', extent=(0, lfp_t.max(), ncells, 0),
               cmap=psth_colormap(noise_level=0.1))

    cell_i2e = d['cell_index2electrode']
    print 'cell_i2e=',cell_i2e
    last_electrode = cell_i2e[0]
    for k,e in enumerate(cell_i2e):
        if e != last_electrode:
            plt.axhline(k, c='k', alpha=0.5)
            last_electrode = e

    ytick_locs = list()
    for e in d['electrode_order']:
        elocs = np.array([k for k,el in enumerate(cell_i2e) if el == e])
        emean = elocs.mean()
        ytick_locs.append(emean+0.5)
    plt.yticks(ytick_locs, d['electrode_order'])
    plt.ylabel('Electrode')

    plt.axvline(syllable_start, c='k', linestyle='--', linewidth=3.0, alpha=0.7)
    plt.axvline(syllable_end, c='k', linestyle='--', linewidth=3.0, alpha=0.7)
    """

    # plot the biosound properties
    sprops = d['syllable_props'][syllable_index]
    aprops = USED_ACOUSTIC_PROPS

    vals = [sprops[a] for a in aprops]
    ax = plt.subplot(gs[:top_height, (left_width+5):])
    plt.axhline(0, c='k')
    for k,(aprop,v) in enumerate(zip(aprops,vals)):
        bx = k
        rgb = np.array(ACOUSTIC_PROP_COLORS_BY_TYPE[aprop]).astype('int')
        clr_hex = '#%s' % "".join(map(chr, rgb)).encode('hex')
        plt.bar(bx, v, color=clr_hex, alpha=0.7)

    # plt.bar(range(len(aprops)), vals, color='#c0c0c0')
    plt.axis('tight')
    plt.ylim(-1.5, 1.5)
    plt.xticks(np.arange(len(aprops))+0.5, [ACOUSTIC_PROP_NAMES[aprop] for aprop in aprops], rotation=90)
    plt.ylabel('Z-score')

    # plot the LFP power spectra
    gs_i = top_height + top_bottom_sep
    gs_e = gs_i + middle_height + 1

    f = d['psd_freq']
    ax = plt.subplot(gs[gs_i:gs_e, (left_width+5):])
    plt.imshow(sprops['lfp_psd'], interpolation='nearest', aspect='auto', origin='upper',
               extent=(f.min(), f.max(), nelectrodes, 0), cmap=viridis, vmin=-2., vmax=2.)
    plt.colorbar(label='Z-scored Log Power')
    plt.xlabel('Frequency (Hz)')
    plt.yticks(np.arange(nelectrodes)+0.5, d['electrode_order'])
    plt.ylabel('Electrode')

    fname = os.path.join(get_this_dir(), 'figure.svg')
    plt.savefig(fname, facecolor='w', edgecolor='none')

    plt.show()
예제 #20
0
def draw_figures(data_dir='/auto/tdrive/mschachter/data'):

    bird = 'GreBlu9508M'
    block = 'Site4'
    segment = 'Call1'
    hemi = 'L'
    fname = '%s_%s_%s_%s' % (bird, block, segment, hemi)
    exp_dir = os.path.join(data_dir, bird)

    preproc_file = os.path.join(exp_dir, 'preprocess', 'RNNPreprocess_%s.h5' % fname)
    rp = RNNPreprocessTransform.load(preproc_file)

    pred_file = os.path.join(exp_dir, 'rnn', 'LFPEnvelope_%s.h5' % fname)
    hf = h5py.File(pred_file, 'r')
    Ypred = np.array(hf['Ypred'])
    hf.close()

    assert Ypred.shape[0] == rp.U.shape[0]

    stim_id = 277
    trial = 5

    i = (rp.event_df.stim_id == stim_id) & (rp.event_df.trial == trial)
    assert i.sum() == 1
    start_time = rp.event_df[i].start_time.values[0]
    end_time = rp.event_df[i].end_time.values[0]
    si = int(start_time*rp.sample_rate)
    ei = int(end_time * rp.sample_rate)

    spec = rp.U[si:ei, :].T
    spec_freq = rp.spec_freq
    spec_env = spec.sum(axis=0)
    spec_env /= spec_env.max()

    lfp = rp.Yraw[si:ei, :].T
    lfp_pred = Ypred[si:ei, :].T

    nt = spec.shape[1]
    t = np.arange(nt) / rp.sample_rate

    index2electrode = list(rp.index2electrode)
    electrode_order = ROSTRAL_CAUDAL_ELECTRODES_LEFT
    if hemi == 'R':
        electrode_order = ROSTRAL_CAUDAL_ELECTRODES_RIGHT

    fig = plt.figure(figsize=(23, 13), facecolor='w')
    gs = plt.GridSpec(100, 1)

    ax = plt.subplot(gs[:25, :])
    spec_env *= (spec_freq.max() - spec_freq.min())
    spec_env += spec_freq.min()
    plot_spectrogram(t, spec_freq, spec, ax=ax, ticks=True, fmax=8000., colormap='SpectroColorMap', colorbar=False)
    plt.plot(t, spec_env, 'k-', linewidth=5.0, alpha=0.7)
    plt.axis('tight')

    ax = plt.subplot(gs[30:, :])
    nelectrodes = len(index2electrode)
    lfp_spacing = 5.
    for k in range(nelectrodes):
        e = electrode_order[nelectrodes-k-1]
        n = index2electrode.index(e)
        offset = k*lfp_spacing
        plt.plot(t, lfp[n, :] + offset, 'k-', alpha=0.7, linewidth=5.0)
        plt.plot(t, lfp_pred[n, :] + offset, 'r-', alpha=0.7, linewidth=5.0)
    plt.yticks([])
    plt.axis('tight')
    plt.xlabel('Time (s)')
    plt.ylabel('LFP')

    fname = os.path.join(get_this_dir(), 'encoder_pred.svg')
    plt.savefig(fname, facecolor=fig.get_facecolor(), edgecolor='none')

    plt.show()
예제 #21
0
sound_lengths = (sound_offset - sound_onset) / fs_mic  # in s
if long_thresh > 0:
    long_sounds = np.squeeze(np.where(sound_lengths > long_thresh))  # in s
else:
    long_sounds = np.squeeze(np.where(sound_lengths < -long_thresh))  # in s

high_corrs = np.squeeze(
    np.where(np.asarray(max_corrs[template]) > corr_thresh))
long_high = common_elements(long_sounds, high_corrs)
long_high_vocal = common_elements(long_sounds, nonplayback)
for sound in long_high_vocal:
    sound_wav = mic[np.int(sound_onset[sound]):np.int(sound_offset[sound])]
    sound_t, sound_freq, sound_timefreq, sound_rms = spectrogram(
        sound_wav, fs_mic, spec_sample_rate=1000, freq_spacing=50)
    figure(1)
    plot_spectrogram(sound_t, sound_freq, sound_timefreq)
    pause(.2)
    close(1)

for sound in long_sounds:
    sound_wav = mic[np.int(sound_onset[sound]):np.int(sound_offset[sound])]
    sound_t, sound_freq, sound_timefreq, sound_rms = spectrogram(
        sound_wav, fs_mic, spec_sample_rate=1000, freq_spacing=50)
    figure(1)
    plot_spectrogram(sound_t, sound_freq, sound_timefreq)
    pause(.1)
    close(1)

fig = figure()
ax = fig.add_subplot(111, projection='3d')
plot(max_corrs[2][long_high_vocal], max_corrs[3][long_high_vocal],
예제 #22
0
def draw_figures(data_dir='/auto/tdrive/mschachter/data', bird='GreBlu9508M', output_dir='/auto/tdrive/mschachter/data/sounds'):

    spec_colormap()

    exp_dir = os.path.join(data_dir, bird)
    exp_file = os.path.join(exp_dir, '%s.h5' % bird)
    stim_file = os.path.join(exp_dir, 'stims.h5')
    
    exp = Experiment.load(exp_file, stim_file)
    
    bird = exp.bird_name
    all_stim_ids = list()
    # iterate through the segments and get the stim ids from each epoch table
    for seg in exp.get_all_segments():
        etable = exp.get_epoch_table(seg)
        stim_ids = etable['id'].unique()
        all_stim_ids.extend(stim_ids)

    stim_ids = np.unique(all_stim_ids)

    stim_info = list()
    for stim_id in stim_ids:
        si = exp.stim_table['id'] == stim_id
        assert si.sum() == 1, "More than one stimulus defined for id=%d" % stim_id
        stype = exp.stim_table['type'][si].values[0]
        if stype == 'call':
            stype = exp.stim_table['callid'][si].values[0]

        # get sound pressure waveform
        sound = exp.sound_manager.reconstruct(stim_id)
        waveform = np.array(sound.squeeze())
        sample_rate = float(sound.samplerate)
        stim_dur = len(waveform) / sample_rate

        stim_info.append( (stim_id, stype, sample_rate, waveform, stim_dur))

    durations = np.array([x[-1] for x in stim_info])
    max_dur = durations.max()
    min_dur = durations.min()

    max_fig_size = 15.
    min_fig_size = 5.

    for stim_id,stype,sample_rate,waveform,stim_dur in stim_info:

        fname = os.path.join(output_dir, '%s_stim_%d.wav' % (stype, stim_id))
        print 'Writing %s...' % fname
        wavfile.write(fname, sample_rate, waveform)

        dfrac = (stim_dur - min_dur) / (max_dur - min_dur)
        fig_width = dfrac*(max_fig_size - min_fig_size) + min_fig_size

        spec_t,spec_freq,spec,rms = spectrogram(waveform, sample_rate, sample_rate, 136., min_freq=300, max_freq=8000,
                                                log=True, noise_level_db=80, rectify=True, cmplx=False)

        figsize = (fig_width, 5)
        fig = plt.figure(figsize=figsize)
        plot_spectrogram(spec_t, spec_freq, spec, colormap='SpectroColorMap', colorbar=False)
        plt.title('Stim %d: %s' % (stim_id, stype))

        fname = os.path.join(output_dir, '%s_stim_%d.png' % (stype, stim_id))
        plt.savefig(fname, facecolor='w')
        plt.close('all')