计算采样数据中符号变化之间的距离
Calculate the distance between the sign change in sampled data
我想恢复采样数据数组中的周期:
signal = [45, 46, -12, -12.5, 32, 35, 34, 25, 23, -23, -65, -3, 43, 23]
我想统计一个时期的大小。句点以正数开始,然后移动到负数,然后 returns 移动到正数。
例如:
period1=[45 46 -12 -12.5 32] # length=5
period2=[32 35 34 25 23 -23 -65 -3 43] # length=8
如何做到这一点?
这里的技巧是使用 numpy.diff() 两次。第一个 diff 可用于查找符号交叉点。第二个 diff 可用于查找这些交叉点之间的距离。
代码:
import numpy as np
# put the data into a numpy array
signal = np.array(
[45, 46, -12, -12.5, 0, 32, 35, 34, 25, 23, -23, -65, -3, 43, 23])
# consider zeros to be negative, since we are looking for return to positive
signal[np.where(signal == 0.0)] = -1e-100
# find any returns to positive
return_to_positive = 1 + np.where(2 == np.diff(np.sign(signal)))[0]
# the periods are the distance between `return to positives`
periods = np.diff(return_to_positive)
输出:
>>> print(periods)
[8]
魔法解释:
我们首先需要确保输出数据中没有零。这是因为我们想要 clean 过零。将任何零设置为刚好小于零,因为我们希望第一个正值是周期的开始。
# consider zeros to be negative, since we are looking for return to positive
signal[np.where(signal == 0.0)] = -1e-100
取信号符号,然后求差分。 2
处的任何位置都是信号从负变为正的地方。将 1
添加到索引中,因为之前的 diff
从数组中删除了一个元素。 (旁注,pandas
为您完成此操作)
# find the indices for any returns to positive
return_to_positive = 1 + np.where(2 == np.diff(np.sign(signal)))[0]
最后,计算每个零交叉点的指数之间的距离以获得周期。
# the periods are the distance between `return to positives`
periods = np.diff(return_to_positive)
我想恢复采样数据数组中的周期:
signal = [45, 46, -12, -12.5, 32, 35, 34, 25, 23, -23, -65, -3, 43, 23]
我想统计一个时期的大小。句点以正数开始,然后移动到负数,然后 returns 移动到正数。
例如:
period1=[45 46 -12 -12.5 32] # length=5
period2=[32 35 34 25 23 -23 -65 -3 43] # length=8
如何做到这一点?
这里的技巧是使用 numpy.diff() 两次。第一个 diff 可用于查找符号交叉点。第二个 diff 可用于查找这些交叉点之间的距离。
代码:
import numpy as np
# put the data into a numpy array
signal = np.array(
[45, 46, -12, -12.5, 0, 32, 35, 34, 25, 23, -23, -65, -3, 43, 23])
# consider zeros to be negative, since we are looking for return to positive
signal[np.where(signal == 0.0)] = -1e-100
# find any returns to positive
return_to_positive = 1 + np.where(2 == np.diff(np.sign(signal)))[0]
# the periods are the distance between `return to positives`
periods = np.diff(return_to_positive)
输出:
>>> print(periods)
[8]
魔法解释:
我们首先需要确保输出数据中没有零。这是因为我们想要 clean 过零。将任何零设置为刚好小于零,因为我们希望第一个正值是周期的开始。
# consider zeros to be negative, since we are looking for return to positive
signal[np.where(signal == 0.0)] = -1e-100
取信号符号,然后求差分。 2
处的任何位置都是信号从负变为正的地方。将 1
添加到索引中,因为之前的 diff
从数组中删除了一个元素。 (旁注,pandas
为您完成此操作)
# find the indices for any returns to positive
return_to_positive = 1 + np.where(2 == np.diff(np.sign(signal)))[0]
最后,计算每个零交叉点的指数之间的距离以获得周期。
# the periods are the distance between `return to positives`
periods = np.diff(return_to_positive)