在Python中,我想要以给定大小的块计算信号的高/低状态(
samples_to_process
)。
所需的两个计算是上升沿之间的索引数 (
length_between_high_states
) 和信号为高电平的索引数 (high_state_length
)。
计算必须跨块有状态。
让我们举一个可重现的小例子:
数据 = np.array([0,1,1,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,0,0,0, 0,0,0,0,1,1,1,0,0,0,0,1,1,1,1,0,0])
如果这个数组一次读取8个项目,则第一次迭代是
高状态长度= 2 高状态之间的长度 = 4
然后
高状态长度= 3 高状态之间的长度 = 9
我相信我有正确的逻辑来读取数组的第一个状态和信号变化,但是信号中的后续状态变化以及跨块携带状态尚未实现:
import numpy as np
#total array size = 25
data = np.array([0,1,1,0,1,1,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,1,0,0])
#size of samples of data array to process in each read
samples_to_process = 8
samples = np.zeros(samples_to_process, dtype=np.complex64)
threshold = 0.5
index = 0
#slice(index, index+samples_to_process)
for index in range(0, data.size, samples_to_process):
samples=data[index:index+samples_to_process]
print(samples)
while True
start_index = np.argmax(samples > threshold)
stop_index = np.argmax(samples[start_index:] < threshold) + start_index
next_start_index = np.argmax(samples[stop_index:] > threshold) + stop_index
length_between_high_states = next_start_index - start_index
high_state_length = stop_index - start_index
# how to calculate remainder state and pass into next itr
print("next loop")
问题是如何在迭代之间传递信号状态以包含在后续计算中。
您只需要跟踪最后一个上升沿以及最后一个样本的状态。然后,您会在每个上升沿和每个下降沿获得一个事件。
import numpy as np
#total array size = 25
data = np.array([0,1,1,0,1,1,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,1,0,0])
#size of samples of data array to process in each read
samples_to_process = 8
threshold = 0.5
last = 0
lastrise = 0
for index in range(0, data.size, samples_to_process):
samples=data[index:index+samples_to_process]
for j,n in enumerate(samples):
i = index + j
# Is this a rising edge?
if n and not last:
if lastrise:
print('since last rising edge:', i-lastrise)
lastrise = i
# Is this a falling edge?
elif not n and last:
print('pulse width', i-lastrise)
last = n