Exemple #1
0
def estim_diff(input_sig, input_seg_len, output_sig, output_seg_len, nperseg,
               num_of_seg, nperseg_step):
    """The difference between estimators in both directions. 
    inputs:
        seg_num:        number of segments to divide the music piece into. In practice
                        we always divide the music to seg_num+1 segments ignoring to 
                        last segment.
                                
        nperseg:        initial Welch method window size
    
    """
    res = np.empty(num_of_seg)

    for i in range(num_of_seg):
        # converting int16 wav file to a float signal and cutting the signal to
        # pieces of size seg_len. Also selecting the mono signal

        temp_input_sig = pcm2float(
            input_sig[input_seg_len * i:input_seg_len * (i + 1)], 'float32')[:,
                                                                             0]
        temp_output_sig = pcm2float(
            output_sig[output_seg_len * i:output_seg_len * (i + 1)],
            'float32')[:, 0]
        t = time()
        S_inp = welch(temp_input_sig, nperseg=nperseg * nperseg_step)[1]
        S_out = welch(temp_output_sig, nperseg=nperseg * nperseg_step)[1]

        res[i] = delta_estimator_3(S_out / S_inp, S_inp) - delta_estimator_3(
            S_inp / S_out, S_out)
        #out=float2pcm(output_sig,'int16')
    return res
def SIC_method(X,Y,order=100):   
    #low-passing to take LFP only
    
    #h_for=AR_fit(X,Y,order)
    #y_new=signal.fftconvolve(h_for,X)
    #h_back=AR_fit(Y,X,order)
    #x_new=signal.fftconvolve(h_back,Y)

    #Sx=welch(x_new,nperseg=1000)[1]
    #Sy=welch(y_new,nperseg=1000)[1]

    Sy=welch(Y,nperseg=1000)[1]
    Sx=welch(X,nperseg=1000)[1]

    X_Y=delta_estimator_3(Sy/Sx,Sx)
    Y_X=delta_estimator_3(Sx/Sy,Sy)
            
    #mask1=Sy!=0
    #mask2=Sx[mask1]!=0
    #X_Y=eval('delta_estimator_'+str(method_no))(Sy[mask1][mask2][1:-1]/Sx[mask1][mask2][1:-1],Sx[mask1][mask2][1:-1])
    #Y_X=eval('delta_estimator_'+str(method_no))(Sx[mask1][mask2][1:-1]/Sy[mask1][mask2][1:-1],Sy[mask1][mask2][1:-1])
    #X_Y=eval('delta_estimator_'+str(method_no))(Sy[mask1][mask2][1:-1]/Sx[mask1][mask2][1:-1],Sx[mask1][mask2][1:-1])
    #Y_X=eval('delta_estimator_'+str(method_no))(Sx[mask1][mask2][1:-1]/Sy[mask1][mask2][1:-1],Sy[mask1][mask2][1:-1])

    #X_Y=np.var(y_new)/float(np.sum(h_for**2)*np.var(X))
    #Y_X=np.var(x_new)/float(np.sum(h_back**2)*np.var(Y))
    
    return X_Y,Y_X    
def SIC_method(X, Y, method_no, order=200):
    #low-passing to take LFP only

    h_for = AR_fit(X, Y, order)
    y_new = signal.fftconvolve(h_for, X)
    h_back = AR_fit(Y, X, order)
    x_new = signal.fftconvolve(h_back, Y)

    Sx = welch(x_new, nperseg=500)[1]
    Sy = welch(y_new, nperseg=500)[1]

    #Sy=welch(Y,nperseg=500)[1]
    #Sx=welch(X,nperseg=500)[1]

    mask1 = Sy != 0
    mask2 = Sx[mask1] != 0
    #plt.plot(Sx)
    #plt.show()
    X_Y = eval('delta_estimator_' +
               str(method_no))(Sy[mask1][mask2][1:-1] / Sx[mask1][mask2][1:-1],
                               Sx[mask1][mask2][1:-1])
    Y_X = eval('delta_estimator_' +
               str(method_no))(Sx[mask1][mask2][1:-1] / Sy[mask1][mask2][1:-1],
                               Sy[mask1][mask2][1:-1])
    #return abs(X_Y)<abs(Y_X)
    return X_Y, Y_X
