简体   繁体   English

傅里叶变换结果中的意外相移 (np.fft)

[英]Unexpected phase shift in the results of Fourier transform (np.fft)

I'm looking for a clarification of Fourier transform principles.我正在寻找傅里叶变换原理的说明。

I try to do something quite simple: Create a signal (sine wave with a given frequency and phase shift) and recreate its params with Fourier transform.我尝试做一些非常简单的事情:创建一个信号(具有给定频率和相移的正弦波)并使用傅立叶变换重新创建其参数。 Frequency estimations work fine, but when it comes to phase, it looks like I get systematic shift (-pi/2).频率估计工作正常,但是当涉及到相位时,看起来我得到了系统偏移 (-pi/2)。

import numpy as np
import matplotlib.pyplot as plt

duration = 4.0 # lenght of window (in sec)
ticks_per_sec = 400.0 # sampling interval 
samples = int(ticks_per_sec*duration)

phase_shift = np.pi / 2 # sin wave shift in angle
freq = 1 # sine wave freq in Hz

phase_shift  = round(ticks_per_sec/freq * phase_shift/(2*np.pi)) # angle value translated to no of ticks
t = np.arange(phase_shift, samples+phase_shift) / ticks_per_sec

s =  1 * np.sin(2.0 * np.pi * freq * t)
N = s.size

fig, axs = plt.subplots(1, 3, figsize=(18, 6))
axs[0].grid(True)
axs[0].set_ylabel("Amplitude")
axs[0].set_xlabel("Time [s]")
axs[0].set_title(f"F: {freq}, Phase shift: {phase_shift} ticks.")
axs[0].plot(np.arange(samples)/ticks_per_sec, s) 

f = np.linspace(0, ticks_per_sec, N)
fft = np.fft.fft(s)
peak_pos = np.argmax(np.abs(fft[:N//2])) 

axs[1].set_ylabel("Amplitude")
axs[1].set_xlabel("Frequency [Hz]")
axs[1].set_title(f"Peak bar: {peak_pos}")
barlist = axs[1].bar(f[:N // 2], np.abs(fft)[:N // 2] * (1 / (N//2)), width=1.5)  # 1 / N is a normalization factor
barlist[peak_pos].set_color('r')


axs[2].set_ylabel("Angle")
axs[2].set_xlabel("Frequency [Hz]")
axs[2].set_title(f"Peak angle: {np.angle(fft[peak_pos])}")
barlist = axs[2].bar(f[:N // 2], np.angle(fft)[:N // 2], width=1.5) 
barlist[peak_pos].set_color('r')

fig.show()

Plotted Results of the code above上面代码的绘制结果

Please help me if there's a bug in my code that I can't notice, or I misunderstand something.如果我的代码中有我无法注意到的错误,或者我误解了什么,请帮助我。

Thank you in advance.先感谢您。

Your code is just fine, this is not a programming issue.您的代码很好,这不是编程问题。

Let's recall that a sine wave can be expressed as a cosine wave with a phase shift (or vice versa), now remember that sine function as an inherent phase shift of -pi/2 in real Fourier basis relatively to cosine.让我们回想一下,正弦波可以表示为具有相移的余弦波(反之亦然),现在记住正弦函数是实傅立叶基中相对于余弦的 -pi/2 的固有相移。

This means that your code should output a pi/2 phase angle when replacing np.sin by np.cos , ie returns input phase_shift , or equivalently, returns a phase angle of zero when specifying phase_shift = np.pi / 2 , ie phase shift and sine phase compensate each other.这意味着您的代码应输出更换时的pI / 2相位角np.sinnp.cos返回输入phase_shift ,或等效地,指定当返回零相位角phase_shift = np.pi / 2phase shift和正弦相位相互补偿。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM