Exemplo n.º 1
0
def opto_get_all_lfp_analytic_quick(opto_dataset,fa,fb):
    Fs = 1000.0
    order = 4
    data = metaloadmat(opto_dataset_compact)['lfp']
    data = data.transpose((0,2,1))
    data = bandfilter(hilbert(data),fa,fb,Fs,order)
    return data
Exemplo n.º 2
0
def opto_get_lfp_analytic(opto_dataset,channel,fa,fb,order=4):
    '''
    Retrieves channel or channels from opto LFP dataset
    Channels are 1-indexed
    
    Parameters
    ----------
    opto_dataset : string
        path or string identifier for a dataset
    channel:
        1-indexed channel ID or None to return a NTimes x NChannel array
        of all LFP data
    fa:
        low frequency of band-pass, or 'None' to use a low-pass filter.
        if fb is 'None' then this is the cutoff for a high-pass filter.
    fb:
        high-frequency of band-pass, or 'None to use a high-pass filter.
        if fa is 'None' then this is the cutoff for a low-pass filter
    '''
    print 1
    Fs = opto_get_Fs(opto_dataset)
    if channel is None:
        print 2
        data = metaloadmat(opto_dataset)['LFP'].T
        #return array([bandfilter(x,fa,fb,Fs,order) for x in data])
        print 3
        problems = [(i,x,fa,fb,Fs,order) for i,x in enumerate(data)]
        print 4
        data = np.array(parmap(__opto_get_lfp_analytic_helper__,problems,verbose=1))
        return squeeze(data)
    else:
        assert channel>=1
        data = metaloadmat(opto_dataset)['LFP'][:,channel-1]
        return hilbert(bandfilter(data,fa,fb,Fs,order))
Exemplo n.º 3
0
    def filterAFSK(self, audio_input):
        differential = numpy.diff(audio_input, 1)
        frequency_domain = numpy.abs(signaltools.hilbert(differential))
        pir_filter = signal.firwin(numtaps=2,
                                   cutoff=1800,
                                   nyq=self.SAMPLE_RATE / 2)

        return signal.filtfilt(pir_filter, [1.0], frequency_domain)
Exemplo n.º 4
0
def complex_eigenstructure(region):
    region = region.reshape(-1, region.shape[-1])

    region = hilbert(region, axis=-1)
    region = np.hstack([region.real, region.imag])

    cov = region.dot(region.T)
    vals = np.linalg.eigvals(cov)
    return np.abs(vals.max() / vals.sum())
Exemplo n.º 5
0
def fit_sig_decay(signal, s_filter=51, poly=2):
    """
        returns list with three coefficients that describe the exponential decay curve
        of the signal
    """
    t = np.array(range(signal.shape[0]), dtype=float)
    amplitude_envelope = np.abs(sigtool.hilbert(signal))
    smoothed_envelop = savgol_filter(amplitude_envelope, s_filter, poly)
    coefficients, _ = curve_fit(exp_func, t, smoothed_envelop)
    return coefficients
def fit_sig_decay(signal, s_filter=51, poly=2):
    """
        returns list with three coefficients that describe the exponential decay curve
        of the signal
    """
    t = np.array(range(signal.shape[0]), dtype=float)
    amplitude_envelope = np.abs(sigtool.hilbert(signal))
    smoothed_envelop = savgol_filter(amplitude_envelope, s_filter, poly)
    coefficients, _ = curve_fit(exp_func, t, smoothed_envelop)
    return coefficients
Exemplo n.º 7
0
def phase_sync(X):
    
    # Computes the PS_value as explained in Ponce et al. 2015
    import numpy as np
    import scipy.signal.signaltools as sigtool

    htx = sigtool.hilbert(X)
    # Converts (real, imag) to an angle
    px = np.angle(htx)
        
    return np.sqrt(np.mean(np.cos(px))**2) # + np.mean(np.sin(px))**2) #PS_value      
Exemplo n.º 8
0
def phase_sync(X):

    # Computes the PS_value as explained in Ponce et al. 2015
    import numpy as np
    import scipy.signal.signaltools as sigtool

    htx = sigtool.hilbert(X)
    # Converts (real, imag) to an angle
    px = np.angle(htx)

    return np.sqrt(np.mean(
        np.cos(px))**2)  # + np.mean(np.sin(px))**2) #PS_value
Exemplo n.º 9
0
def PLV(X, Y):
    """
    FUNC: phasediff - phase locking value
    DESCR: Compute the phase difference using the hilbert transform of s0
    and s1 and then the phase lock

    return
        PLV    : the phase difference
    
    """
    import numpy as np
    import scipy.signal.signaltools as sigtool

    htx = sigtool.hilbert(X)
    hty = sigtool.hilbert(Y)
    px = np.angle(htx)
    py = np.angle(hty)
    
    psi = px - py
    
    return np.sqrt(np.mean(np.cos(psi))**2 + np.mean(np.sin(psi))**2) #PLV_value
Exemplo n.º 10
0
def demodulate(dd):
    y_diff = np.diff(dd, 1)
    y_env = np.abs(sigtool.hilbert(y_diff, N=len(y_diff) + 1))
    N_FFT = float(len(y_env))
    w = np.hanning(len(y_env))
    y_f = np.fft.fft(np.multiply(y_env, w))
    y_f = 10 * np.log10(np.abs(y_f[0:int(N_FFT / 2)] / N_FFT))
    mean = 0.22
    sampled_signal = y_env[int(Fs / Fbit / 2):len(y_env):Fs / Fbit]
    rx_data = np.zeros(len(sampled_signal), dtype=int)
    for i, bit in enumerate(sampled_signal):
        rx_data[i] = 0 if bit > mean else 1
    return list(rx_data)