Exemple #4
0
def estim_diff(input_sig, input_seg_len, output_sig, output_seg_len, nperseg, num_of_seg, nperseg_step):
    """The difference between estimators in both directions. 
    inputs:
        seg_num:        number of segments to divide the music piece into. In practice
                        we always divide the music to seg_num+1 segments ignoring to 
                        last segment.
                                
        nperseg:        initial Welch method window size
    
    """
    res=np.empty(num_of_seg)
    
    for i in range(num_of_seg):
        # converting int16 wav file to a float signal and cutting the signal to 
        # pieces of size seg_len. Also selecting the mono signal

        temp_input_sig=pcm2float(input_sig[input_seg_len*i:input_seg_len*(i+1)],'float32')[:,0]
        temp_output_sig=pcm2float(output_sig[output_seg_len*i:output_seg_len*(i+1)],'float32')[:,0]
        t=time()
        S_inp=welch(temp_input_sig,nperseg=nperseg*nperseg_step)[1]    
        S_out=welch(temp_output_sig,nperseg=nperseg*nperseg_step)[1]    

        res[i]=delta_estimator_3(S_out/S_inp,S_inp)-delta_estimator_3(S_inp/S_out,S_out)
        #out=float2pcm(output_sig,'int16')
    return res
def SIC_method(X, Y, order=100):
    #low-passing to take LFP only

    #h_for=AR_fit(X,Y,order)
    #y_new=signal.fftconvolve(h_for,X)
    #h_back=AR_fit(Y,X,order)
    #x_new=signal.fftconvolve(h_back,Y)

    #Sx=welch(x_new,nperseg=1000)[1]
    #Sy=welch(y_new,nperseg=1000)[1]

    Sy = welch(Y, nperseg=1000)[1]
    Sx = welch(X, nperseg=1000)[1]

    X_Y = delta_estimator_3(Sy / Sx, Sx)
    Y_X = delta_estimator_3(Sx / Sy, Sy)

    #mask1=Sy!=0
    #mask2=Sx[mask1]!=0
    #X_Y=eval('delta_estimator_'+str(method_no))(Sy[mask1][mask2][1:-1]/Sx[mask1][mask2][1:-1],Sx[mask1][mask2][1:-1])
    #Y_X=eval('delta_estimator_'+str(method_no))(Sx[mask1][mask2][1:-1]/Sy[mask1][mask2][1:-1],Sy[mask1][mask2][1:-1])
    #X_Y=eval('delta_estimator_'+str(method_no))(Sy[mask1][mask2][1:-1]/Sx[mask1][mask2][1:-1],Sx[mask1][mask2][1:-1])
    #Y_X=eval('delta_estimator_'+str(method_no))(Sx[mask1][mask2][1:-1]/Sy[mask1][mask2][1:-1],Sy[mask1][mask2][1:-1])

    #X_Y=np.var(y_new)/float(np.sum(h_for**2)*np.var(X))
    #Y_X=np.var(x_new)/float(np.sum(h_back**2)*np.var(Y))

    return X_Y, Y_X
Exemple #6
0
def SIC(X,Y):
    Sx=welch(X,nperseg=500)[1]
    Sy=welch(Y,nperseg=500)[1]
    Delta_X_Y=np.sum(Sy)/(np.sum(Sx)*np.sum(Sy/Sx))
    Delta_Y_X=np.sum(Sx)/(np.sum(Sy)*np.sum(Sx/Sy))
    if Delta_X_Y>Delta_Y_X:
        print "X_t causes Y_t"
    else:
        print "Y_t causes X_t"
def SIC_method(X,Y,method_no,order=200):   
    #low-passing to take LFP only

    h_for=AR_fit(X,Y,order)
    y_new=signal.fftconvolve(h_for,X)
    h_back=AR_fit(Y,X,order)
    x_new=signal.fftconvolve(h_back,Y)
    
    Sx=welch(x_new,nperseg=500)[1]
    Sy=welch(y_new,nperseg=500)[1]

    #Sy=welch(Y,nperseg=500)[1]
    #Sx=welch(X,nperseg=500)[1]
    
    mask1=Sy!=0
    mask2=Sx[mask1]!=0
    #plt.plot(Sx)
    #plt.show()
    X_Y=eval('delta_estimator_'+str(method_no))(Sy[mask1][mask2][1:-1]/Sx[mask1][mask2][1:-1],Sx[mask1][mask2][1:-1])
    Y_X=eval('delta_estimator_'+str(method_no))(Sx[mask1][mask2][1:-1]/Sy[mask1][mask2][1:-1],Sy[mask1][mask2][1:-1])
    #return abs(X_Y)<abs(Y_X)
    return X_Y,Y_X
