首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >np.angle返回的不准确相位

np.angle返回的不准确相位
EN

Stack Overflow用户
提问于 2020-12-26 15:12:49
回答 1查看 370关注 0票数 1

30degrees).

  • Then i产生2个正弦波,第一个为基频= 50 Hz,amplitude=10,phase=0,第二个为基频= 100 Hz,幅值=5,相位= np.pi/6 (即np.abs()

  • I i将它们相加,并对所加的信号进行快速傅立叶变换),

  • i利用np.abs()

  • I计算信号在50 Hz和100 Hz时的幅值,用np.angle()

  • This计算信号在50 Hz和100 Hz的相位,得到

代码语言:javascript
复制
'magnitude_50 Hz': 9.997827675356993, 'phase_50 HZ': -89.0677734968239, 
'magnitude_150 Hz': 4.990392258900833, 'phase_150 HZ': -57.231981462145704,

返回的震级分别接近10级和5级。但这个阶段不是零度和三十度。

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

我想了解一下我的相位计算有什么问题。我的密码在下面。

代码语言:javascript
复制
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}

代码可以像这样运行

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-26 18:18:51

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

如果您希望获得sin信号所引用的相位,则只需将np.angle的结果增加90度即可。

代码语言:javascript
复制
phase_one = degrees(np.angle(fft[index_one])) + 90
phase_two = degrees(np.angle(fft[index_two])) + 90
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65457765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档