在 numpy 中 - 如何通过将正弦曲线与正弦曲线和余弦曲线相关联来计算正弦曲线的相位和幅度?
In numpy - how do I compute phase and amplitude of a sinusoid by correlating it with a sine and cosine?
我有一个频率已知的传入正弦波。我知道可以通过计算它与正弦和余弦的相关性来计算它的相位和幅度。我将如何使用 numpy 执行此操作?
或者也许有更好的方法来做到这一点。查看 this here,但我不知道如何在 numpy 中进行计算。
我是一个麻木的菜鸟。我将不胜感激。
一种方便的方法是利用欧拉公式 e^(i phi) = cos phi + i sin phi
:
def get_cos_params(samples):
N = len(samples)
x = np.linspace(-np.pi, np.pi, N, endpoint=False)
template = np.exp(1j * x)
corr = 2 / N * template@samples
R = np.abs(corr)
phi = np.log(corr).imag
return R, phi
示例:
N = np.random.randint(10, 1000)
phi = np.random.uniform(-np.pi, np.pi)
R = np.random.uniform(0.1, 10)
x = np.linspace(-np.pi, np.pi, N, endpoint=False)
signal = R * np.cos(x-phi)
R_recon, phi_recon = get_cos_params(signal)
print(np.isclose(R, R_recon), np.isclose(phi, phi_recon))
# True True
我有一个频率已知的传入正弦波。我知道可以通过计算它与正弦和余弦的相关性来计算它的相位和幅度。我将如何使用 numpy 执行此操作?
或者也许有更好的方法来做到这一点。查看 this here,但我不知道如何在 numpy 中进行计算。
我是一个麻木的菜鸟。我将不胜感激。
一种方便的方法是利用欧拉公式 e^(i phi) = cos phi + i sin phi
:
def get_cos_params(samples):
N = len(samples)
x = np.linspace(-np.pi, np.pi, N, endpoint=False)
template = np.exp(1j * x)
corr = 2 / N * template@samples
R = np.abs(corr)
phi = np.log(corr).imag
return R, phi
示例:
N = np.random.randint(10, 1000)
phi = np.random.uniform(-np.pi, np.pi)
R = np.random.uniform(0.1, 10)
x = np.linspace(-np.pi, np.pi, N, endpoint=False)
signal = R * np.cos(x-phi)
R_recon, phi_recon = get_cos_params(signal)
print(np.isclose(R, R_recon), np.isclose(phi, phi_recon))
# True True