Exemple #8
0
def linear_filter_SIC(FO=5,
                      BO=5,
                      ksim_size=1000,
                      x_size=10000,
                      AR_amp_upperbound=0.1,
                      noise_amp=0.1,
                      in_noise=False,
                      out_noise=False,
                      denoise=False,
                      win_length=500):
    """ The main function for generating a toy model consisting of two linear filters. The input of the first filter is white noise and the output
    of it is feeded to the second filter as X_t (the cause). The output of the second filter in turn is considered to be Y_t. The coefficients of the filters
    are also i.i.d gaussian variables. The function finally returns the SDRs in both directions for a number of trials specified by ksim_size.
    The linear filters are defined according scipy IIR filters:  
                                
                                scipy.signal.lfilter(b, a, x, axis=-1, zi=None)     (**)
                                
    (See "http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.lfilter.html" for more info on IIR filters)
        
    input:
        FO:                     int: Forward order of IIR filter(length of b in (**))
        BO:                     int: Backward order of IIR filter(length of a in (**))
        ksim_size:              int: the number of trials of these toy model
        x_size:                 int: the length of the input white noise to the first filter
        AR_amp_upperbound:      float: a coefficient to control the largness of the feedback coefficients (to maitain BIBO stability)
        noise_amp:              float: the amplitude of noise (standard deviation of the white additive noise)
        in_noise:               boolean: whether there is input additive noise
        out_noise:              boolean: whether there is output additive noise
        denoise:                boolean: whether we denoise the observations by fitting an autoregressive filter in both directions (presummably the real model!)
    
    output:
        Dir                    numpy ndarray.float64: stored values of SDR for both directions for different trials and also the variances of X_t and Y_t
    """
    #initializing Dir matrix
    Dir = np.empty((ksim_size, 2, 2))

    #looping for different trials
    for ksim in xrange(ksim_size):
        #setting the initial coefficients of filter assuming that the first element of coefficients of filters in both directions is 1 (for symmetry)
        A_ = np.random.randn(FO) * AR_amp_upperbound
        A = np.hstack((np.ones(1), -A_))
        B_ = np.random.randn(FO) * AR_amp_upperbound
        B = np.hstack((np.ones(1), -B_))

        E_ = np.random.randn(BO - 1) * AR_amp_upperbound
        F_ = np.random.randn(BO - 1) * AR_amp_upperbound
        E = np.hstack((np.ones(1), -E_))
        F = np.hstack((np.ones(1), -F_))

        #re-setting the coefficients in case either of the two filters are not BIBO stable
        while np.any(np.abs(np.roots(E)) >= 0.99) or np.any(
                np.abs(np.roots(A)) >= 0.99):
            A_ = np.random.randn(FO) * AR_amp_upperbound
            A = np.hstack((np.ones(1), -A_))
            E_ = np.random.randn(BO - 1) * AR_amp_upperbound
            E = np.hstack((np.ones(1), -E_))
        while np.any(np.abs(np.roots(F)) > 0.99) or np.any(
                np.abs(np.roots(B)) >= 0.99):
            B_ = np.random.randn(FO) * AR_amp_upperbound
            B = np.hstack((np.ones(1), -B_))
            F_ = np.random.randn(BO - 1) * AR_amp_upperbound
            F = np.hstack((np.ones(1), -F_))

        # printing the final selected coefficients
        print("A:", A)
        print("B:", B)

        # generating the cause, X_t (here x) and the effect, Y_t (here y)
        x = signal.lfilter(A, E, np.random.randn(x_size))
        y = signal.lfilter(B, F, x)

        # storing total variances of cause and effect time series
        Dir[ksim, 0, 0] = np.var(x)
        Dir[ksim, 1, 1] = np.var(y)

        # adding noise
        if out_noise:
            y += np.random.randn(x_size) * noise_amp
        if in_noise:
            x += np.random.randn(x_size) * noise_amp

        # denoising if necessarily
        if denoise:
            order = 20  #the AR order
            h_for = AR_fit(x, y, order)
            y_new = signal.fftconvolve(h_for, x)
            h_back = AR_fit(y, x, order)
            x_new = signal.fftconvolve(h_back, y)

            Dir[ksim, 0,
                1] = np.var(y_new) / float(np.sum(h_for**2) * np.var(x))
            Dir[ksim, 1,
                0] = np.var(x_new) / float(np.sum(h_back**2) * np.var(y))

        else:
            Sy = welch(y, nperseg=win_length)[1]
            Sx = welch(x, nperseg=win_length)[1]
            Dir[ksim, 0, 1] = SDR_estimator(Sy / Sx, Sx)
            Dir[ksim, 1, 0] = SDR_estimator(Sx / Sy, Sy)

    return Dir
