# Why is DFT magnitude less than expected?

Code for signal:

def my_s(t):
return 2*np.sin(2*np.pi*50*t) + np.sin(2*np.pi*70*t+np.pi/4)

N = int(70*2.5) # sampling rate
T = 1 / N # sampling interval

x = np.arange(0, np.pi/9, T)
y = my_s(x)


Why first true bin is less 2? Why a lot of false bins?

What is a phase graph? What can be understood from it? How can I calculate the period?

## 1 Answer

Remember that a discrete signal has two directions of discreteness:

• limited duration in time: the number of samples you have
• limited sampling rate: the interval between each sample

The sampling rate will limit your maximum frequency via Nyquist, and the duration will limit the frequency resolution.

Your signal is composed of 62 samples, and of a duration of pi/9 ~ 0.348s, which gives you frequency bins for the fft of 1/duration ~2.869 hZ.

As you can see from your graph, you don't have a frequency bin centred on 50Hz, but on 50.8hz (the 17th bin). If you change your frequency in your signal definition to 50.8, you will see that the peak of the fft is much more pronounced (green squares for 50.8, blue circles for 50Hz), as it the signals fits better into the bins:

Of course, if you're measuring a real signal, you can't just change its frequency to match your bins. You can instead measure the signal for a longer time, so that the frequency resolution becomes greater, and the effects of the edges of the signal become less important. Here with a signal 9 times longer:

Another problem with DFT, is the effect of the start and stop of your signal, which can be reduced by multiplying with a Hamming window:

YH=y*np.hamming(len(x))

You can see that your peaks at 50 and 70 Hz are much more pronounced. Note that because the Hamming window has values between 0 and 1, the amplitude of your signal is reduced, so you need to divide the output of the fft by the mean of the Hamming window

You can also combine both techniques, use other types of windows.... See the links in the comment by OverLordGoldDragon above.

• I recommend Tukey over Hamming for simply representing a general signal, but if it's stationary and we want less leaks then Hamming is good - discussion under this post. Commented Jan 7, 2023 at 13:36