# properly implementing FFT in python problem

I have signal which a sinous wave with frequency 1MHZ and DC signal at 0.9V.
The signal is sampled every T the total length is 5e-6 sec so for T=3.301028538570082e-09 i have 220 samples.

sampling frequency is Fs=302935884.4722904Hz The original sinous plot is shown bellow. As you can see Sinous amplitude is 0.1V But is the last two FFT plot there is not destiguish between DC and my 1MHZ signal,also the amplitudes are not an in the analog picture. Where did i go wrong? The ful python code with the sample table is shown bellow. Thanks.

Code:

# -*- coding: utf-8 -*-
"""
Created on Thu Mar 30 13:04:11 2023

@author: Asus
"""

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
#%matplotlib qt

array_fft=dataset_fft.values
Ts=array_fft[4][0]-array_fft[3][0];
Fs=1/Ts #Hz
L=np.size(array_fft)
freq_vec=Fs*np.arange(0,1,1/220)
L=np.size(freq_vec)
fft_y=fft(array_fft[:,1],220)
fig=plt.figure()
ax=fig.subplots()
ax.grid()
cursor=Cursor(ax, horizOn=True,vertOn=True,useblit=True,color='r',linewidth =1)
#ax.plot(array_fft[:,0],array_fft[:,1])
ax.plot(freq_vec,abs(fft_y)/220)
plt.show()


sample table:

time    V(n020)
0.000000000000000e+000  8.924428e-001
3.301028538570077e-009  8.922886e-001
6.602057077140153e-009  8.920012e-001
9.903085615710230e-009  8.915803e-001
1.320411415428031e-008  8.910261e-001
1.650514269285038e-008  8.903385e-001
1.980617123142046e-008  8.895176e-001
2.310719976999053e-008  8.885633e-001
3.310719976999053e-008  8.853104e-001
4.310719976999053e-008  8.813376e-001
5.310719976999053e-008  8.768258e-001
6.310719976999053e-008  8.719212e-001
7.310719976999052e-008  8.667433e-001
8.310719976999052e-008  8.613904e-001
9.310719976999052e-008  8.559445e-001
1.031071997699905e-007  8.504745e-001
1.231071997699905e-007  8.396893e-001
1.431071997699905e-007  8.294162e-001
1.631071997699906e-007  8.199491e-001
1.831071997699906e-007  8.115207e-001
2.131071997699906e-007  8.012237e-001
2.431071997699906e-007  7.941512e-001
2.731071997699906e-007  7.906163e-001
3.031071997699907e-007  7.907807e-001
3.331071997699907e-007  7.946613e-001
3.631071997699907e-007  8.021348e-001
3.931071997699908e-007  8.129446e-001
4.231071997699908e-007  8.267109e-001
4.531071997699908e-007  8.429437e-001
4.831071997699908e-007  8.610607e-001
5.131071997699908e-007  8.804088e-001
5.331071997699908e-007  8.936476e-001
5.631071997699909e-007  9.134864e-001
5.931071997699909e-007  9.326717e-001
6.231071997699909e-007  9.505267e-001
6.531071997699910e-007  9.664334e-001
6.831071997699910e-007  9.798563e-001
7.131071997699910e-007  9.903588e-001
7.431071997699911e-007  9.976145e-001
7.731071997699911e-007  1.001411e+000
8.031071997699911e-007  1.001653e+000
8.331071997699912e-007  9.983562e-001
8.531071997699912e-007  9.942504e-001
8.731071997699912e-007  9.886890e-001
8.931071997699912e-007  9.817557e-001
9.131071997699912e-007  9.735535e-001
9.331071997699913e-007  9.642041e-001
9.531071997699913e-007  9.538467e-001
9.731071997699911e-007  9.426368e-001
9.931071997699909e-007  9.307446e-001
1.013107199769991e-006  9.183531e-001
1.033107199769991e-006  9.056551e-001
1.053107199769990e-006  8.928512e-001
1.073107199769990e-006  8.801460e-001
1.093107199769990e-006  8.677447e-001
1.113107199769990e-006  8.558499e-001
1.133107199769990e-006  8.446571e-001
1.153107199769989e-006  8.343519e-001
1.173107199769989e-006  8.251061e-001
1.203107199769989e-006  8.135566e-001
1.233107199769989e-006  8.051701e-001
1.263107199769988e-006  8.002657e-001
1.293107199769988e-006  7.990336e-001
1.323107199769988e-006  8.015288e-001
1.353107199769987e-006  8.076692e-001
1.383107199769987e-006  8.172396e-001
1.413107199769987e-006  8.298985e-001
1.443107199769987e-006  8.451898e-001
1.473107199769986e-006  8.625590e-001
1.503107199769986e-006  8.813735e-001
1.523107199769986e-006  8.943822e-001
1.543107199769986e-006  9.075180e-001
1.573107199769985e-006  9.269971e-001
1.603107199769985e-006  9.455855e-001
1.633107199769985e-006  9.626255e-001
1.663107199769985e-006  9.775270e-001
1.693107199769984e-006  9.897886e-001
1.723107199769984e-006  9.990119e-001
1.753107199769984e-006  1.004910e+000
1.783107199769983e-006  1.007312e+000
1.813107199769983e-006  1.006159e+000
1.843107199769983e-006  1.001504e+000
1.873107199769983e-006  9.935084e-001
1.903107199769982e-006  9.824341e-001
1.933107199769982e-006  9.686427e-001
1.963107199769982e-006  9.525865e-001
1.993107199769981e-006  9.348007e-001
2.023107199769981e-006  9.158893e-001
2.053107199769981e-006  8.965079e-001
2.083107199769981e-006  8.773422e-001
2.113107199769980e-006  8.590813e-001
2.143107199769980e-006  8.423911e-001
2.173107199769980e-006  8.278856e-001
2.203107199769979e-006  8.161016e-001
2.233107199769979e-006  8.074761e-001
2.263107199769979e-006  8.023291e-001
2.293107199769978e-006  8.008513e-001
2.323107199769978e-006  8.030988e-001
2.353107199769978e-006  8.089899e-001
2.373107199769978e-006  8.148408e-001
2.393107199769978e-006  8.221229e-001
2.413107199769977e-006  8.307174e-001
2.433107199769977e-006  8.404839e-001
2.453107199769977e-006  8.512621e-001
2.473107199769977e-006  8.628752e-001
2.493107199769977e-006  8.751321e-001
2.513107199769976e-006  8.878315e-001
2.533107199769976e-006  9.007647e-001
2.553107199769976e-006  9.137201e-001
2.573107199769976e-006  9.264866e-001
2.593107199769976e-006  9.388579e-001
2.613107199769975e-006  9.506359e-001
2.633107199769975e-006  9.616345e-001
2.653107199769975e-006  9.716824e-001
2.673107199769975e-006  9.806260e-001
2.703107199769975e-006  9.916830e-001
2.733107199769974e-006  9.995958e-001
2.763107199769974e-006  1.004115e+000
2.793107199769974e-006  1.005104e+000
2.823107199769973e-006  1.002543e+000
2.853107199769973e-006  9.965196e-001
2.883107199769973e-006  9.872273e-001
2.913107199769973e-006  9.749626e-001
2.943107199769972e-006  9.601180e-001
2.973107199769972e-006  9.431753e-001
3.003107199769972e-006  9.246952e-001
3.033107199769971e-006  9.053026e-001
3.063107199769971e-006  8.856674e-001
3.093107199769971e-006  8.664808e-001
3.123107199769971e-006  8.484289e-001
3.153107199769970e-006  8.321643e-001
3.183107199769970e-006  8.182788e-001
3.213107199769970e-006  8.072786e-001
3.243107199769969e-006  7.995634e-001
3.273107199769969e-006  7.954113e-001
3.303107199769969e-006  7.949686e-001
3.333107199769969e-006  7.982451e-001
3.353107199769968e-006  8.024388e-001
3.373107199769968e-006  8.081621e-001
3.393107199769968e-006  8.153199e-001
3.413107199769968e-006  8.237936e-001
3.423107199769968e-006  8.284810e-001
3.433107199769968e-006  8.334432e-001
3.443107199769967e-006  8.386596e-001
3.453107199769967e-006  8.441087e-001
3.463107199769967e-006  8.497679e-001
3.473107199769967e-006  8.556136e-001
3.483107199769967e-006  8.616217e-001
3.493107199769967e-006  8.677673e-001
3.503107199769967e-006  8.740249e-001
3.513107199769967e-006  8.803685e-001
3.523107199769967e-006  8.867720e-001
3.533107199769967e-006  8.932088e-001
3.543107199769967e-006  8.996524e-001
3.553107199769966e-006  9.060764e-001
3.563107199769966e-006  9.124544e-001
3.573107199769966e-006  9.187604e-001
3.593107199769966e-006  9.310538e-001
3.613107199769966e-006  9.427583e-001
3.633107199769966e-006  9.536871e-001
3.663107199769965e-006  9.682541e-001
3.693107199769965e-006  9.801879e-001
3.723107199769965e-006  9.890857e-001
3.753107199769965e-006  9.946566e-001
3.783107199769964e-006  9.967242e-001
3.813107199769964e-006  9.952271e-001
3.843107199769964e-006  9.902158e-001
3.873107199769964e-006  9.818498e-001
3.903107199769963e-006  9.703925e-001
3.933107199769963e-006  9.562072e-001
3.963107199769963e-006  9.397500e-001
3.993107199769963e-006  9.215599e-001
4.023107199769962e-006  9.022462e-001
4.053107199769962e-006  8.824695e-001
4.083107199769962e-006  8.629198e-001
4.113107199769961e-006  8.442901e-001
4.143107199769961e-006  8.272491e-001
4.173107199769961e-006  8.124120e-001
4.203107199769961e-006  8.003163e-001
4.233107199769960e-006  7.913983e-001
4.263107199769960e-006  7.859771e-001
4.293107199769960e-006  7.842424e-001
4.323107199769959e-006  7.862490e-001
4.353107199769959e-006  7.919157e-001
4.373107199769959e-006  7.976262e-001
4.393107199769959e-006  8.047760e-001
4.413107199769959e-006  8.132467e-001
4.423107199769959e-006  8.179345e-001
4.433107199769958e-006  8.228985e-001
4.443107199769958e-006  8.281181e-001
4.453107199769958e-006  8.335719e-001
4.463107199769958e-006  8.392373e-001
4.473107199769958e-006  8.450908e-001
4.483107199769958e-006  8.511083e-001
4.493107199769958e-006  8.572647e-001
4.503107199769958e-006  8.635347e-001
4.513107199769958e-006  8.698925e-001
4.523107199769958e-006  8.763117e-001
4.533107199769957e-006  8.827658e-001
4.543107199769957e-006  8.892283e-001
4.553107199769957e-006  8.956727e-001
4.563107199769957e-006  9.020725e-001
4.573107199769957e-006  9.084017e-001
4.583107199769957e-006  9.146345e-001
4.603107199769957e-006  9.267105e-001
4.623107199769957e-006  9.381067e-001
4.643107199769956e-006  9.486422e-001
4.673107199769956e-006  9.624765e-001
4.703107199769956e-006  9.735320e-001
4.733107199769956e-006  9.814385e-001
4.763107199769955e-006  9.859397e-001
4.793107199769955e-006  9.868951e-001
4.823107199769955e-006  9.842795e-001
4.853107199769954e-006  9.781802e-001
4.883107199769954e-006  9.687927e-001
4.913107199769954e-006  9.564171e-001
4.943107199769954e-006  9.414513e-001
4.973107199769953e-006  9.243839e-001
4.993107199769953e-006  9.121249e-001
5.000000000000000e-006  9.077891e-001


The amplitudes have been distorted by the spectral leakage of the strong DC tone.

The "truth" for the expected levels on a dB scale (convenient for magnitude on spectrum plots) assuming the DC is 0.9V and the AC is 0.1 V peak would be:

DC: $$20log_{10}(0.9) = 0.92$$ dB

AC (each of the two tones): $$20log_{10}(V_p/2) = 20log_{10}(0.1/2) = -26$$ dB

The DC tone over the finite duration of the FFT will result in a Sinc in frequency with the main lobe going to a null at $$1/T$$ where $$T$$ is the duration of the capture. Thus we can see how the subsequent side-lobes which only roll-off at a rate of $$1/f$$ could interfere (either swamp out, or modify the level of) higher frequency tones.

A couple suggestions can improve the visibility when using the FFT and plotting the magnitude spectrum:

• Plot magnitude on a dB scale.
• Zero pad the FFT to interpolate more samples in frequency (doesn't add any more info but can visually fill in details we don't otherwise see).
• Increase frequency resolution by increasing the time duration of the signal (this will decrease the width of the main lobe of the Sinc, and therefore reduce all the sidelobes as well).
• Use windowing to significantly reduce the spectral leakage sidelobes; this comes at the expense of increasing frequency resolution, but given the benefit of significantly smaller side-lobes, this is in most cases a good trade to make.

