python - Python FFT 中的 DC 项 - 常数项的幅度

标签 python numpy matplotlib signal-processing fft

我创建了一个 FFT 类/对象,它获取存储在 2D 数组中的信号并生成其输入的后续 FFT,然后将其打印到 matplotlib 图形。

经过大量阅读后,我认识到,由于窗口化,数据集中理想情况下需要 2^x 个点和整数个周期,因此峰值的幅度永远不会是 100 % 准确(但大致正确)。

但是,当我向信号添加直流偏移时,由于某种原因,0 Hz 频率的峰值始终是实际直流/恒定偏移的两倍!例如,如果我将 2 添加到 x Hz 的正弦波,我会在 FFT 上得到 x Hz 处的峰值,并在 0 处得到 4 的峰值。

这是为什么 - 我可以纠正这个问题吗?

谢谢!

我使用的代码如下:

import numpy as np
import matplotlib.pyplot as plt

class FFT:
    def __init__(self, time, signal, buff=1, scaling=2, centre=False): 
        self.signal = signal
        self.buff = buff
        self.time = time
        self.scaling = scaling
        self.centre = centre
        if (centre):
            self.scaling = 1
    def fft(self):
        self.Y = np.fft.fft(self.signal, self.buff * len(self.signal))  # Do fft on signal and store
        if (self.centre is True):
            self.Y = np.fft.fftshift(self.Y)  # centre 0 frequency in centre
        self.__graph__()
    def __graph__(self):
        self.N = len(self.Y) / self.scaling  # get FFT length (halved to avoid reflection)
        print (self.N)
        self.fa = 1 / (self.time[1] - self.time[0])  # get time interval & sampling frequency of FFT
        if (self.centre is True):
        self.t_axis = np.linspace(-self.fa / 2 * self.scaling, self.fa / 2 * self.scaling, self.N, endpoint=True)  # create x axis vector from 0 to nyquist freq. (fa/2) with N values
        else:
            self.t_axis = np.linspace(0, self.fa / self.scaling, self.N, endpoint=True)  # create x axis vector from 0 to nyquist freq. (fa/2) with N values
    def show(self, absolute=True):

        if absolute:
            plt.plot(self.t_axis, ((2.0) * self.buff / (self.N * (self.scaling))) * np.abs(self.Y[0:self.N]))
        else:
            plt.plot(self.t_axis, ((2.0) * self.buff / (self.Ns * (self.scaling))) * self.Y[0:self.N])  # multiply y axis by 2/N to get actual values
        plt.grid()
        plt.show()

def sineExample(start=0, dur=128, samples=16384):    
    t = np.linspace(start, dur + start, samples, True)
    print(t)
    f = 10.0  # Frequency in Hz
    A = 10.0  # Amplitude in Unit
    retarr = np.zeros(len(t))    
    retarr = np.column_stack((t, retarr))
    for row in range(len(retarr)):
        retarr[row][1] = A * np.sin(2 * np.pi * f * retarr[row][0]) + 2 # Signal  
    print(retarr)
    return retarr

hTArray = sineExample()
# plt.plot(hTArray[:,0], hTArray[:,1])
# plt.grid()
# plt.show()

myFFT = FFT(hTArray[:, 0], hTArray[:, 1], scaling=2,centre=False)
myFFT.fft()
myFFT.show()

最佳答案

事实上,恰恰相反。严格实数数据的完整复数 FFT 结果中的所有其他频率都分为 2 个结果箱,并镜像为共轭复数,因此,当按 1/N 缩放时,纯正弦波幅度的一半,除了 DC 分量和 N/2 余弦分量之外,不会分成 2 个 FFT 结果,因此不会减半。

关于python - Python FFT 中的 DC 项 - 常数项的幅度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29865785/

相关文章:

python - 使用 Jenkins 进行测试时处理硬件资源

Python:搜索和连接

python - 为 x 的两个值插值一个峰值 - Python

python - matplotlib plt.figsize() 参数不适用于 pandas DataFrame 图

Python - 在某些点绘制速度和加速度矢量

python - 将 RGB 图像加载为 ndarray,并绘制颜色变化的图

python - Django:我必须在应用程序之前登录管理站点

python - 在 Azure 上发布 Flask Web 应用程序

python - 用 0 错误 :nan not supported for input type 替换 nan

python - 有没有办法计算 make_blobs 生成的数据集的 cluster_std ?