Exemplo n.º 11
0
def PLV(X, Y):
    """
    FUNC: phasediff - phase locking value
    DESCR: Compute the phase difference using the hilbert transform of s0
    and s1 and then the phase lock

    return
        PLV    : the phase difference
    
    """
    import numpy as np
    import scipy.signal.signaltools as sigtool

    htx = sigtool.hilbert(X)
    hty = sigtool.hilbert(Y)
    px = np.angle(htx)
    py = np.angle(hty)

    psi = px - py

    return np.sqrt(np.mean(np.cos(psi))**2 +
                   np.mean(np.sin(psi))**2)  #PLV_value
Exemplo n.º 12
0
def analyze_data():

    root_data = 'reservoir_data/'
    datadir_1 = 'data_fede_format_20020508_pen_3_rec_2'
    datadir_2 = 'data_fede_format_20020608_pen_2_rec_2'
    num_trials = 3
    duration_sync = 4000
    datadir = datadir_1
    what = 'teach'
    sync_bioamp_channel = 305
    delta_mem = 25
    smoothing_len = 100
    duration_rec = 6000
    duration = 6000
    show_activations = True

    for this_trial in range(1,num_trials):
        raw_data_input = np.loadtxt('reservoir_data/data_fede_format_20020508_pen_3_rec_2/raw_data_input_data_fede_format_20020508_pen_3_rec_2_'+str(this_trial)+'.txt')
        raw_data = np.loadtxt('reservoir_data/data_fede_format_20020508_pen_3_rec_2/raw_data_data_fede_format_20020508_pen_3_rec_2_'+str(this_trial)+'.txt')

        ############# MEMBRANE
        # Time vector for analog signals
        Fs    = 1000/1e3 # Sampling frequency (in kHz)
        T     = duration
        nT    = np.round (Fs*T)
        timev = np.linspace(0,T,nT)
        #Conversion from spikes to analog
        membrane = lambda t,ts: np.atleast_2d(np.exp((-(t-ts)**2)/(2*delta_mem**2)))
        
        #teach on reconstructed signal
        X = L.ts2sig(timev, membrane, raw_data_input[:,0], raw_data_input[:,1], n_neu = 256)
        Y = L.ts2sig(timev, membrane, raw_data[:,0], raw_data[:,1], n_neu = 256)     
        #generate teaching signal orthogonal signal to input
        teach = np.loadtxt('/home/federico/projects/work/Berkeley_wehr/Tools/'+datadir+'/1_.txt')#
        framepersec = len(teach)/15
        teach = teach[0:framepersec/(duration_rec/1000)]
        #interpolate to match lenght
        signal_ad = [np.linspace(0,duration_rec,len(teach)), teach]
        ynew = np.linspace(0,duration_rec,nT+1)
        s = interpolate.interp1d(signal_ad[0], signal_ad[1],kind="linear")

        teach_sig = s(ynew)     
        teach_sig = np.abs(sigtool.hilbert(teach_sig)) #get envelope
        teach_sig = smooth(teach_sig,window_len=smoothing_len,window='hanning')
     
        if what == 'teach':
            teach_and_plot(X, Y, teach_sig, timev,  show_activations= show_activations)
        if what == 'test':
            test_and_plot(X, Y, teach_sig, timev,  show_activations= show_activations)
def hilbert(signal):
    """
  Wrapper for signal.hilbert() to optimize runtime
  """
    # Padding to optimize length
    padding = np.zeros(int(2**np.ceil(np.log2(len(signal)))) - len(signal))
    padding = np.zeros(int(2**np.ceil(np.log2(len(signal)))) - len(signal))

    tohilbert = np.hstack((signal, padding))

    # get envelope and cut padding
    result = sigtool.hilbert(tohilbert)
    result = result[0:len(signal)]

    return np.abs(result)
Exemplo n.º 14
0
      def draw_graph(self,amp,t,color="red",abs_checked=0, env_checked=0):
           """Updates the graph with new data/annotations"""

           nsamples = len(amp)
           if (nsamples > 100000):
              plotsamples = 4096
           elif (nsamples > 50000):
              plotsamples = 2048
           else:
              plotsamples = 1024

           ds_t,ds_amp  = my_resample(t,amp,plotsamples)

           maxval = np.max(ds_amp)
           minval = np.min(ds_amp)

           if abs_checked:
                ds_amp = np.abs(ds_amp)

           if env_checked:
                env = np.abs(sigtool.hilbert(ds_amp)) 
                ds_amp = env
                #ds_t = np.arange(0,len(env),t[1])


           # Set up the plot
           p = self.canvas.ax.plot(ds_t, ds_amp,color=color,lw=1)

           self.canvas.ax.grid(True)
#           self.canvas.ax.axis([10,ds_t[-1],minval,0])
#           self.canvas.ax.set_xscale("log", nonposx='clip')

    
           # Annotation
           self.canvas.ax.set_xlabel('Time (s)')
           self.canvas.ax.set_ylabel('Displacement')
  
           return p