Below I show the result for the OP's waveform with plotting on a dB scale and zero padding to more samples, zooming in on the area of interest:

We think we see evidence of other frequencies (many in addition to the OP's 1 MHz), but this is all just spectral leakage of the DC tone! Below I plot the result when the signal is just a constant 0.9V DC using the same processing as above. We clearly see from this that our signal of interest is completely swamped and obscured by the spectral leakage:

The same plot after windowing with a Kaiser window (using $$\beta=6$$) and then properly compensating for the coherent gain of the window, results in a much better picture of the spectral content. Here we see the accurate levels anticipated for both the DC and sinusoidal signal, and also is revealed that the actual frequency is slightly less than 1 MHz (which we can make out as well in the OP's plot that the cycle hasn't completed at the 5e-6 which we would otherwise expect with a true 1MHz frequency.

And for comparison to that above, the processing of the DC only signal when properly windowed appears as in the plot below, in which case we see the additional spectral artifacts in the plot above is the other signal present in addition to DC:

• Hello Dan, I want to understand how many samples do i need for my fft. for example: \ F=1Mhz my signal is Total=5u sec Ts=3.70446423448e-09 sec so i have 5e-6/Ts=1349.72 samples (from 5u). how many samples do i need for getting good fft? Thanks. Mar 30 at 19:21
• I used your same number of samples, with zero padding enough to interpolate more samples out to 10x the length. Your question is based on the frequency resolution you want, which is detailed here in other posts, but without windowing the resolution in Hz is 1/T where T is the duration of the signal (regardless of sampling rate). Windowing will typically double this, but that is worth doing. See the post I referenced in the answer for further details as well as this post here: This post may help: dsp.stackexchange.com/questions/41058/…. Mar 30 at 20:45
• Hello Dan, I could not see in you plots my 1MHz signal with amplitude as the sine wave. Mar 30 at 20:57
• @lub2354 the dominant signal in your captured waveform using the sampling rate in your code is at 700 KHz. The sine wave shown in your plot also appears visually to be less than 1 MHz, but the 2nd plot from the bottom is showing the signals that are present with the assumed sampling rate given. Note that the level of those sinusoidal sidebands in the spectrum are right at the -26 dB expected given your amplitudes. Mar 30 at 21:17