def fft_data(arr, ft='lag'):
    """
    Returns the windowed fft of a time/freq array along the time axis, freq axis, or both.
    
    Parameters
    ==========
    arr: complex array
         (nfreq x ntimes) array of complex visibilities
    ft: str
         transform to return, either lag, m, or mlag

    Returns
    =======
    Returns the fft array that was asked for in ft argument.
    """
    freq_window = np.hanning(arr.shape[0])[:, np.newaxis] 
    time_window = np.hanning(arr.shape[-1])[np.newaxis, :]

    if ft=='lag':
        return np.fft.fftshift(np.fft.fft(freq_window * arr, axis=0), axes=0)
    elif ft=='m':
        return np.fft.fftshift(np.fft.fft(time_window * arr, axis=-1), axes=-1)
    elif ft=='mlag':
        return np.fft.fftshift(np.fft.fft2(freq_window * time_window * arr))
    else:
        raise Exception('only lag, m, or mlag allowed')
def new_resize_data_fft(data,factor=3,window=14):
    xwidth, ywidth = data.shape
    max_size = max(data.shape)

    print window
    #create hanning window and apply
    #hx = kaiser(xwidth,window)
    #hy = kaiser(ywidth,window)
    hx = hanning(xwidth)
    hy = hanning(ywidth)
    ham2d = sqrt(outer(hx,hy))

    if window is not None:
        data = data*ham2d

    #just create an array odd number as big
    #to avoid overuns etc - It needs to be padded anyhow
    max_size = max_size*factor

    if not max_size % 2:
        print "Not correct"
        return -1
        
    new_array = zeros([max_size, max_size], dtype="complex")
    x_start = (max_size - 1)/2 - (xwidth-1)/2
    x_end = x_start + xwidth
    y_start = (max_size - 1)/2 - (xwidth-1)/2
    y_end = y_start + ywidth

    print x_start,x_end,y_start,y_end

    new_array[x_start:x_end,y_start:y_end] = data
        
    return new_array,ham2d
Example #3
0
def load_signature(guids,sample_rate,nbits):

    amp,ampS, t= .49,.49,.05                   #Variable handels for Bits
    tBit = np.arange(0,t*sample_rate+1)/float(sample_rate)  #time Vector for Bits
    F0,F1,Fsync = 19600, 19300, 19900                       #Bits Carrier Frequency


    y0=amp*np.cos(2*np.pi*F0*tBit)                          #Bit0
    winB=np.hanning(len(y0))                                #Applying Windows
    y0=y0*winB

    y1=amp*np.cos(2*np.pi*F1*tBit)                          #Bit1
    y1=y1*winB                                              #Applying Windows

    ysync=ampS*np.cos(2*np.pi*Fsync*tBit)                   #Bit for Synchronization
    winS=np.hanning(len(ysync))                             #Applying Windows
    ysync=ysync*winS

    ySilence=amp*np.sin(2*np.pi*0*tBit)                     #Adding Silence in between all bits



    y0=np.concatenate([ySilence,y0,ySilence,ysync])         #Bit0 coupled with silence and Sync
    y1=np.concatenate([ySilence,y1,ySilence,ysync])         #Bit1 coupled with silence and Sync


    guids1=guids[0]                                         #First available guid
    signature=ysync                                         #Declare and place Sync
    for g in guids1:                                        #Complete Concatenated Signature
        if g=='0':                                          # concatenate Bit0
            signature=np.concatenate([signature,y0])
        else:                                               # concatenate Bit1
            signature=np.concatenate([signature,y1])

    return signature
Example #4
0
    def test_calculate_lanczos_kernel(self):
        """
        Tests the kernels implemented in C against their numpy counterpart.
        """
        x = np.linspace(-5, 5, 11)

        values = calculate_lanczos_kernel(x, 5, "hanning")
        np.testing.assert_allclose(
            values["only_sinc"], np.sinc(x), atol=1E-9)
        np.testing.assert_allclose(
            values["only_taper"], np.hanning(len(x)), atol=1E-9)
        np.testing.assert_allclose(
            values["full_kernel"], np.sinc(x) * np.hanning(len(x)),
            atol=1E-9)

        values = calculate_lanczos_kernel(x, 5, "blackman")
        np.testing.assert_allclose(
            values["only_sinc"], np.sinc(x), atol=1E-9)
        np.testing.assert_allclose(
            values["only_taper"], np.blackman(len(x)), atol=1E-9)
        np.testing.assert_allclose(
            values["full_kernel"], np.sinc(x) * np.blackman(len(x)),
            atol=1E-9)

        values = calculate_lanczos_kernel(x, 5, "lanczos")
        np.testing.assert_allclose(
            values["only_sinc"], np.sinc(x), atol=1E-9)
        np.testing.assert_allclose(
            values["only_taper"], np.sinc(x / 5.0), atol=1E-9)
        np.testing.assert_allclose(
            values["full_kernel"], np.sinc(x) * np.sinc(x / 5.0),
            atol=1E-9)
def spec_est2(A,d1,d2,win=True):

    """    computes 2D spectral estimate of A
           obs: the returned array is fftshifted
           and consistent with the f1,f2 arrays
           d1,d2 are the sampling rates in rows,columns   """
    
    import numpy as np

    l1,l2,l3 = A.shape
    df1 = 1./(l1*d1)
    df2 = 1./(l2*d2)
    f1Ny = 1./(2*d1)
    f2Ny = 1./(2*d2)

    f1 = np.arange(-f1Ny,f1Ny,df1)
    f2 = np.arange(-f2Ny,f2Ny,df2)
    
    if win == True:
        wx = np.matrix(np.hanning(l1))
        wy =  np.matrix(np.hanning(l2))
        window_s = np.repeat(np.array(wx.T*wy),l3).reshape(l1,l2,l3)
    else:
        window_s = np.ones((l1,l2,l3))

    an = np.fft.fft2(A*window_s,axes=(0,1))
    E = (an*an.conjugate()) / (df1*df2) / ((l1*l2)**2)
    E = np.fft.fftshift(E)
    E = E.mean(axis=2)

    return np.real(E),f1,f2,df1,df2,f1Ny,f2Ny
Example #6
0
 def hanning_taper(self, side='both', channels=None, offset=0):
     """Hanning taper
     
     Parameters
     ----------
     side : {'left', 'right', 'both'}
     channels : {None, int}
         The number of channels to taper. If None 5% of the total
         number of channels are tapered.
     offset : int
     
     Returns
     -------
     channels
     
     """
     if channels is None:
         channels = int(round(len(self()) * 0.02))
         if channels < 20:
             channels = 20
     dc = self.data
     if side == 'left' or side == 'both':
         dc[..., offset:channels+offset] *= (
             np.hanning(2*channels)[:channels])
         dc[...,:offset] *= 0. 
     if side== 'right' or side == 'both':
         if offset == 0:
             rl = None
         else:
             rl = -offset
         dc[..., -channels-offset:rl] *= (
             np.hanning(2*channels)[-channels:])
         if offset != 0:
             dc[..., -offset:] *= 0.
     return channels
Example #7
0
def lcn_octaves(X, kernel):
    """Apply octave-varying contrast normalization to an input with a given
    kernel.

    Notes:
    * This is the variant introduced in the LVCE Section of Chapter 5.
    * This approach is painfully heuristic, and tuned for the dimensions used
        in this work (36 bpo, 7 octaves).

    Parameters
    ----------
    X : np.ndarray, ndim=2, shape[1]==252.
        CQT representation, with 36 bins per octave and 252 filters.
    kernel : np.ndarray
        Convolution kernel (should be roughly low-pass).

    Returns
    -------
    Z : np.ndarray
        The processed output.
    """
    if X.shape[-1] != 252:
        raise ValueError(
            "Apologies, but this method is currently designed for input "
            "representations with a last dimension of 252.")
    x_hp = highpass(X, kernel)
    x_73 = local_l2norm(x_hp, np.hanning(73).reshape(1, -1))
    x_37 = local_l2norm(x_hp, np.hanning(37).reshape(1, -1))
    x_19 = local_l2norm(x_hp, np.hanning(19).reshape(1, -1))
    x_multi = np.array([x_73, x_37, x_19]).transpose(1, 2, 0)
    w = _create_triband_mask()**2.0
    return (x_multi * w).sum(axis=-1)
Example #8
0
 def applyWindow(self, window="hanning", ww=0, cf=0):
     '''
     Apply window function to frequency domain data
     cf: the frequency the window is centered over [Hz]
     ww: the window width [Hz], if ww equals 0 the window covers the full range
     '''
     self.info("Applying %s window ..." % window)
     if window == "hanning":
         if ww == 0:
             w = np.hanning(self.numfreq)
         else:
             pos = int((cf - self.lowF) / self.deltaF)
             halfwidth = int(ww / (2.0 * self.deltaF))
             w = np.zeros(self.numfreq)
             w[pos - halfwidth:pos + halfwidth] = np.hanning(2 * halfwidth)
     elif window == "hamming":
         if ww == 0:
             w = np.hamming(self.numfreq)
         else:
             pos = int((cf - self.lowF) / self.deltaF)
             halfwidth = int(ww / (2.0 * self.deltaF))
             w = np.zeros(self.numfreq)
             w[pos - halfwidth:pos + halfwidth] = np.hamming(2 * halfwidth)
     elif window == "blackman":
         if ww == 0:
             w = np.blackman(self.numfreq)
         else:
             pos = int((cf - self.lowF) / self.deltaF)
             halfwidth = int(ww / (2.0 * self.deltaF))
             w = np.zeros(self.numfreq)
             w[pos - halfwidth:pos + halfwidth] = np.blackman(2 * halfwidth)
     self.data = self.data * w
     self.done()
    def __init__(self):
        """ """
#         self.in_filename = 'WURB-1_20160612T230008+0200_N57.6629E12.6390_FS-384_Enil_UTAN_KON_TESTFIL-INDATA-FDX.wav'
        self.in_filename = 'TEST_FS384.wav'
        self.out_filename = 'TEST_FDX.wav'
        self.sample_rate_in = 384000
        self.sample_rate_out = 38400 ###### 44100
        self.channels = 1
        self.sample_width = 2 # 16 bits.
#         self.buffer_size_in = 1024 * 16
        self.buffer_size_in = 1024 * 4
#         self.buffer_size_in = 1024 * 2
#         self.buffer_size_out = 1878
#         self.buffer_size_out = 648
#         self.buffer_size_out = 204
        self.buffer_size_out = 408
#        self.buffer_size_out = 202
        self.divide_factor = 10
        self.overlap_in = self.buffer_size_in / 2
        self.overlap_out = self.buffer_size_out / 2
        # Create Hann window
#         self.window_in=0.5-np.cos(np.arange(self.buffer_size_in,dtype='float')*2.0*np.pi/(self.buffer_size_in-1))*0.5
#         self.window_out=0.5-np.cos(np.arange(self.buffer_size_out,dtype='float')*2.0*np.pi/(self.buffer_size_out-1))*0.5
        self.window_in=np.hanning(self.buffer_size_in)
        self.window_out=np.hanning(self.buffer_size_out)
        #
        self.extra_freq_bins = None
        # Perform translation.
        self.from_fs384_to_fdx()       
def de_disperse(Data, DM, del_t):
        """ De-disperses pulsar data. First fourier transforms data along time axis then get 
        the fourier frequencies with length "ntimes". The FT is then multiplied by the delay 
        phase factor, correcting for dispersion, and data is transformed back.
        
        Parameters
        ----------
        Data : float or array of floats
                Pulsar data array. Assumes (freq, corr_prod, time)
        DM :
                Pulsar dispersion measure in pc/cm**3 
        del_t: 
                Total observation time in seconds 
        Returns
        -------
        De-dispersed data

        """
        n_times = Data.shape[-1]
        FT_Data = np.fft.fft(np.hanning(n_times)[np.newaxis, :] * Data, axis=-1)
        ft_freq = np.fft.fftfreq(n_times)
        delay_nu = time_delay(DM, Data.shape[0])[0]
        FT_Data = FT_Data * np.exp(2j * np.pi * ft_freq[np.newaxis, :] * delay_nu[:, np.newaxis] * n_times / del_t)

        return np.fft.fft(np.hanning(n_times)[np.newaxis, :] * FT_Data, axis=-1)
Example #11
0
def hanningWindow(nx, ny, nPixX, nPixY):
	"""
	Return a Hanning window in 2D
	
	Args:
	    nx (TYPE): number of pixels in x-direction of mask
	    ny (TYPE): number of pixels in y-direction of mask
	    nPixX (TYPE): number of pixels in x-direction of the transition
	    nPixY (TYPE): number of pixels in y-direction of the transition
	
	Returns:
		 real: 2D apodization mask
		
	"""					
	winX = np.hanning(nPixX)
	winY = np.hanning(nPixY)

	winOutX = np.ones(nx)
	winOutX[0:nPixX/2] = winX[0:nPixX/2]
	winOutX[-nPixX/2:] = winX[-nPixX/2:]

	winOutY = np.ones(ny)
	winOutY[0:nPixY/2] = winY[0:nPixY/2]
	winOutY[-nPixY/2:] = winY[-nPixY/2:]		

	return np.outer(winOutX, winOutY)
def ripoc(f, g, M = 50, fitting_shape = (9, 9)):

    hy = numpy.hanning(f.shape[0])
    hx = numpy.hanning(f.shape[1])
    hw = hy.reshape(hy.shape[0], 1) * hx

    ff = f * hw
    gg = g * hw

    F = scipy.fftpack.fft2(ff)
    G = scipy.fftpack.fft2(gg)

    F = scipy.fftpack.fftshift(numpy.log(numpy.abs(F)))
    G = scipy.fftpack.fftshift(numpy.log(numpy.abs(G)))

    FLP = logpolar(F, (F.shape[0] / 2, F.shape[1] / 2), M)
    GLP = logpolar(G, (G.shape[0] / 2, G.shape[1] / 2), M)

    R = poc(FLP, GLP)

    angle = -R[1] / F.shape[0] * 360
    scale = 1.0 - R[2] / 100

    center = tuple(numpy.array(g.shape) / 2)
    rot = cv2.getRotationMatrix2D(center, -angle, 1.0 + (1.0 - scale))

    g_dash = cv2.warpAffine(g, rot, (g.shape[1], g.shape[0]), flags=cv2.INTER_LANCZOS4)

    t = poc(f, g_dash)

    return (t[1], t[2], angle, scale)
Example #13
0
    def gen_spec(self,x,m):
        itsreal = np.isreal(x[0])

        lx = x.size
        nt = (lx +m -1) // m
        xb = np.append(x,np.zeros(-lx+nt*m))
        xc = np.append(np.roll(x,int(m/2)),np.zeros(nt*m - lx))


        xr = np.reshape(xb, (m,nt), order='F') * np.outer(np.hanning(m),np.ones(nt))
        xs = np.reshape(xc, (m,nt), order='F') * np.outer(np.hanning(m),np.ones(nt))

        xm = np.zeros((m,2*nt),dtype='complex')
        xm[:,::2] = xr
        xm[:,1::2] = xs
        #xm=xr

        if itsreal:
            spec = np.fft.fft(xm,m,axis=0)[int(m/2):]
        else:
            spec = np.fft.fftshift(np.fft.fft(xm,m,axis=0))
        mx = np.max(spec)

        pwr = 64*(20* np.log(np.abs(spec)/mx + 1e-6)  + 60 )/60

        return np.real(pwr)
Example #14
0
def bell(size, ratio):
  n = size / ratio
  first_half = numpy.hanning(n * 2)[:n] * 65535
  r = size - n
  second_half = numpy.hanning(r * 2)[r:] * 65535
  bell = list(first_half) + list(second_half) + [0]
  return bell
Example #15
0
def compute_window(x, crop1, crop2, MARGIN=0, Pow=1):
    #
    #    __margin__
    #   /          \
    #  c1          c2


    # 1D Function
    # -----------
    w = np.ones(x)
    c1 = crop1 + MARGIN
    c2 = crop2+MARGIN
    w[:c1] = np.hanning(2*c1)[:c1]**Pow
    w[-c2:] = np.hanning(2*c2)[-c2:]**Pow

    #~ import matplotlib.pyplot as plt
    #~ plt.figure()
    #~ plt.plot(w)
    #~ plt.plot(1-w)
    #~ plt.show()

    # Turn into 2D : distance to the center
    # -------------------------------------
    R, C = generate_coords((x, x))
    rmax = int(R.max()-0.5)
    M = np.int32(rmax-np.sqrt((R+0.5)**2+(C+0.5)**2));
    M[M<0] = 0
    return w[M]
def window_func_2d(height, width):
    win_col = np.hanning(width)
    win_row = np.hanning(height)
    mask_col, mask_row = np.meshgrid(win_col, win_row)

    win = mask_col * mask_row

    return win
Example #17
0
 def comp_sig_repr(self):
     """Computes the signal representation, stft
     """
     if not hasattr(self.audioObject, '_data'):
         self.audioObject._read()
     
     if self.verbose:
         print ("Computing the chosen signal representation:")
     
     nc = self.audioObject.channels
     if nc != 2:
         raise ValueError("This implementation only deals "+
                          "with stereo audio!")
     
     self.sig_repr = {}
     
     # if more than 1min of signal, take 1 min in the middle
     # better way : sample data so as to take randomly in the signal
     lengthData = self.audioObject.data.shape[0]
     startData = 0
     endData = lengthData
     oneMinLenData = 60*self.audioObject.samplerate
     oneMinLenData *= 2 # or more than 1min?
     
     if lengthData>oneMinLenData:
         startData = (lengthData - oneMinLenData)/2
         endData = startData + oneMinLenData
         
     if self.sig_repr_params['tfrepresentation'] == 'stftold':
         self.sig_repr[0], freqs, times = ao.stft(
             self.audioObject.data[startData:endData,0],
             window=np.hanning(self.sig_repr_params['wlen']),
             hopsize=self.sig_repr_params['hopsize'],
             nfft=self.sig_repr_params['fsize'],
             fs=self.audioObject.samplerate
             )
         
         self.sig_repr[1], freqs, times = ao.stft(
             self.audioObject.data[startData:endData,1],
             window=np.hanning(self.sig_repr_params['wlen']),
             hopsize=self.sig_repr_params['hopsize'],
             nfft=self.sig_repr_params['fsize'],
             fs=self.audioObject.samplerate
             )
     else:
         self.tft.computeTransform(
             self.audioObject.data[startData:endData,0],)
         self.sig_repr[0] = self.tft.transfo
         self.tft.computeTransform(
             self.audioObject.data[startData:endData,1],)
         self.sig_repr[1] = self.tft.transfo
         freqs = self.tft.freq_stamps
             
     # keeping the frequencies, not computing them each time
     self.freqs = freqs
     
     del self.audioObject.data
