Beispiel #1
0
def bold_convolution(bold_timeseries, duration, win_func=boxcar):
              
    window=win_func(duration)
    
    n_roi = bold_timeseries.data.shape[0]
    convolved_bold = []
    for i in range(n_roi):
        convolution = convolve(np.abs(bold_timeseries.data[i]), window)
        convolved_bold.append(convolution)
    
    ts_convolved = TimeSeries(np.vstack(convolved_bold), 
                              sampling_interval=np.float(bold_timeseries.sampling_interval))
    
    return ts_convolved
Beispiel #2
0
def bold_convolution(bold_timeseries, duration, win_func=boxcar):
              
    window=win_func(duration)
    
    n_roi = bold_timeseries.data.shape[0]
    convolved_bold = []
    for i in range(n_roi):
        convolution = convolve(np.abs(bold_timeseries.data[i]), window)
        convolved_bold.append(convolution)
    
    ts_convolved = TimeSeries(np.vstack(convolved_bold), 
                              sampling_interval=np.float(bold_timeseries.sampling_interval))
    
    return ts_convolved
Beispiel #3
0
 def process(self, signals):
     InstrumentBase.process(self, signals)
     sout = []
     for sig in signals:
         outSig = Signal()
         outSig.name = u'FIR (' + sig.name + u')'
         
         #new = signaltools.lfilter(self.num, self.den, sig.dataY)
         new = signaltools.convolve(sig.get_dataY(), self.__den)
         outSig.set_data(new, None)
         
         sout.append(outSig)
     
     self['xAxisLabel'] = sig['xAxisLabel']
     self['yAxisLabel'] = sig['yAxisLabel']
     
     return sout
Beispiel #4
0

signal = ifft(ifftshift(signal))

# Adding noise to the signal
snr = -80
snr = db_to_power(snr)
noise = SignalSource(snr, SignalSource.TYPE_GAUSS).get_signal(signal.shape)
signal += noise

signal = np.complex128(np.real(signal))

# Shift  signal to zero frequency
first_signal_position = signal_positions.ravel().nonzero()[0][0] - int(window_length / 2)
n_samples_shift = first_signal_position + int(n_signal_positions / 2)
exponential = np.exp(-2j * np.pi * (n_samples_shift / window_length) * np.arange(0, window_length))
signal *= exponential

antialiasing_filter = np.load('filter_coefficients.npy')
plot_dft_abs_phase(signal, frequency_discretization, is_in_db=True)

# antializsing filter
signal = convolve(signal, antialiasing_filter, mode='same')

# decimation
signal = signal[::3]

plot_dft_abs_phase(signal, frequency_discretization/3, is_in_db=True)
# input("Press Enter to finish...")

Beispiel #5
0
def filt1d(array, nmed, nlin, fill = False, circ = False):
    """Nonlinear (median+boxcar) filter with edge reflection."""
    nmed = 2 * int(nmed/2) + 1
    N = len(array)
    if N < (3*nmed): 
        return array
    # Check for NaNs
    lnan = numpy.isnan(array)
    nnan = sum(lnan)
    if (nnan != 0):
        lnotnan = numpy.where(lnan==0)
        s = numpy.shape(lnotnan)
        if (len(s)>1):
            s = numpy.size(lnotnan)
            lnotnan = numpy.reshape(lnotnan, s)
            med = numpy.median(array[lnotnan])
        # Fill in any data gaps by interpolating over them ...
        work = numpy.zeros(N)
        il = numpy.min(lnotnan)
        ih = numpy.max(lnotnan)
        xnew = numpy.arange(ih-il+1) + il
        f = interpolate.interp1d(lnotnan, array[lnotnan])  
        work[il:ih+1] = f(xnew)
        # ... or extending slope of nearest data points if at the edges
        if (il!=0):
            slope = work[il+1] - work[il]
            for i in range(il): work[i] = work[il] - slope*(il-i)
        if (ih!=N-1):
            slope = work[ih] - work[ih-1]
            for i in range(N-ih-1)+ih+1: work[i] = work[ih] + slope*(i-ih)
    else:
        work = numpy.copy(array)
    # Edge reflection
    nr = min(20, nlin)
    sz = max([nmed, nlin])
    if sz >= (N-1): 
        sz = N-2
    if circ != False:
        wl = work[N-sz:]
        wr = work[:sz]
    else:
        wl = array[0:sz]
        pivot = numpy.median(array[:nr])
        wl = 2 * pivot - wl
        wl = wl[::-1] # reverse array
        wr = array[N-sz:N]
        pivot = numpy.median(array[N-nr:N])
        wr = 2 * pivot - wr
        wr = wr[::-1] # reverse array
    work2 = numpy.zeros(N + 2 * sz)
    work2[0:sz] = wl
    work2[sz:sz+N] = work
    work2[sz+N:2*sz+N] = wr
    # Filter
    if nmed > 1: work = signaltools.medfilt(work2, nmed) 
    else: work = work2
    box = scipy.signal.boxcar(2*nlin+1)
    work = signaltools.convolve(work, box) / float(2*nlin+1)
    padd = (len(work) - N - 2 * sz) / 2
    work = work[padd:padd+N+2*sz]
    # return to orginal array bounds
    result = work[sz:N+sz]
    # replace bad data if present
    if (fill==False) * (nnan!=0): result[lnan] = numpy.nan
    return result
