0

I have a signal $3.5 \cdot 10^{-5}s$ long as shown below.

It was unevenly sampled so I did cubic spine interpolation.

My signal as shown in time domain about 0.9V and 1MHz frequency.

I want to make an accurate FFT picture with $F_s=10\texttt{Mhz}$ so $T_s=10^{-7}s$, hence
$N = 3.5*10^{-5}/10^{-7}=350$

I want an accurate FFT with $1\texttt{Hz}$ bins, so $350$ samples is too little (I'll get spectral leakage.)

What could be done to perform a good FFT in this case?

from scipy.fftpack import fft
#import plotly
#import chart_studio.plotly as py
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor
from gekko import GEKKO
#%matplotlib qt
new_x=np.arange(0,3.5e-05,1/(1e7))
dataset_fft=pd.read_table("sinus_1mhz.txt")
array_fft=dataset_fft.values
m=GEKKO()
m.x=m.Param(new_x)
m.y=m.Var()
m.cspline(m.x,m.y,array_fft[:,0],array_fft[:,1])
m.options.IMODE=2
m.solve(disp=False)

fig=plt.figure()

ax=fig.subplots() ax.grid() cursor=Cursor(ax, horizOn=True,vertOn=True,useblit=True,color='r',linewidth =1) #plt.plot(array_fft[:,0],array_fft[:,1])

plt.plot(m.x,m.y) plt.xlabel("time") plt.ylabel("amp") plt.show()

enter image description here

JRE
  • 2,240
  • 1
  • 11
  • 20
lub2354
  • 113
  • 2

1 Answers1

1

The OP appears to want to have exactly one bin for every 1 Hz spacing in the FFT result. This is arbitrary and there is no requirement of such for a "proper FFT". What is a driving consideration in this regard is the desired frequency resolution of the result. The bin spacing and the frequency resolution are not necessarily related (see what occurs with zero padding for example). The primary driver to frequency resolution is the total time duration of the actual samples captured, independent of what sampling rate is used. (The sampling rate sets the range of the result in the frequency domain, as the resulting samples will extend from $0$ to $N-1$ where $0$ corresponds to DC and $N$ corresponds to the sampling rate.). Consideration for frequency resolution would be our needs to distinguish two signals closely spaced in the frequency domain- this is demonstrated at the link on zero padding given above.

Having sufficient resolution bandwidth, and not having spectral leakage effects, are two independent considerations. The resolution bandwidth is set by the total time duration of the capture, and minimizing spectral leakage is done by using more advanced windows (multiply the time domain capture by a window with tapers the beginning and the end of the capture, prior to taking the FFT).

The frequency resolution as given by the equivalent noise BW (ENBW) or resolution BW (RBW) in Hz is $1/T$ where $T$ is the capture duration in seconds, regardless of sampling rate, for an FFT without any further windowing to reduce spectral leakage. Such windowing only increases RBW (typically on the order of 2x, see this post for an accurate computation and further intuition) so, for example, if “accurate FFT” means being able to resolve 1 Hz in frequency, a capture length of at least 2 seconds would be required.

Thus for “accurate FFT” I recommend using a capture length commensurate with the desired RBW allowing for use of additional windowing, and then window to reduce the effects of spectral leakage. Thus assumes the waveform is sufficiently stationary over the duration of the capture since the average frequency over that duration will be presented. In this case with 350 samples at the 10 MHz sampling rate, $T = 350/10e6 = 35 \mu s$. $1/T = 28.6$ KHz. So the resolution bandwidth without use of further windowing is 28.6 KHz. Given the rectangular window in this case, spectral leakage will be dominant- and since there is a large DC offset, the leakage from this offset will swamp out much of the lower frequency content. Using a more advanced window will make the resolution bandwidth wider; to reduce that requires a longer $T$.

For computing accurate power spectrums (power spectral density), I recommend the Welch method.

Dan Boschen
  • 50,942
  • 2
  • 57
  • 135
  • Hello Dan,I will take a interpolated signal of 1 second and resample it with F=10^7Hz then i will have 1Hz for every bin. but there is a law that the number of fft bins needs to be power of two. the closest power of two to my 10Mhz top frequency is 2^24 which is 16 million bins. do i have to follow this "power of two" law in my FFT? Thanks. – lub2354 Apr 05 '23 at 08:37
  • It does not need to be power of two, that is just when the radix 2 is most efficient. Why do you want 1 Hz resolution? Do you really need that? If so you will want 2 seconds of data and then window it before taking the FFT. I like the Kaiser window but to be simpler you could start with a Hamming or other window. – Dan Boschen Apr 05 '23 at 12:06
  • Also you can take 2 seconds of data and sample it at a 20 Hz rate and still have 1 Hz for every bin (if you even need that). The number of samples dictates the sampling rate and the frequency spacing of the bins is $f_s/N$ The duration and windowing set the actual frequency resolution -- not the bin spacing itself. I think this post may help you: https://dsp.stackexchange.com/questions/37927/what-happens-when-n-increases-in-n-point-dft/37931#37931 – Dan Boschen Apr 05 '23 at 12:11
  • Hello , So given Top frequency of 10Mhz and we want to catch every multiple of a 1Mhz so we need 10 samples? how can i get an acurate result on that 1Mhz bin sample? As i see it, we say to the fft that we have only 10 bins and we will get a series spectral leakage. Or even when i do 1e4 samples. i still will get spectral leakage. how do i compensate the spectrl leakage and get an accurate result for 0 1Mhz 2MHz etc..? Thanks. – lub2354 Apr 10 '23 at 21:36
  • @lub23354 The accuracy (precision) of the result is set by your total time duration. The spectral leakage is controlled by windowing prior to taking the FFT. So yes increase the number of samples (which is increasing the time duration), but then apply a window prior to taking the FFT to reduce the spectral leakage. Is that clear? – Dan Boschen Apr 10 '23 at 21:46