You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用SciPy计算FFT时出现周期性幅度误差的问题排查

FFT计算叠加正弦信号时的幅度周期性误差问题

计算叠加正弦信号的FFT时,所得结果与初始输入不匹配。代码逻辑为:先生成数组,叠加多个频率不同但幅度恒定的正弦信号;随后复现STFT函数的计算逻辑(STFT也得到相同错误结果)。

当前核心问题:

  • 选择2^n大小的窗口(如8192)时,FFT幅度会出现周期性误差,部分误差甚至超过15%
  • 选择非2^n的窗口(如10000)时,该问题不会出现
  • 尝试调整窗口宽度以获取正确幅度,结果与预期完全相反

相关代码如下:

import numpy as np
from scipy.fft import fft, fftshift

# Define the sample rate and the duration of the signal
sample_rate = 10000  # in Hz, common sample rate in audio
duration = 100     # in seconds, 10 milliseconds

# Create a time array
t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)

# Frequencies of the sine waves
frequencies = np.arange(1000,4000, 100)
result = np.zeros(t.shape[0])

# Create sine waves
for freq in frequencies:
    result += np.sin(2 * np.pi * freq * t)

def compute_fft_sections(data, sample_rate, x):
    n = len(data)
    section_length = 8192
    overlap = int(section_length * (0.5))

    fft_results = []
    normalised_sections=[]
    for i in range(0, n - overlap, overlap):

        # Stop if the end of the array is reached
        if i + section_length >= n:
            break

        section = data[i:i + section_length]

        # Apply Hanning window
        window = np.hanning(len(section))
        windowed_section = section * window

        # Normalize the windowed section
        # Here we normalize by the sum of the window values
        normalized_section = windowed_section / window.sum()
        normalised_sections.append(section)
        normalised_sections.append(normalized_section)

        # fft + downsample symetric
        fft_result = fft(normalized_section)
        fft_result = fftshift(fft_result)
        fft_result = fft_result[:len(fft_result)//2]
        #fft_result = fft_result[::len(fft_result)]

        # append to results
        fft_results.append(fft_result*2)

    return fft_results, normalised_sections

# Example usage
fft_sections, normalised_sections = compute_fft_sections(result,
                                    sample_rate=0.0001, 
                                    x=120)

附上周期性误差示意图:
周期性FFT误差示意图

内容的提问来源于stack exchange,提问作者Dev0ran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 07:35:30