Beispiel #6
0
 def process(self, signals):
     InstrumentBase.process(self, signals)
     from scipy.fftpack import fft
     from scipy.signal import signaltools
     from Numeric import convolve
     
     dOverlay    = self['overlay']
     dAverages   = self['averages']
     dLinenumber = FFT_LINENUMBER[self['linenumber']]
     dWindow     = self['window']
     # get float from overlay string etc. "2/3" = float(2./3.)
     overlay = [float(a) for a in split(FFT_OVERLAY[dOverlay], '/')]
     if len(overlay)==1: fOverlay = float(overlay[0])
     else: fOverlay = overlay[0]/overlay[1]
     
     length      = dLinenumber
     fft_length  = dLinenumber
     
     dIncrement = int(length * (1-fOverlay))
     
     sout = []
     for s in signals:   # process for every signal
         data = s.get_dataY()
         datalength = s.get_size()
         
         if datalength < length:
             data = resize(data, (length,))
             datalength = length
         
         av_from = 0
         
         if dWindow!=0: # rectangular
             wfunc = getWindowFunction(dWindow, length)
         
         for i in range(dAverages):
             av_to = av_from+length
             if av_to>datalength: 
                 break # end of data
             
             dt = data[av_from:av_to]
             l = len(dt)
             if dWindow!=0: # rectangular
                 dt = signaltools.convolve(dt, wfunc) # weighting by window function
             
             spectrum = fft(dt)
             spectrum_real = abs(spectrum[:fft_length/2].real)
             # recursive average
             if i>1:
                 out = 0.5*(out + spectrum_real) # averaging
             else:
                 out = spectrum_real # initial or single
             
             av_from += dIncrement
         
         #sig = abs(fft(data)[1:len(s.dataY)/2+1].real)
         outSig = SignalSpectrum()
         outSig.name = self.name + ': ' + s.name
         df = 1.#TODO: s['fs']/float(dLinenumber)
         outSig.set_data(out, None)#, df*arange(df, dLinenumber+df))
         outSig['xAxisLabel']    = self['xAxisLabel']
         outSig['yAxisLabel']    = self['yAxisLabel']
         outSig['xUnit']         = self['xUnit']
         sout.append(outSig)
     
     return sout
Beispiel #7
0
def filt1d(array, nmed, nlin, fill=False, circ=False):
    """Nonlinear (median+boxcar) filter with edge reflection."""
    nmed = 2 * int(nmed / 2) + 1
    N = len(array)
    if N < (3 * nmed):
        return array
    # Check for NaNs
    lnan = numpy.isnan(array)
    nnan = sum(lnan)
    if (nnan != 0):
        lnotnan = numpy.where(lnan == 0)
        s = numpy.shape(lnotnan)
        if (len(s) > 1):
            s = numpy.size(lnotnan)
            lnotnan = numpy.reshape(lnotnan, s)
            med = numpy.median(array[lnotnan])
        # Fill in any data gaps by interpolating over them ...
        work = numpy.zeros(N)
        il = numpy.min(lnotnan)
        ih = numpy.max(lnotnan)
        xnew = numpy.arange(ih - il + 1) + il
        f = interpolate.interp1d(lnotnan, array[lnotnan])
        work[il:ih + 1] = f(xnew)
        # ... or extending slope of nearest data points if at the edges
        if (il != 0):
            slope = work[il + 1] - work[il]
            for i in range(il):
                work[i] = work[il] - slope * (il - i)
        if (ih != N - 1):
            slope = work[ih] - work[ih - 1]
            for i in range(N - ih - 1) + ih + 1:
                work[i] = work[ih] + slope * (i - ih)
    else:
        work = numpy.copy(array)
    # Edge reflection
    nr = min(20, nlin)
    sz = max([nmed, nlin])
    if sz >= (N - 1):
        sz = N - 2
    if circ != False:
        wl = work[N - sz:]
        wr = work[:sz]
    else:
        wl = array[0:sz]
        pivot = numpy.median(array[:nr])
        wl = 2 * pivot - wl
        wl = wl[::-1]  # reverse array
        wr = array[N - sz:N]
        pivot = numpy.median(array[N - nr:N])
        wr = 2 * pivot - wr
        wr = wr[::-1]  # reverse array
    work2 = numpy.zeros(N + 2 * sz)
    work2[0:sz] = wl
    work2[sz:sz + N] = work
    work2[sz + N:2 * sz + N] = wr
    # Filter
    if nmed > 1: work = signaltools.medfilt(work2, nmed)
    else: work = work2
    box = scipy.signal.boxcar(2 * nlin + 1)
    work = signaltools.convolve(work, box) / float(2 * nlin + 1)
    padd = (len(work) - N - 2 * sz) / 2
    work = work[padd:padd + N + 2 * sz]
    # return to orginal array bounds
    result = work[sz:N + sz]
    # replace bad data if present
    if (fill == False) * (nnan != 0): result[lnan] = numpy.nan
    return result