Exemple #9
0
def linear_filter_decreasing_impulse(FO=5,
                                     BO=5,
                                     ksim_size=1000,
                                     x_size=10000,
                                     AR_amp_upperbound=0.1,
                                     noise_amp=0.1,
                                     in_noise=False,
                                     out_noise=False,
                                     denoise=False):
    """linear_filter_SIC() rewritten where the coefficients of IR functions are picked in a way that their absolute value decreases by the increase of time. For the definitions
    of arguments or the description of the logic of the code, see the body of linear_filter_SIC()."""
    Dir = np.empty((ksim_size, 2, 2))
    for ksim in xrange(ksim_size):
        A_ = np.random.randn(FO) * AR_amp_upperbound
        A_idx = abs(A_).argsort()[::-1]
        A = A_[A_idx]
        B_ = np.random.randn(FO) * AR_amp_upperbound
        B_idx = abs(B_).argsort()[::-1]
        B = B_[B_idx]

        E_ = np.random.randn(BO - 1) * AR_amp_upperbound
        F_ = np.random.randn(BO - 1) * AR_amp_upperbound
        E_idx = abs(E_).argsort()[::-1]
        F_idx = abs(F_).argsort()[::-1]
        E = np.hstack((np.ones(1), E_[E_idx]))
        F = np.hstack((np.ones(1), F_[F_idx]))
        while np.any(np.abs(np.roots(E)) >= 0.98) or np.any(
                np.abs(np.roots(A)) >= 0.98):
            A_ = np.random.randn(FO) * AR_amp_upperbound
            A_idx = abs(A_).argsort()[::-1]
            A = A_[A_idx]
            E_ = np.random.randn(BO - 1) * AR_amp_upperbound
            E_idx = abs(E_).argsort()[::-1]
            E = np.hstack((np.ones(1), E_[E_idx]))
        while np.any(np.abs(np.roots(F)) > 0.98) or np.any(
                np.abs(np.roots(B)) >= 0.98):
            B_ = np.random.randn(FO) * AR_amp_upperbound
            B_idx = abs(B_).argsort()[::-1]
            B = B_[B_idx]
            F_ = np.random.randn(BO - 1) * AR_amp_upperbound
            F_idx = abs(F_).argsort()[::-1]
            F = np.hstack((np.ones(1), F_[F_idx]))
        print("A:", A)
        print("B:", B)

        x = signal.lfilter(A, E, np.random.randn(x_size))
        y = signal.lfilter(B, F, x)
        Dir[ksim, 0, 0] = np.var(x)
        Dir[ksim, 1, 1] = np.var(y)

        if out_noise:
            y += np.random.randn(x_size) * noise_amp
        if in_noise:
            x += np.random.randn(x_size) * noise_amp

        if denoise:
            order = 100

            h_for = AR_fit(x, y, order)
            y_new = signal.fftconvolve(h_for, x)
            h_back = AR_fit(y, x, order)
            x_new = signal.fftconvolve(h_back, y)

            Dir[ksim, 0,
                1] = np.var(y_new) / float(np.sum(h_for**2) * np.var(x))
            Dir[ksim, 1,
                0] = np.var(x_new) / float(np.sum(h_back**2) * np.var(y))

        else:
            Sy = welch(y, nperseg=1000)[1]
            Sx = welch(x, nperseg=1000)[1]

            Dir[ksim, 0, 1] = SDR_estimator(Sy / Sx, Sx)

            Dir[ksim, 1, 0] = SDR_estimator(Sx / Sy, Sy)
    return Dir