具有 2 个 cosf() 周期的信号的 FFT 输出

发布于 2025-01-19 17:42:28 字数 1306 浏览 2 评论 0原文

我正在使用 ZeroFFT 库转换信号。我从中得到的结果并不是我直观地期望的。

作为测试,我向 FFT 算法提供一个包含两个完整余弦周期的缓冲区:

在此处输入图像描述

通过 512 个样本进行采样。

它们以 int16_t 值的形式提供。我期望返回的是 256 个振幅,其值为 [ 0, 4095, 0, 0, ..., 0 ]

相反,这是结果: 2 2052 4086 2053 0 2 2 2 1 2 2 2 4 4 3 4...

而且它变得更奇怪了!如果我向它提供相同的信号,但发生了变化(因此 sinf() 超过 0 .. 4*pi 而不是 cosf() 函数)我得到了完全不同的结果: 4 10 2 16 2 4 4 4 2 2 2 3 2 4 3 4

这引发了问题:

1。具有相同周期的正弦信号和余弦信号不是包含完全相同的频率吗?

2。如果我给它提供一个正好有 2 个余弦周期的缓冲区,那么傅立叶变换不会导致除 1 个频率之外的所有零吗?

我生成测试信号为:

static void setup_reference(void)
{
  for (int i=0; i<CAPTURESZ; ++i)
  {
    const float phase = 2 * 3.14159f * i / (256.0f);
    reference_wave[i] = (int16_t) (cosf(phase) * 0x7fff);
  }
}

并调用 ZeroFFT 函数 注意:

ZeroFFT(reference_Wave, CAPTURESZ);

ZeroFFT 文档指出应用了汉宁窗。

I am transforming a signal using the ZeroFFT library. The results I get from it, are not what I would intuitively expect.

As a test, I feed the FFT algorithm with a buffer that contains two full cycles of cosine:

enter image description here

Which is sampled over 512 samples.

Which are fed as int16_t values. What I expected to get back, is 256 amplitudes, with the values [ 0, 4095, 0, 0, ..., 0 ].

Instead, this is the result:
2 2052 4086 2053 0 2 2 2 1 2 2 2 4 4 3 4...

And it gets weirder! If I feed it the same signal, but shifted (so sinf() over 0 .. 4*pi instead of cosf() function) I get a completely different result: 4 10 2 16 2 4 4 4 2 2 2 3 2 4 3 4

This throws up the questions:

1. Doesn't a sine signal and cosine signal with same period, contain exactly the same frequencies?

2. If I feed it a buffer with exactly 2 cycles of cosine, wouldn't the Fourier transform result in all zeros, except for 1 frequency?

I generate my test signal as:

static void setup_reference(void)
{
  for (int i=0; i<CAPTURESZ; ++i)
  {
    const float phase = 2 * 3.14159f * i / (256.0f);
    reference_wave[i] = (int16_t) (cosf(phase) * 0x7fff);
  }
}

And call the ZeroFFT function as:

ZeroFFT(reference_Wave, CAPTURESZ);

Note: the ZeroFFT docs state that a Hanning window is applied.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的往事 2025-01-26 17:42:28

窗户会导致一些光谱泄漏。包括窗口函数,波浪现在看起来像这样:

“

如果我将其喂给一个恰好有2个余弦周期的缓冲液,除了1个频率外,傅立叶变换不会结果吗?

是的,如果您没有窗口而这样做。实际上两个频率:您期望的正频率和等效的负频率,尽管并非所有FFT功能都将包括其输出中的负频率(对于真实输入,结果是Hermitian-Memmetric,但在该输出中没有额外的信息负频率)。出于实际原因,由于输入信号和FFT计算都不是精确的,因此您可能不会在其他任何地方获得零,但应该接近 - 这主要是浮点输出的关注点。

顺便说一句,我并不是说窗口不好,但是在这种特殊情况下(完美的周期性输入),它对您不利。

至于正弦波,结果的幅度应该相似(在理性之内 - 不应该预期),但是您使用的FFT功能的评论

复杂部分被丢弃,然后返回实际值。

尽管相位移位不会改变幅度,但它们会改变结果的阶段,因此也会改变其实际成分。

Windowing causes some spectral leakage. Including the window function, the wave now looks like this:

windows cosine

If I feed it a buffer with exactly 2 cycles of cosine, wouldn't the Fourier transform result in all zeros, except for 1 frequency?

Yes, if you do it without windowing. Actually two frequencies: both the positive frequency that you expect, and the equivalent negative frequency, though not all FFT functions will include the negative frequencies in their output (for Real input, the result is Hermitian-symmetric, there is no extra information in the negative frequencies). For practical reasons, since neither the input signal nor the FFT calculation are exact, you may not get exactly zero everywhere else either, but it should be close - that's mainly a concern for floating point output.

By the way by this I don't mean that windowing is bad, but in this special case (perfectly periodic input) it didn't work out in your favour.

As for the sine wave, the magnitudes of the result should be similar (within reason - exactness shouldn't be expected), but the comments on the FFT function you used mention

The complex portion is discarded, and the real values are returned.

While phase shifts would not change the magnitudes much, they change the phases of the results, and therefore also their Real component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文