Exemplo n.º 15
0
def demodulate2fsk(fileName):
    preemble = [
        1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0
    ]
    fs, y = wavfile.read(fileName)
    y_diff = np.diff(y, 1)
    y_env = np.abs(sigtool.hilbert(y_diff))
    h = signal.firwin(numtaps=100, cutoff=Fbit * 2, nyq=Fs / 2)
    y_filtered = signal.lfilter(h, 1.0, y_env)
    mean = np.mean(y_filtered)
    rx_data = []
    sampled_signal = y_filtered[int(Fs / Fbit / 2):len(y_filtered):int(Fs /
                                                                       Fbit)]
    for bit in sampled_signal:
        if bit > mean:
            rx_data.append(0)
        else:
            rx_data.append(1)
    for i in range(len(rx_data) - 24):
        if (rx_data[i:i + 24] == preemble):
            indexStart = i + 24
            break
    rx_data = rx_data[indexStart:]
    return frombits(rx_data)
Exemplo n.º 16
0
def detectEnvelope(snddata):

    return np.abs(sigtool.hilbert(snddata))
Exemplo n.º 17
0
def amp(x):
    '''
    Extracts amplitude envelope using Hilbert transform. X must be narrow
    band. No padding is performed so watch out for boundary effects
    '''
    return abs(hilbert(x))
Exemplo n.º 18
0
def ang(x):
    '''
    Uses the Hilbert transform to extract the phase of x. X should be
    narrow-band. The signal is not padded, so be wary of boundary effects.
    '''
    return angle(hilbert(x))
Exemplo n.º 19
0
def pghilbert(x):
    '''
    Extract phase gradient using the hilbert transform. See also pdiff.
    '''
    x = array(x)
    return pdiff(angle(hilbert(x)))
Exemplo n.º 20
0
Arquivo: hht.py Projeto: rainly/armor
def main():
      
  y = array([1,2,3,3,2,1,1,2,3,4,5,6,7,2,3,4,4,3,2,4,5,6,7,9,0])
  x = array([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24])

  y = array([1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2])
  x = array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13])


  y = array([1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1])
  x = array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13])

  y = [1, 2, 1, 2, 1, 2, 1, 3, 0, 2, 1, 2, 2, 1, 2]
  x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14]

  y = [4, 3, 4, 2, 5, 1, 6, 3, 9, 0,10, 1,11, 4,13]
  x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14]

  y = [0, -0.1, 1, 1, 2, 2.1, 2, 3, 3, 2, 2, 0, 0, 5, 5, 2, 2, 6, 6]
  x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18]

  print 'x-y:',
  print len(x)-len(y)
  
  F = cubicSpline(x,y)
  upper,lower = extremaDetection(y)
  #Fu = cubicSpline([v[0] for v in upper], [v[1] for v in upper])
  #Fl = cubicSpline([v[0] for v in lower], [v[1] for v in lower])
  #xnew = arange( min(x), max(x), 0.1)
  #ynew = interpolate.splev(xnew, F)
  #ynew_u = interpolate.splev(xnew, Fu)
  #ynew_l = interpolate.splev(xnew, Fl)
  #ynew_m = (ynew_u + ynew_l) /2

  #y1 = (ynew - ynew_m)   #first mode=original data interpolated - average trend
  y1, ynew_m = getNextMode(ynew, xnew)
  
  pyplot.plot(x,y,'.', xnew,ynew,'-', xnew, ynew_u, '--', xnew, ynew_l, '--',
              xnew, ynew_m,'--', xnew, y1, '-', xnew, [0]*len(xnew), '-' )
  pyplot.legend(['data', 'interpolation', 'upper', 'lower', '"mean"','first mode'], loc='best')
  pyplot.show()

  #computing the analytic signal (x + i.hilbertTransformOf(x) )
  Hy1 = signaltools.hilbert(y1)
  #print Hy1

  #extracting the phase
  phase1 = [cmath.phase(i) for i in Hy1]
  pyplot.plot(xnew, phase1, '.')
  pyplot.title('phase plot for the first mode y1, from the analytic signal')
  pyplot.show()
  #plotting the instantaneous frequency omega = dtheta/dt
  omega1=[]
  for i in range(len(phase1)-1):
      om = (phase1[i+1]-phase1[i])
      if om > 3.15:
          om = om- 2*pi
      if om < -3.15:
          om += 2*pi
      
      om = om/(xnew[i+1]-xnew[i])
      omega1.append(om)
      
  pyplot.plot(xnew[10:-11], omega1[10:-10],'.')
  pyplot.title('instantanenous frequency omega1 for the first mode')
  pyplot.show()
Exemplo n.º 21
0
def fsk_demodulator(fileName):

    # read the wave file

    fs, sig = wf.read(fileName)

    # Bandpass filter mark and space frequencies:

    center_freq = (mark_f + space_f) / 2

    trans_width = 260  # Width of transition from pass band to stop band, Hz (assume for now.But must be calculated from Spectrum)

    numtaps = 10  # Size of the FIR filter.

    #1. Bandpass filter for mark frequency:

    band_1 = [center_freq, 2 * mark_f - center_freq]  # Desired pass band, Hz

    edges_1 = [
        0, band_1[0] - trans_width, band_1[0], band_1[1],
        band_1[1] + trans_width, 0.5 * fs
    ]

    band_pass_1 = signal.remez(numtaps, edges_1, [0, 1, 0], Hz=fs)

    mark_filtered = signal.lfilter(band_pass_1, 1, sig)

    #2. Bandpass filter for space frequency:

    band_2 = [2 * space_f - center_freq, center_freq]  # Desired pass band, Hz

    edges_2 = [
        0, band_2[0] - trans_width, band_2[0], band_2[1],
        band_2[1] + trans_width, 0.5 * fs
    ]

    band_pass_2 = signal.remez(numtaps, edges_2, [0, 1, 0], Hz=fs)

    space_filtered = signal.lfilter(band_pass_2, 1, sig)

    #3. Envelope detector on mark and space bandpass filtered signals:

    mark_env = np.abs(sigtool.hilbert(mark_filtered))
    space_env = np.abs(sigtool.hilbert(space_filtered))

    #4. Compare and decision:

    seq = range(0, len(mark_env))
    rx = []
    for i in seq:
        if (mark_env[i] > space_env[i]).any():
            rx.append(1)
        if (mark_env[i] < space_env[i]).any():
            rx.append(0)
        if (mark_env[i] == space_env[i]).any():
            rx.append(0)

    #5. Convert bit to byte:

    bytes = [
        sum([byte[b] << b for b in range(0, 8)])
        for byte in zip(*(iter(rx), ) * 8)
    ]

    #6. Converting bytearray to hexadecimal string

    hex = ', '.join(format(x, '02x') for x in bytes)

    return hex
