使用numpy fft提取相位信息 [英] extracting phase information using numpy fft

查看:725
本文介绍了使用numpy fft提取相位信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用快速傅立叶变换来提取单个正弦函数的相移.我知道在纸上,如果我们表示将函数转换为T,则我们具有以下关系:

I am trying to use a fast fourier transform to extract the phase shift of a single sinusoidal function. I know that on paper, If we denote the transform of our function as T, then we have the following relations:

但是,我发现虽然我能够准确捕获余弦波的频率,但是除非我以极高的速率采样,否则相位是不准确的.例如:

However, I am finding that while I am able to accurately capture the frequency of my cosine wave, the phase is inaccurate unless I sample at an extremely high rate. For example:

import numpy as np
import pylab as pl

num_t = 100000
t = np.linspace(0,1,num_t)
dt = 1.0/num_t
w = 2.0*np.pi*30.0
phase = np.pi/2.0

amp = np.fft.rfft(np.cos(w*t+phase))
freqs = np.fft.rfftfreq(t.shape[-1],dt)

print (np.arctan2(amp.imag,amp.real))[30]

pl.subplot(211)
pl.plot(freqs[:60],np.sqrt(amp.real**2+amp.imag**2)[:60])
pl.subplot(212)
pl.plot(freqs[:60],(np.arctan2(amp.imag,amp.real))[:60])
pl.show()

使用num = 100000点,我得到的相位为1.57173880459.

Using num=100000 points I get a phase of 1.57173880459.

使用num = 10000点,我得到的相位为1.58022110476.

Using num=10000 points I get a phase of 1.58022110476.

使用num = 1000点,我得到了1.6650441064的相位.

Using num=1000 points I get a phase of 1.6650441064.

出了什么问题?即使有1000点,我每个周期也有33点,这足以解决它.有没有办法增加计算的频率点的数量?有什么办法可以减少点数吗?

What's going wrong? Even with 1000 points I have 33 points per cycle, which should be enough to resolve it. Is there maybe a way to increase the number of computed frequency points? Is there any way to do this with a "low" number of points?

从进一步的实验看来,我每个周期需要约1000个点才能准确地提取一个相.为什么?!

from further experimentation it seems that I need ~1000 points per cycle in order to accurately extract a phase. Why?!

进一步的实验表明,准确性与每个循环的点数有关,而不是绝对数.增加每个周期的采样点数可以使相位更加准确,但是如果信号频率和采样点数都增加相同的倍数,则精度将保持不变.

EDIT 2: further experiments indicate that accuracy is related to number of points per cycle, rather than absolute numbers. Increasing the number of sampled points per cycle makes phase more accurate, but if both signal frequency and number of sampled points are increased by the same factor, the accuracy stays the same.

推荐答案

您的点在整个时间间隔内分布不均,末端的点数增加了一倍:01是同一点.显然,您获得的分数越多,重要性就越不重要,但仍然会出现一些错误.您可以完全避免它,linspace对此有一个标志.它还具有一个标志,可以直接将dt与数组一起返回.

Your points are not distributed equally over the interval, you have the point at the end doubled: 0 is the same point as 1. This gets less important the more points you take, obviusly, but still gives some error. You can avoid it totally, the linspace has a flag for this. Also it has a flag to return you the dt directly along with the array.

t, dt = np.linspace(0, 1, num_t, endpoint=False, retstep=True)

代替

t = np.linspace(0,1,num_t)
dt = 1.0/num_t

然后就可以了:)

这篇关于使用numpy fft提取相位信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