Python 如何确定“A”的振幅和频率;“振荡”;弹道
这是行人在拥挤中移动的轨迹 正如你所看到的,他/她的头做了一个类似摆动的动作。所以不是一条完美的窦形曲线,也不是一条直线 是否可以为这条不规则曲线定义“振幅”和“频率” 更新: 到目前为止,我尝试了两种不同的方法:Python 如何确定“A”的振幅和频率;“振荡”;弹道,python,simulation,Python,Simulation,这是行人在拥挤中移动的轨迹 正如你所看到的,他/她的头做了一个类似摆动的动作。所以不是一条完美的窦形曲线,也不是一条直线 是否可以为这条不规则曲线定义“振幅”和“频率” 更新: 到目前为止,我尝试了两种不同的方法: 第一种是基于计算转折点,然后用它们生成样条曲线 第二种方法基于Ramer-Douglas-Peucker算法的建议并使用该算法 结果如下: 我看到RDP的问题是自由参数dist。低值(意味着更多细节)意味着结果轨迹中的振荡。所以我必须小心这个参数。此外,splined曲线更加平滑
dist
。低值(意味着更多细节)意味着结果轨迹中的振荡。所以我必须小心这个参数。此外,splin
ed曲线更加平滑
你怎么看?没有绝对正确的方法来做到这一点。下面只是一种方法 让我们从一个假设/主张开始,即人们通常希望走直线。因此,您可以使用一组小线段来估计人的预期路径。(有一个。) 然后生成
这个距离数组是一个时间序列。然后,您可以使用时间序列的频率作为振幅的度量,并使用a来查找其主频(或多个频率)。以下是您链接到的RDP模块的更新,该模块应使用向量与Python 3.x配合使用:
import functools
def autocast(function):
@functools.wraps(function)
def wrapper(self, other):
if isinstance(other, self.__class__):
if len(other) != len(self):
raise ValueError('Object dimensions are not equivalent!')
return function(self, other)
return function(self, self.__class__(size=len(self), init=other))
return wrapper
class Vector:
__slots__ = '__data'
def __init__(self, *args, size=0, init=0):
self.__data = list(map(float, args if args else [init] * size))
def __repr__(self):
return self.__class__.__name__ + repr(tuple(self))
@autocast
def __cmp__(self, other):
return (self.__data > other.__data) - (self.__data < other.__data)
def __lt__(self, other):
return self.__cmp__(other) < 0
def __le__(self, other):
return self.__cmp__(other) <= 0
def __eq__(self, other):
return self.__cmp__(other) == 0
def __ne__(self, other):
return self.__cmp__(other) != 0
def __gt__(self, other):
return self.__cmp__(other) > 0
def __ge__(self, other):
return self.__cmp__(other) >= 0
def __bool__(self):
return any(self)
def __len__(self):
return len(self.__data)
def __getitem__(self, key):
return self.__data[key]
def __setitem__(self, key, value):
self.__data[key] = float(value)
def __delitem__(self, key):
self[key] = 0
def __iter__(self):
return iter(self.__data)
def __reversed__(self):
return reversed(self.__data)
def __contains__(self, item):
return item in self.__data
@autocast
def __add__(self, other):
return Vector(*(a + b for a, b in zip(self, other)))
@autocast
def __sub__(self, other):
return Vector(*(a - b for a, b in zip(self, other)))
@autocast
def __mul__(self, other):
return Vector(*(a * b for a, b in zip(self, other)))
@autocast
def __truediv__(self, other):
return Vector(*(a / b for a, b in zip(self, other)))
@autocast
def __floordiv__(self, other):
return Vector(*(a // b for a, b in zip(self, other)))
@autocast
def __mod__(self, other):
return Vector(*(a % b for a, b in zip(self, other)))
@autocast
def __divmod__(self, other):
result = tuple(divmod(a, b) for a, b in zip(self, other))
return Vector(*(a for a, b in result)), Vector(*(b for a, b in result))
@autocast
def __pow__(self, other):
return Vector(*(a ** b for a, b in zip(self, other)))
@autocast
def __radd__(self, other):
return Vector(*(a + b for a, b in zip(other, self)))
@autocast
def __rsub__(self, other):
return Vector(*(a - b for a, b in zip(other, self)))
@autocast
def __rmul__(self, other):
return Vector(*(a * b for a, b in zip(other, self)))
@autocast
def __rtruediv__(self, other):
return Vector(*(a / b for a, b in zip(other, self)))
@autocast
def __rfloordiv__(self, other):
return Vector(*(a // b for a, b in zip(other, self)))
@autocast
def __rmod__(self, other):
return Vector(*(a % b for a, b in zip(other, self)))
@autocast
def __rdivmod__(self, other):
result = tuple(divmod(a, b) for a, b in zip(other, self))
return Vector(*(a for a, b in result)), Vector(*(b for a, b in result))
@autocast
def __rpow__(self, other):
return Vector(*(a ** b for a, b in zip(other, self)))
@autocast
def __iadd__(self, other):
for key in range(len(self)):
self[key] += other[key]
return self
@autocast
def __isub__(self, other):
for key in range(len(self)):
self[key] -= other[key]
return self
@autocast
def __imul__(self, other):
for key in range(len(self)):
self[key] *= other[key]
return self
@autocast
def __itruediv__(self, other):
for key in range(len(self)):
self[key] /= other[key]
return self
@autocast
def __ifloordiv__(self, other):
for key in range(len(self)):
self[key] //= other[key]
return self
@autocast
def __imod__(self, other):
for key in range(len(self)):
self[key] %= other[key]
return self
@autocast
def __ipow__(self, other):
for key in range(len(self)):
self[key] **= other[key]
return self
def __neg__(self):
return Vector(*(-value for value in self))
def __pos__(self):
return Vector(*(+value for value in self))
def __abs__(self):
return Vector(*(abs(value) for value in self))
def __get_magnitude(self):
return sum(value ** 2 for value in self) ** 0.5
def __set_magnitude(self, value):
self *= value / self.magnitude
magnitude = property(__get_magnitude, __set_magnitude)
###############################################################################
def point_line_distance(point, start, end):
if start == end:
return (point - start).magnitude
es, sp = end - start, start - point
return abs(es[0] * sp[1] - es[1] * sp[0]) / es.magnitude
def rdp(points, epsilon):
dmax = index = 0
start, *middle, end = points
for i in range(1, len(points) - 1):
d = point_line_distance(points[i], start, end)
if d > dmax:
index, dmax = i, d
if dmax >= epsilon:
return rdp(points[:index+1], epsilon)[:-1] + \
rdp(points[index:], epsilon)
return start, end
谢谢你的提示。我将尝试使用RDP,看看会发生什么。当我点击带有实现的链接时,一个窗口会弹出一个名为“下载”的下载窗口。你能检查一下链接吗?谢谢你指出断开的链接。我已将其替换为指向同一代码的github链接。我想问您答案的第二部分(然后生成真实数据点与线段的距离)。您的意思是:对于所有点:对于所有线段:计算距离吗?@Tengis:您使用样条线的想法看起来不错。现在,下一步是计算每个数据点到样条曲线(或RDP线段)的距离。计算到直线的距离并不难。代码在RDP实现中。对于样条曲线,可以使用一组线段近似样条曲线,然后使用相同的方法。文件“RDP.py”,第17行def uu init uu(self,*args,size=0,init=0):^SyntaxError:无效语法。运行此代码是否需要python 3x?是的,对此表示抱歉。或者,您可以将
向量
命名为点
或任何适合您的名称。我不知道问题是否在于此类的名称。
import functools
def autocast(function):
@functools.wraps(function)
def wrapper(self, other):
if isinstance(other, self.__class__):
if len(other) != len(self):
raise ValueError('Object dimensions are not equivalent!')
return function(self, other)
return function(self, self.__class__(size=len(self), init=other))
return wrapper
class Vector:
__slots__ = '__data'
def __init__(self, *args, size=0, init=0):
self.__data = list(map(float, args if args else [init] * size))
def __repr__(self):
return self.__class__.__name__ + repr(tuple(self))
@autocast
def __cmp__(self, other):
return (self.__data > other.__data) - (self.__data < other.__data)
def __lt__(self, other):
return self.__cmp__(other) < 0
def __le__(self, other):
return self.__cmp__(other) <= 0
def __eq__(self, other):
return self.__cmp__(other) == 0
def __ne__(self, other):
return self.__cmp__(other) != 0
def __gt__(self, other):
return self.__cmp__(other) > 0
def __ge__(self, other):
return self.__cmp__(other) >= 0
def __bool__(self):
return any(self)
def __len__(self):
return len(self.__data)
def __getitem__(self, key):
return self.__data[key]
def __setitem__(self, key, value):
self.__data[key] = float(value)
def __delitem__(self, key):
self[key] = 0
def __iter__(self):
return iter(self.__data)
def __reversed__(self):
return reversed(self.__data)
def __contains__(self, item):
return item in self.__data
@autocast
def __add__(self, other):
return Vector(*(a + b for a, b in zip(self, other)))
@autocast
def __sub__(self, other):
return Vector(*(a - b for a, b in zip(self, other)))
@autocast
def __mul__(self, other):
return Vector(*(a * b for a, b in zip(self, other)))
@autocast
def __truediv__(self, other):
return Vector(*(a / b for a, b in zip(self, other)))
@autocast
def __floordiv__(self, other):
return Vector(*(a // b for a, b in zip(self, other)))
@autocast
def __mod__(self, other):
return Vector(*(a % b for a, b in zip(self, other)))
@autocast
def __divmod__(self, other):
result = tuple(divmod(a, b) for a, b in zip(self, other))
return Vector(*(a for a, b in result)), Vector(*(b for a, b in result))
@autocast
def __pow__(self, other):
return Vector(*(a ** b for a, b in zip(self, other)))
@autocast
def __radd__(self, other):
return Vector(*(a + b for a, b in zip(other, self)))
@autocast
def __rsub__(self, other):
return Vector(*(a - b for a, b in zip(other, self)))
@autocast
def __rmul__(self, other):
return Vector(*(a * b for a, b in zip(other, self)))
@autocast
def __rtruediv__(self, other):
return Vector(*(a / b for a, b in zip(other, self)))
@autocast
def __rfloordiv__(self, other):
return Vector(*(a // b for a, b in zip(other, self)))
@autocast
def __rmod__(self, other):
return Vector(*(a % b for a, b in zip(other, self)))
@autocast
def __rdivmod__(self, other):
result = tuple(divmod(a, b) for a, b in zip(other, self))
return Vector(*(a for a, b in result)), Vector(*(b for a, b in result))
@autocast
def __rpow__(self, other):
return Vector(*(a ** b for a, b in zip(other, self)))
@autocast
def __iadd__(self, other):
for key in range(len(self)):
self[key] += other[key]
return self
@autocast
def __isub__(self, other):
for key in range(len(self)):
self[key] -= other[key]
return self
@autocast
def __imul__(self, other):
for key in range(len(self)):
self[key] *= other[key]
return self
@autocast
def __itruediv__(self, other):
for key in range(len(self)):
self[key] /= other[key]
return self
@autocast
def __ifloordiv__(self, other):
for key in range(len(self)):
self[key] //= other[key]
return self
@autocast
def __imod__(self, other):
for key in range(len(self)):
self[key] %= other[key]
return self
@autocast
def __ipow__(self, other):
for key in range(len(self)):
self[key] **= other[key]
return self
def __neg__(self):
return Vector(*(-value for value in self))
def __pos__(self):
return Vector(*(+value for value in self))
def __abs__(self):
return Vector(*(abs(value) for value in self))
def __get_magnitude(self):
return sum(value ** 2 for value in self) ** 0.5
def __set_magnitude(self, value):
self *= value / self.magnitude
magnitude = property(__get_magnitude, __set_magnitude)
###############################################################################
def point_line_distance(point, start, end):
if start == end:
return (point - start).magnitude
es, sp = end - start, start - point
return abs(es[0] * sp[1] - es[1] * sp[0]) / es.magnitude
def rdp(points, epsilon):
dmax = index = 0
start, *middle, end = points
for i in range(1, len(points) - 1):
d = point_line_distance(points[i], start, end)
if d > dmax:
index, dmax = i, d
if dmax >= epsilon:
return rdp(points[:index+1], epsilon)[:-1] + \
rdp(points[index:], epsilon)
return start, end
>>> from pprint import pprint
>>> line = [Vector(0.0, 0.0),
Vector(1.0, 0.0),
Vector(2.0, 0.0),
Vector(2.0, 1.0),
Vector(2.0, 2.0),
Vector(1.0, 2.0),
Vector(0.0, 2.0),
Vector(0.0, 1.0),
Vector(0.0, 0.0)]
>>> pprint(rdp(line, 1.0))
(Vector(0.0, 0.0),
Vector(2.0, 0.0),
Vector(2.0, 2.0),
Vector(0.0, 2.0),
Vector(0.0, 0.0))
>>>