Exemplo n.º 22
0
def analyze_data():

    root_data = 'reservoir_data/'
    datadir_1 = 'data_fede_format_20020508_pen_3_rec_2'
    datadir_2 = 'data_fede_format_20020608_pen_2_rec_2'
    num_trials = 3
    duration_sync = 4000
    datadir = datadir_1
    what = 'teach'
    sync_bioamp_channel = 305
    delta_mem = 25
    smoothing_len = 100
    duration_rec = 6000
    duration = 6000
    show_activations = True

    for this_trial in range(1, num_trials):
        raw_data_input = np.loadtxt(
            'reservoir_data/data_fede_format_20020508_pen_3_rec_2/raw_data_input_data_fede_format_20020508_pen_3_rec_2_'
            + str(this_trial) + '.txt')
        raw_data = np.loadtxt(
            'reservoir_data/data_fede_format_20020508_pen_3_rec_2/raw_data_data_fede_format_20020508_pen_3_rec_2_'
            + str(this_trial) + '.txt')

        ############# MEMBRANE
        # Time vector for analog signals
        Fs = 1000 / 1e3  # Sampling frequency (in kHz)
        T = duration
        nT = np.round(Fs * T)
        timev = np.linspace(0, T, nT)
        #Conversion from spikes to analog
        membrane = lambda t, ts: np.atleast_2d(
            np.exp((-(t - ts)**2) / (2 * delta_mem**2)))

        #teach on reconstructed signal
        X = L.ts2sig(timev,
                     membrane,
                     raw_data_input[:, 0],
                     raw_data_input[:, 1],
                     n_neu=256)
        Y = L.ts2sig(timev,
                     membrane,
                     raw_data[:, 0],
                     raw_data[:, 1],
                     n_neu=256)
        #generate teaching signal orthogonal signal to input
        teach = np.loadtxt(
            '/home/federico/projects/work/Berkeley_wehr/Tools/' + datadir +
            '/1_.txt')  #
        framepersec = len(teach) / 15
        teach = teach[0:framepersec / (duration_rec / 1000)]
        #interpolate to match lenght
        signal_ad = [np.linspace(0, duration_rec, len(teach)), teach]
        ynew = np.linspace(0, duration_rec, nT + 1)
        s = interpolate.interp1d(signal_ad[0], signal_ad[1], kind="linear")

        teach_sig = s(ynew)
        teach_sig = np.abs(sigtool.hilbert(teach_sig))  #get envelope
        teach_sig = smooth(teach_sig,
                           window_len=smoothing_len,
                           window='hanning')

        if what == 'teach':
            teach_and_plot(X,
                           Y,
                           teach_sig,
                           timev,
                           show_activations=show_activations)
        if what == 'test':
            test_and_plot(X,
                          Y,
                          teach_sig,
                          timev,
                          show_activations=show_activations)
