我创建了一个FFT类/对象,它将存储在2D数组中的信号生成,并在将其输入到matplotlib图之前生成其输入的后续FFT。
经过大量的阅读后,我很欣赏由于窗口化,需要在数据集中有理想的2 ^ x个点数和一个整数个周期,峰值的幅度永远不会是100 %准确(但大致正确)。
然而,当我为信号添加DC偏移时,由于某种原因,0 Hz频率的峰值始终是实际DC /常数偏移的两倍!例如,如果我将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()
答案 0 :(得分:1)