Beispiel #1
0
def meta_highpass(metAry, freq, length = 0.005, window='hann', copy=True):
    """
    Perform a two pass Type I FIR filter of cut-off freq(uency) on the given 
    1D metaArray, once forward and once backward. 
    
    meta_highpass(metAry) === metAry - meta_lowpass(metAry)
    
    Inputs:
        metAry      Target metaArray 
        freq        Cut-off frequency (float, in metAry unit)
        length      Length of the FIR filter (See notes below)
        window      Window function for the FIR filter
        copy        Whether to return a copy or modify inplace
    
    See meta_lowpass for details
    """
    loary = meta_lowpass(metAry, freq, length=length, window=window, copy=True)
    
    name_str = 'High pass filtered at ' + engUnit(freq, unit = 'Hz', sigfig=3)
    
    if copy: ary = metAry.copy()
    else:    ary = metAry
    
    ary.data -= loary.data
    
    if type(metAry['name']) is str:
        ary['name'] = metAry['name'] + ' (' + name_str + ')'
    else:
        ary['name'] = name_str
    
    if copy: return ary
    else:    return
Beispiel #2
0
def meta_lowpass(metAry, freq, length = 0.005, window='hann', copy = True):
    """
    Perform a two pass Type I FIR filter of cut-off freq(uency) on the given 
    1D metaArray, once forward and once backward.
    
    Inputs:
        metAry      Target metaArray 
        freq        Cut-off frequency (float, in metAry unit)
        length      Length of the FIR filter (See notes below)
        window      Window function for the FIR filter
        copy        Whether to return a copy or modify inplace
    
    Length
        If given as float type, it will be interpreted as percentage length
        (duration) of the input metaArray.
        
        If given as int type, it will be interpreted as the desire number of 
        taps for FIR filter.
        
        The default FIR length is 0.5% of that in the input metaArray, mimimum 3.
        The exact number of taps is rounded to the next odd number, in order to 
        meet the type I conditions.
    
    Scipy.signal.firwin support the following window options:
        boxcar
        triang
        blackman
        hamming
        hann
        bartlett
        flattop
        parzen
        bohman
        blackmanharris
        nuttall
        barthann
        kaiser (needs beta)
        gaussian (needs std)
        general_gaussian (needs power, width)
        slepian (needs width)
        chebwin (needs attenuation)
    """
    
    assert metAry.ndim is 1, "Only 1D metaArray accepted, there are %i dimemsions in the given data." % metAry.ndim
    
    if copy: ary = metAry.copy()
    else:    ary = metAry
    
    # Work out the Nyquist frequency
    Nyquist = ary.get_smp_rate() / 2
    
    # Normalise frequency
    name_str = 'Low pass filtered at ' + engUnit(freq, unit = 'Hz', sigfig=3)
    freq = float(freq) / Nyquist
    
    # Number of taps
    if type(length) is float:
        length = meta_fir_len(ary, length = length)
    elif type(length) is int:
        pass
    else:
        raise ValueError('Unexpected variable type for length: ' + str(type(length)))
    
    # a = [1.]
    b = firwin(length, freq, window=window)
    
    ary.data = filtfilt(b, [1.], ary.data)
    
    if type(ary['name']) is str:
        ary['name'] += ' (' + name_str + ')'
    else:
        ary['name'] = name_str
    
    if copy: return ary
    else:    return