c - 使用 atan2 的 FFT 相角 - 奇怪的行为。相移偏移?展开?
问题描述
我正在测试和执行简单的 FFT,我对相移感兴趣。我使用 10 个周期的正弦曲线生成 256 个样本的简单数组。
我对这些样本执行 FFT 并接收复杂数据 (2x128)。比我计算这些数据的大小和 FFT 看起来像预期的那样:
然后我想计算 fft 复数输出的相移。我正在使用atan2。组合输出 fft_magnitude (blue) + fft+phase (red) 看起来像这样: 这几乎是我对“小”问题的期望。我知道这是环绕,但如果我想象展开它,幅度峰值的相移读数为 36 度,我认为它应该为 0,因为我的输入正弦波根本没有移动。
如果我移动这个 -36 度(蓝色是同相的,红色是移动的,蓝色打印仅供参考)正弦波看起来像这样:
如果我对这个红色数据执行 FFT,幅度 + 相位输出看起来像这样:
所以很容易想象,展开相位在幅度峰值处将接近 0。所以有36度偏移。但是,如果我准备每 256 个样本 20 个周期和 0 个相移的数据会发生什么
如果我然后执行 FFT,这是一个输出(幅度 + 相位): 我可以告诉你是否会在 72 度处穿过峰值点。所以现在有 72 度偏移。
谁能给我一个提示,为什么会这样?atan2() 相位输出是否与频率相关,偏移量为 2pi/周期(360 度/周期)?如何打开它并获得正确的结果(我找不到可以打开的 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], 8); Serial.print(","); Serial.print(phase_data[i], 8); Serial.print("\n");
}
Serial.print("\n\n");
delay(10000);
}
解决方案
如果正弦波在 FFT 的孔径宽度中恰好是整数周期,那么裸 FFT 加上 atan2() 只能正确测量输入正弦波的起始相位。
如果信号不完全是整数周期(某个其他频率),那么您必须通过执行 FFTshift 来重新定位数据(将数据旋转 N/2)。然后,FFT 将正确测量原始数据中心的相位,并远离由 FFT 的有限长度矩形窗口产生的圆形不连续性。
如果您想要数据中除中心以外的某个点的相位,您可以使用对中心的频率和相位的估计来重新计算其他位置的相位。
还有其他窗口函数(Blackman-Nutall 等)可能会产生比矩形窗口更好的相位估计,但通常不如使用 FFTShift 的估计好。
推荐阅读
- julia - Julia 1.0.0:尝试添加 PyPlot 包时出错
- sql-server - 错误 - 过程没有参数,并且在动态查询中提供了参数
- regex - 根据搜索词上方和下方的行内容进行搜索
- javascript - 使用 ES6 模块为从 CDN 加载的 JS 导入 TS 类型
- react-native - React-Native :: WebView :: 一些没有明显模式的空白页面
- azure - 如何在 Azure Application Insights Analytics 上独树一帜
- python - 无法检索数据,因为在使用 get_popular 查询 del.icio.us API 时名称或服务未知
- c# - 串行端口数据接收事件处理程序多次触发
- c# - Path.TryJoin 在 .NET Core 中相对于 Path.Combine 的优势是什么?
- c# - 流 - 内容的长度