java 移动其他窗口_移动窗口平均值不等

TL;DR: 无论如何我可以摆脱我的第二个 for -loop?

我在2D网格上有一系列时间点 . 为了消除它们位置的快速波动,我在一个帧窗口上平均坐标 . 现在在我的情况下,它想要包含特定点的帧,如果它的行程比 cut_off 值更远 .

在第一个 for -loop中,我遍历所有帧并定义移动窗口 . 然后,我计算当前帧与移动窗口中每个帧之间的距离 . 在我从所有帧中仅抓取那些位置后, x 和 y 组件的行程都没有超过 cut_off . 现在我想计算移动窗口所有这些选定帧中每个点的平均位置( note: 所选帧的数量可以小于 n_window ) . 这导致我第二个 for -loop . 在这里,我迭代所有点并实际 grab 帧中的位置,其中当前点没有比 cut_off 传播更远 . 从这些选定的帧中,我计算坐标的平均值,并将其用作当前帧的新值 .

这最后 for -loop减慢了整个处理过程 . 我无法想出一个更好的方法来完成这个计算 . 有什么建议?

MWE

提出评论以澄清 .

import numpy as np

# Generate a timeseries with 1000 frames, each

# containing 50 individual points defined by their

# x and y coordinates

n_frames = 1000

n_points = 50

n_coordinates = 2

timeseries = np.random.randint(-100, 100, [n_frames, n_points, n_coordinates])

# Set window size to 20 frames

n_window = 20

# Distance cut off

cut_off = 60

# Set up empty array to hold results

avg_data_store = np.zeros([n_frames, timeseries.shape[1], 2])

# Iterate over all frames

for frame in np.arange(0, n_frames):

# Set the frame according to the window size that we're looking at

t_before = int(frame - (n_window / 2))

t_after = int(frame + (n_window / 2))

# If we're trying to access frames below 0, set the lowest one to 0

if t_before < 0:

t_before = 0

# Trying to access frames that are not in the trajectory, set to last frame

if t_after > n_frames - 1:

t_after = n_frames - 1

# Grab x and y coordinates for all points in the corresponding window

pos_before = timeseries[t_before:frame]

pos_after = timeseries[frame + 1:t_after + 1]

pos_now = timeseries[frame]

# Calculate the distance between the current frame and the windows before/after

d_before = np.abs(pos_before - pos_now)

d_after = np.abs(pos_after - pos_now)

# Grab indices of frames+points, that are below the cut off

arg_before = np.argwhere(np.all(d_before < cut_off, axis=2))

arg_after = np.argwhere(np.all(d_after < cut_off, axis=2))

# Iterate over all points

for i in range(0, timeseries.shape[1]):

# Create temp array

temp_stack = pos_now[i]

# Grab all frames in which the current point did _not_

# travel farther than `cut_off`

all_before = arg_before[arg_before[:, 1] == i][:, 0]

all_after = arg_after[arg_after[:, 1] == i][:, 0]

# Grab the corresponding positions for this points in these frames

all_pos_before = pos_before[all_before, i]

all_pos_after = pos_after[all_after, i]

# If we have any frames for that point before / after

# stack them into the temp array

if all_pos_before.size > 0:

temp_stack = np.vstack([all_pos_before, temp_stack])

if all_pos_after.size > 0:

temp_stack = np.vstack([temp_stack, all_pos_after])

# Calculate the moving window average for the selection of frames

avg_data_store[frame, i] = temp_stack.mean(axis=0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值