不同阵列的 Numpy Sinus 行为

Numpy Sinus Behavior for different arrays

你好,根据我放入 sinus 的阵列,我得到完全不同的输出。 test1、test3 是它不起作用的示例。这是怎么回事?

test1 = np.sin(50.*2.*np.pi*np.arange(0., 512., dtype=np.float64))
test2 = np.sin(50.*2.*np.pi*np.linspace(0., 512., 512, endpoint=True, dtype=np.float64))
test3 = np.sin(50.*2.*np.pi*np.linspace(0., 512., 512, endpoint=False, dtype=np.float64))

plt.plot(np.arange(0, 512), test1)
plt.plot(np.arange(0, 512), test2)
plt.plot(np.arange(0, 512), test3)
plt.show()

编辑: 好的,在做了一些进一步的研究之后,这是实际的问题:使用 test1 和 test3 我违反了奈奎斯特定理并且仅对零附近的值进行采样。要解决此问题,需要提高采样率或降低频率。

这不是窦性行为。我减小了大小并打印了您提供给 np.sin().

的数组
import numpy as np

SIZE = 4

print(np.arange(0., float(SIZE), dtype=np.float64))
print(np.linspace(0., float(SIZE), SIZE, endpoint=True, dtype=np.float64))
print(np.linspace(0., float(SIZE), SIZE, endpoint=False, dtype=np.float64))

区别现在很明显了。通过改变端点,你改变了值的边界,所以你改变了步骤,从而改变了所有的值。

[0. 1. 2. 3.]
[0. 1.33333333 2.66666667 4.]
[0. 1. 2. 3.]

所以正弦给出了不同的输出。

我认为简短的回答是您希望 numpy.sin 接受 degrees 中的角度作为参数,但文档指定它接受 radians.

看起来数字正在按预期绘制。可视化所有三个图(即 test1、test2 和 test3)的一种方法是使用子图:

import numpy as np
import matplotlib.pyplot as plt

test1 = np.sin(50.*2.*np.pi*np.arange(0., 512., dtype=np.float64))
test2 = np.sin(50.*2.*np.pi*np.linspace(0., 512., 512, endpoint=True, dtype=np.float64))
test3 = np.sin(50.*2.*np.pi*np.linspace(0., 512., 512, endpoint=False, dtype=np.float64))

fig, axs = plt.subplots(3, sharex=True, sharey=True, gridspec_kw={'hspace': 0})

axs[0].plot(np.arange(0, 512), test1)
axs[1].plot(np.arange(0, 512), test2)
axs[2].plot(np.arange(0, 512), test3)
plt.show()

当您执行以下操作时:

print(f"Max of test1: {max(test1)}\nMin of test1: {min(test1)}")
print(f"Max of test2: {max(test2)}\nMin of test2: {min(test2)}")
print(f"Max of test3: {max(test3)}\nMin of test3: {min(test3)}")

输出

Max of test1: 1.4412955306804755e-11
Min of test1: -1.2978086425591747e-11
Max of test2: 0.9999952753720377
Min of test2: -0.9999952753719793
Max of test3: 1.4412955306804755e-11
Min of test3: -1.2978086425591747e-11

可能的解决方案

对我来说,问题看起来是图表上的 y 限制在 -1 到 1 之间,这太高以至于无法(清晰地)可视化 test1 和 test3。如果您想更详细地查看 test1 和 test3(在图表上),您可以这样做:

test1 = np.sin(50.*2.*np.pi*np.arange(0., 512., dtype=np.float64))
test2 = np.sin(50.*2.*np.pi*np.linspace(0., 512., 512, endpoint=True, dtype=np.float64))
test3 = np.sin(50.*2.*np.pi*np.linspace(0., 512., 512, endpoint=False, dtype=np.float64))

fig, axs = plt.subplots(3, sharex=True, sharey=True, gridspec_kw={'hspace': 0})

axs[0].set_ylim((min(test1), max(test1)))
axs[0].plot(np.arange(0, 512), test1)
axs[1].plot(np.arange(0, 512), test2)
axs[2].set_ylim((min(test1), max(test1)))
axs[2].plot(np.arange(0, 512), test3)
plt.show()

补充说明

根据 numpy.sindocumentation,作为 x 的参数是 radians 中的角度,不要与 degrees 混淆。

numpy.linspace documentation 的另一点是

Note that the step size changes when endpoint is False.

这是一个简单的例子:

np.linspace(0., 512., 5, endpoint=True, dtype=np.float64)

输出

array([  0., 128., 256., 384., 512.])  

np.linspace(0., 512., 5, endpoint=False, dtype=np.float64)

输出

array([  0. , 102.4, 204.8, 307.2, 409.6])

现在,如果快速检查 numpy.sin 每个数组中的最大值,即 512 和 409.6

np.sin(409.6)

输出

0.9294631796005904

np.sin(512)

输出

0.07951849401287635

因此,差异。