Package fieldpy :: Package core :: Module helper_fns_cython
[hide private]
[frames] | no frames]

Module helper_fns_cython

A collection of useful helper functions which are written in cython.

So far just:


Note: for the doctests to run this module has to be compiled with: python2 setup.py build_ext --inplace

Functions [hide private]
list of np.array
filter_and_resample(...)
filter_and_resample(ndarray time, ndarray data, double filter_window, double new_sample_int, first_sample=None)
 
get_ind_closest(...)
get_ind_closest(ndarray vec, double value, guess_ind=None)
Variables [hide private]
  __package__ = 'fieldpy.core'
  __test__ = {u'filter_and_resample (line 23)':...
Function Details [hide private]

filter_and_resample(...)

 

filter_and_resample(ndarray time, ndarray data, double filter_window, double new_sample_int, first_sample=None)

Filters and resamples a time signal with a running average in a given time intervall +/-filter_window/2. The filter has a PHASE SHIFT for a UN-equally sampled signal. Accepts masked arrays but not "structured arrays" or "recarrays".

Parameters:
  • time - the times of the timeseries
  • data - the data of the timeseries
  • filter_window - filter time interval
  • new_sample_int - the new sampling interval (in units of time)
  • first_sample - the time of the first sample in the resampled signal. If None (default) it is (approximatly) set to time[0]+filter_window
Returns: list of np.array
Returns [new_time, new_data]

Note: 1) ignores any masks in the time variable. 2) will not work if it finds no values within filter window

To Do: Correct above point 2) (as well in helper_fns.py)

>>> tim = np.linspace(-3,90,1000); data = tim**2
>>> filter_window = 30; new_sample_int = 30
>>> filter_and_resample(tim, data, filter_window, new_sample_int)
(array([ -0.,  30.,  60.,  90.]), array([   62.98183318,   975.90237234,  3678.22848073,  6826.19355542]))
>>> data = np.ma.MaskedArray(data)
>>> filter_and_resample(tim, data, filter_window, new_sample_int)
(array([ -0.,  30.,  60.,  90.]), array([   62.98183318,   975.90237234,  3678.22848073,  6826.19355542]))
>>> data[1:10] = np.ma.masked
>>> filter_and_resample(tim, data, filter_window, new_sample_int)
(array([ -0.,  30.,  60.,  90.]), array([   65.73049119,   975.90237234,  3678.22848073,  6826.19355542]))

get_ind_closest(...)

 

get_ind_closest(ndarray vec, double value, guess_ind=None)

Finds the index (ind) of vec such that vec[ind]-value is smallest. Only works reliably for monotone vectors. As start value it uses the value given in guess_ind.

This function is somewhat slower than get_ind_closest but can be called directly from Python. (When calling from cython use get_ind_closest_faster)

Parameters:
  • vec - the vector, usually a time
  • value - the value which is compared to vec
  • guess_ind - a guess for index around which the search (symmetric) will be started.
Returns:
The index

Note: The algorithm is fairly dum, something like bisection might work much faster.

>>> tim =np.linspace(-3,100,1000)
>>> get_ind_closest(tim, 49.2)
506
>>> get_ind_closest(tim, 49.2, 400)
506


Variables Details [hide private]

__test__

Value:
{u'filter_and_resample (line 23)': u'''
    Filters and resamples a time signal with a running average in a
    given time intervall +/-filter_window/2.  The filter has a PHASE
    SHIFT for a UN-equally sampled signal.  Accepts masked arrays but
    not "structured arrays" or "recarrays".


    @param time: the times of the timeseries
...