Exemplo n.º 23
0
def encode_and_poke(duration_rec=15000,delta_mem=25, sync_bioamp_channel=305, smoothing_len=90, trial = 1, what = 'teach', datadir = 'data_fede_format_20020508_pen_3_rec_2',  show_activations = False, save_data = 1, save_data_dir = 'reservoir_data'):
        
    duration_sync = 4000
    #figs = figure()
    data_command = '/home/federico/projects/work/trunk/data/Berkeley/rad-auditory/wehr/Tools/'+datadir+'/do_stim.sh'
    command1 = subprocess.Popen(['sh', data_command, str(trial)])
    #command1 = subprocess.Popen(['sh', '/home/federico/projects/work/trunk/data/Insects/Insect Neurophys Data/do_teach.sh'])
    out = nsetup.stimulate({},duration=duration_rec+duration_sync, send_reset_event=False)
    command1 = subprocess.Popen(['killall', 'aplay'])
    #signal = go_reconstruct_signal_from_out(out,figs,upch=300,dnch=305,delta_up=0.1,delta_dn=0.1,do_detrend=False)

    #use delta as syn
    raw_data = out[0].raw_data()
    sync_index = raw_data[:,1] == sync_bioamp_channel
    sync_index = np.where(sync_index)[0]
    time_start = raw_data[sync_index[2],0]
    #delete what happen before the syn index
    index_to_del = np.where(raw_data[:,0] < time_start+duration_sync)
    raw_data = np.delete(raw_data, index_to_del,axis=0)
    #make it start from zero
    raw_data[:,0] = raw_data[:,0] - np.min(raw_data[:,0])
    duration = np.max(raw_data[:,0])

    ############# MEMBRANE
    # Time vector for analog signals
    Fs    = 1000/1e3 # Sampling frequency (in kHz)
    T     = duration
    nT    = np.round (Fs*T)
    timev = np.linspace(0,T,nT)
    #Conversion from spikes to analog
    membrane = lambda t,ts: np.atleast_2d(np.exp((-(t-ts)**2)/(2*delta_mem**2)))
    
    #extract input and output
    dnch = 300
    upch = 305
    index_dn = np.where(raw_data[:,1] == dnch)[0]
    index_up = np.where(raw_data[:,1] == upch)[0]
    raw_data_input = []
    raw_data_input.extend(raw_data[index_dn,:])
    raw_data_input.extend(raw_data[index_up,:])
    raw_data_input = np.reshape(raw_data_input,[len(index_dn)+len(index_up),2])
    index_up = np.where(raw_data_input[:,1] == upch)[0]
    index_dn = np.where(raw_data_input[:,1] == dnch)[0]
    raw_data_input[index_dn,1] = 1
    raw_data_input[index_up,1] = 0   
    raw_data = out[0].raw_data()
    sync_index = raw_data[:,1] == sync_bioamp_channel
    sync_index = np.where(sync_index)[0]
    time_start = raw_data[sync_index[2],0]
    #delete what happen before the syn index
    index_to_del = np.where(raw_data[:,0] < time_start)
    raw_data = np.delete(raw_data, index_to_del,axis=0)
    #make it start from zero
    raw_data[:,0] = raw_data[:,0] - np.min(raw_data[:,0])
    
    index_to_del = np.where(raw_data[:,1] > 255)
    raw_data = np.delete(raw_data, index_to_del,axis=0)

    np.savetxt(save_data_dir+'/raw_data_input_'+str(datadir)+'_'+str(trial)+'.txt', raw_data_input)
    np.savetxt(save_data_dir+'/raw_data_'+str(datadir)+'_'+str(trial)+'.txt', raw_data)

    #teach on reconstructed signal
    X = L.ts2sig(timev, membrane, raw_data_input[:,0], raw_data_input[:,1], n_neu = 256)
    Y = L.ts2sig(timev, membrane, raw_data[:,0], raw_data[:,1], n_neu = 256)     
    #generate teaching signal orthogonal signal to input
    teach = np.loadtxt('/home/federico/projects/work/trunk/data/Berkeley/rad-auditory/wehr/Tools/'+datadir+'/1_.txt')#
    #teach = np.fromfile(open(trainfile),np.int16)[24:]
    #teach = teach/np.max(teach)
    framepersec = len(teach)/15
    teach = teach[0:framepersec/(duration_rec/1000)]
    #interpolate to match lenght
    signal_ad = [np.linspace(0,duration_rec,len(teach)), teach]
    ynew = np.linspace(0,duration_rec,nT+1)
    s = interpolate.interp1d(signal_ad[0], signal_ad[1],kind="linear")

    teach_sig = s(ynew)     
    teach_sig = np.abs(sigtool.hilbert(teach_sig)) #get envelope
    teach_sig = smooth(teach_sig,window_len=smoothing_len,window='hanning')
 
    if what == 'teach':
        teach_and_plot(X, Y, teach_sig, timev,  show_activations= show_activations)
    if what == 'test':
        test_and_plot(X, Y, teach_sig, timev,  show_activations= show_activations)
Exemplo n.º 24
0
def hilbert3(x,axis=0):
    m=np.ma.size(x,axis=axis)
    n=next_fast_len(m)
    r=hilbert(x,N=n,axis=axis)
    r=r.take(indices=range(m), axis=axis)
    return(r)
Exemplo n.º 25
0
y = list()

with open('buffer.txt', 'r') as f:
    for line in f:
        if line != '\n' or '':
            y.append(int(line.strip()))

print('File is loaded')
"""
DETECTOR
"""
print('Diff launch')
y_diff = np.diff(y, 1)  # differentiator
print('Hilbert launch')
y_env = np.abs(hilbert(y_diff))  # Hilbert' filter
print('Medfilt launch')
m_w = int(len(y) / 30)  # Median filter's window
y_filtered = medfilt(y_env, m_w + (m_w + 1) % 2)  # Median filter
print('Norm code')
y_filtered = y_filtered / max(y_filtered)  # Signal normalization
print('Code processing')

code = list()
for i in range(0, N):
    if y_filtered[int((1 / (2 * Fbit) + i / Fbit) / (dur / len(y)))] >= 0.75:
        code.append(1)  # High level
    elif y_filtered[int((1 / (2 * Fbit) + i / Fbit) / (dur / len(y)))] <= 0.55:
        code.append(0)  # low level
    else:
        code.append('?')  # Undetected
Exemplo n.º 26
0
		l3.append(k)
	elif 0.75 <= v < 1.0:
		k=0.9*(math.sin(2*3.14*300*v))
		l4.append(k)

#	l=math.sin(v)
#	f.write((str(v))+" "+(str(k))+" "+(str(l) + "\n"))

comb= l1+l2+l3+l4
k=[]
for i in range(len(comb)):
	i1=i/10000.
	k.append(i1)
	f.write(str(i1)+" "+(str(comb[i])+"\n"))

hsignal = sigtool.hilbert(comb)

amp = [abs(hsignal[i])for i in range(len(hsignal))]
ph  = [phase(hsignal[i])for i in range(len(hsignal))]

tm=[]
mxt=[]
mnt=[]

for i in range(1,len(ph)):
	if ph[i-1] > ph[i]:
		tm.append(i-1)
		mxt.append(ph[i-1])
		mnt.append(ph[i])

