如何使np.滚动工作更快的一维阵列?



我通过np生成两个零数组。0,那么我用np。滚动制作移位阵列。但是当我调用np时。滚动循环,它工作很慢。有什么方法来加快我的代码?下面是代码:

preamble_length = 256
threshold_level = 100
sample_rate = 750e3
decimation_factor = 6
preamble_combination = [1,-1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1,-1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1,  -1, 1, 1,1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1,1]
sequence = np.zeros(preamble_length)
buffer_filter = np.zeros(preamble_length)
size_array = sample_rate / decimation_factor

rxDataReal = np.real(downsample(rxData, decimation_factor)) #rxData is a array of complex numbers

rxDataDownSampled = rxDataReal
check = 0
find_max = 0
peak_max = 0
preamble_ready = 0
received_flag = False
size_array = int(size_array)
main_counter = 0
#In this section the np.roll working very slow
for main_counter in range(size_array):
if(preamble_ready == 0):
if(rxDataDownSampled[main_counter] < 0):
check_sign = 1
else:
check_sign = -1

sequence = np.roll(sequence, -1)#this
buffer_filter = np.roll(buffer_filter, -1)#and this
sequence[preamble_length-1] = check_sign
bufferSum = sequence * preamble_combination
buffer_filter[preamble_length-1] = np.sum(bufferSum)
find_max = np.max(buffer_filter)


if(find_max >= threshold_level):
peak_max = find_max
sequence = np.zeros(preamble_ready)
buffer_filter = np.zeros(preamble_length)
print('Value of peak_max: ', peak_max)
received_flag = True

if(received_flag==True):
break
preamble_value = peak_max

所以你的roll正在做:

In [118]: x=np.arange(10)
In [119]: np.roll(x,-1)
Out[119]: array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])

你可以看看np.roll代码;它可能更一般,它必须以某种方式,将x的所有值复制到一个新数组中。这可能会快一点,因为它不会像一般的那样:

In [120]: y=np.zeros_like(x)
...: y[:-1] = x[1:]; y[-1] = x[0]
In [121]: y
Out[121]: array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])

<<h2>倍/h2>不,它并不更快:

In [122]: x=np.arange(100000)
In [123]: timeit np.roll(x,-1)
82.1 µs ± 102 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
In [124]: %%timeit 
...: y=np.zeros_like(x)
...: y[:-1] = x[1:]; y[-1] = x[0]
...: 
...: 
93.4 µs ± 4.84 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

其他计时:

In [128]: timeit y=x[1:].copy()
52.4 µs ± 164 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
In [129]: timeit np.concatenate((x[1:],x[0:1]))
58.6 µs ± 289 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

最新更新