python - 修复 Numpy 中的相位展开错误

标签 python numpy phase

我有一系列展开的阶段,有一些展开错误,包括 +/- Pi 的倍数的跳跃:

import numpy
a = numpy.array([0.5, 1.3, 2.4, 10.3, 10.8, 10.2, 7.6, 3.2, 2.9])

在此示例中,在 2.4 和 10.3 之间有第一个 2 个周期的跳跃,在 7.6 和 3.2 之间有一个 -1 个周期的跳跃。我想删除跳跃。问题是,当您删除一个跳跃时,您需要相应地增加或减少系列的其余部分,而不仅仅是跳跃发生的值。

是否有更简洁的方法(无循环/循环更少,速度更快):

jumpsexist = 1
while jumpsexist:
    # Look for absolute differences greater than Pi
    jump = numpy.abs((numpy.roll(a,-1) -a)) > numpy.pi
    if jump[:-1].any():
        # Find the index of the first jump
        jumpind = numpy.argmax(jump) + 1
        # Calculate the number of cycles in that jump
        cycles = ((a[jumpind] - a[jumpind- 1]) / numpy.pi).astype("Int8")
        # Remove the cycles
        a[jumpind:] -= cycles * numpy.pi
    else:
        break

最佳答案

NumPy 提供函数 numpy.unwrap() 用于相位展开。使用默认参数值,它将校正以 2π 为模的相位数组,使得所有跳跃都小于或等于 π:

>>> a = numpy.array([0.5, 1.3, 2.4, 10.3, 10.8, 10.2, 7.6, 3.2, 2.9])
>>> numpy.unwrap(a)
array([ 0.5       ,  1.3       ,  2.4       ,  4.01681469,  4.51681469,
        3.91681469,  1.31681469,  3.2       ,  2.9       ])

关于python - 修复 Numpy 中的相位展开错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10126125/

相关文章:

python - 过滤数据框并根据给定条件添加新列

fft - 如何将 "zero-phase-adjust"DFT输出?

python - 如何从 Python 向 GTK 的 "recently used"文件列表中添加一个项目?

python - Django:在循环中保存查询集项是否有效?

python - 标记数据框的索引

python - 在 Python 中将 click.progressbar 与多处理一起使用

python - 如何更新数据框给出元组列表和生成器

plugins - 如何在 Maven2 构建中执行有序任务

phase - 什么是相位展开以及为什么需要它

python - 绘图破折号,带有 2 个按钮输入和一个下拉输入的回调