dat=open("dat.txt","w")
Exemplo n.º 27
0
snr = 10*np.log10(np.mean(np.square(y)) / np.mean(np.square(noise)))
print "SNR = %fdB" % snr
y=np.add(y,noise)
#view the data after adding noise
plot_data(y)

"""
Differentiator
"""
y_diff = np.diff(y,1)

"""
Envelope detector + low-pass filter
"""
#create an envelope detector and then low-pass filter
y_env = np.abs(sigtool.hilbert(y_diff))
h=signal.firwin( numtaps=100, cutoff=Fbit*2, nyq=Fs/2)
y_filtered=signal.lfilter( h, 1.0, y_env)
#view the data after adding noise
N_FFT = float(len(y_filtered))
f = np.arange(0,Fs/2,Fs/N_FFT)
w = np.hanning(len(y_filtered))
y_f = np.fft.fft(np.multiply(y_filtered,w))
y_f = 10*np.log10(np.abs(y_f[0:N_FFT/2]/N_FFT))
pl.subplot(3,1,1)
pl.plot(t[0:Fs*N_prntbits/Fbit],m[0:Fs*N_prntbits/Fbit])
pl.xlabel('Time (s)')
pl.ylabel('Frequency (Hz)')
pl.title('Original VCO output vs. time')
pl.grid(True)
pl.subplot(3,1,2)
Exemplo n.º 28
0
def encode_and_poke(duration_rec=15000,
                    delta_mem=25,
                    sync_bioamp_channel=305,
                    smoothing_len=90,
                    trial=1,
                    what='teach',
                    datadir='data_fede_format_20020508_pen_3_rec_2',
                    show_activations=False,
                    save_data=1,
                    save_data_dir='reservoir_data'):

    duration_sync = 4000
    #figs = figure()
    data_command = '/home/federico/projects/work/trunk/data/Berkeley/rad-auditory/wehr/Tools/' + datadir + '/do_stim.sh'
    command1 = subprocess.Popen(['sh', data_command, str(trial)])
    #command1 = subprocess.Popen(['sh', '/home/federico/projects/work/trunk/data/Insects/Insect Neurophys Data/do_teach.sh'])
    out = nsetup.stimulate({},
                           duration=duration_rec + duration_sync,
                           send_reset_event=False)
    command1 = subprocess.Popen(['killall', 'aplay'])
    #signal = go_reconstruct_signal_from_out(out,figs,upch=300,dnch=305,delta_up=0.1,delta_dn=0.1,do_detrend=False)

    #use delta as syn
    raw_data = out[0].raw_data()
    sync_index = raw_data[:, 1] == sync_bioamp_channel
    sync_index = np.where(sync_index)[0]
    time_start = raw_data[sync_index[2], 0]
    #delete what happen before the syn index
    index_to_del = np.where(raw_data[:, 0] < time_start + duration_sync)
    raw_data = np.delete(raw_data, index_to_del, axis=0)
    #make it start from zero
    raw_data[:, 0] = raw_data[:, 0] - np.min(raw_data[:, 0])
    duration = np.max(raw_data[:, 0])

    ############# MEMBRANE
    # Time vector for analog signals
    Fs = 1000 / 1e3  # Sampling frequency (in kHz)
    T = duration
    nT = np.round(Fs * T)
    timev = np.linspace(0, T, nT)
    #Conversion from spikes to analog
    membrane = lambda t, ts: np.atleast_2d(
        np.exp((-(t - ts)**2) / (2 * delta_mem**2)))

    #extract input and output
    dnch = 300
    upch = 305
    index_dn = np.where(raw_data[:, 1] == dnch)[0]
    index_up = np.where(raw_data[:, 1] == upch)[0]
    raw_data_input = []
    raw_data_input.extend(raw_data[index_dn, :])
    raw_data_input.extend(raw_data[index_up, :])
    raw_data_input = np.reshape(raw_data_input,
                                [len(index_dn) + len(index_up), 2])
    index_up = np.where(raw_data_input[:, 1] == upch)[0]
    index_dn = np.where(raw_data_input[:, 1] == dnch)[0]
    raw_data_input[index_dn, 1] = 1
    raw_data_input[index_up, 1] = 0
    raw_data = out[0].raw_data()
    sync_index = raw_data[:, 1] == sync_bioamp_channel
    sync_index = np.where(sync_index)[0]
    time_start = raw_data[sync_index[2], 0]
    #delete what happen before the syn index
    index_to_del = np.where(raw_data[:, 0] < time_start)
    raw_data = np.delete(raw_data, index_to_del, axis=0)
    #make it start from zero
    raw_data[:, 0] = raw_data[:, 0] - np.min(raw_data[:, 0])

    index_to_del = np.where(raw_data[:, 1] > 255)
    raw_data = np.delete(raw_data, index_to_del, axis=0)

    np.savetxt(
        save_data_dir + '/raw_data_input_' + str(datadir) + '_' + str(trial) +
        '.txt', raw_data_input)
    np.savetxt(
        save_data_dir + '/raw_data_' + str(datadir) + '_' + str(trial) +
        '.txt', raw_data)

    #teach on reconstructed signal
    X = L.ts2sig(timev,
                 membrane,
                 raw_data_input[:, 0],
                 raw_data_input[:, 1],
                 n_neu=256)
    Y = L.ts2sig(timev, membrane, raw_data[:, 0], raw_data[:, 1], n_neu=256)
    #generate teaching signal orthogonal signal to input
    teach = np.loadtxt(
        '/home/federico/projects/work/trunk/data/Berkeley/rad-auditory/wehr/Tools/'
        + datadir + '/1_.txt')  #
    #teach = np.fromfile(open(trainfile),np.int16)[24:]
    #teach = teach/np.max(teach)
    framepersec = len(teach) / 15
    teach = teach[0:framepersec / (duration_rec / 1000)]
    #interpolate to match lenght
    signal_ad = [np.linspace(0, duration_rec, len(teach)), teach]
    ynew = np.linspace(0, duration_rec, nT + 1)
    s = interpolate.interp1d(signal_ad[0], signal_ad[1], kind="linear")

    teach_sig = s(ynew)
    teach_sig = np.abs(sigtool.hilbert(teach_sig))  #get envelope
    teach_sig = smooth(teach_sig, window_len=smoothing_len, window='hanning')

    if what == 'teach':
        teach_and_plot(X,
                       Y,
                       teach_sig,
                       timev,
                       show_activations=show_activations)
    if what == 'test':
        test_and_plot(X,
                      Y,
                      teach_sig,
                      timev,
                      show_activations=show_activations)
Exemplo n.º 29
0
snr = 10*np.log10(np.mean(np.square(y)) / np.mean(np.square(noise)))
print "SNR = %fdB" % snr
y=np.add(y,noise)
#view the data after adding noise
plot_data(y)

"""
Differentiator
"""
y_diff = np.diff(y,1)

"""
Envelope detector + low-pass filter
"""
#create an envelope detector and then low-pass filter
y_env = np.abs(sigtool.hilbert(y_diff))
h=signal.firwin( numtaps=100, cutoff=Fbit*2, nyq=Fs/2)
y_filtered=signal.lfilter( h, 1.0, y_env)
#view the data after adding noise
N_FFT = float(len(y_filtered))
f = np.arange(0,Fs/2,Fs/N_FFT)
w = np.hanning(len(y_filtered))
y_f = np.fft.fft(np.multiply(y_filtered,w))
y_f = 10*np.log10(np.abs(y_f[0:N_FFT/2]/N_FFT))
pl.subplot(3,1,1)
pl.plot(t[0:Fs*N_prntbits/Fbit],m[0:Fs*N_prntbits/Fbit])
pl.xlabel('Time (s)')
pl.ylabel('Frequency (Hz)')
pl.title('Original VCO output vs. time')
pl.grid(True)
pl.subplot(3,1,2)
Exemplo n.º 30
0
def calculation(
    filename
):  ###SNR calculation, hand in filename of file containing filtered Ex,Ey,Ez after response (coreas style)
    f1 = open(filename, 'r')
    freq = []

    Power = []

    c = (3.e8)  ##speed of light in m/s

    thermal = 300.  ##Thermal component of noise from electronics in Kelvin
    #~ thermal = 40.		##Thermal component of noise from electronics in Kelvin
    #print "THIS IS SIMULATED WITH 300 K THERMAL NOISE"

    nf = n + 1  ###giving in odd numbered time series size will avoid weird kinks in the spectrum
    freq1 = (rfftfreq(nf, dt))  ##in Hz

    df = abs(freq1[0] - freq1[1])
    Temp = []
    B = []
    Therm = []

    farea = open(
        'areavsfreq150MHz.dat', 'r'
    )  ###antenna file with effective area, needed for noise, this is integrated response over theta and phi
    freq = []
    eff_area = []
    # ~ eff_height=[]
    # ~ impedance=[]
    for line in farea:
        if not line.strip().startswith("#"):
            columns = line.split()
            #print columns

            eff_area.append(float(columns[1]))
            # ~ eff_height.append(float(columns[2]))###not needed
            # ~ impedance.append(float(columns[3]))

    eff_area = np.array(eff_area)

    #print "area",eff_area.size
    neff = 0

    for i in range(0, len(freq1)):
        y = freq1[i]

        freq.append(y)
        if y <= fmax and y >= fmin:  ###defines freq band we want in Hz
            y = y / (10**6
                     )  ##convert to MHz since noise func needs freq in MHz
            x = noise_func(y)

            lamb = c / freq1[i]

            Brightnesstemp = (x * lamb * lamb / (2. * kb)
                              )  ###calculates temperature of galactic noise

            B.append(Brightnesstemp)
            Therm.append(thermal)
            Temptot = (Brightnesstemp + thermal)
            Temp.append(Temptot)

            Power.append(kb * Temptot * df * 2 *
                         eff_area[neff])  ###Power in each freq bin

        else:
            y = y / (10**6)
            x = noise_func(y)

            lamb = c / freq1[i]

            Brightnesstemp = (x * lamb * lamb / (2 * kb))

            B.append(Brightnesstemp)
            Therm.append(thermal)
            Temptot = (Brightnesstemp + thermal)
            Temp.append(Temptot)

            Power.append(0)  ### Noise outside band set to zero

        neff = neff + 1
    del freq1
    freq = np.array(freq)
    ##Integrating out the solid angle

    Power = (
        np.array(Power)) * 0.5  ###in W		######Because of half polarization

    df = abs(freq[0] - freq[1])

    Amplitude = (np.sqrt(Power * 2 * imp)
                 )  ##in V/m						####Power to amplitude in freq space

    Amplitude = Amplitude.astype(complex64)  ##Converting to complex type

    norm = Amplitude.size  ####Required normalization according to numpy

    ##Adding phase infor to noise amplitude

    for i in range(0, len(Amplitude)):
        phi = (2 * np.pi * random.random())  #*i*freq[i]/norm)

        #Amplitude[i] = Amplitude[i]*exp(-1j*phi)*np.sqrt(2*norm)#####Normalization originally used for paper
        Amplitude[i] = Amplitude[i] * exp(-1j * phi) * (
            2 * norm)  #####Normalization for erratum

        if Amplitude[i] != 0:
            Amplitude[i] = Amplitude[i] * (
                2 * (73.2 / imp)**0.5
            )  ####Corrected (4*(73.2/imp)**0.5) to (2*(73.2/imp)**0.5)###73.2 is avg impedence for dipole antenna

    totnoisepow = ((Power[0]) + (2 * np.sum(Power[1:])))  #

    print "total in freq domain", totnoisepow
    ####################Reading in signal###############
    time = []
    sigtime = []

    eX = []
    eY = []
    eZ = []
    for line in f1:
        if not line.strip().startswith("#"):
            columns = line.split()
            t = float(columns[0])  ##time information
            E1 = float(
                columns[1]
            )  ####This is how coreas writes out file. ZHAires uses the opposite order
            E2 = float(columns[2])
            E3 = float(columns[3])

            eX.append(E1)
            eY.append(E2)
            eZ.append(E3)

            sigtime.append(t)

    Xenv = np.abs(sigtool.hilbert(
        eX))  # hilbert(s) actually returns the analytical signal
    Yenv = np.abs(sigtool.hilbert(
        eY))  # hilbert(s) actually returns the analytical signal
    eX = np.array(eX)
    eY = np.array(eY)

    Xmaxenv = np.amax(Xenv)
    Ymaxenv = np.amax(Yenv)

    t = sigtime[0]
    for k in range(0, n):
        time.append(t)
        t = t + dt
    nt = len(time)

    ##Amplitude of noise in time domain
    tAmp = (np.fft.irfft(Amplitude))

    #print "lengths",len(tAmp), len(Amplitude)
    tPowN = tAmp * tAmp  #/(73.2)
    totnoisepowtime = np.sum(tPowN)

    print "total in time dom", totnoisepowtime

    Noiseenv = np.abs(sigtool.hilbert(tAmp))
    Noisemaxenv = (10**6) * np.amax(Noiseenv)
    maxpownoise = np.amax(tPowN)

    ###Uncomment to plot brightness##
    # ~ Xplt = np.linspace(1,500,5000)
    # ~ Yplt = noise_func(Xplt)
    # ~ fig3=plt.figure(figsize=(7,4.85),facecolor="white")
    # ~ ax3 = fig3.add_subplot(111)
    # ~ ax3.plot(Xplt,Yplt,label = 'Brightness')
    # ~ ax3.set_xlim([1,400])
    # ~ ax3.set_ylim([10**(-21),10**(-19)])
    # ~ xlabels=[0.1,1,10,100,400,500]

    # ~ ax3.loglog()
    # ~ ax3.set_xticklabels(xlabels, ha='center')
    # ~ ax3.yaxis.labelpad = 0.0001
    # ~ ax3.set_ylabel('Brightness(Wm$^{-2}$Hz$^{-1}$sr$^{-1}$)',va='center',fontsize=15,labelpad = 10)
    # ~ ax3.set_xlabel('Frequency (MHz)',va='center',fontsize=15,labelpad = 10)

    # ~ fig3.tight_layout(w_pad=0.5,h_pad = 1.2)

    # ~ del Xplt,Yplt
    ########################################Calculating RMS noise######################################

    rmsnoise = 0.
    for kj in range(0, tAmp.size):
        rmsnoise = rmsnoise + (tAmp[kj] * tAmp[kj])
    rmsnoise = (10**6) * ((rmsnoise / tAmp.size)**0.5)  ##########microvolt

    SNRx = (Xmaxenv * Xmaxenv) / (rmsnoise * rmsnoise)
    SNRy = (Ymaxenv * Ymaxenv) / (rmsnoise * rmsnoise)

    return B, Temp, Power, sigtime, tAmp, eX, eY, eZ, Xenv, freq, Therm, Amplitude, time, SNRx, SNRy
