在我调用函数后,有人能解释为什么y的所有元素都等于零吗?
import matplotlib.pyplot as plt
import numpy as np
def hilbertImpulseResponse(k):
if k == 0:
return 0
else:
return ((2/(np.pi*k)) * (np.sin((np.pi*k)/2)) * (np.sin((np.pi*k)/2)))
size = 5
n = np.arange(-size, size + 1)
y = np.arange(-size, size + 1)
for i in np.arange(-size, size + 1):
y[i] = hilbertImpulseResponse(i)
print(y)
提前感谢
np.arange
正在将dtype
推断为np.int64
。所以所有的价值观都被打破了!
尝试指定dtype
:
import matplotlib.pyplot as plt
import numpy as np
def hilbertImpulseResponse(k):
if k == 0:
return 0
else:
return ((2/(np.pi*k)) * (np.sin((np.pi*k)/2)) * (np.sin((np.pi*k)/2)))
size = 5
n = np.arange(-size, size + 1, dtype=np.float64)
y = np.arange(-size, size + 1, dtype=np.float64)
for i in np.arange(-size, size + 1):
y[i] = hilbertImpulseResponse(i)
print(y)
您希望如何在剩下的过程中使用y
?最好将这些值存储在标准Pythondict
:中
import matplotlib.pyplot as plt
import numpy as np
def hilbertImpulseResponse(k):
if k == 0:
return 0
else:
return ((2/(np.pi*k)) * (np.sin((np.pi*k)/2)) * (np.sin((np.pi*k)/2)))
size = 5
y = {i:hilbertImpulseResponse(i) for i in range(-size, size + 1)}
print(y)
或者可能是双重的:
y = [(i,hilbertImpulseResponse(i)) for i in range(-size, size + 1)]
或者甚至在需要时仅生成CCD_ 7值。