我已经创建了一个FFT类/对象,它接收存储在2D数组中的信号,并在将其输出到matplotlib图形之前生成其输入的后续FFT。在
经过大量的阅读,我意识到,由于窗口化,数据集中需要2^x的点数和整数个周期,峰值的振幅永远不会是100%准确的(但将是近似正确的)。在
但是,当我给信号加上直流偏移量时,由于某些原因,0赫兹的频率峰值总是实际直流/恒定偏移量的两倍!例如,如果我把2加到一个x赫兹的正弦波上,在FFT上得到一个x赫兹的峰值,在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结果,因此没有减半之外,它们是纯正弦波振幅的一半。在
相关问题 更多 >
编程相关推荐