numpy - DC Term in Python FFT - Amplitude of Constant Term -
i've created fft class/object takes signal stored in 2d array , produces subsequent fft of input, before printing matplotlib graph.
after great deal of reading, appreciate due windowing, need have ideally 2^x number of points , integer number of cycles in data set, amplitude of peaks never 100% accurate (but approximately right).
however, when add dc offset signal, reason, 0 hz frequency has peak double actual dc/constant offset! example, if add 2 sine wave of x hz, peak @ x hz on fft, , peak of 4 @ 0.
why - , can correct this?
thanks!
the code using below:
import numpy np import matplotlib.pyplot 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)) # fft on signal , store if (self.centre 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 # fft length (halved avoid reflection) print (self.n) self.fa = 1 / (self.time[1] - self.time[0]) # time interval & sampling frequency of fft if (self.centre true): self.t_axis = np.linspace(-self.fa / 2 * self.scaling, self.fa / 2 * self.scaling, self.n, endpoint=true) # create x axis vector 0 nyquist freq. (fa/2) n values else: self.t_axis = np.linspace(0, self.fa / self.scaling, self.n, endpoint=true) # create x axis vector 0 nyquist freq. (fa/2) 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 2/n 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 = 10.0 # amplitude in unit retarr = np.zeros(len(t)) retarr = np.column_stack((t, retarr)) row in range(len(retarr)): retarr[row][1] = * 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()
actually, opposite. other frequencies in full complex fft result of strictly real data split 2 result bins , mirrored complex conjugated, half pure sinusoids amplitude when scaled 1/n, except dc component , n/2 cosine component, not split 2 fft results, , not halved.
Comments
Post a Comment