Example #18
0
    def initialize(self, frame, target_centre, target_shape, context=2,
                   features=no_op, learn_filter=learn_mosse,
                   increment_filter=increment_mosse, response_cov=3,
                   n_perturbations=10, noise_std=0.04, l=0.01,
                   normalize=normalizenorm_vec, mask=True,
                   boundary='constant'):

        self.target_shape = target_shape
        self.learn_filter = learn_filter
        self.increment_filter = increment_filter
        self.features = features
        self.l = l
        self.normalize = normalize
        self.boundary = boundary

        # compute context shape
        self.context_shape = np.round(context * np.asarray(target_shape))
        self.context_shape[0] += (0 if np.remainder(self.context_shape[0], 2)
                                  else 1)
        self.context_shape[1] += (0 if np.remainder(self.context_shape[1], 2)
                                  else 1)

        # compute subframe size
        self.subframe_shape = self.context_shape + 8
        # compute target centre coordinates in subframe
        self.subframe_target_centre = PointCloud((
            self.subframe_shape // 2)[None])

        # extract subframe
        subframe = frame.extract_patches(target_centre,
                                         patch_size=self.subframe_shape)[0]

        # compute features
        subframe = self.features(subframe)

        # obtain targets
        targets = extract_targets(subframe, self.subframe_target_centre,
                                  self.context_shape, n_perturbations,
                                  noise_std)

        # generate gaussian response
        self.response = generate_gaussian_response(self.target_shape[-2:],
                                                   response_cov)

        if mask:
            cy = np.hanning(self.context_shape[0])
            cx = np.hanning(self.context_shape[1])
            self.cosine_mask = cy[..., None].dot(cx[None, ...])

        targets_pp = []
        for j, t in enumerate(targets):
            targets_pp.append(self._preprocess_vec(t))
        targets_pp = np.asarray(targets_pp)

        self.filter, self.num, self.den = self.learn_filter(
            targets_pp, self.response, l=self.l, boundary=self.boundary)
Example #19
0
def get_wins(correlation):
    
    # Initialize array for windows
    win_signl = np.zeros(len(correlation.data))
    win_noise = np.zeros(len(correlation.data))
    success = False
    
    # Determine window bounds for signal window
    s_0 = int((len(correlation.data)-1)/2)
    t_lo = int((correlation.stats.sac['dist']/minp.g_speed-minp.hw)*\
    correlation.stats.sampling_rate)
    t_hi = int((correlation.stats.sac['dist']/minp.g_speed+minp.hw)*\
    correlation.stats.sampling_rate)
    w_ind = (s_0-t_hi+1, s_0-t_lo+1, s_0+t_lo, s_0+t_hi)

    if w_ind[2] < w_ind[1] and minp.win_overlap == False:
        if minp.verbose == True:
            print 'No windows found. (Windows overlap) '
        return win_signl, win_noise, success
        
    
    # Construct signal window
    if minp.window == 'boxcar':
         win_signl[w_ind[0]:w_ind[1]] += 1.
         win_signl[w_ind[2]:w_ind[3]] += 1.
    elif minp.window == 'hann':
         win_signl[w_ind[0]:w_ind[1]] += np.hanning(w_ind[1]-w_ind[0])
         win_signl[w_ind[2]:w_ind[3]] += np.hanning(w_ind[3]-w_ind[2])
   
    
    # Determine window bounds for noise window
    noisewinshift = minp.sepsignoise*minp.hw
    t_lo = t_hi + int(noisewinshift*correlation.stats.sampling_rate)
    t_hi = t_lo + int(2*minp.hw*correlation.stats.sampling_rate)
    w_ind = (s_0-t_hi+1, s_0-t_lo+1, s_0+t_lo, s_0+t_hi)
    
    # Out of bounds?
    if w_ind[0] < 0 or w_ind[3] > len(correlation.data):
        if minp.verbose == True:
            print 'No windows found. (Noise window not covered by data)'
        # return two zero arrays - no measurement possible
        return win_noise, win_noise, success

    # Construct noise window
    if minp.window == 'boxcar':
         win_noise[w_ind[0]:w_ind[1]] += 1.
         win_noise[w_ind[2]:w_ind[3]] += 1.
    elif minp.window == 'hann':
         win_noise[w_ind[0]:w_ind[1]] += np.hanning(w_ind[1]-w_ind[0])
         win_noise[w_ind[2]:w_ind[3]] += np.hanning(w_ind[3]-w_ind[2])
    success = True
    
    return win_signl, win_noise, success

    
    
Example #20
0
def hanning2D(M, N):
    """
    A 2D hanning window, as per IDL's hanning function.  See numpy.hanning for the 1D description
    """
    if N <= 1:
        return np.hanning(M)
    elif M <= 1:
        return np.hanning(N) # scalar unity; don't window if dims are too small
    else:
        return np.outer(np.hanning(M),np.hanning(N))
Example #21
0
  def test_winf(self):
    signal = np.arange(10)
    winf = np.arange(5)
    windows = utils.sliding_window(signal, 5, 2, winf)
    self.assertEqual(windows.shape, (3, 5))
    np.testing.assert_equal(windows[:, 0], [0, 0, 0])
    np.testing.assert_equal(windows[0, :], np.arange(5) ** 2)

    self.assertEqual(utils.sliding_window(signal, 5, 2, np.hanning(5)).dtype, 
      np.hanning(5).dtype)
Example #22
0
def spec2ts(spec, sr):
    """Create time series with random (normal) phases from power spectrum."""
    phase = np.random.normal(0, pi, len(spec))
    ts_elev = np.fft.irfft(np.sqrt(spec*len(spec)*sr)*np.exp(1j*phase))
    # Taper down last second of ts so it can repeat
    ramp = np.hanning(sr*2)[0:sr]
    ts_elev[:sr] = ts_elev[:sr]*ramp
    ramp = np.hanning(sr*2)[sr:]
    ts_elev[-sr:] = ts_elev[-sr:]*ramp
    return ts_elev
Example #23
0
 def mainloop(self, q,q2,st):
     j={'F3':[], 'O2':[], 'O1':[], 'F8':[], 'F4':[], 'FC6':[], 'AF3':[], 'P7':[], 'P8':[], 'FC5':[], 'T8':[], 'AF4':[], 'F7':[], 'T7':[]}
     b={}
     #q2.put("Hi")
     firstdat=q.recv()
     #fileh=open("C:/Users/Gaurav/Desktop/Testlogs.txt", "w")
     #fileh.write(str(firstdat))
     #fileh.close()
     #q2.put('Hi')
     if type(firstdat)==str:q2.put(firstdat)
     #if firstdat=="j":q2.put("j")
     for i in self.sensors:
         current=firstdat[i]
         b[i]=current
         artree=PreprocessUtils.highpass(current)
         win32=numpy.hanning(512)
         artree=numpy.array(artree)
         stuff1=win32*artree
         stuff2=PreprocessUtils.bin_power(stuff1, [1,4,8,12,30], 128)
         stuff4=abs(20*numpy.log(stuff2))
         stuff5=tuple(stuff4[0])
         j[i].append(stuff5)
         #time.sleep(16/128)
     q2.send(j)
     while True:
         
         secondat=q.recv()
         #q2.send("recieved one")
         #fileh=open("C:/Users/Gaurav/Desktop/Testlogs.txt", "w")
         #fileh.write(str(secondat))
         #fileh.close()
         #q2.put('Hi')
         if type(secondat)==str:q2.put(secondat)
         #if str(secondat)=="die": q2.put("die");break
         for i in self.sensors:
             current=b[i]
             del current[0:16]
             current+=secondat[i]
             #q2.put(len(current))
             b[i]=current
             artree=PreprocessUtils.highpass(current)
             #q2.send("Highpass")
             win32=numpy.hanning(512)
             artree=numpy.array(artree)
             stuff1=win32*artree
             stuff2=PreprocessUtils.bin_power(stuff1, [1,4,8,12,30], 128)
             stuff4=abs(20*numpy.log(stuff2))
             stuff5=tuple(stuff4[0])
             j[i].append(stuff5)
             #time.sleep(16/128)
         #if len(j["FC5"])==st:
         q2.send(j)
         for i in j.keys():
             j[i]=[]
Example #24
0
    def __next__(self):
        seq_id = np.random.randint(self.n_seq)
        start_frame = self.ranges[seq_id][0]
        end_frame = self.ranges[seq_id][1]
        idx = np.random.randint(end_frame-start_frame-self.lookahead)
        lh_idx = idx + np.random.randint(self.lookahead)+1
        bbox = np.copy(self.gt[seq_id][idx])
        lh_bbox = np.copy(self.gt[seq_id][lh_idx])
        img_path = self.img_dir + ('/%s/%06d.JPEG'%(self.seq_names[seq_id], (start_frame-1)+idx))
        lh_img_path = self.img_dir + ('/%s/%06d.JPEG'%(self.seq_names[seq_id],(start_frame-1)+lh_idx))
           
        # resize the image mainly for reducing computational cost
        scale = 1
        if bbox[2:].prod() > self.max_object_sz**2:
            scale = self.max_object_sz/(bbox[2:].max())
        image = Image.open(img_path).convert('RGB')
        lh_image = Image.open(lh_img_path).convert('RGB')
        if scale!=1:
            image = image.resize((int(image.size[0]*scale),int(image.size[1]*scale)), Image.BICUBIC)
            lh_image = lh_image.resize((int(lh_image.size[0]*scale),int(lh_image.size[1]*scale)), Image.BICUBIC)
            bbox = np.round(bbox*scale)
            lh_bbox = np.round(lh_bbox*scale)
       
        # calculate sizes
        target_sz = bbox[2:]
        target_center = bbox[:2] + np.floor(target_sz/2)
        target_cell_sz = np.ceil(target_sz/self.cell_sz)
        window_sz = get_search_size(target_sz)
        window_cell_sz = np.ceil(window_sz/self.cell_sz)
        window_cell_sz = window_cell_sz - (window_cell_sz%2) + 1
        
        # get center cropped patch for current image
        center = bbox[:2] + np.floor(target_sz/2)
        patch = get_search_patch(image, center, window_sz)
        
        # get center cropped patch for lookahead image
        # patch size is same
        lh_target_sz = lh_bbox[2:]
        lh_center = lh_bbox[:2] + np.floor(lh_target_sz/2)
        lh_patch = get_search_patch(lh_image, lh_center, window_sz)

        # get some windows
        cos_window = np.outer(np.hanning(window_cell_sz),np.hanning(window_cell_sz))
        cos_window = cos_window[None,None,:,:].repeat(self.feat_num_channels,axis=1)
        output_sigma = target_cell_sz*self.output_sigma_factor
        label_map = gaussian_shaped_labels(output_sigma, window_cell_sz, target_sz)

        # tensorize them
        patch = torch.from_numpy(patch[None,:]).float()
        lh_patch = torch.from_numpy(lh_patch[None,:]).float()
        label_map = torch.from_numpy(label_map[None,None,:]).float()
        cos_window = torch.from_numpy(cos_window).float()

        return patch, lh_patch, target_cell_sz, label_map, cos_window, seq_id
Example #25
0
def hanning2d(M, N):
    """
    A 2D hanning window, as per IDL's hanning function.  See numpy.hanning for
    the 1d description.  Copied from http://code.google.com/p/agpy/source/browse/trunk/agpy/psds.py?r=343
    """
    # scalar unity; don't window if dims are too small
    if N <= 1:
        return np.hanning(M)
    elif M <= 1:
        return np.hanning(N)
    else:
        return np.outer(np.hanning(M), np.hanning(N))
Example #26
0
    def applyWindow(self, samples, window='hanning'):

        if window == 'bartlett':
            return samples*np.bartlett(len(samples))
        elif window == 'blackman':
            return samples*np.hanning(len(samples))
        elif window == 'hamming':
            return samples*np.hamming(len(samples))
        elif window == 'kaiser':
            return samples*np.kaiser(len(samples))
        else:
            return samples*np.hanning(len(samples))
Example #27
0
    def initialize(self, frame, target_centre, target_shape, context=2,
                   features=no_op, learn_filter=learn_kcf,
                   kernel_correlation=gaussian_correlation, response_cov=3,
                   l=0.01, normalize=normalizenorm_vec, mask=True,
                   boundary='constant', **kwargs):

        self.target_shape = target_shape
        self.learn_filter = learn_filter
        self.kernel_correlation = kernel_correlation
        self.features = features
        self.l = l
        self.normalize = normalize
        self.boundary = boundary

        # compute context shape
        self.context_shape = np.round(context * np.asarray(target_shape))
        self.context_shape[0] += (0 if np.remainder(self.context_shape[0], 2)
                                  else 1)
        self.context_shape[1] += (0 if np.remainder(self.context_shape[1], 2)
                                  else 1)

        # compute subframe size
        self.subframe_shape = self.context_shape + 8
        # compute target centre coordinates in subframe
        self.subframe_target_centre = PointCloud((
            self.subframe_shape // 2)[None])

        # extract subframe
        subframe = frame.extract_patches(target_centre,
                                         patch_size=self.subframe_shape)[0]

        # compute features
        subframe = self.features(subframe)

        # obtain targets
        target = subframe.extract_patches(self.subframe_target_centre,
                                          patch_size=self.context_shape)[0]

        # generate gaussian response
        self.response = generate_gaussian_response(self.target_shape[-2:],
                                                   response_cov)

        if mask:
            cy = np.hanning(self.context_shape[0])
            cx = np.hanning(self.context_shape[1])
            self.cosine_mask = cy[..., None].dot(cx[None, ...])

        target = self._preprocess_vec(target.pixels)

        self.alpha, self.target = self.learn_filter(
            target, self.response, kernel_correlation=self.kernel_correlation,
            l=self.l, boundary=self.boundary, **kwargs)
Example #28
0
def _compute_fft(i_data, q_data, correct_phase, iq_correction_wideband,
        hide_differential_dc_offset, convert_to_dbm, apply_window, decimation, iqswapedbit, Rx_Bw):

    Nsamp = len(i_data)
    rbw = Rx_Bw/Nsamp
    
    if hide_differential_dc_offset:
        i_data = i_data - np.mean(i_data)
        q_data = q_data - np.mean(q_data)
    
    if apply_window:
        i_data = i_data * np.hanning(len(i_data))
        q_data = q_data * np.hanning(len(q_data))
    
    
    if correct_phase:
        phi2_deg = 52   # phase error after which the T.D algorithm is skipped to avoid noise floor jumping
        # Measuring phase error
        phi_rad, Phi_deg = measurePhaseError(i_data, q_data)
        if decimation == 1: # F.D + T.D corrections
            if abs(Phi_deg) < phi2_deg:
                # T.D correction
                i_cal, q_cal = _calibrate_i_q_tarek1(i_data, q_data, phi_rad)
                # F.D correction
                i_data, q_data = imageAttenuation(i_cal, q_cal, Phi_deg, iqswapedbit, iq_correction_wideband, Rx_Bw, rbw)
            else:
                # F.D correction only at the edges
                q_data = q_data * np.sqrt(sum(i_data ** 2)/sum(q_data ** 2))
                i_data, q_data = imageAttenuation(i_data, q_data, Phi_deg, iqswapedbit, iq_correction_wideband, Rx_Bw, rbw)
        else:
            # Only T.D correction at the decimation level > 1
            i_data, q_data = _calibrate_i_q_tarek1(i_data, q_data, phi_rad)
        i_data = i_data - np.mean(i_data)
        q_data = q_data - np.mean(q_data)

    iq = i_data + 1j * q_data

    if apply_window:
        iq = iq * np.hanning(len(i_data))

    power_spectrum = np.abs(np.fft.fftshift(np.fft.fft(iq)))/len(i_data)

    if convert_to_dbm:
        power_spectrum = 20 * np.log10(power_spectrum)

    if hide_differential_dc_offset:
        median_index = len(power_spectrum) // 2
        power_spectrum[median_index] = (power_spectrum[median_index - 1]
            + power_spectrum[median_index + 1]) / 2

    return power_spectrum
Example #29
0
    def test_dft_2d(self):
        """Test the discrete Fourier transform on 2D data"""
        N = 16
        da = xr.DataArray(np.random.rand(N,N), dims=['x','y'],
                        coords={'x':range(N),'y':range(N)}
                         )
        ft = xrft.dft(da, shift=False)
        npt.assert_almost_equal(ft.values, np.fft.fftn(da.values))

        ft = xrft.dft(da, shift=False, window=True, detrend='constant')
        dim = da.dims
        window = np.hanning(N) * np.hanning(N)[:, np.newaxis]
        da_prime = (da - da.mean(dim=dim)).values
        npt.assert_almost_equal(ft.values, np.fft.fftn(da_prime*window))
def window(data, window='hanning', renormalize=1):
    ''' Windows the data with hanning window
        If renormalize is set to 1, the data is multiplied by
        a factor which makes the mean squared amplitude of the
        windowed data equal to the mean squared amplitude of the
        original data.  This is important for normalizing a PSD
        correctly.
    '''
    N = len(data)
    rescale = (8/3.)**0.5  # normalize the power for a hanning window
    if renormalize == 1:
        return data*np.hanning(N)*rescale
    else:
        return data*np.hanning(N)
 def _init_window(self, fs):
     self.fs = fs
     self.window_size = int(MAGIC_NUMBER * fs)
     self.window = np.hanning(self.window_size)
Example #32
0
def make_sound(rate=44100, frequency=5000, duration=0.1, amplitude=1,
               fade=0.01, chans='L+TTL'):
    """
    Build sounds and save bin file for upload to soundcard or play via
    sounddevice lib.

    :param rate: sample rate of the soundcard use 96000 for Bpod,
                    defaults to 44100 for soundcard
    :type rate: int, optional
    :param frequency: (Hz) of the tone, if -1 will create uniform random white
                    noise, defaults to 10000
    :type frequency: int, optional
    :param duration: (s) of sound, defaults to 0.1
    :type duration: float, optional
    :param amplitude: E[0, 1] of the sound 1=max 0=min, defaults to 1
    :type amplitude: intor float, optional
    :param fade: (s) time of fading window rise and decay, defaults to 0.01
    :type fade: float, optional
    :param chans: ['mono', 'L', 'R', 'stereo', 'L+TTL', 'TTL+R'] number of
                   sound channels and type of output, defaults to 'L+TTL'
    :type chans: str, optional
    :return: streo sound from mono definitions
    :rtype: np.ndarray with shape (Nsamples, 2)
    """
    sample_rate = rate  # Sound card dependent,
    tone_duration = duration  # sec
    fade_duration = fade  # sec

    tvec = np.linspace(0, tone_duration, tone_duration * sample_rate)
    tone = amplitude * np.sin(2 * np.pi * frequency * tvec)  # tone vec

    len_fade = int(fade_duration * sample_rate)
    fade_io = np.hanning(len_fade * 2)
    fadein = fade_io[:len_fade]
    fadeout = fade_io[len_fade:]
    win = np.ones(len(tvec))
    win[:len_fade] = fadein
    win[-len_fade:] = fadeout

    tone = tone * win
    ttl = np.ones(len(tone)) * 0.99
    one_ms = round(sample_rate/1000) * 10
    ttl[one_ms:] = 0
    null = np.zeros(len(tone))

    if frequency == -1:
        tone = amplitude * np.random.rand(tone.size)

    if chans == 'mono':
        sound = np.array(tone)
    elif chans == 'L':
        sound = np.array([tone, null]).T
    elif chans == 'R':
        sound = np.array([null, tone]).T
    elif chans == 'stereo':
        sound = np.array([tone, tone]).T
    elif chans == 'L+TTL':
        sound = np.array([tone, ttl]).T
    elif chans == 'TTL+R':
        sound = np.array([ttl, tone]).T

    return sound
Example #33
0
        lag_sum = 0
        sumarray = np.zeros(n + lag)
        sumarray[:n] = data[frame_index:frame_index + n]
        sumarray[:n] *= data[frame_index + lag:frame_index + n + lag]
        lag_sum = np.sum(sumarray[:n])
        ccf.append(lag_sum)
        nccf.append(lag_sum / np.sqrt(e[frame_index] * e[frame_index + lag]))
    return ccf, nccf


# storage
data = util.load_wav(input_file)
cc_correlogram = []
nccf_correlogram = []
best_frequencies = []
hann = np.hanning(frame_width)

# pre-calculate normalization factor data
squared_data = []
for i in range(0, len(data)):
    squared_data.append(data[i]**2)
e = [0.0]

for i in range(0, frame_width - 1):
    e[0] += squared_data[i]

for i in range(0, len(data) - frame_width):
    e.append(e[i - 1] - squared_data[i - 1] + squared_data[i + frame_width])

for i in tqdm(range(0, int((len(data) - frame_width * 2) / spacing))):
    cc, ncc = nccf(data, i * spacing, e, bitrate // f_max, bitrate // f_min)
Example #34
0
def hanning2d(M, N):
    """
    A 2D hanning window created by outer product.
    """
    return np.outer(np.hanning(M), np.hanning(N))
def tracker(hp, run, design, frame_name_list, pos_x, pos_y, target_w, target_h,
            final_score_sz, filename, image, templates_z, scores, start_frame):
    num_frames = np.size(frame_name_list)
    # stores tracker's output for evaluation
    bboxes = np.zeros((num_frames, 4))

    scale_factors = hp.scale_step**np.linspace(-np.ceil(hp.scale_num / 2),
                                               np.ceil(hp.scale_num / 2),
                                               hp.scale_num)
    # cosine window to penalize large displacements
    hann_1d = np.expand_dims(np.hanning(final_score_sz), axis=0)
    penalty = np.transpose(hann_1d) * hann_1d
    penalty = penalty / np.sum(penalty)

    context = design.context * (target_w + target_h)
    z_sz = np.sqrt(np.prod((target_w + context) * (target_h + context)))
    x_sz = float(design.search_sz) / design.exemplar_sz * z_sz

    # thresholds to saturate patches shrinking/growing
    min_z = hp.scale_min * z_sz
    max_z = hp.scale_max * z_sz
    min_x = hp.scale_min * x_sz
    max_x = hp.scale_max * x_sz

    # run_metadata = tf.RunMetadata()
    # run_opts = {
    #     'options': tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE),
    #     'run_metadata': run_metadata,
    # }

    run_opts = {}

    # with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        # Coordinate the loading of image files.
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        # save first frame position (from ground-truth)
        bboxes[
            0, :] = pos_x - target_w / 2, pos_y - target_h / 2, target_w, target_h

        image_, templates_z_ = sess.run(
            [image, templates_z],
            feed_dict={
                siam.pos_x_ph: pos_x,
                siam.pos_y_ph: pos_y,
                siam.z_sz_ph: z_sz,
                filename: frame_name_list[0]
            })
        new_templates_z_ = templates_z_

        t_start = time.time()

        # Get an image from the queue
        for i in range(1, num_frames):
            scaled_exemplar = z_sz * scale_factors
            scaled_search_area = x_sz * scale_factors
            scaled_target_w = target_w * scale_factors
            scaled_target_h = target_h * scale_factors
            image_, scores_ = sess.run(
                [image, scores],
                feed_dict={
                    siam.pos_x_ph: pos_x,
                    siam.pos_y_ph: pos_y,
                    siam.x_sz0_ph: scaled_search_area[0],
                    siam.x_sz1_ph: scaled_search_area[1],
                    siam.x_sz2_ph: scaled_search_area[2],
                    templates_z: np.squeeze(templates_z_),
                    filename: frame_name_list[i],
                },
                **run_opts)
            scores_ = np.squeeze(scores_)
            # penalize change of scale
            scores_[0, :, :] = hp.scale_penalty * scores_[0, :, :]
            scores_[2, :, :] = hp.scale_penalty * scores_[2, :, :]
            # find scale with highest peak (after penalty)
            new_scale_id = np.argmax(np.amax(scores_, axis=(1, 2)))
            # update scaled sizes
            x_sz = (1 - hp.scale_lr
                    ) * x_sz + hp.scale_lr * scaled_search_area[new_scale_id]
            target_w = (
                1 - hp.scale_lr
            ) * target_w + hp.scale_lr * scaled_target_w[new_scale_id]
            target_h = (
                1 - hp.scale_lr
            ) * target_h + hp.scale_lr * scaled_target_h[new_scale_id]
            # select response with new_scale_id
            score_ = scores_[new_scale_id, :, :]
            score_ = score_ - np.min(score_)
            score_ = score_ / np.sum(score_)
            # apply displacement penalty
            score_ = (1 - hp.window_influence
                      ) * score_ + hp.window_influence * penalty
            pos_x, pos_y = _update_target_position(pos_x, pos_y, score_,
                                                   final_score_sz,
                                                   design.tot_stride,
                                                   design.search_sz,
                                                   hp.response_up, x_sz)
            # convert <cx,cy,w,h> to <x,y,w,h> and save output
            bboxes[
                i, :] = pos_x - target_w / 2, pos_y - target_h / 2, target_w, target_h
            # update the target representation with a rolling average
            if hp.z_lr > 0:
                new_templates_z_ = sess.run(
                    [templates_z],
                    feed_dict={
                        siam.pos_x_ph: pos_x,
                        siam.pos_y_ph: pos_y,
                        siam.z_sz_ph: z_sz,
                        image: image_
                    })

                templates_z_ = (1 - hp.z_lr) * np.asarray(
                    templates_z_) + hp.z_lr * np.asarray(new_templates_z_)

            # update template patch size
            z_sz = (1 - hp.scale_lr
                    ) * z_sz + hp.scale_lr * scaled_exemplar[new_scale_id]

            # if run.visualization:
            #     show_frame(image_, bboxes[i,:], 1)

        t_elapsed = time.time() - t_start
        speed = num_frames / t_elapsed

        # Finish off the filename queue coordinator.
        coord.request_stop()
        coord.join(threads)

        # from tensorflow.python.client import timeline
        # trace = timeline.Timeline(step_stats=run_metadata.step_stats)
        # trace_file = open('timeline-search.ctf.json', 'w')
        # trace_file.write(trace.generate_chrome_trace_format())

    plt.close('all')

    return bboxes, speed
Example #36
0
def inverse_spectrum_truncation(psd,
                                max_filter_len,
                                low_frequency_cutoff=None,
                                trunc_method=None):
    """Modify a PSD such that the impulse response associated with its inverse
    square root is no longer than `max_filter_len` time samples. In practice
    this corresponds to a coarse graining or smoothing of the PSD.

    Parameters
    ----------
    psd : FrequencySeries
        PSD whose inverse spectrum is to be truncated.
    max_filter_len : int
        Maximum length of the time-domain filter in samples.
    low_frequency_cutoff : {None, int}
        Frequencies below `low_frequency_cutoff` are zeroed in the output.
    trunc_method : {None, 'hann'}
        Function used for truncating the time-domain filter.
        None produces a hard truncation at `max_filter_len`.

    Returns
    -------
    psd : FrequencySeries
        PSD whose inverse spectrum has been truncated.

    Raises
    ------
    ValueError
        For invalid types or values of `max_filter_len` and `low_frequency_cutoff`.

    Notes
    -----
    See arXiv:gr-qc/0509116 for details.
    """
    # sanity checks
    if type(max_filter_len) is not int or max_filter_len <= 0:
        raise ValueError('max_filter_len must be a positive integer')
    if low_frequency_cutoff is not None and low_frequency_cutoff < 0 \
        or low_frequency_cutoff > psd.sample_frequencies[-1]:
        raise ValueError(
            'low_frequency_cutoff must be within the bandwidth of the PSD')

    N = (len(psd) - 1) * 2

    inv_asd = FrequencySeries((1. / psd)**0.5, delta_f=psd.delta_f, \
        dtype=complex_same_precision_as(psd))

    inv_asd[0] = 0
    inv_asd[N / 2] = 0
    q = TimeSeries(numpy.zeros(N), delta_t=(N / psd.delta_f), \
        dtype=real_same_precision_as(psd))

    if low_frequency_cutoff:
        kmin = int(low_frequency_cutoff / psd.delta_f)
        inv_asd[0:kmin] = 0

    ifft(inv_asd, q)

    trunc_start = max_filter_len / 2
    trunc_end = N - max_filter_len / 2

    if trunc_method == 'hann':
        trunc_window = Array(numpy.hanning(max_filter_len), dtype=q.dtype)
        q[0:trunc_start] *= trunc_window[max_filter_len / 2:max_filter_len]
        q[trunc_end:N] *= trunc_window[0:max_filter_len / 2]

    q[trunc_start:trunc_end] = 0
    psd_trunc = FrequencySeries(numpy.zeros(len(psd)), delta_f=psd.delta_f, \
                                dtype=complex_same_precision_as(psd))
    fft(q, psd_trunc)
    psd_trunc *= psd_trunc.conj()
    psd_out = 1. / abs(psd_trunc)

    return psd_out
Example #37
0
def find_closest(date):
    segundos = time.mktime(date.timetuple())
    indice = np.argmin(np.abs(df_speed.index.values - segundos))
    print('Hora de la captura',
          datetime.datetime.fromtimestamp(df_speed.index[indice]))
    print('Diferencia en minutos', (df_speed.index[indice] - segundos) / 60)

    waveform = df_speed.iloc[indice]

    b, a = signal.butter(3, 2 * 10 / fs, 'highpass', analog=False)
    l = np.size(waveform)
    l_mitad = int(l / 2)
    f = np.arange(l) / l * fs
    #----------quitamos los 2 HZ del principio

    label = 'Peak'
    cte_p = 1
    cte_c = np.sqrt(2)

    hann = np.hanning(l)
    wave_f = signal.filtfilt(b, a, waveform)
    sptrm_P = np.abs(np.fft.fft(wave_f * 2 * hann) / l)
    sptrm_C = np.abs(np.fft.fft(wave_f * 1.63 * hann) / l)

    TRH = 0 * np.std(sptrm_P[0:l_mitad]) / 3

    indexes, properties = find_peaks(cte_p * 2 * sptrm_C[0:l_mitad],
                                     height=TRH,
                                     prominence=0.01,
                                     width=1,
                                     rel_height=0.75)

    minorLocator = AutoMinorLocator()
    #plt.figure(num=None, figsize=(24, 11), dpi=80, facecolor='w', edgecolor='k')
    plt.figure(num=None, figsize=(18, 8), dpi=80, facecolor='w', edgecolor='k')
    ax1 = plt.subplot2grid((4, 4), (0, 0), colspan=4, rowspan=4)
    plt.plot(f[0:l_mitad], cte_p * 2 * sptrm_P[0:l_mitad], 'b')
    #plt.plot(f[indexes]   , cte_p*2*sptrm_P[indexes]  ,'o')

    for counter, k in enumerate(harm.columns):

        if k[0] == 'i':
            print(counter, k)
            index = int(harm.iloc[indice][counter])
            if f[index] != 0:
                plt.plot(f[index], cte_p * 2 * sptrm_P[index], 'o')
                plt.text(f[index], cte_p * 2 * sptrm_P[index], str(k))

    plt.ylabel('mm/s ' + label)
    plt.title(str(date))
    plt.xlabel('Hz')
    plt.grid(True)

    plt.vlines(x=f[indexes],
               ymin=cte_p * 2 * sptrm_P[indexes] - properties["prominences"],
               ymax=cte_p * 2 * sptrm_P[indexes],
               color="C1")
    plt.hlines(y=properties["width_heights"],
               xmin=f[properties["left_ips"].astype(int)],
               xmax=f[properties["right_ips"].astype(int)],
               color="C1")
    #    plt.hlines(y=properties["width_heights"], xmin=f[properties["left_bases"].astype(int)],xmax=f[properties["right_bases"].astype(int)], color = "C2")

    ax1.xaxis.set_minor_locator(minorLocator)

    plt.tick_params(which='both', width=2)
    plt.tick_params(which='major', length=7)
    plt.tick_params(which='minor', length=4, color='r')

    return
Example #38
0
def _loop_once():
    '''Run the main loop once
    This uses the global variables from setup and start, and adds a set of global variables
    '''
    global parser, args, config, r, response, patch, monitor, ft_host, ft_port, ft_input
    global timeout, hdr_input, start, channel_items, channame, chanindx, item, prefix, begsample, endsample
    global scale_window, offset_window, window, taper, frequency, band_items, bandname, bandlo, bandhi, lohi, dat, power, chan, band, meandat, sample, F, i, lo, hi, count, key

    monitor.loop()
    time.sleep(patch.getfloat('general', 'delay'))

    scale_window = patch.getfloat('scale', 'window', default=1.)
    offset_window = patch.getfloat('offset', 'window', default=0.)
    window = patch.getfloat('processing', 'window', default=2)
    window = EEGsynth.rescale(window, slope=scale_window, offset=offset_window)

    monitor.update('window', window)

    window = int(round(window * hdr_input.fSample))  # in samples
    taper = np.hanning(window)
    frequency = np.fft.rfftfreq(window, 1.0 / hdr_input.fSample)

    band_items = config.items('band')
    bandname = []
    bandlo = []
    bandhi = []
    for item in band_items:
        # channel numbers are one-offset in the ini file, zero-offset in the code
        lohi = patch.getfloat('band', item[0], multiple=True)
        bandname.append(item[0])
        bandlo.append(lohi[0])
        bandhi.append(lohi[1])

    monitor.debug(bandname, bandlo, bandhi)

    hdr_input = ft_input.getHeader()
    if (hdr_input.nSamples - 1) < endsample:
        raise RuntimeError("buffer reset detected")
    if hdr_input.nSamples < window:
        # there are not yet enough samples in the buffer
        monitor.info("Waiting for data...")
        return

    # get the most recent data segment
    begsample = hdr_input.nSamples - window
    endsample = hdr_input.nSamples - 1
    dat = ft_input.getData([begsample, endsample]).astype(np.double)
    dat = dat[:, chanindx]

    # demean the data to prevent spectral leakage
    dat = detrend(dat, axis=0, type='constant')

    # taper the data
    dat = dat * taper[:, np.newaxis]

    # compute the FFT over the sample direction
    F = np.fft.rfft(dat, axis=0)

    power = [0] * len(channame) * len(bandname)
    i = 0
    for chan in range(F.shape[1]):
        for lo, hi in zip(bandlo, bandhi):
            power[i] = 0
            count = 0
            for sample in range(len(frequency)):
                if frequency[sample] >= lo and frequency[sample] <= hi:
                    power[i] += abs(F[sample, chan] * F[sample, chan])
                    count += 1
            if count > 0:
                power[i] /= count
            i += 1

    monitor.debug(power)

    i = 0
    for chan in channame:
        for band in bandname:
            key = "%s.%s.%s" % (prefix, chan, band)
            patch.setvalue(key, power[i])
            i += 1
Example #39
0
"""

from warnings import warn

import numpy as np
from matplotlib.colors import is_color_like
from matplotlib.figure import Figure
from itertools import compress  # noqa
import matplotlib
import matplotlib.pyplot as plt
import sphinx_gallery.backreferences

from local_module import N  # N = 1000

t = np.arange(N) / float(N)
win = np.hanning(N)
print(is_color_like('r'))
fig, ax = plt.subplots()
ax.plot(t, win, color='r')
ax.text(0, 1, 'png', size=40, va='top')
fig.tight_layout()
orig_dpi = 80. if matplotlib.__version__[0] < '2' else 100.
assert plt.rcParams['figure.dpi'] == orig_dpi
plt.rcParams['figure.dpi'] = 70.
assert plt.rcParams['figure.dpi'] == 70.
listy = [0, 1]
compress('abc', [0, 0, 1])
warn('This warning should show up in the output', RuntimeWarning)
x = Figure()  # plt.Figure should be decorated (class), x shouldn't (inst)
# nested resolution resolves to numpy.random.mtrand.RandomState:
rng = np.random.RandomState(0)
Example #40
0
 def smooth1d(x, window_len):
     s = np.r_[2 * x[0] - x[window_len:1:-1], x,
               2 * x[-1] - x[-1:-window_len:-1]]
     w = np.hanning(window_len)
     y = np.convolve(w / w.sum(), s, mode='same')
     return y[window_len - 1:-window_len + 1]
Example #41
0
def mavfft_fttd(logfile, multi_log):
    '''display fft for raw ACC data in logfile'''
    '''object to store data about a single FFT plot'''
    class PlotData(object):
        def __init__(self, ffth):
            self.seqno = -1
            self.fftnum = ffth.N
            self.sensor_type = ffth.type
            self.instance = ffth.instance
            self.sample_rate_hz = ffth.smp_rate
            self.multiplier = ffth.mul
            self.data = {}
            self.data["X"] = []
            self.data["Y"] = []
            self.data["Z"] = []
            self.holes = False
            self.freq = None

        def add_fftd(self, fftd):
            if fftd.N != self.fftnum:
                print("Skipping ISBD with wrong fftnum (%u vs %u)\n" %
                      (fftd.N, self.fftnum))
                return
            if self.holes:
                print("Skipping ISBD(%u) for ISBH(%u) with holes in it" %
                      (fftd.seqno, self.fftnum))
                return
            if fftd.seqno != self.seqno + 1:
                print("ISBH(%u) has holes in it" % fftd.N)
                self.holes = True
                return
            self.seqno += 1
            self.data["X"].extend(fftd.x)
            self.data["Y"].extend(fftd.y)
            self.data["Z"].extend(fftd.z)

        def overlap_windows(self, plotdata):
            newplotdata = copy.deepcopy(self)
            if plotdata.tag() != self.tag():
                print("Invalid FFT data tag (%s vs %s) for window overlap" %
                      (plotdata.tag(), self.tag()))
                return self
            if plotdata.fftnum <= self.fftnum:
                print("Invalid FFT sequence (%u vs %u) for window overlap" %
                      (plotdata.fftnum, self.fftnum))
                return self
            newplotdata.data["X"] = numpy.split(numpy.asarray(
                self.data["X"]), 2)[1].tolist() + numpy.split(
                    numpy.asarray(plotdata.data["X"]), 2)[0].tolist()
            newplotdata.data["Y"] = numpy.split(numpy.asarray(
                self.data["Y"]), 2)[1].tolist() + numpy.split(
                    numpy.asarray(plotdata.data["Y"]), 2)[0].tolist()
            newplotdata.data["Z"] = numpy.split(numpy.asarray(
                self.data["Z"]), 2)[1].tolist() + numpy.split(
                    numpy.asarray(plotdata.data["Z"]), 2)[0].tolist()
            return newplotdata

        def prefix(self):
            if self.sensor_type == 0:
                return "Accel"
            elif self.sensor_type == 1:
                return "Gyro"
            else:
                return "?Unknown Sensor Type?"

        def tag(self):
            return str(self)

        def __str__(self):
            return "%s[%u]" % (self.prefix(), self.instance)

    print("Processing log %s" % logfile)
    mlog = mavutil.mavlink_connection(logfile)

    # see https://holometer.fnal.gov/GH_FFT.pdf for a description of the techniques used here
    things_to_plot = []
    plotdata = None
    prev_plotdata = {}
    msgcount = 0
    hntch_mode = None
    hntch_option = None
    batch_mode = None
    start_time = time.time()
    thr_total = 0.
    thr_count = 0

    while True:
        m = mlog.recv_match(condition=args.condition)
        if m is None:
            break
        msgcount += 1
        if msgcount % 1000 == 0:
            sys.stderr.write(".")
        msg_type = m.get_type()
        if msg_type == "ISBH":
            if plotdata is not None:
                sensor = plotdata.tag()
                # close off previous data collection
                # in order to get 50% window overlap we need half the old data + half the new data and then the new data itself
                if args.fft_overlap and sensor in prev_plotdata:
                    things_to_plot.append(
                        prev_plotdata[sensor].overlap_windows(plotdata))
                things_to_plot.append(plotdata)
                prev_plotdata[sensor] = plotdata
            plotdata = PlotData(m)
            continue

        if msg_type == "ISBD":
            if plotdata is None:
                sys.stderr.write("?(fftnum=%u)" % m.N)
                continue
            plotdata.add_fftd(m)

        if msg_type == "PARM":
            if m.Name == "INS_HNTCH_MODE":
                hntch_mode = m.Value
            elif m.Name == "INS_HNTCH_OPTS":
                hntch_option = m.Value
            elif m.Name == "INS_LOG_BAT_OPT":
                batch_mode = m.Value

        # get an average read of the throttle value assuming a stable hover above 1m
        if args.notch_params and msg_type == "CTUN":
            if m.Alt > 1:
                thr_total += m.ThO
                thr_count = thr_count + 1

    print("", file=sys.stderr)
    time_delta = time.time() - start_time
    print("%us messages  %u messages/second  %u kB/second" %
          (msgcount, msgcount / time_delta,
           os.stat(filename).st_size / time_delta))
    print("Extracted %u fft data sets" % len(things_to_plot), file=sys.stderr)
    if args.notch_params:
        thr_ref = thr_total / thr_count
        print("Throttle average %f" % thr_ref)

    sum_fft = {}
    freqmap = {}
    sample_rates = {}
    counts = {}
    window = {}
    S2 = {}
    hntch_mode_names = {0: "No", 1: "Throttle", 2: "RPM", 3: "ESC", 4: "FFT"}
    hntch_option_names = {
        0: "Single",
        1: "Double",
        2: "Dynamic",
        4: "Loop-Rate"
    }
    batch_mode_names = {0: "Pre-filter", 1: "Sensor-rate", 2: "Post-filter"}
    fft_peak = int(args.fft_peak)

    first_freq = None
    for thing_to_plot in things_to_plot:

        fft_len = len(thing_to_plot.data["X"])

        if thing_to_plot.tag() not in sum_fft:
            sum_fft[thing_to_plot.tag()] = {
                "X": numpy.zeros(fft_len // 2 + 1),
                "Y": numpy.zeros(fft_len // 2 + 1),
                "Z": numpy.zeros(fft_len // 2 + 1),
            }
            counts[thing_to_plot.tag()] = 0

        if thing_to_plot.tag() not in window:
            if args.fft_window == 'hanning':
                # create hanning window
                window[thing_to_plot.tag()] = numpy.hanning(fft_len)
            elif args.fft_window == 'blackman':
                # create blackman window
                window[thing_to_plot.tag()] = numpy.blackman(fft_len)
            else:
                window[thing_to_plot.tag()] = numpy.arange(fft_len)
                window[thing_to_plot.tag()].fill(1)
            # calculate NEBW constant
            S2[thing_to_plot.tag()] = numpy.inner(window[thing_to_plot.tag()],
                                                  window[thing_to_plot.tag()])

        for axis in ["X", "Y", "Z"]:
            # only plot the selected axis
            if axis not in args.axis:
                continue
            # normalize data and convert to dps in order to produce more meaningful magnitudes
            if thing_to_plot.sensor_type == 1:
                d = numpy.array(numpy.degrees(
                    thing_to_plot.data[axis])) / float(
                        thing_to_plot.multiplier)
            else:
                d = numpy.array(thing_to_plot.data[axis]) / float(
                    thing_to_plot.multiplier)
            if len(d) == 0:
                print("No data?!?!?!")
                continue
            if len(window[thing_to_plot.tag()]) != fft_len:
                print("Skipping corrupted frame of length %d" % fft_len)
                continue

            # apply window to the input
            d *= window[thing_to_plot.tag()]
            # perform RFFT
            d_fft = numpy.fft.rfft(d)
            # convert to squared complex magnitude
            d_fft = numpy.square(abs(d_fft))
            # remove DC component
            d_fft[0] = 0
            d_fft[-1] = 0
            # accumulate the sums
            sum_fft[thing_to_plot.tag()][axis] += d_fft
            freq = numpy.fft.rfftfreq(len(d),
                                      1.0 / thing_to_plot.sample_rate_hz)
            freqmap[thing_to_plot.tag()] = freq

        sample_rates[thing_to_plot.tag()] = thing_to_plot.sample_rate_hz
        counts[thing_to_plot.tag()] += 1

    numpy.seterr(divide='ignore')
    for sensor in sum_fft:
        print("Sensor: %s" % str(sensor))
        fig = pylab.figure(str(sensor))
        for axis in ["X", "Y", "Z"]:
            # only plot the selected axis
            if axis not in args.axis:
                continue

            # normalize output to averaged PSD
            psd = 2 * (sum_fft[sensor][axis] /
                       counts[sensor]) / (sample_rates[sensor] * S2[sensor])

            # calculate peaks from linear accel data
            # the accel data is less noisy than the gyro data
            if sensor == 'Accel[0]' and axis == "X" and args.notch_params:
                linear_psd = numpy.sqrt(psd)
                peaks, _ = signal.find_peaks(psd, prominence=0.1)
                peak_freqs = freqmap[sensor][peaks]
                print("Peaks: %s" % str(peak_freqs))
                print("INS_HNTCH_REF = %.4f" % thr_ref)
                print("INS_HNTCH_FREQ = %.1f" % float(peak_freqs[fft_peak]))
                print("INS_HNTCH_BW = %.1f" %
                      (float(peak_freqs[fft_peak]) / 2.))

            # linerize of requested
            if args.fft_output == 'lsd':
                psd = numpy.sqrt(psd)
            # convert to db if requested
            if args.fft_scale == 'db':
                psd = 10 * numpy.log10(psd)
            # add file name to axis if doing multi-file plot
            if multi_log:
                # remove extension and path
                (log_name, _, _) = logfile.rpartition('.')
                (_, _, log_name) = log_name.rpartition('/')
                legend_label = '%s (%s)' % (axis, log_name)
            else:
                legend_label = axis
            pylab.plot(freqmap[sensor], psd, label=legend_label)
        pylab.legend(loc='upper right')
        pylab.xlabel('Hz')
        scale_label = ''
        psd_label = 'PSD'
        if args.fft_scale == 'db':
            scale_label = "dB "
        if args.fft_output == 'lsd':
            if str(sensor).startswith("Gyro"):
                pylab.ylabel('LSD $' + scale_label + 'd/s/\sqrt{Hz}$')
            else:
                pylab.ylabel('LSD $' + scale_label + 'm/s^2/\sqrt{Hz}$')
        else:
            if str(sensor).startswith("Gyro"):
                pylab.ylabel('PSD $' + scale_label + 'd^2/s^2/Hz$')
            else:
                pylab.ylabel('PSD $' + scale_label + 'm^2/s^4/Hz$')

        if hntch_mode is not None and hntch_option is not None and batch_mode is not None:
            option_label = ""
            for hopt in hntch_option_names:
                if hopt & int(hntch_option) != 0:
                    if len(option_label) > 0:
                        option_label += "+"
                    option_label += hntch_option_names[hopt]
            textstr = '\n'.join(
                (r'%s tracking' % (hntch_mode_names[hntch_mode], ),
                 r'%s notch' % (option_label, ),
                 r'%s sampling' % (batch_mode_names[batch_mode], )))

            props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)

            pylab.text(0.5,
                       0.95,
                       textstr,
                       fontsize=12,
                       verticalalignment='top',
                       bbox=props,
                       transform=pylab.gca().transAxes)
Example #42
0
        N_MICS):  #inicializando un buffer de 6*WINDOW_SIZE para cada microfono
    buffers.append(np.zeros((WINDOW_SIZE * 6), dtype=np.int16))

for window_index in range(251):
    # for window_index in range(len(mic_windows[0])):
    if window_index % 50 == 0:
        print("{0} de {1} ventanas".format(window_index, len(mic_windows[0])))

    # Se guarda la nueva ventana desplazando a una vieja
    for i in range(N_MICS):
        buffers[i] = np.roll(buffers[i], -WINDOW_SIZE)
        buffers[i][-WINDOW_SIZE:] = mic_windows[i][window_index]

    buffer1_fft = []
    for i in range(N_MICS):
        b = buffers[i][:BUFFER_SIZE] * np.hanning(BUFFER_SIZE)
        buffer1_fft.append(np.fft.fft(b))

    buffer2_fft = []
    for i in range(N_MICS):
        b = buffers[i][-BUFFER_SIZE:] * np.hanning(BUFFER_SIZE)
        buffer2_fft.append(np.fft.fft(b))

    X1 = np.vstack(buffer1_fft)
    X2 = np.vstack(buffer2_fft)
    S1 = np.zeros((BUFFER_SIZE), dtype=complex)
    S2 = np.zeros((BUFFER_SIZE), dtype=complex)

    for m in range(N_MICS):  # Para luego calcular la R
        saved_windows1[m] = np.delete(saved_windows1[m], 0, 0)
        saved_windows1[m] = np.vstack([saved_windows1[m], X1[m]])
Example #43
0
    def M_frameAnalysis(self,
                        L_sec=0.08,
                        STEP_sec=0.02,
                        window_shape="hamming",
                        remove_dc=True,
                        mark_sec_v=np.zeros(0)):
        """
        **Description:** Split the data vector or matrix into frames and concatenate the frames into a matrix

        **Parameters:** L_sec, STEP_sec, window_shape, remove_dc

        **Status:** OK
        """

        #  --- self.data_v (nbChannel=1, LT_n)
        L_n = int(round(L_sec * self.x_sr_hz))
        if not (L_n % 2):
            L_n += 1  # in order to have an odd-length window

        LD_n = int((L_n - 1) / 2)
        L_sec = float(L_n) / self.x_sr_hz
        LD_sec = float(LD_n) / self.x_sr_hz

        if window_shape == "boxcar":
            fenetre_v = np.ones((L_n))
        elif window_shape == "hanning":
            fenetre_v = np.hanning(L_n)
        elif window_shape == "hamming":
            fenetre_v = np.hamming(L_n)
        elif window_shape == "blackman":
            fenetre_v = np.blackman(L_n)
        else:
            print "no other window_shape implemented so far"

        LT_n = self.data_v.shape[1]

        # --- convert analysis window to a matrix
        nbChannel = self.data_v.shape[0]
        # --- fenetre_v (nbChannel, L_n)
        fenetre_m = np.tile(fenetre_v, (nbChannel, 1))
        coefNorm = 0.5 / np.sum(fenetre_v)

        if len(mark_sec_v):
            mark_n_v = (mark_sec_v - self.x_start) * self.x_sr_hz + 1
            STEP_sec = np.mean(np.diff(mark_sec_v))
        else:
            STEP_n = int(round(STEP_sec * self.x_sr_hz))
            STEP_sec = float(STEP_n) / self.x_sr_hz
            nbFrame = float(LT_n - L_n) / float(STEP_n)
            mark_n_v = (np.arange(0, nbFrame) * STEP_n) + LD_n
            mark_sec_v = self.x_start + (mark_n_v - 1) / self.x_sr_hz

        nbFrame = len(mark_n_v)

        signal_3m = np.zeros((nbChannel, L_n, nbFrame))
        for numFrame in range(0, len(mark_n_v)):
            # --- signal_m (nbChannel, L_n)
            signal_m = self.data_v[:,
                                   int(mark_n_v[numFrame]) -
                                   LD_n:int(mark_n_v[numFrame]) + LD_n + 1]
            # --- somme (nbChannel, 1)
            if remove_dc:
                somme_v = np.mean(signal_m, axis=1, keepdims=True)
            else:
                somme_v = 0

            signal_3m[:, :, numFrame] = coefNorm * np.multiply(
                (signal_m - np.tile(somme_v, (1, L_n))), fenetre_m)

        # --- def __init__(self, name, data_v, x_label, x_sr_hz, x_start, y_label, y_sr_hz, y_start):
        output = C_Descriptor('M_frameAnalysis(' + self.name + ')', signal_3m,
                              'Frame [sec]', 1. / STEP_sec, mark_sec_v[0],
                              self.x_label, self.x_sr_hz, 0)
        return output
Example #44
0
    def __init__(self,
                 feature_type='HDT',
                 padding=2.2,
                 feature_bandwidth_sigma=0.2,
                 spatial_bandwidth_sigma_factor=1 / float(16),
                 adaptation_rate_range_max=0.0025,
                 adaptation_rate_scale_range_max=0.005,
                 lambda_value=1e-4,
                 kernel='gaussian',
                 sub_feature_type="",
                 sub_sub_feature_type=""):
        """
        :param feature_type:
        :param padding:
        :param feature_bandwidth_sigma:
        :param spatial_bandwidth_sigma_factor:
        :param adaptation_rate_range_max:
        :param adaptation_rate_scale_range_max:
        :param lambda_value:
        :param kernel:
        """
        self.feature_type = feature_type
        self.padding = padding
        self.feature_bandwidth_sigma = feature_bandwidth_sigma
        self.spatial_bandwidth_sigma_factor = spatial_bandwidth_sigma_factor
        self.adaptation_rate_range_max = adaptation_rate_range_max
        self.adaptation_rate_scale_range_max = adaptation_rate_scale_range_max
        self.lambda_value = lambda_value
        self.kernel = kernel
        self.sub_sub_feature_type = sub_sub_feature_type
        self.name = 'KCF_' + feature_type

        if feature_type == 'HDT':
            from keras.applications.vgg19 import VGG19
            import theano

            self.base_model = VGG19(include_top=False, weights='imagenet')
            self.extract_model_function = theano.function(
                [self.base_model.input], [
                    self.base_model.get_layer('block1_conv2').output,
                    self.base_model.get_layer('block2_conv2').output,
                    self.base_model.get_layer('block3_conv4').output,
                    self.base_model.get_layer('block4_conv4').output,
                    self.base_model.get_layer('block5_conv4').output
                ],
                allow_input_downcast=True)

            # we first resize all the response maps to a size of 40*60 (store the resize scale)
            # because average target size is 81 *52
            self.resize_size = (241, 161)
            # store pre-computed cosine window, here is a multiscale CNN, here we have 5 layers cnn:
            self.cos_window = []
            self.y = []
            self.yf = []
            self.response_all = []
            self.max_list = []
            for i in range(5):
                cos_wind_sz = np.divide(self.resize_size, 2**i)
                self.cos_window.append(
                    np.outer(np.hanning(cos_wind_sz[0]),
                             np.hanning(cos_wind_sz[1])))
                grid_y = np.arange(cos_wind_sz[0]) - np.floor(
                    cos_wind_sz[0] / 2)
                grid_x = np.arange(cos_wind_sz[1]) - np.floor(
                    cos_wind_sz[1] / 2)
                # desired output (gaussian shaped), bandwidth proportional to target size
                output_sigma = np.sqrt(
                    np.prod(cos_wind_sz)) * self.spatial_bandwidth_sigma_factor
                rs, cs = np.meshgrid(grid_x, grid_y)
                y = np.exp(-0.5 / output_sigma**2 * (rs**2 + cs**2))
                self.y.append(y)
                self.yf.append(np.fft.fft2(y, axes=(0, 1)))

            # some hyperparameter for HDT
            self.loss_acc_time = 5
            self.stability = np.zeros(shape=(5, 1))
            self.A = 0.011
Example #45
0
def stft(x, window_size, stride, fft_size, window_type='hanning', center=True, pad_mode='reflect'):
    """Computes the short-time Fourier transform

    Args:
        x (~nnabla.Variable): Time domain sequence of size `batch_size x sample_size`.
        window_size (int): Size of STFT analysis window.
        stride (int): Number of samples that we shift the window, also called `hop size`.
        fft_size (int): Size of the FFT, the output will have `fft_size // 2+ 1` frequency bins.
        window_type (str): Analysis window, can be either `hanning`, `hamming` or `rectangular`.
            For convenience, also `window_type=None` is supported which is equivalent to `window_type='rectangular'`.
        center (bool): If `True`, then the signal `x` is padded by half the FFT size using reflection padding.
        pad_mode (str): Padding mode, which can be `'constant'` or `'reflect'`. `'constant'` pads with `0`.

    Returns:
        Returns real and imaginary parts of STFT result.

        * :obj:`~nnabla.Variable`: Real part of STFT of size `batch_size x fft_size//2 + 1 x frame_size`.
        * :obj:`~nnabla.Variable`: Imaginary part of STFT of size `batch x fft_size//2 + 1 x frame_size`.
    """
    from nnabla.parameter import get_parameter, get_parameter_or_create
    conv_r = get_parameter('conv_r')
    conv_i = get_parameter('conv_i')

    if conv_r is None or conv_i is None:
        if window_type == 'hanning':
            window_func = np.hanning(window_size + 1)[:-1]
        elif window_type == 'hamming':
            window_func = np.hamming(window_size + 1)[:-1]
        elif window_type == 'rectangular' or window_type is None:
            window_func = np.ones(window_size)
        else:
            raise ValueError("Unknown window type {}.".format(window_type))

        # pad window if `fft_size > window_size`
        if fft_size > window_size:
            diff = fft_size - window_size
            window_func = np.pad(
                window_func, (diff//2, diff - diff//2), mode='constant')
        elif fft_size < window_size:
            raise ValueError(
                "FFT size has to be as least as large as window size.")

        # compute STFT filter coefficients
        mat_r = np.zeros((fft_size//2 + 1, 1, fft_size))
        mat_i = np.zeros((fft_size//2 + 1, 1, fft_size))

        for w in range(fft_size//2+1):
            for t in range(fft_size):
                mat_r[w, 0, t] = np.cos(2. * np.pi * w * t / fft_size)
                mat_i[w, 0, t] = -np.sin(2. * np.pi * w * t / fft_size)
        mat_r = mat_r * window_func
        mat_i = mat_i * window_func

        conv_r = get_parameter_or_create(
            'conv_r', initializer=mat_r, need_grad=False)
        conv_i = get_parameter_or_create(
            'conv_i', initializer=mat_i, need_grad=False)

    if center:
        # pad at begin/end (per default this is a reflection padding)
        x = pad(x, (fft_size // 2, fft_size // 2), mode=pad_mode)

    # add channel dimension
    x = reshape(x, (x.shape[0], 1, x.shape[1]), inplace=False)

    # compute STFT
    y_r = convolution(x, conv_r, stride=(stride,))
    y_i = convolution(x, conv_i, stride=(stride,))

    return y_r, y_i
Example #46
0
from checkdirectories import *
dir_training = '_'
if simulatedcheck:
    dir_training = dir_training + 'evalita_'
if hsc:
    dir_training = dir_training + 'hscma_'
if fittizio:
    dir_training = dir_training + 'dls_'
dirAudio2 = '/Signals/Mixed_Sources/' + nameplace
if nameplace == 'Kitchen':
    pathdir_fittizio = pathdir_fittizio + 'DIRHALibriSpeechsource_083seconds_reverberation/'
    mic_log_mel = 13  #numero di microfoni nella cucina
else:
    mic_log_mel = 15
    pathdir_fittizio = pathdir_fittizio + 'DIRHALibriSpeechsource_074seconds_reverberation/'
window = np.hanning(N)
pathdir = pathdir_evalita + 'AUDIO_FILES/'
micArray, num, fi, lsb, usb = getPhi(nameplace, pathdir, dirArray, dirWall,
                                     formattext)
del lsb, getPhi
fi = np.asarray(fi)
ax = usb[0] * 1000.0
ay = usb[1] * 1000.0
az = usb[2] * 1000.0
context = 15
numContext = int((context - 1) / 2)
CNNkernel = [256]
DenseNeuron = [1024, 1024, 1024, 1024, 1024]
sizekernelCNN = 4
stridesCNN = 3
startSimulationSingleChannel(context, CNNkernel, sizekernelCNN, stridesCNN,
Example #47
0
def compute_epochs_csd(epochs,
                       mode='multitaper',
                       fmin=0,
                       fmax=np.inf,
                       fsum=True,
                       tmin=None,
                       tmax=None,
                       n_fft=None,
                       mt_bandwidth=None,
                       mt_adaptive=False,
                       mt_low_bias=True,
                       projs=None,
                       verbose=None):
    """Estimate cross-spectral density from epochs

    Note: Baseline correction should be used when creating the Epochs.
          Otherwise the computed cross-spectral density will be inaccurate.

    Note: Results are scaled by sampling frequency for compatibility with
          Matlab.

    Parameters
    ----------
    epochs : instance of Epochs
        The epochs.
    mode : str
        Spectrum estimation mode can be either: 'multitaper' or 'fourier'.
    fmin : float
        Minimum frequency of interest.
    fmax : float | np.inf
        Maximum frequency of interest.
    fsum : bool
        Sum CSD values for the frequencies of interest. Summing is performed
        instead of averaging so that accumulated power is comparable to power
        in the time domain. If True, a single CSD matrix will be returned. If
        False, the output will be a list of CSD matrices.
    tmin : float | None
        Minimum time instant to consider. If None start at first sample.
    tmax : float | None
        Maximum time instant to consider. If None end at last sample.
    n_fft : int | None
        Length of the FFT. If None the exact number of samples between tmin and
        tmax will be used.
    mt_bandwidth : float | None
        The bandwidth of the multitaper windowing function in Hz.
        Only used in 'multitaper' mode.
    mt_adaptive : bool
        Use adaptive weights to combine the tapered spectra into PSD.
        Only used in 'multitaper' mode.
    mt_low_bias : bool
        Only use tapers with more than 90% spectral concentration within
        bandwidth. Only used in 'multitaper' mode.
    projs : list of Projection | None
        List of projectors to use in CSD calculation, or None to indicate that
        the projectors from the epochs should be inherited.
    verbose : bool, str, int, or None
        If not None, override default verbose level (see mne.verbose).

    Returns
    -------
    csd : instance of CrossSpectralDensity
        The computed cross-spectral density.
    """
    # Portions of this code adapted from mne/connectivity/spectral.py

    # Check correctness of input data and parameters
    if fmax < fmin:
        raise ValueError('fmax must be larger than fmin')
    tstep = epochs.times[1] - epochs.times[0]
    if tmin is not None and tmin < epochs.times[0] - tstep:
        raise ValueError('tmin should be larger than the smallest data time '
                         'point')
    if tmax is not None and tmax > epochs.times[-1] + tstep:
        raise ValueError('tmax should be smaller than the largest data time '
                         'point')
    if tmax is not None and tmin is not None:
        if tmax < tmin:
            raise ValueError('tmax must be larger than tmin')
    if epochs.baseline is None:
        warnings.warn('Epochs are not baseline corrected, cross-spectral '
                      'density may be inaccurate')

    if projs is None:
        projs = cp.deepcopy(epochs.info['projs'])
    else:
        projs = cp.deepcopy(projs)

    picks_meeg = pick_types(epochs[0].info,
                            meg=True,
                            eeg=True,
                            eog=False,
                            ref_meg=False,
                            exclude='bads')
    ch_names = [epochs.ch_names[k] for k in picks_meeg]

    # Preparing time window slice
    tstart, tend = None, None
    if tmin is not None:
        tstart = np.where(epochs.times >= tmin)[0][0]
    if tmax is not None:
        tend = np.where(epochs.times <= tmax)[0][-1] + 1
    tslice = slice(tstart, tend, None)
    n_times = len(epochs.times[tslice])
    n_fft = n_times if n_fft is None else n_fft

    # Preparing frequencies of interest
    sfreq = epochs.info['sfreq']
    frequencies = fftfreq(n_fft, 1. / sfreq)
    freq_mask = (frequencies > fmin) & (frequencies < fmax)
    frequencies = frequencies[freq_mask]
    n_freqs = len(frequencies)

    if n_freqs == 0:
        raise ValueError('No discrete fourier transform results within '
                         'the given frequency window. Please widen either '
                         'the frequency window or the time window')

    # Preparing for computing CSD
    logger.info('Computing cross-spectral density from epochs...')
    if mode == 'multitaper':
        # Compute standardized half-bandwidth
        if mt_bandwidth is not None:
            half_nbw = float(mt_bandwidth) * n_times / (2 * sfreq)
        else:
            half_nbw = 2

        # Compute DPSS windows
        n_tapers_max = int(2 * half_nbw)
        window_fun, eigvals = dpss_windows(n_times,
                                           half_nbw,
                                           n_tapers_max,
                                           low_bias=mt_low_bias)
        n_tapers = len(eigvals)
        logger.info('    using multitaper spectrum estimation with %d DPSS '
                    'windows' % n_tapers)

        if mt_adaptive and len(eigvals) < 3:
            warnings.warn('Not adaptively combining the spectral estimators '
                          'due to a low number of tapers.')
            mt_adaptive = False
    elif mode == 'fourier':
        logger.info('    using FFT with a Hanning window to estimate spectra')
        window_fun = np.hanning(n_times)
        mt_adaptive = False
        eigvals = 1.
        n_tapers = None
    else:
        raise ValueError('Mode has an invalid value.')

    csds_mean = np.zeros((len(ch_names), len(ch_names), n_freqs),
                         dtype=complex)

    # Compute CSD for each epoch
    n_epochs = 0
    for epoch in epochs:
        epoch = epoch[picks_meeg][:, tslice]

        # Calculating Fourier transform using multitaper module
        x_mt, _ = _mt_spectra(epoch, window_fun, sfreq, n_fft)

        if mt_adaptive:
            # Compute adaptive weights
            _, weights = _psd_from_mt_adaptive(x_mt,
                                               eigvals,
                                               freq_mask,
                                               return_weights=True)
            # Tiling weights so that we can easily use _csd_from_mt()
            weights = weights[:, np.newaxis, :, :]
            weights = np.tile(weights, [1, x_mt.shape[0], 1, 1])
        else:
            # Do not use adaptive weights
            if mode == 'multitaper':
                weights = np.sqrt(eigvals)[np.newaxis, np.newaxis, :,
                                           np.newaxis]
            else:
                # Hack so we can sum over axis=-2
                weights = np.array([1.])[:, None, None, None]

        # Picking frequencies of interest
        x_mt = x_mt[:, :, freq_mask]

        # Calculating CSD
        # Tiling x_mt so that we can easily use _csd_from_mt()
        x_mt = x_mt[:, np.newaxis, :, :]
        x_mt = np.tile(x_mt, [1, x_mt.shape[0], 1, 1])
        y_mt = np.transpose(x_mt, axes=[1, 0, 2, 3])
        weights_y = np.transpose(weights, axes=[1, 0, 2, 3])
        csds_epoch = _csd_from_mt(x_mt, y_mt, weights, weights_y)

        # Scaling by number of samples and compensating for loss of power due
        # to windowing (see section 11.5.2 in Bendat & Piersol).
        if mode == 'fourier':
            csds_epoch /= n_times
            csds_epoch *= 8 / 3.

        # Scaling by sampling frequency for compatibility with Matlab
        csds_epoch /= sfreq

        csds_mean += csds_epoch
        n_epochs += 1

    csds_mean /= n_epochs

    logger.info('[done]')

    # Summing over frequencies of interest or returning a list of separate CSD
    # matrices for each frequency
    if fsum is True:
        csd_mean_fsum = np.sum(csds_mean, 2)
        csd = CrossSpectralDensity(csd_mean_fsum,
                                   ch_names,
                                   projs,
                                   epochs.info['bads'],
                                   frequencies=frequencies,
                                   n_fft=n_fft)
        return csd
    else:
        csds = []
        for i in range(n_freqs):
            csds.append(
                CrossSpectralDensity(csds_mean[:, :, i],
                                     ch_names,
                                     projs,
                                     epochs.info['bads'],
                                     frequencies=frequencies[i],
                                     n_fft=n_fft))
        return csds
Example #48
0
    ax[1].set_ylabel('$-\delta I_{tes}$ ($\\mu$A)')
    y1 = tes.Rshunt * (pulses.iv.ItesFinal - pulses.iv.IbiasFinal) * cumtrapz(
        templatePulse, dx=pulses.dt)
    y2 = tes.Rshunt * cumtrapz(templatePulse**2, dx=pulses.dt)
    ax[2].plot(t[1:], y1 / eV, '--r')
    ax[2].plot(t[1:], y2 / eV, '--b')
    ax[2].plot(t[1:], (y1 + y2) / eV, '--k')
    ax[2].set_ylabel('Pulse integral (eV)')
    ax[-1].set_xlabel('Time')
    #mpl.show()

    #from scipy.fftpack import rfft, irfft
    #from numpy import rfft, irfft

    nPoints = len(templatePulse)
    window = np.hanning(nPoints)
    windowedTemplate = window * templatePulse
    windowNorm = np.sum(window)

    noise = RunningStats()
    for i in pulses.iBaseline:
        pulse = pulses[i]
        y = pulse.Vsquid - pulse.preTriggerBaseline()
        noise.push(abs(np.fft.rfft(window * y))**2)
    noisePsd = noise.mean()  #/nPoints**2/windowNorm
    mpl.figure()
    f = (np.arange(0, len(noisePsd)) / (len(noisePsd) * pulses.dt))
    mpl.loglog(f, np.sqrt(noisePsd))
    mpl.ylabel('Noise PSD (V/rtHz)')
    mpl.xlabel('f (Hz)')
Example #49
0
def NAT_stim(exptevents,
             exptparams,
             stimfmt='gtgram',
             separate_files_only=False,
             channels=18,
             rasterfs=100,
             f_min=200,
             f_max=20000,
             mono=False,
             binaural=False,
             **options):
    """
    :param exptevents: from baphy
    :param exptparams: from baphy
    :param stimfmt: string, currently must be 'wav' or 'gtgram'
    :param separate_files_only: boolean [=False]
        if True, just return each individual sound file rather than combinations
        (eg, as specified for binaural stim)
    :param channels: int
        number of gtgram channels
    :param rasterfs: int
        gtgram sampling rate
    :param f_min: float
        gtgram min frequency
    :param f_max: float
        gtgram max frequency
    :param mono: boolean [False]
        if True, collapse wavs to single channel (dumb control for binaural)
    :param binaural:
        if True, apply model to simulate sound at each ear. Currently, a very dumb HRTF
    :param options: dict
        extra stuff to pass through
    :return:
        stim, tags, stimparam
    """
    ReferenceClass = exptparams['TrialObject'][1]['ReferenceClass']
    ReferenceHandle = exptparams['TrialObject'][1]['ReferenceHandle'][1]
    OveralldB = exptparams['TrialObject'][1]['OveralldB']

    if exptparams['TrialObject'][1]['ReferenceClass'] == 'BigNat':
        sound_root = Path(exptparams['TrialObject'][1]['ReferenceHandle'][1]
                          ['SoundPath'].replace("H:/", "/auto/data/"))

        #stim_epochs = exptevents.loc[exptevents.name.str.startswith("Stim"),'name'].tolist()
        #print(exptevents.loc[exptevents.name.str.startswith("Stim"),'name'].tolist()[:10])
        #wav1=[e.split(' , ')[1].split("+")[0].split(":")[0].replace("STIM_","") for e in stim_epochs]
        #wav2=[e.split(' , ')[1].split("+")[0].split(":")[0].replace("STIM_","") for e in stim_epochs]

        stim_epochs = exptparams['TrialObject'][1]['ReferenceHandle'][1][
            'Names']
        #print(exptparams['TrialObject'][1]['ReferenceHandle'][1]['Names'][:10])
        wav1 = [e.split("+")[0].split(":")[0] for e in stim_epochs]
        wav2 = [e.split("+")[1].split(":")[0] for e in stim_epochs]
        chan1 = [int(e.split("+")[0].split(":")[1]) - 1 for e in stim_epochs]
        chan2 = [int(e.split("+")[1].split(":")[1]) - 1 for e in stim_epochs]
        #log.info(wav1[0],chan1[0],wav2[0],chan2[0])
        file_unique = wav1.copy()
        file_unique.extend(wav2)
        file_unique = list(set(file_unique))
        if 'NULL' in file_unique:
            file_unique.remove('NULL')

    elif ReferenceClass == 'NaturalSounds':
        subset = ReferenceHandle['Subsets']
        if subset == 1:
            sound_root = Path(
                f'/auto/users/svd/code/baphy/Config/lbhb/SoundObjects/@NaturalSounds/sounds'
            )
        else:
            sound_root = Path(
                f'/auto/users/svd/code/baphy/Config/lbhb/SoundObjects/@NaturalSounds/sounds_set{subset}'
            )
        stim_epochs = ReferenceHandle['Names']
        file_unique = [f.replace('.wav', '') for f in stim_epochs]

        wav1 = file_unique.copy()
        chan1 = [0] * len(wav1)
        wav2 = ["NULL"] * len(wav1)
        chan2 = [0] * len(wav1)

    elif ReferenceClass == 'OverlappingPairs':
        bg_folder = ReferenceHandle['BG_Folder']
        fg_folder = ReferenceHandle['FG_Folder']

        bg_root = Path(
            f'/auto/users/hamersky/baphy/Config/lbhb/SoundObjects/@OverlappingPairs/{bg_folder}'
        )
        fg_root = Path(
            f'/auto/users/hamersky/baphy/Config/lbhb/SoundObjects/@OverlappingPairs/{fg_folder}'
        )

        stim_epochs = ReferenceHandle['Names']
        #print(exptparams['TrialObject'][1]['ReferenceHandle'][1]['Names'][:10])
        wav1 = [e.split("_")[0].split("-")[0] for e in stim_epochs]
        wav2 = [e.split("_")[1].split("-")[0] for e in stim_epochs]
        chan1 = [
            int(e.split("_")[0].split("-")[3]) -
            1 if e.split("_")[0] != 'null' else 0 for e in stim_epochs
        ]
        chan2 = [
            int(e.split("_")[1].split("-")[3]) -
            1 if e.split("_")[1] != 'null' else 0 for e in stim_epochs
        ]
        #log.info(wav1[0],chan1[0],wav2[0],chan2[0])
        #wav1 = [wav for wav in wav1 if wav != 'null']
        #wav2 = [wav for wav in wav2 if wav != 'null']

        file_unique = wav1.copy()
        file_unique.extend(wav2)
        file_unique = list(set(file_unique))
        file_unique = [f for f in file_unique if f != 'null']

        log.info(
            'NOTE: Stripping spaces from epoch names in OverlappingPairs files'
        )
        stim_epochs = [s.replace(" ", "") for s in stim_epochs]
    else:
        raise ValueError(
            f"ReferenceClass {ReferenceClass} gtgram not supported.")

    max_chans = np.max(np.concatenate([np.array(chan1), np.array(chan2)])) + 1
    max_chans_was = max_chans
    if mono:
        log.info("Forcing mono stimulus, averaging across space")
        chan1 = [0] * len(wav1)
        chan2 = [0] * len(wav2)
        max_chans = 1

    PreStimSilence = ReferenceHandle['PreStimSilence']
    Duration = ReferenceHandle['Duration']
    PostStimSilence = ReferenceHandle['PostStimSilence']
    log.info(f"Pre/Dur/Pos: {PreStimSilence}/{Duration}/{PostStimSilence}")

    wav_unique = {}
    fs0 = None
    for filename in file_unique:
        if ReferenceClass == "OverlappingPairs":
            try:
                fs, w = wavfile.read(Path(bg_root) / (filename + '.wav'))
            except:
                fs, w = wavfile.read(Path(fg_root) / (filename + '.wav'))
        else:
            fs, w = wavfile.read(sound_root / (filename + '.wav'))
        if fs0 is None:
            fs0 = fs
        elif fs != fs0:
            raise ValueError(
                "fs mismatch between wav files. Need to implement resampling!")

        #print(f"{filename} fs={fs} len={w.shape}")
        duration_samples = int(np.floor(Duration * fs))

        # 10ms ramp at onset:
        w = w[:duration_samples].astype(float)
        ramp = np.hanning(.01 * fs * 2)
        ramp = ramp[:int(np.floor(len(ramp) / 2))]
        w[:len(ramp)] *= ramp
        w[-len(ramp):] = w[-len(ramp):] * np.flipud(ramp)

        # scale peak-to-peak amplitude to OveralldB
        w = w / np.max(np.abs(w)) * 5
        sf = 10**((80 - OveralldB) / 20)
        w /= sf

        wav_unique[filename] = w[:, np.newaxis]

    if separate_files_only:
        # combine into pairs that were actually presented
        wav_all = wav_unique
    else:
        wav_all = {}
        fs_all = {}
        for (f1, c1, f2, c2, n) in zip(wav1, chan1, wav2, chan2, stim_epochs):
            #print(f1,f2)
            if f1.upper() != "NULL":
                w1 = wav_unique[f1]
                if f2.upper() != "NULL":
                    w2 = wav_unique[f2]
                else:
                    w2 = np.zeros(w1.shape)
            else:
                w2 = wav_unique[f2]
                w1 = np.zeros(w2.shape)
            w = np.zeros((w1.shape[0], max_chans))
            if (binaural is None) | (binaural == False):
                #log.info(f'binaural model: None')
                w[:, [c1]] = w1
                w[:, [c2]] += w2
            else:
                #log.info(f'binaural model: {binaural}')
                #import pdb; pdb.set_trace()
                db_atten = 10
                factor = 10**(-db_atten / 20)
                w[:, [c1]] = w1 * 1 / (1 + factor) + w2 * factor / (1 + factor)
                w[:,
                  [c2]] += w2 * 1 / (1 + factor) + w1 * factor / (1 + factor)

            wav_all[n] = w

    if stimfmt == 'wav':
        for f, w in wav_all.items():
            wav_all[f] = np.concatenate([
                np.zeros((int(np.floor(fs * PreStimSilence)), max_chans)), w,
                np.zeros((int(np.floor(fs * PostStimSilence)), max_chans))
            ],
                                        axis=0)
        return wav_all

    if stimfmt == 'gtgram':
        window_time = 1 / rasterfs
        hop_time = 1 / rasterfs

        duration_bins = int(np.floor(rasterfs * Duration))
        sg_pre = np.zeros((channels, int(np.floor(rasterfs * PreStimSilence))))
        sg_null = np.zeros((channels, duration_bins))
        sg_post = np.zeros(
            (channels, int(np.floor(rasterfs * PostStimSilence))))

        sg_unique = {}
        stimparam = {'f_min': f_min, 'f_max': f_max, 'rasterfs': rasterfs}
        padbins = int(np.ceil((window_time - hop_time) / 2 * fs))

        for (f, w) in wav_all.items():
            if len(sg_unique) % 100 == 99:
                log.info(f"i={len(sg_unique)+1} {f} {w.std(axis=0)}")
            sg = [
                gtgram(np.pad(w[:, i], [padbins, padbins]), fs, window_time,
                       hop_time, channels, f_min, f_max)
                if w[:, i].var() > 0 else sg_null for i in range(w.shape[1])
            ]

            sg = [
                np.concatenate(
                    [sg_pre,
                     np.abs(s[:, :duration_bins])**0.5, sg_post],
                    axis=1) for s in sg
            ]

            if mono & (max_chans_was > 1):
                sgshuff = np.random.permutation(sg[0].flatten())
                sgshuff = np.reshape(sgshuff, sg[0].shape)
                sg.append(sgshuff)
            sg_unique[f] = np.concatenate(sg, axis=0)

        return sg_unique, list(sg_unique.keys()), stimparam
    # Iterate through the audio file's samples constructing blocks.
    for i in range(0, len(ra), cs - overlap_c):
        raw_chunk = ra[i:i + cs]
        # Check if we need to pad raw_chunk on the right, i.e. are we
        # at the end of the file.
        if len(raw_chunk) < cs:
            diff = cs - len(raw_chunk)
            raw_chunk = np.concatenate([raw_chunk, np.zeros(diff)], axis=0)

        padded_chunk = get_chunk_with_margin(ra, i, cs, peek)

        X.append(padded_chunk)
    clip_X.append(X)

hann_window = np.hanning(600)
if __name__ == '__main__':
    X_n = tf.placeholder(tf.float32, shape=[None, 1000])
    X = tf.placeholder(tf.float32, shape=[None, 600])

    Z_mu = encoder(X_n, z_dim)
    Z = Z_mu

    X_hat = decoder(Z)

    saver = tf.train.Saver()

    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    saver.restore(sess, model_name)
Example #51
0
def generate_cosine_mask(patch_shape):
    r"""
    """
    cy = np.hanning(patch_shape[0])
    cx = np.hanning(patch_shape[1])
    return cy[..., None].dot(cx[None, ...])
Example #52
0
    for i in indices:
        #print i
        y = X_train[i]
        frq, E = getSpectrum(y, Fs)
        smoothed = np.convolve(kernel, E, mode='SAME')
        logeeg = np.log10(smoothed)
        logeeg0 = np.interp(frq0, frq, logeeg)
        if doNorm:
            V[icnt, :] = (logeeg0 - logeeg0.mean()) / logeeg0.std()
        else:
            V[icnt, :] = logeeg0
        icnt += 1
    return V


kernel = np.hanning(80)
kernel = kernel / kernel.sum()

frqmax = 100
frq0 = np.arange(0, frqmax, 1)
doNorm = 1

L = len(X_train)
L_t = len(X_test)

indx = np.arange(0, L)
indx_t = np.arange(0, L_t)

V_train = computeVec(X_train, indx, frq0, doNorm, kernel)
V_test = computeVec(X_test, indx_t, frq0, doNorm, kernel)
Example #53
0
def spectral_connectivity(data,
                          method='coh',
                          indices=None,
                          sfreq=2 * np.pi,
                          mode='multitaper',
                          fmin=None,
                          fmax=np.inf,
                          fskip=0,
                          faverage=False,
                          tmin=None,
                          tmax=None,
                          mt_bandwidth=None,
                          mt_adaptive=False,
                          mt_low_bias=True,
                          cwt_frequencies=None,
                          cwt_n_cycles=7,
                          block_size=1000,
                          n_jobs=1,
                          verbose=None):
    """Compute various frequency-domain and time-frequency domain connectivity
    measures.

    The connectivity method(s) are specified using the "method" parameter.
    All methods are based on estimates of the cross- and power spectral
    densities (CSD/PSD) Sxy and Sxx, Syy.

    The spectral densities can be estimated using a multitaper method with
    digital prolate spheroidal sequence (DPSS) windows, a discrete Fourier
    transform with Hanning windows, or a continuous wavelet transform using
    Morlet wavelets. The spectral estimation mode is specified using the
    "mode" parameter.

    By default, the connectivity between all signals is computed (only
    connections corresponding to the lower-triangular part of the
    connectivity matrix). If one is only interested in the connectivity
    between some signals, the "indices" parameter can be used. For example,
    to compute the connectivity between the signal with index 0 and signals
    "2, 3, 4" (a total of 3 connections) one can use the following:

    indices = (np.array([0, 0, 0],    # row indices
               np.array([2, 3, 4])))  # col indices

    con_flat = spectral_connectivity(data, method='coh', indices=indices, ...)

    In this case con_flat.shape = (3, n_freqs). The connectivity scores are
    in the same order as defined indices.

    Supported Connectivity Measures:

    The connectivity method(s) is specified using the "method" parameter. The
    following methods are supported (note: E[] denotes average over epochs).
    Multiple measures can be computed at once by using a list/tuple, e.g.
    "['coh', 'pli']" to compute coherence and PLI.

    'coh' : Coherence given by

                 | E[Sxy] |
        C = ---------------------
            sqrt(E[Sxx] * E[Syy])

    'cohy' : Coherency given by

                   E[Sxy]
        C = ---------------------
            sqrt(E[Sxx] * E[Syy])

    'imcoh' : Imaginary coherence [1] given by

                  Im(E[Sxy])
        C = ----------------------
            sqrt(E[Sxx] * E[Syy])

    'plv' : Phase-Locking Value (PLV) [2] given by

        PLV = |E[Sxy/|Sxy|]|

    'ppc' : Pairwise Phase Consistency (PPC), an unbiased estimator of squared
            PLV [3].

    'pli' : Phase Lag Index (PLI) [4] given by

        PLI = |E[sign(Im(Sxy))]|

    'pli2_unbiased' : Unbiased estimator of squared PLI [5].

    'wpli' : Weighted Phase Lag Index (WPLI) [5] given by

                  |E[Im(Sxy)]|
        WPLI = ------------------
                  E[|Im(Sxy)|]

    'wpli2_debiased' : Debiased estimator of squared WPLI [5].

    References
    ----------

    [1] Nolte et al. "Identifying true brain interaction from EEG data using
        the imaginary part of coherency" Clinical neurophysiology, vol. 115,
        no. 10, pp. 2292-2307, Oct. 2004.

    [2] Lachaux et al. "Measuring phase synchrony in brain signals" Human brain
        mapping, vol. 8, no. 4, pp. 194-208, Jan. 1999.

    [3] Vinck et al. "The pairwise phase consistency: a bias-free measure of
        rhythmic neuronal synchronization" NeuroImage, vol. 51, no. 1,
        pp. 112-122, May 2010.

    [4] Stam et al. "Phase lag index: assessment of functional connectivity
        from multi channel EEG and MEG with diminished bias from common
        sources" Human brain mapping, vol. 28, no. 11, pp. 1178-1193,
        Nov. 2007.

    [5] Vinck et al. "An improved index of phase-synchronization for electro-
        physiological data in the presence of volume-conduction, noise and
        sample-size bias" NeuroImage, vol. 55, no. 4, pp. 1548-1565, Apr. 2011.

    Parameters
    ----------
    data : array, shape=(n_epochs, n_signals, n_times)
           or list/generator of array, shape =(n_signals, n_times)
           or list/generator of SourceEstimate
           or Epochs
        The data from which to compute connectivity. Note that it is also
        possible to combine multiple signals by providing a list of tuples,
        e.g., data = [(arr_0, stc_0), (arr_1, stc_1), (arr_2, stc_2)],
        corresponds to 3 epochs, and arr_* could be an array with the same
        number of time points as stc_*.
    method : string | list of string
        Connectivity measure(s) to compute.
    indices : tuple of arrays | None
        Two arrays with indices of connections for which to compute
        connectivity. If None, all connections are computed.
    sfreq : float
        The sampling frequency.
    mode : str
        Spectrum estimation mode can be either: 'multitaper', 'fourier', or
        'cwt_morlet'.
    fmin : float | tuple of floats
        The lower frequency of interest. Multiple bands are defined using
        a tuple, e.g., (8., 20.) for two bands with 8Hz and 20Hz lower freq.
        If None the frequency corresponding to an epoch length of 5 cycles
        is used.
    fmax : float | tuple of floats
        The upper frequency of interest. Multiple bands are dedined using
        a tuple, e.g. (13., 30.) for two band with 13Hz and 30Hz upper freq.
    fskip : int
        Omit every "(fskip + 1)-th" frequency bin to decimate in frequency
        domain.
    faverage : boolean
        Average connectivity scores for each frequency band. If True,
        the output freqs will be a list with arrays of the frequencies
        that were averaged.
    tmin : float | None
        Time to start connectivity estimation.
    tmax : float | None
        Time to end connectivity estimation.
    mt_bandwidth : float | None
        The bandwidth of the multitaper windowing function in Hz.
        Only used in 'multitaper' mode.
    mt_adaptive : bool
        Use adaptive weights to combine the tapered spectra into PSD.
        Only used in 'multitaper' mode.
    mt_low_bias : bool
        Only use tapers with more than 90% spectral concentration within
        bandwidth. Only used in 'multitaper' mode.
    cwt_frequencies : array
        Array of frequencies of interest. Only used in 'cwt_morlet' mode.
    cwt_n_cycles: float | array of float
        Number of cycles. Fixed number or one per frequency. Only used in
        'cwt_morlet' mode.
    block_size : int
        How many connections to compute at once (higher numbers are faster
        but require more memory).
    n_jobs : int
        How many epochs to process in parallel.
    verbose : bool, str, int, or None
        If not None, override default verbose level (see mne.verbose).

    Returns
    -------
    con : array | list of arrays
        Computed connectivity measure(s). The shape of each array is either
        (n_signals, n_signals, n_frequencies) mode: 'multitaper' or 'fourier'
        (n_signals, n_signals, n_frequencies, n_times) mode: 'cwt_morlet'
        when "indices" is None, or
        (n_con, n_frequencies) mode: 'multitaper' or 'fourier'
        (n_con, n_frequencies, n_times) mode: 'cwt_morlet'
        when "indices" is specified and "n_con = len(indices[0])".
    freqs : array
        Frequency points at which the connectivity was computed.
    times : array
        Time points for which the connectivity was computed.
    n_epochs : int
        Number of epochs used for computation.
    n_tapers : int
        The number of DPSS tapers used. Only defined in 'multitaper' mode.
        Otherwise None is returned.
    """
    if n_jobs > 1:
        parallel, my_epoch_spectral_connectivity, _ = \
                parallel_func(_epoch_spectral_connectivity, n_jobs,
                              verbose=verbose)

    # format fmin and fmax and check inputs
    if fmin is None:
        fmin = -np.inf  # set it to -inf, so we can adjust it later

    fmin = np.asarray((fmin, )).ravel()
    fmax = np.asarray((fmax, )).ravel()
    if len(fmin) != len(fmax):
        raise ValueError('fmin and fmax must have the same length')
    if np.any(fmin > fmax):
        raise ValueError('fmax must be larger than fmin')

    n_bands = len(fmin)

    # assign names to connectivity methods
    if not isinstance(method, (list, tuple)):
        method = [method]  # make it a list so we can iterate over it

    n_methods = len(method)
    con_method_types = []
    for m in method:
        if m in _CON_METHOD_MAP:
            method = _CON_METHOD_MAP[m]
            con_method_types.append(method)
        elif isinstance(m, basestring):
            raise ValueError('%s is not a valid connectivity method' % m)
        else:
            # add custom method
            method_valid, msg = _check_method(m)
            if not method_valid:
                raise ValueError('The supplied connectivity method does '
                                 'not have the method %s' % msg)
            con_method_types.append(m)

    # determine how many arguments the compute_con_function needs
    n_comp_args = [
        len(getargspec(mtype.compute_con).args) for mtype in con_method_types
    ]

    # we only support 3 or 5 arguments
    if any([n not in (3, 5) for n in n_comp_args]):
        raise ValueError('The compute_con function needs to have either '
                         '3 or 5 arguments')

    # if none of the comp_con functions needs the PSD, we don't estimate it
    accumulate_psd = any([n == 5 for n in n_comp_args])

    if isinstance(data, Epochs):
        times_in = data.times  # input times for Epochs input type
        sfreq = data.info['sfreq']

    # loop over data; it could be a generator that returns
    # (n_signals x n_times) arrays or SourceEstimates
    epoch_idx = 0
    logger.info('Connectivity computation...')
    for epoch_block in _get_n_epochs(data, n_jobs):

        if epoch_idx == 0:
            # initialize everything
            first_epoch = epoch_block[0]

            # get the data size and time scale
            n_signals, n_times_in, times_in =\
                    _get_and_verify_data_sizes(first_epoch)

            if times_in is None:
                # we are not using Epochs or SourceEstimate(s) as input
                times_in = np.linspace(0.0,
                                       n_times_in / sfreq,
                                       n_times_in,
                                       endpoint=False)

            n_times_in = len(times_in)
            tmin_idx = 0
            tmax_idx = n_times_in
            tmin_true = times_in[0]
            tmax_true = times_in[-1]
            if tmin is not None:
                tmin_idx = np.argmin(np.abs(times_in - tmin))
                tmin_true = times_in[tmin_idx]
            if tmax is not None:
                tmax_idx = np.argmin(np.abs(times_in - tmax)) + 1
                tmax_true = times_in[tmax_idx - 1]  # time of last point used

            times = times_in[tmin_idx:tmax_idx]
            n_times = len(times)

            if indices is None:
                # only compute r for lower-triangular region
                indices_use = tril_indices(n_signals, -1)
            else:
                indices_use = check_indices(indices)

            # number of connectivities to compute
            n_cons = len(indices_use[0])

            logger.info('    computing connectivity for %d connections' %
                        n_cons)

            logger.info(
                '    using t=%0.3fs..%0.3fs for estimation (%d points)' %
                (tmin_true, tmax_true, n_times))

            # get frequencies of interest for the different modes
            if mode in ['multitaper', 'fourier']:
                # fmin fmax etc is only supported for these modes
                # decide which frequencies to keep
                freqs_all = fftfreq(n_times, 1. / sfreq)
                freqs_all = freqs_all[freqs_all >= 0]
            elif mode == 'cwt_morlet':
                # cwt_morlet mode
                if cwt_frequencies is None:
                    raise ValueError('define frequencies of interest using '
                                     'cwt_frequencies')
                else:
                    cwt_frequencies = cwt_frequencies.astype(np.float)
                if any(cwt_frequencies > (sfreq / 2.)):
                    raise ValueError('entries in cwt_frequencies cannot be '
                                     'larger than Nyquist (sfreq / 2)')
                freqs_all = cwt_frequencies
            else:
                raise ValueError('mode has an invalid value')

            # check that fmin corresponds to at least 5 cycles
            five_cycle_freq = 5. * sfreq / float(n_times)

            if len(fmin) == 1 and fmin[0] == -np.inf:
                # we use the 5 cycle freq. as default
                fmin = [five_cycle_freq]
            else:
                if any(fmin < five_cycle_freq):
                    warn('fmin corresponds to less than 5 cycles, '
                         'spectrum estimate will be unreliable')

            # create a frequency mask for all bands
            freq_mask = np.zeros(len(freqs_all), dtype=np.bool)
            for f_lower, f_upper in zip(fmin, fmax):
                freq_mask |= ((freqs_all >= f_lower) & (freqs_all <= f_upper))

            # possibly skip frequency points
            for pos in xrange(fskip):
                freq_mask[pos + 1::fskip + 1] = False

            # the frequency points where we compute connectivity
            freqs = freqs_all[freq_mask]
            n_freqs = len(freqs)

            # get the freq. indices and points for each band
            freq_idx_bands = [
                np.where((freqs >= fl) & (freqs <= fu))[0]
                for fl, fu in zip(fmin, fmax)
            ]
            freqs_bands = [freqs[freq_idx] for freq_idx in freq_idx_bands]

            # make sure we don't have empty bands
            for i, n_f_band in enumerate([len(f) for f in freqs_bands]):
                if n_f_band == 0:
                    raise ValueError(
                        'There are no frequency points between '
                        '%0.1fHz and %0.1fHz. Change the band specification '
                        '(fmin, fmax) or the frequency resolution.' %
                        (fmin[i], fmax[i]))

            if n_bands == 1:
                logger.info('    frequencies: %0.1fHz..%0.1fHz (%d points)' %
                            (freqs_bands[0][0], freqs_bands[0][-1], n_freqs))
            else:
                logger.info('    computing connectivity for the bands:')
                for i, bfreqs in enumerate(freqs_bands):
                    logger.info('     band %d: %0.1fHz..%0.1fHz '
                                '(%d points)' %
                                (i + 1, bfreqs[0], bfreqs[-1], len(bfreqs)))

            if faverage:
                logger.info('    connectivity scores will be averaged for '
                            'each band')

            # get the window function, wavelets, etc for different modes
            if mode == 'multitaper':
                # compute standardized half-bandwidth
                if mt_bandwidth is not None:
                    half_nbw = float(mt_bandwidth) * n_times / (2 * sfreq)
                else:
                    half_nbw = 4

                # compute dpss windows
                n_tapers_max = int(2 * half_nbw)
                window_fun, eigvals = dpss_windows(n_times,
                                                   half_nbw,
                                                   n_tapers_max,
                                                   low_bias=mt_low_bias)
                n_tapers = len(eigvals)
                logger.info('    using multitaper spectrum estimation with '
                            '%d DPSS windows' % n_tapers)

                if mt_adaptive and len(eigvals) < 3:
                    warn('Not adaptively combining the spectral estimators '
                         'due to a low number of tapers.')
                    mt_adaptive = False

                n_times_spectrum = 0  # this method only uses the freq. domain
                wavelets = None
            elif mode == 'fourier':
                logger.info('    using FFT with a Hanning window to estimate '
                            'spectra')

                window_fun = np.hanning(n_times)
                mt_adaptive = False
                eigvals = 1.
                n_tapers = None
                n_times_spectrum = 0  # this method only uses the freq. domain
                wavelets = None
            elif mode == 'cwt_morlet':
                logger.info('    using CWT with Morlet wavelets to estimate '
                            'spectra')

                # reformat cwt_n_cycles if we have removed some frequencies
                # using fmin, fmax, fskip
                cwt_n_cycles = np.asarray((cwt_n_cycles, )).ravel()
                if len(cwt_n_cycles) > 1:
                    if len(cwt_n_cycles) != len(cwt_frequencies):
                        raise ValueError(
                            'cwt_n_cycles must be float or an '
                            'array with the same size as cwt_frequencies')
                    cwt_n_cycles = cwt_n_cycles[freq_mask]

                # get the Morlet wavelets
                wavelets = morlet(sfreq,
                                  freqs,
                                  n_cycles=cwt_n_cycles,
                                  zero_mean=True)
                eigvals = None
                n_tapers = None
                window_fun = None
                n_times_spectrum = n_times
            else:
                raise ValueError('mode has an invalid value')

            # unique signals for which we actually need to compute PSD etc.
            sig_idx = np.unique(np.r_[indices_use[0], indices_use[1]])

            # map indices to unique indices
            idx_map = [np.searchsorted(sig_idx, ind) for ind in indices_use]

            # allocate space to accumulate PSD
            if accumulate_psd:
                if n_times_spectrum == 0:
                    psd_shape = (len(sig_idx), n_freqs)
                else:
                    psd_shape = (len(sig_idx), n_freqs, n_times_spectrum)
                psd = np.zeros(psd_shape)
            else:
                psd = None

            # create instances of the connectivity estimators
            con_methods = [
                mtype(n_cons, n_freqs, n_times_spectrum)
                for mtype in con_method_types
            ]

            sep = ', '
            metrics_str = sep.join([method.name for method in con_methods])
            logger.info('    the following metrics will be computed: %s' %
                        metrics_str)

        # check dimensions and time scale
        for this_epoch in epoch_block:
            _get_and_verify_data_sizes(this_epoch, n_signals, n_times_in,
                                       times_in)

        if n_jobs == 1:
            # no parallel processing
            for this_epoch in epoch_block:
                logger.info('    computing connectivity for epoch %d' %
                            (epoch_idx + 1))

                # con methods and psd are updated inplace
                _epoch_spectral_connectivity(this_epoch,
                                             sig_idx,
                                             tmin_idx,
                                             tmax_idx,
                                             sfreq,
                                             mode,
                                             window_fun,
                                             eigvals,
                                             wavelets,
                                             freq_mask,
                                             mt_adaptive,
                                             idx_map,
                                             block_size,
                                             psd,
                                             accumulate_psd,
                                             con_method_types,
                                             con_methods,
                                             n_signals,
                                             n_times,
                                             accumulate_inplace=True)
                epoch_idx += 1
        else:
            # process epochs in parallel
            logger.info('    computing connectivity for epochs %d..%d' %
                        (epoch_idx + 1, epoch_idx + len(epoch_block)))

            out = parallel(
                my_epoch_spectral_connectivity(this_epoch,
                                               sig_idx,
                                               tmin_idx,
                                               tmax_idx,
                                               sfreq,
                                               mode,
                                               window_fun,
                                               eigvals,
                                               wavelets,
                                               freq_mask,
                                               mt_adaptive,
                                               idx_map,
                                               block_size,
                                               psd,
                                               accumulate_psd,
                                               con_method_types,
                                               None,
                                               n_signals,
                                               n_times,
                                               accumulate_inplace=False)
                for this_epoch in epoch_block)

            # do the accumulation
            for this_out in out:
                for method, parallel_method in zip(con_methods, this_out[0]):
                    method.combine(parallel_method)
                if accumulate_psd:
                    psd += this_out[1]

            epoch_idx += len(epoch_block)

    # normalize
    n_epochs = epoch_idx
    if accumulate_psd:
        psd /= n_epochs

    # compute final connectivity scores
    con = []
    for method, n_args in zip(con_methods, n_comp_args):
        if n_args == 3:
            # compute all scores at once
            method.compute_con(slice(0, n_cons), n_epochs)
        else:
            # compute scores block-wise to save memory
            for i in xrange(0, n_cons, block_size):
                con_idx = slice(i, i + block_size)
                psd_xx = psd[idx_map[0][con_idx]]
                psd_yy = psd[idx_map[1][con_idx]]
                method.compute_con(con_idx, n_epochs, psd_xx, psd_yy)

        # get the connectivity scores
        this_con = method.con_scores

        if this_con.shape[0] != n_cons:
            raise ValueError('First dimension of connectivity scores must be '
                             'the same as the number of connections')
        if faverage:
            if this_con.shape[1] != n_freqs:
                raise ValueError('2nd dimension of connectivity scores must '
                                 'be the same as the number of frequencies')
            con_shape = (n_cons, n_bands) + this_con.shape[2:]
            this_con_bands = np.empty(con_shape, dtype=this_con.dtype)
            for band_idx in xrange(n_bands):
                this_con_bands[:, band_idx] =\
                    np.mean(this_con[:, freq_idx_bands[band_idx]], axis=1)
            this_con = this_con_bands

        con.append(this_con)

    if indices is None:
        # return all-to-all connectivity matrices
        logger.info('    assembling connectivity matrix')
        con_flat = con
        con = []
        for this_con_flat in con_flat:
            this_con = np.zeros(
                (n_signals, n_signals) + this_con_flat.shape[1:],
                dtype=this_con_flat.dtype)
            this_con[indices_use] = this_con_flat
            con.append(this_con)

    logger.info('[Connectivity computation done]')

    if n_methods == 1:
        # for a single method return connectivity directly
        con = con[0]

    if faverage:
        # for each band we return the frequencies that were averaged
        freqs = freqs_bands

    return con, freqs, times, n_epochs, n_tapers
    def _frame(self, samples, sample_rate, window_ms):
        stride_ms = window_ms / 2
        stride_size = int(0.001 * sample_rate * stride_ms)
        window_size = int(0.001 * sample_rate * window_ms)

        # Extract strided windows
        truncate_size = (len(samples) - window_size) % stride_size
        samples = samples[:len(samples) - truncate_size]
        nshape = (window_size, (len(samples) - window_size) // stride_size + 1)
        nstrides = (samples.strides[0], samples.strides[0] * stride_size)
        windows = np.lib.stride_tricks.as_strided(samples,
                                                  shape=nshape,
                                                  strides=nstrides)

        assert np.all(windows[:, 1] == samples[stride_size:(stride_size +
                                                            window_size)])

        # Window weighting, squared Fast Fourier Transform (fft), scaling

        weighting = np.hanning(window_size)[:, None]

        fft = np.fft.fft(windows * weighting, axis=0)
        fft = np.absolute(fft)
        fft = fft**2
        scale = np.sum(weighting**2) * sample_rate
        fft[1:-1, :] *= 2.0 / scale
        fft[(0, -1), :] /= scale
        fft = np.log(fft)

        # Prepare fft frequency list

        freqs = float(sample_rate) / window_size * np.arange(fft.shape[0])

        frames = []
        for i in range(40):
            bands = []
            band0 = []
            band1 = []
            band2 = []
            band3 = []
            band4 = []
            j = 0
            for freq in freqs:
                if freq <= 333.3:
                    band0.append(fft[j][i])
                elif freq > 333.3 and freq <= 666.7:
                    band1.append(fft[j][i])
                elif freq > 666.7 and freq <= 1333.3:
                    band2.append(fft[j][i])
                elif freq > 1333.3 and freq <= 2333.3:
                    band3.append(fft[j][i])
                elif freq > 2333.3 and freq <= 4000:
                    band4.append(fft[j][i])

                j += 1
            bands.append(np.sum(band0) / np.shape(band0)[0])
            bands.append(np.sum(band1) / np.shape(band1)[0])
            bands.append(np.sum(band2) / np.shape(band2)[0])
            bands.append(np.sum(band3) / np.shape(band3)[0])
            bands.append(np.sum(band4) / np.shape(band4)[0])
            frames.append(bands)

        return frames
Example #55
0
# Signal reconstruction
def signal_reconstruction(X_estimated):
    X_estimated = X_estimated.T
    temp = np.zeros(len(x_t))
    for i in range(X_estimated.shape[0]):
        temp[(i * 512):(i * 512) +
             512] = temp[(i * 512):(i * 512) +
                         512] + X_estimated[i, 0:512].flatten()
        temp[((i * 512) + 512):((i * 512) +
                                1024)] = X_estimated[i, 512:1024].flatten()
    return temp


N = 1024
f, n = np.arange(N), np.arange(N)
han = np.hanning(1024).reshape(1024, 1)

print(I_DFT(f))

# For speaker signal trs.wav
x_s, _ = librosa.load('data/trs.wav', sr=None)
x_s = x_s.reshape(len(x_s), 1)

# STFT of signal trs.wav
S = np.abs(stft(x_s))

# Initializing matrix W and H
W_S, H_S = init_W_H(S.shape[0], 30, S.shape[1])

# Non-negative Matrix Factorization
W_S, H_S = NMF(S, W_S, H_S)
 def __init__(self, model, cfg):
     super(SiamCARTracker, self).__init__()
     hanning = np.hanning(cfg.SCORE_SIZE)
     self.window = np.outer(hanning, hanning)  # 生成 汉宁窗
     self.model = model  # 跟踪网络
     self.model.eval()  # 测试模式
Example #57
0
def simulate_raw(raw,
                 stc,
                 trans,
                 src,
                 bem,
                 cov='simple',
                 blink=False,
                 ecg=False,
                 chpi=False,
                 head_pos=None,
                 mindist=1.0,
                 interp='cos2',
                 iir_filter=None,
                 n_jobs=1,
                 random_state=None,
                 verbose=None):
    """Simulate raw data

    Head movements can optionally be simulated using the ``head_pos``
    parameter.

    Parameters
    ----------
    raw : instance of Raw
        The raw template to use for simulation. The ``info``, ``times``,
        and potentially ``first_samp`` properties will be used.
    stc : instance of SourceEstimate
        The source estimate to use to simulate data. Must have the same
        sample rate as the raw data.
    trans : dict | str | None
        Either a transformation filename (usually made using mne_analyze)
        or an info dict (usually opened using read_trans()).
        If string, an ending of `.fif` or `.fif.gz` will be assumed to
        be in FIF format, any other ending will be assumed to be a text
        file with a 4x4 transformation matrix (like the `--trans` MNE-C
        option). If trans is None, an identity transform will be used.
    src : str | instance of SourceSpaces
        Source space corresponding to the stc. If string, should be a source
        space filename. Can also be an instance of loaded or generated
        SourceSpaces.
    bem : str | dict
        BEM solution  corresponding to the stc. If string, should be a BEM
        solution filename (e.g., "sample-5120-5120-5120-bem-sol.fif").
    cov : instance of Covariance | str | None
        The sensor covariance matrix used to generate noise. If None,
        no noise will be added. If 'simple', a basic (diagonal) ad-hoc
        noise covariance will be used. If a string, then the covariance
        will be loaded.
    blink : bool
        If true, add simulated blink artifacts. See Notes for details.
    ecg : bool
        If true, add simulated ECG artifacts. See Notes for details.
    chpi : bool
        If true, simulate continuous head position indicator information.
        Valid cHPI information must encoded in ``raw.info['hpi_meas']``
        to use this option.
    head_pos : None | str | dict | tuple | array
        Name of the position estimates file. Should be in the format of
        the files produced by maxfilter. If dict, keys should
        be the time points and entries should be 4x4 ``dev_head_t``
        matrices. If None, the original head position (from
        ``info['dev_head_t']``) will be used. If tuple, should have the
        same format as data returned by `head_pos_to_trans_rot_t`.
        If array, should be of the form returned by `read_head_pos`.
    mindist : float
        Minimum distance between sources and the inner skull boundary
        to use during forward calculation.
    interp : str
        Either 'cos2', 'linear', or 'zero', the type of forward-solution
        interpolation to use between forward solutions at different
        head positions.
    iir_filter : None | array
        IIR filter coefficients (denominator) e.g. [1, -1, 0.2].
    n_jobs : int
        Number of jobs to use.
    random_state : None | int | np.random.RandomState
        The random generator state used for blink, ECG, and sensor
        noise randomization.
    verbose : bool, str, int, or None
        If not None, override default verbose level (see mne.verbose).

    Returns
    -------
    raw : instance of Raw
        The simulated raw file.

    See Also
    --------
    read_head_pos
    simulate_evoked
    simulate_stc
    simalute_sparse_stc

    Notes
    -----
    Events coded with the position number (starting at 1) will be stored
    in the trigger channel (if available) at times corresponding to t=0
    in the ``stc``.

    The resulting SNR will be determined by the structure of the noise
    covariance, the amplitudes of ``stc``, and the head position(s) provided.

    The blink and ECG artifacts are generated by 1) placing impulses at
    random times of activation, and 2) convolving with activation kernel
    functions. In both cases, the scale-factors of the activation functions
    (and for the resulting EOG and ECG channel traces) were chosen based on
    visual inspection to yield amplitudes generally consistent with those
    seen in experimental data. Noisy versions of the blink and ECG
    activations will be stored in the first EOG and ECG channel in the
    raw file, respectively, if they exist.

    For blink artifacts:

        1. Random activation times are drawn from an inhomogeneous poisson
           process whose blink rate oscillates between 4.5 blinks/minute
           and 17 blinks/minute based on the low (reading) and high (resting)
           blink rates from [1]_.
        2. The activation kernel is a 250 ms Hanning window.
        3. Two activated dipoles are located in the z=0 plane (in head
           coordinates) at ±30 degrees away from the y axis (nasion).
        4. Activations affect MEG and EEG channels.

    For ECG artifacts:

        1. Random inter-beat intervals are drawn from a uniform distribution
           of times corresponding to 40 and 80 beats per minute.
        2. The activation function is the sum of three Hanning windows with
           varying durations and scales to make a more complex waveform.
        3. The activated dipole is located one (estimated) head radius to
           the left (-x) of head center and three head radii below (+z)
           head center; this dipole is oriented in the +x direction.
        4. Activations only affect MEG channels.

    .. versionadded:: 0.10.0

    References
    ----------
    .. [1] Bentivoglio et al. "Analysis of blink rate patterns in normal
           subjects" Movement Disorders, 1997 Nov;12(6):1028-34.
    """
    if not isinstance(raw, _BaseRaw):
        raise TypeError('raw should be an instance of Raw')
    times, info, first_samp = raw.times, raw.info, raw.first_samp
    raw_verbose = raw.verbose

    # Check for common flag errors and try to override
    if not isinstance(stc, _BaseSourceEstimate):
        raise TypeError('stc must be a SourceEstimate')
    if not np.allclose(info['sfreq'], 1. / stc.tstep):
        raise ValueError('stc and info must have same sample rate')
    if len(stc.times) <= 2:  # to ensure event encoding works
        raise ValueError('stc must have at least three time points')

    stim = False if len(pick_types(info, meg=False, stim=True)) == 0 else True
    n_jobs = check_n_jobs(n_jobs)

    rng = check_random_state(random_state)
    if interp not in ('cos2', 'linear', 'zero'):
        raise ValueError('interp must be "cos2", "linear", or "zero"')

    if head_pos is None:  # use pos from file
        dev_head_ts = [info['dev_head_t']] * 2
        offsets = np.array([0, len(times)])
        interp = 'zero'
    # Use position data to simulate head movement
    else:
        if isinstance(head_pos, string_types):
            head_pos = read_head_pos(head_pos)
        if isinstance(head_pos, np.ndarray):
            head_pos = head_pos_to_trans_rot_t(head_pos)
        if isinstance(head_pos, tuple):  # can be an already-loaded pos file
            transs, rots, ts = head_pos
            ts -= first_samp / info['sfreq']  # MF files need reref
            dev_head_ts = [
                np.r_[np.c_[r, t[:, np.newaxis]], [[0, 0, 0, 1]]]
                for r, t in zip(rots, transs)
            ]
            del transs, rots
        elif isinstance(head_pos, dict):
            ts = np.array(list(head_pos.keys()), float)
            ts.sort()
            dev_head_ts = [head_pos[float(tt)] for tt in ts]
        else:
            raise TypeError('unknown head_pos type %s' % type(head_pos))
        bad = ts < 0
        if bad.any():
            raise RuntimeError('All position times must be >= 0, found %s/%s'
                               '< 0' % (bad.sum(), len(bad)))
        bad = ts > times[-1]
        if bad.any():
            raise RuntimeError('All position times must be <= t_end (%0.1f '
                               'sec), found %s/%s bad values (is this a split '
                               'file?)' % (times[-1], bad.sum(), len(bad)))
        if ts[0] > 0:
            ts = np.r_[[0.], ts]
            dev_head_ts.insert(0, info['dev_head_t']['trans'])
        dev_head_ts = [{
            'trans': d,
            'to': info['dev_head_t']['to'],
            'from': info['dev_head_t']['from']
        } for d in dev_head_ts]
        if ts[-1] < times[-1]:
            dev_head_ts.append(dev_head_ts[-1])
            ts = np.r_[ts, [times[-1]]]
        offsets = raw.time_as_index(ts)
        offsets[-1] = len(times)  # fix for roundoff error
        assert offsets[-2] != offsets[-1]
        del ts

    src = _ensure_src(src, verbose=False)
    if isinstance(bem, string_types):
        bem = read_bem_solution(bem, verbose=False)
    if isinstance(cov, string_types):
        if cov == 'simple':
            cov = make_ad_hoc_cov(info, verbose=False)
        else:
            cov = read_cov(cov, verbose=False)
    assert np.array_equal(offsets, np.unique(offsets))
    assert len(offsets) == len(dev_head_ts)
    approx_events = int(
        (len(times) / info['sfreq']) / (stc.times[-1] - stc.times[0]))
    logger.info('Provided parameters will provide approximately %s event%s' %
                (approx_events, '' if approx_events == 1 else 's'))

    # Extract necessary info
    meeg_picks = pick_types(info, meg=True, eeg=True, exclude=[])  # for sim
    meg_picks = pick_types(info, meg=True, eeg=False, exclude=[])  # for CHPI
    fwd_info = pick_info(info, meeg_picks)
    fwd_info['projs'] = []  # Ensure no 'projs' applied
    logger.info(
        'Setting up raw simulation: %s position%s, "%s" interpolation' %
        (len(dev_head_ts), 's' if len(dev_head_ts) != 1 else '', interp))

    verts = stc.vertices
    verts = [verts] if isinstance(stc, VolSourceEstimate) else verts
    src = _restrict_source_space_to(src, verts)

    # array used to store result
    raw_data = np.zeros((len(info['ch_names']), len(times)))

    # figure out our cHPI, ECG, and blink dipoles
    ecg_rr = blink_rrs = exg_bem = hpi_rrs = None
    ecg = ecg and len(meg_picks) > 0
    chpi = chpi and len(meg_picks) > 0
    if chpi:
        hpi_freqs, hpi_rrs, hpi_pick, hpi_ons = _get_hpi_info(info)[:4]
        hpi_nns = hpi_rrs / np.sqrt(np.sum(hpi_rrs * hpi_rrs,
                                           axis=1))[:, np.newaxis]
        # turn on cHPI in file
        raw_data[hpi_pick, :] = hpi_ons.sum()
        _log_ch('cHPI status bits enbled and', info, hpi_pick)
    if blink or ecg:
        R, r0 = fit_sphere_to_headshape(info, units='m', verbose=False)[:2]
        exg_bem = make_sphere_model(r0,
                                    head_radius=R,
                                    relative_radii=(0.97, 0.98, 0.99, 1.),
                                    sigmas=(0.33, 1.0, 0.004, 0.33),
                                    verbose=False)
    if blink:
        # place dipoles at 45 degree angles in z=0 plane
        blink_rrs = np.array([[np.cos(np.pi / 3.),
                               np.sin(np.pi / 3.), 0.],
                              [-np.cos(np.pi / 3.),
                               np.sin(np.pi / 3), 0.]])
        blink_rrs /= np.sqrt(np.sum(blink_rrs * blink_rrs, axis=1))[:,
                                                                    np.newaxis]
        blink_rrs *= 0.96 * R
        blink_rrs += r0
        # oriented upward
        blink_nns = np.array([[0., 0., 1.], [0., 0., 1.]])
        # Blink times drawn from an inhomogeneous poisson process
        # by 1) creating the rate and 2) pulling random numbers
        blink_rate = (1 + np.cos(2 * np.pi * 1. / 60. * times)) / 2.
        blink_rate *= 12.5 / 60.
        blink_rate += 4.5 / 60.
        blink_data = rng.rand(len(times)) < blink_rate / info['sfreq']
        blink_data = blink_data * (rng.rand(len(times)) + 0.5)  # varying amps
        # Activation kernel is a simple hanning window
        blink_kernel = np.hanning(int(0.25 * info['sfreq']))
        blink_data = np.convolve(blink_data, blink_kernel,
                                 'same')[np.newaxis, :] * 1e-7
        # Add rescaled noisy data to EOG ch
        ch = pick_types(info, meg=False, eeg=False, eog=True)
        noise = rng.randn(blink_data.shape[1]) * 5e-6
        if len(ch) >= 1:
            ch = ch[-1]
            raw_data[ch, :] = blink_data * 1e3 + noise
        else:
            ch = None
        _log_ch('Blinks simulated and trace', info, ch)
        del blink_kernel, blink_rate, noise
    if ecg:
        ecg_rr = np.array([[-R, 0, -3 * R]])
        max_beats = int(np.ceil(times[-1] * 80. / 60.))
        # activation times with intervals drawn from a uniform distribution
        # based on activation rates between 40 and 80 beats per minute
        cardiac_idx = np.cumsum(
            rng.uniform(60. / 80., 60. / 40., max_beats) *
            info['sfreq']).astype(int)
        cardiac_idx = cardiac_idx[cardiac_idx < len(times)]
        cardiac_data = np.zeros(len(times))
        cardiac_data[cardiac_idx] = 1
        # kernel is the sum of three hanning windows
        cardiac_kernel = np.concatenate([
            2 * np.hanning(int(0.04 * info['sfreq'])),
            -0.3 * np.hanning(int(0.05 * info['sfreq'])),
            0.2 * np.hanning(int(0.26 * info['sfreq']))
        ],
                                        axis=-1)
        ecg_data = np.convolve(cardiac_data, cardiac_kernel,
                               'same')[np.newaxis, :] * 15e-8
        # Add rescaled noisy data to ECG ch
        ch = pick_types(info, meg=False, eeg=False, ecg=True)
        noise = rng.randn(ecg_data.shape[1]) * 1.5e-5
        if len(ch) >= 1:
            ch = ch[-1]
            raw_data[ch, :] = ecg_data * 2e3 + noise
        else:
            ch = None
        _log_ch('ECG simulated and trace', info, ch)
        del cardiac_data, cardiac_kernel, max_beats, cardiac_idx

    stc_event_idx = np.argmin(np.abs(stc.times))
    if stim:
        event_ch = pick_channels(info['ch_names'],
                                 _get_stim_channel(None, info))[0]
        raw_data[event_ch, :] = 0.
    else:
        event_ch = None
    _log_ch('Event information', info, event_ch)
    used = np.zeros(len(times), bool)
    stc_indices = np.arange(len(times)) % len(stc.times)
    raw_data[meeg_picks, :] = 0.
    hpi_mag = 70e-9
    last_fwd = last_fwd_chpi = last_fwd_blink = last_fwd_ecg = src_sel = None
    zf = None  # final filter conditions for the noise
    # don't process these any more if no MEG present
    for fi, (fwd, fwd_blink, fwd_ecg, fwd_chpi) in \
        enumerate(_iter_forward_solutions(
            fwd_info, trans, src, bem, exg_bem, dev_head_ts, mindist,
            hpi_rrs, blink_rrs, ecg_rr, n_jobs)):
        # must be fixed orientation
        # XXX eventually we could speed this up by allowing the forward
        # solution code to only compute the normal direction
        fwd = convert_forward_solution(fwd,
                                       surf_ori=True,
                                       force_fixed=True,
                                       verbose=False)
        if blink:
            fwd_blink = fwd_blink['sol']['data']
            for ii in range(len(blink_rrs)):
                fwd_blink[:, ii] = np.dot(fwd_blink[:, 3 * ii:3 * (ii + 1)],
                                          blink_nns[ii])
            fwd_blink = fwd_blink[:, :len(blink_rrs)]
            fwd_blink = fwd_blink.sum(axis=1)[:, np.newaxis]
        # just use one arbitrary direction
        if ecg:
            fwd_ecg = fwd_ecg['sol']['data'][:, [0]]

        # align cHPI magnetic dipoles in approx. radial direction
        if chpi:
            for ii in range(len(hpi_rrs)):
                fwd_chpi[:, ii] = np.dot(fwd_chpi[:, 3 * ii:3 * (ii + 1)],
                                         hpi_nns[ii])
            fwd_chpi = fwd_chpi[:, :len(hpi_rrs)].copy()

        if src_sel is None:
            src_sel = _stc_src_sel(fwd['src'], stc)
            verts = stc.vertices
            verts = [verts] if isinstance(stc, VolSourceEstimate) else verts
            diff_ = sum([len(v) for v in verts]) - len(src_sel)
            if diff_ != 0:
                warn('%s STC vertices omitted due to fwd calculation' % diff_)
        if last_fwd is None:
            last_fwd, last_fwd_blink, last_fwd_ecg, last_fwd_chpi = \
                fwd, fwd_blink, fwd_ecg, fwd_chpi
            continue

        # set up interpolation
        n_pts = offsets[fi] - offsets[fi - 1]
        if interp == 'zero':
            interps = None
        else:
            if interp == 'linear':
                interps = np.linspace(1, 0, n_pts, endpoint=False)
            else:  # interp == 'cos2':
                interps = np.cos(0.5 * np.pi * np.arange(n_pts))**2
            interps = np.array([interps, 1 - interps])

        assert not used[offsets[fi - 1]:offsets[fi]].any()
        event_idxs = np.where(stc_indices[offsets[fi - 1]:offsets[fi]] ==
                              stc_event_idx)[0] + offsets[fi - 1]
        if stim:
            raw_data[event_ch, event_idxs] = fi

        logger.info('  Simulating data for %0.3f-%0.3f sec with %s event%s' %
                    (tuple(offsets[fi - 1:fi + 1] / info['sfreq']) +
                     (len(event_idxs), '' if len(event_idxs) == 1 else 's')))

        # Process data in large chunks to save on memory
        chunk_size = 10000
        chunks = np.concatenate((np.arange(offsets[fi - 1], offsets[fi],
                                           chunk_size), [offsets[fi]]))
        for start, stop in zip(chunks[:-1], chunks[1:]):
            assert stop - start <= chunk_size

            used[start:stop] = True
            if interp == 'zero':
                this_interp = None
            else:
                this_interp = interps[:, start - chunks[0]:stop - chunks[0]]
            time_sl = slice(start, stop)
            this_t = np.arange(start, stop) / info['sfreq']
            stc_idxs = stc_indices[time_sl]

            # simulate brain data
            raw_data[meeg_picks, time_sl] = \
                _interp(last_fwd['sol']['data'], fwd['sol']['data'],
                        stc.data[:, stc_idxs][src_sel], this_interp)

            # add sensor noise, ECG, blink, cHPI
            if cov is not None:
                noise, zf = _generate_noise(fwd_info,
                                            cov,
                                            iir_filter,
                                            rng,
                                            len(stc_idxs),
                                            zi=zf)
                raw_data[meeg_picks, time_sl] += noise
            if blink:
                raw_data[meeg_picks, time_sl] += \
                    _interp(last_fwd_blink, fwd_blink, blink_data[:, time_sl],
                            this_interp)
            if ecg:
                raw_data[meg_picks, time_sl] += \
                    _interp(last_fwd_ecg, fwd_ecg, ecg_data[:, time_sl],
                            this_interp)
            if chpi:
                sinusoids = np.zeros((len(hpi_freqs), len(stc_idxs)))
                for fidx, freq in enumerate(hpi_freqs):
                    sinusoids[fidx] = 2 * np.pi * freq * this_t
                    sinusoids[fidx] = hpi_mag * np.sin(sinusoids[fidx])
                raw_data[meg_picks, time_sl] += \
                    _interp(last_fwd_chpi, fwd_chpi, sinusoids, this_interp)

        assert used[offsets[fi - 1]:offsets[fi]].all()

        # prepare for next iteration
        last_fwd, last_fwd_blink, last_fwd_ecg, last_fwd_chpi = \
            fwd, fwd_blink, fwd_ecg, fwd_chpi
    assert used.all()
    raw = RawArray(raw_data, info, first_samp=first_samp, verbose=False)
    raw.verbose = raw_verbose
    logger.info('Done')
    return raw
Example #58
0
def istft(y_r, y_i, window_size, stride, fft_size, window_type='hanning', center=True):
    """Computes the inverse shoft-time Fourier transform

    Note: We use a constant square inverse window for the reconstruction
    of the time-domain signal, therefore, the first and last
    `window_size - stride` are not perfectly reconstructed.

    Args:
        y_r (~nnabla.Variable): Real part of STFT of size `batch_size x fft_size//2 + 1 x frame_size`.
        y_i (~nnabla.Variable): Imaginary part of STFT of size `batch_size x fft_size//2 + 1 x frame_size`.
        window_size (int): Size of STFT analysis window.
        stride (int): Number of samples that we shift the window, also called `hop size`.
        fft_size (int): Size of the FFT, (STFT has `fft_size // 2 + 1` frequency bins).
        window_type (str): Analysis window, can be either `hanning`, `hamming` or `rectangular`.
            For convenience, also `window_type=None` is supported which is equivalent to `window_type='rectangular'`.
        center (bool): If `True`, then it is assumed that the time-domain signal has centered frames.

    Returns:
        ~nnabla.Variable: Time domain sequence of size `batch_size x sample_size`.
    """
    from nnabla.parameter import get_parameter, get_parameter_or_create
    conv_cos = get_parameter('conv_cos')
    conv_sin = get_parameter('conv_sin')

    if conv_cos is None or conv_sin is None:
        if window_type == 'hanning':
            window_func = np.hanning(window_size + 1)[:-1]
        elif window_type == 'hamming':
            window_func = np.hamming(window_size + 1)[:-1]
        elif window_type == 'rectangular' or window_type is None:
            window_func = np.ones(window_size)
        else:
            raise ValueError("Unknown window type {}.".format(window_type))

        # pad window if `fft_size > window_size`
        if fft_size > window_size:
            diff = fft_size - window_size
            window_func = np.pad(
                window_func, (diff//2, diff - diff//2), mode='constant')
        elif fft_size < window_size:
            raise ValueError(
                "FFT size has to be as least as large as window size.")

        # compute inverse STFT filter coefficients
        if fft_size % stride != 0:
            raise ValueError("FFT size needs to be a multiple of stride.")

        inv_window_func = np.zeros_like(window_func)
        for s in range(0, fft_size, stride):
            inv_window_func += np.roll(np.square(window_func), s)

        mat_cos = np.zeros((fft_size//2 + 1, 1, fft_size))
        mat_sin = np.zeros((fft_size//2 + 1, 1, fft_size))

        for w in range(fft_size//2+1):
            alpha = 1.0 if w == 0 or w == fft_size//2 else 2.0
            alpha /= fft_size
            for t in range(fft_size):
                mat_cos[w, 0, t] = alpha * \
                    np.cos(2. * np.pi * w * t / fft_size)
                mat_sin[w, 0, t] = alpha * \
                    np.sin(2. * np.pi * w * t / fft_size)
        mat_cos = mat_cos * window_func / inv_window_func
        mat_sin = mat_sin * window_func / inv_window_func

        conv_cos = get_parameter_or_create(
            'conv_sin', initializer=mat_cos, need_grad=False)
        conv_sin = get_parameter_or_create(
            'conv_cos', initializer=mat_sin, need_grad=False)

    # compute inverse STFT
    x_cos = deconvolution(y_r, conv_cos, stride=(stride,))
    x_sin = deconvolution(y_i, conv_sin, stride=(stride,))

    x = reshape(x_cos - x_sin, (x_cos.shape[0], x_cos.shape[2]))

    if center:
        x = x[:, fft_size//2:-fft_size//2]

    return x
Example #59
0
def get_cos_window(sz):
    w, h = sz
    cos_window = np.hanning(h)[:, np.newaxis].dot(np.hanning(w)[np.newaxis, :])
    return cos_window
import functions
from functions import plot_freq_db as plot_freq
from functions import PulseCompr, normalize
from numpy.fft import fftshift
from scipy import signal

freq = (coe.freq / 1e6)
distance = (coe.distance)
N = coe.N
mean = 0
std = 0.001
noise1 = np.random.normal(mean, std, size=N)
delay = 1000
win = signal.nuttall(N)  #np.blackman(N)
win1 = np.hamming(N)
win2 = np.hanning(N)

#x = signal.hilbert(win)
x = win
x_delay = np.roll(x, delay)
x_win = np.multiply(x, win)
x_win1 = np.multiply(x, win1)
x_win2 = np.multiply(x, win2)
x_win_delay = np.multiply(np.roll(x_win, delay), 1)

plot_freq(freq, x, 'b')
plot_freq(freq, x_win, 'k')
plot_freq(freq, x_win1, 'm')
plot_freq(freq, x_win2, 'g')
plt.title('Frequency Response of a Linear Chirp')
plt.legend(