Exemplo n.º 31
0
def __opto_get_lfp_analytic_helper__((i,data,fa,fb,Fs,order)):
    '''
    Parallel function wrapper for opto_get_lfp_analytic
    '''
    print 5,i
    return i,hilbert(bandfilter(data,fa,fb,Fs,order))
Exemplo n.º 32
0
def amp(x):
    '''
    Extracts amplitude envelope using Hilbert transform. X must be narrow
    band. No padding is performed so watch out for boundary effects
    '''
    return abs(hilbert(x))
#
# Demodulates signal using FSK
#

import matplotlib.pyplot as pyplot
import scipy.signal.signaltools as sigtool
import scipy.signal as signal
import numpy as np

from sim_fsk_encoder import *

# Differentiate received carrier signal
carrier_diff = np.diff(carrier, 1)

# Detect envelope to extract digital data
carrier_env = np.abs(sigtool.hilbert(carrier_diff))

# Create low pass filter
lpf = signal.firwin(numtaps=100, cutoff=bitrate * 2, nyq=Fs / 2)

# Filter our signal
carrier_filtered = signal.lfilter(lpf, 1.0, carrier_env)

# Slice to ones and zeros to extract original bits
mean = np.mean(carrier_filtered)
rx_data = []
sampled_signal = carrier_filtered[int(Fs / bitrate /
                                      2):len(carrier_filtered):int(Fs /
                                                                   bitrate)]
for bit in sampled_signal:
    if bit > mean: