【问题标题】:Inaccurate phase returned by np.anglenp.angle 返回的相位不准确
【发布时间】:2021-04-04 01:16:49
【问题描述】:
  • 我正在生成 2 个正弦波,第一个具有基频 = 50 Hz,幅度=10,相位=0,第二个具有基频 = 100 Hz,幅度 = 5 和相位 =np.pi/6(即 30 度)。
  • 然后我将它们相加,并对相加的信号执行 FFT。
  • 我使用np.abs() 计算 50 Hz 和 100 Hz 信号的幅度
  • 我分别计算了 50 Hz 和 100 Hz 的信号相位 使用np.angle()
  • 这是我得到的结果
'magnitude_50 Hz': 9.997827675356993, 'phase_50 HZ': -89.0677734968239, 
'magnitude_150 Hz': 4.990392258900833, 'phase_150 HZ': -57.231981462145704,

返回的幅度分别非常接近 10 和 5。但相位不是0度和30度。

我也尝试了其他方法,例如 math.atan2cmath.phase,它提供了类似的结果。

我想了解我的相位计算出了什么问题。我的代码如下。

def sine_wave(amplitude1: Union[int, float], amplitude2: Union[int, float], phase1: float, phase2: float, duration: Union[int, float],fund_freq_1: int, fund_freq_2: int, samp_freq: int) -> dict:
  
  # generating the time domain signal

  t = np.linspace(0, duration, int(samp_freq * duration))
  wave1 = amplitude1 * np.sin((2 * np.pi * fund_freq_1 * t)+phase1)
  wave2 = amplitude2 * np.sin((2 * np.pi * fund_freq_2 * t)+phase2)
  combined_wave = np.add(wave1, wave2)
  N = combined_wave.size
  T = 1/samp_freq

  # DFT
  f = np.fft.fftfreq(N, 1 / samp_freq)
  fft = np.fft.fft(combined_wave)

  index_one = np.where(np.isclose(f, fund_freq_1))
  magnitude_one = np.mean(np.abs(fft[index_one]) * (2 / N))
  phase_one = degrees(np.angle(fft[index_one]))
  # phase_one = atan2(fft[index_one].imag, fft[index_one].real)
  # phase_one = degrees(phase(fft[index_one]))

  index_two = np.where(np.isclose(f, fund_freq_2))
  magnitude_two = np.mean(np.abs(fft[index_two]) * (2 / N))
  phase_two = degrees(np.angle(fft[index_two]))
  # phase_two = atan2(fft[index_two].imag, fft[index_one].real)
  # phase_two = degrees(phase(fft[index_two]))

  return {'magnitude_{} Hz'.format(fund_freq_1): magnitude_one,
          'phase_{} HZ'.format(fund_freq_1): phase_one,
          'magnitude_{} Hz'.format(fund_freq_2): magnitude_two,
          'phase_{} HZ'.format(fund_freq_2): phase_two}

代码可以这样运行

sine_wave(amplitude1=10, amplitude2=5, phase1=0, phase2=np.pi/6, duration=0.1, fund_freq_1=50, fund_freq_2=150, samp_freq=10000)

【问题讨论】:

标签: python numpy signal-processing fft phase


【解决方案1】:

在执行 FFT 之后,复数值的相位对应于具有 余弦 的相对相位。由于cos(x)sin(x) 有90 度的相位差,您应该期望您的0 度相位sin 在相同频率下相对于相应的cos 以-90 度的相位被检测到。同样,您的 30 度相位 sin 应该以 -60 度的相位检测到。您的结果值确实非常接近。

如果您希望获得以sin 信号为参考的相位,那么您可以简单地将np.angle 的结果加上90 度:

phase_one = degrees(np.angle(fft[index_one])) + 90
phase_two = degrees(np.angle(fft[index_two])) + 90

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-17
  • 2014-01-27
相关资源
最近更新 更多