简体   繁体   中英

Fast Iteration of numpy arrays

I'm new to python and I'm trying to do some basic signal-processing stuff and I'm having a serious performance problem. Is there a python trick for doing this in a vectorized manner? Basically I'm trying to implement a 1st order filter, but where the filter characteristics may change from one sample to the next. If it were just one filter I would use numpy.signal.lfilter(), but it's a bit trickier. Here's the snippet of code that goes very slowly:

#filter state
state = 0

#perform filtering
for sample in amplitude:
    if( sample == 1.0 ): #attack filter
        sample = (1.0 - att_coeff) * sample + att_coeff * state
    else: #release filter
        sample = (1.0 - rel_coeff) * sample + rel_coeff * state

    state = sample

You could consider using one of the Python-to-native-code converters, such as Cython , Numba or Pythran .

For instance, running your original code with timeit gives me:

$ python -m timeit -s 'from co import co; import numpy as np; a = np.random.random(100000)' 'co(a, .5, .7)'
10 loops, best of 3: 120 msec per loop

while annotating it with Pythran, as in:

#pythran export co(float[], float, float)
def co(amplitude, att_coeff, rel_coeff):
    # filter state
    state = 0

    # perform filtering
    for sample in amplitude:
        if sample == 1.0: # attack filter
            state = (1.0 - att_coeff) * sample + att_coeff * state
        else:             # release filter
            state = (1.0 - rel_coeff) * sample + rel_coeff * state
    return state

and compiling it with

$ pythran co.py

gives me:

$ python -m timeit -s 'from co import co; import numpy as np; a = np.random.random(100000)' 'co(a, .5, .7)' 
1000 loops, best of 3: 253 usec per loop

That's roughly a x470 speedup! I expect Numba and Cython to give similar speedups.

Each entry requires the previous entry, and the previous entry must be calculated before you can calculate the current entry. So each entry must be calculated in series, and you cannot do it in a vectorized (ie mapped, parallel) manner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM