Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python np.angle返回的相位不准确 我正在生成两个正弦波,第一个是基频=50赫兹,振幅=10,相位=0,第二个是基频=100赫兹,振幅=5,相位=np.pi/6(30度) 然后我将它们相加,并对添加的信号执行FFT 我使用np.abs() 我分别计算50赫兹和100赫兹时信号的相位 使用np.angle() 这就是我得到的结果_Python_Numpy_Signal Processing_Fft_Phase - Fatal编程技术网

Python np.angle返回的相位不准确 我正在生成两个正弦波,第一个是基频=50赫兹,振幅=10,相位=0,第二个是基频=100赫兹,振幅=5,相位=np.pi/6(30度) 然后我将它们相加,并对添加的信号执行FFT 我使用np.abs() 我分别计算50赫兹和100赫兹时信号的相位 使用np.angle() 这就是我得到的结果

Python np.angle返回的相位不准确 我正在生成两个正弦波,第一个是基频=50赫兹,振幅=10,相位=0,第二个是基频=100赫兹,振幅=5,相位=np.pi/6(30度) 然后我将它们相加,并对添加的信号执行FFT 我使用np.abs() 我分别计算50赫兹和100赫兹时信号的相位 使用np.angle() 这就是我得到的结果,python,numpy,signal-processing,fft,phase,Python,Numpy,Signal Processing,Fft,Phase,返回的震级分别非常接近10和5。但相位不是0度和30度 我还尝试了其他方法,比如math.atan2和cmath.phase,它提供了类似的结果 我想了解我的相位计算有什么问题。我的代码如下 def sine_wave(amplitude1: Union[int, float], amplitude2: Union[int, float], phase1: float, phase2: float, duration: Union[int, float],fund_freq_1: int, fu

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

我还尝试了其他方法,比如
math.atan2
cmath.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)

在执行FFT之后,复值的相位对应于具有余弦的相对相位。由于
cos(x)
sin(x)
存在90度相位差,因此您应该期望在相同频率下,以-90度的相位检测0度相位
sin
。类似地,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

请不要重复发帖:同样的问题也贴在这里,我在这个问题上问了一些疑问,在“进一步的疑问”下面,你能检查一下吗?
phase_one = degrees(np.angle(fft[index_one])) + 90
phase_two = degrees(np.angle(fft[index_two])) + 90