使用scipy的方波采样不正确

时间:2016-07-08 12:43:40

标签: python numpy matplotlib scipy signal-processing

试图绘制以4Hz采样的2Hz方波(见下图)。为什么我没有得到样本环绕红色的均匀性,如果IMO在-1处标记为第二个红色箭头:

enter image description here

这是我的python代码(假设ipython shell和导入的numpy(作为np),scipy(信号)和matplotlib(plt))

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?sitea\.com$
RewriteRule ^ http://siteb.com%{REQUEST_URI} [NE,L,R]

1 个答案:

答案 0 :(得分:4)

这是因为正常的浮点不精确。您试图在发生跳跃的点处对方波进行采样。这将对浮点数的不精确敏感。

您圈出的值为s2t4[11]t4[11]为2.75。看看:

In [15]: signal.square(2.0*np.pi*2.0*t4[11])
Out[15]: array(1.0)

在参数中添加一小部分,然后得到-1:

In [16]: signal.square(2.0*np.pi*2.0*t4[11] + 1e-14)
Out[16]: array(-1.0)

如果您移动t4以使样本远离不连续点,则应该可以正常工作。

相关问题