你好,取决于我放入窦的阵列,我得到一个完全不同的输出。 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()
其他说明
[documentation的numpy.sin
,它作为x的参数是radians
中的角度,请勿与degrees
混淆。
numpy.linspace
documentation要注意的另一点是
请注意,当端点为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])
现在,如果要快速检查每个数组中最高值(即512和409.6)的numpy.sin
,则>]
np.sin(409.6)
输出
0.9294631796005904
和
np.sin(512)
输出
0.07951849401287635
因此,区别。