Phi=np.random.randn(256*sampleRate,256) File "mtrand.pyx", line 1286, in numpy.random.mtrand.RandomState.randn File "mtrand.pyx", line 1447, in numpy.random.mtrand.RandomState.standard_normal File "_common.pyx", line 636, in numpy.random._common.cont TypeError: 'float' object cannot be interpreted as an integer
时间: 2024-04-22 18:27:56 浏览: 306
这个错误是由于`np.random.randn()`方法中的参数被解释为浮点数而不是整数引起的。这个方法的参数应该是整数,用于指定所生成随机数的形状。请确保`256*sampleRate`是一个整数值。
你可以尝试使用`int()`函数来将`256*sampleRate`转换为整数值,例如:
```python
Phi = np.random.randn(int(256*sampleRate), 256)
```
这样应该就能解决这个问题了。
相关问题
优化这段import numpy as np import matplotlib.pyplot as plt %config InlineBackend.figure_format='retina' def generate_signal(t_vec, A, phi, noise, freq): Omega = 2*np.pi*freq return A * np.sin(Omega*t_vec + phi) + noise * (2*np.random.random def lock_in_measurement(signal, t_vec, ref_freq): Omega = 2*np.pi*ref_freq ref_0 = 2*np.sin(Omega*t_vec) ref_1 = 2*np.cos(Omega*t_vec) # signal_0 = signal * ref_0 signal_1 = signal * ref_1 # X = np.mean(signal_0) Y = np.mean(signal_1) # A = np.sqrt(X**2+Y**2) phi = np.arctan2(Y,X) print("A=", A, "phi=", phi) # t_vec = np.linspace(0, 0.2, 1001) A = 1 phi = np.pi noise = 0.2 ref_freq = 17.77777 # signal = generate_signal(t_vec, A, phi, noise, ref_freq) # lock_in_measurement(signal, t_vec, ref_freq)
import numpy as np
import matplotlib.pyplot as plt
%config InlineBackend.figure_format='retina'
def generate_signal(t_vec, A, phi, noise, freq):
Omega = 2*np.pi*freq
return A * np.sin(Omega*t_vec + phi) + noise * (2*np.random.random)
def lock_in_measurement(signal, t_vec, ref_freq):
Omega = 2*np.pi*ref_freq
ref_0 = 2*np.sin(Omega*t_vec)
ref_1 = 2*np.cos(Omega*t_vec)
signal_0 = signal * ref_0
signal_1 = signal * ref_1
X = np.mean(signal_0)
Y = np.mean(signal_1)
A = np.sqrt(X**2+Y**2)
phi = np.arctan2(Y,X)
print("A=", A, "phi=", phi)
t_vec = np.linspace(0, 0.2, 1001)
A = 1
phi = np.pi
noise = 0.2
ref_freq = 17.77777
signal = generate_signal(t_vec, A, phi, noise, ref_freq)
lock_in_measurement(signal, t_vec, ref_freq)
阅读全文