使用 atan2 的 FFT 相位角 - 奇怪的行为。相移偏移?解开?

我正在测试和执行简单的 FFT,我对相移很感兴趣。 我用 10 个周期用正弦曲线

使用 atan2 的 FFT 相位角 - 奇怪的行为。相移偏移?解开?

生成了 256 个样本的简单数组。

我对这些样本执行 FFT 并接收复杂数据 (2x128)。 比我计算这些数据的大小和 FFT 看起来像预期的那样:

使用 atan2 的 FFT 相位角 - 奇怪的行为。相移偏移?解开?

然后我想从 fft 复数输出计算相移。我正在使用 atan2。 组合输出 fft_magnitude(蓝色)+ fft+phase(红色)如下所示:

使用 atan2 的 FFT 相位角 - 奇怪的行为。相移偏移?解开?

这几乎是我对“小”问题的期望。我知道这是回绕,但如果我想解开它,幅度峰值的相移读数为 36 度,我认为它应该为 0,因为我的输入正弦曲线根本没有移动。

如果我移动这个 -36 度(蓝色同相,红色移动,蓝色打印仅供参考),正弦曲线看起来像这样:

使用 atan2 的 FFT 相位角 - 奇怪的行为。相移偏移?解开?

如果我对这个红色数据执行 FFT,幅度 + 相位输出看起来像这样:

使用 atan2 的 FFT 相位角 - 奇怪的行为。相移偏移?解开?

所以很容易想象展开相位在放大倍数峰值处将接近于 0。 所以有 36 度偏移。但是如果我准备每 256 个样本 20 个周期和 0 相移

使用 atan2 的 FFT 相位角 - 奇怪的行为。相移偏移?解开?

的数据会发生什么

如果我然后执行 FFT,这是一个输出(幅度 + 相位):

使用 atan2 的 FFT 相位角 - 奇怪的行为。相移偏移?解开?

我可以告诉你是否会在 72 度处越过峰值点。所以现在有 72 度的偏移。

谁能告诉我为什么会这样? atan2() 相位输出与频率相关,偏移量为 2pi/cycles (360 deg/cycles) 是否正确? 如何解包并获得正确的结果(我找不到可用的 C 库来解包)。

这是在 ARM Cortex-M7 处理器(嵌入式)上运行的。

#define phaseShift 0
#define cycles 20

#include <arm_math.h>
#include <arm_const_structs.h>

float32_t phi = phaseShift * PI / 180; //phase shift in radians
float32_t data[256];  //input data for fft
float32_t output_buffer[256]; //output buffer from fft
float32_t phase_data[128];  //will contain atan2 values of output from fft (output values are complex)
float32_t magnitude[128];  //will contain absolute values of output from fft (output values are complex)
float32_t incrFactorRadians = cycles * 2 * PI / 255;

arm_rfft_fast_instance_f32 RealFFT_Instance;

void setup()
{
  Serial.begin(115200);
  delay(2000);
  arm_rfft_fast_init_f32(&RealFFT_Instance,256);   //initializing fft to be ready for 256 samples

  for (int i = 0; i < 256; i++)  //print sinusoids
  {
    data[i] = arm_sin_f32(incrFactorRadians * i + phi);
    Serial.print(arm_sin_f32(incrFactorRadians * i),8); Serial.print(","); Serial.print(data[i],8); Serial.print("\n"); //print reference in-phase sinusoid and shifted sinusoid (data for fft)
  }
  Serial.print("\n\n");
  delay(10000);

  arm_rfft_fast_f32(&RealFFT_Instance,data,output_buffer,0); //perform fft

  for (int i = 0; i < 128; i++) //calculate absolute values of an fft output (fft output is complex),and phase shift
  {
    magnitude[i] = output_buffer[i * 2] * output_buffer[i * 2] + output_buffer[(i * 2) + 1] * output_buffer[(i * 2) + 1];
    __ASM("VSQRT.F32 %0,%1" : "=t"(magnitude[i]) : "t"(magnitude[i])); //fast square root ARM DSP function

    phase_data[i] = atan2(output_buffer[i * 2],output_buffer[i * 2 +1]) * 180 / PI;
  }

}

void loop() //print magnitude of fft and phase output every 10 seconds 
{
  for (int i = 0; i < 128; i++)
  {
    Serial.print(magnitude[i],"); Serial.print(phase_data[i],8); Serial.print("\n");
  }
  
  Serial.print("\n\n");
  delay(10000);
}
zhongyi9927 回答:使用 atan2 的 FFT 相位角 - 奇怪的行为。相移偏移?解开?

如果正弦曲线在 FFT 的孔径宽度中恰好是整数周期,那么裸 FFT 加上 atan2() 只能正确测量输入正弦曲线的起始相位。

如果信号不完全是整数周期(某些其他频率),那么您必须通过执行 FFTshift(将数据旋转 N/2)来重新定位数据。然后 FFT 将正确测量原始数据中心的相位,远离 FFT 有限长度矩形窗口产生的圆形不连续性。

如果你想要数据中除中心以外的某个点的相位,你可以使用中心的频率和相位的估计来重新计算其他位置的相位。

还有其他窗口函数(Blackman-Nutall 等人)可能会产生比矩形窗口更好的相位估计,但通常不如使用 FFTShift 进行估计。

本文链接:https://www.f2er.com/22298.html

大家都在问