# Why is FFT module for smaller df showing uexpected malfunctioning in Python?

import numpy as np
import matplotlib.pyplot as plt

Fs = 170                      # sampling rate
Ts = 1.0/Fs                      # sampling interval
t = np.arange(0, 1200, Ts)            # time vector

f_f = np.arange(30, 80.01, 0.01)
n_f = len(f_f)
y2 = 0.0
y2 = sum(np.sin(2 * np.pi * f * t) for f in f_f)

n = len(y2)
k = np.arange(n)
T = n/Fs
frq = k/T               # two sides frequency range
freq = frq[range(int(n/2))]     # one side frequency range

Y = np.fft.fft(y2)/n                # fft computing and normalization
Y = Y[range(int(n/2))]
plt.plot(freq, abs(Y), 'r-')    # Fourier data
plt.show()


Here, as you may have noticed I have chosen df = 0.01 and the corresponding DFT is shown in the attached pic. However, while I am considering 'df = 1' instead, DFT comes out as a sort of continuous square wave over the frequency domain of the signal (expected since the amplitude of all the waves is equal). This is what is expected, Right ?.

But, it is getting distorted for df = 0.1 or smaller?

So, I kept t_upper = 1/df; Yet, it is not as I have been expecting. However, as I am making t_upper >= 10*(1/df), then I am again getting sort of uniform amplitude over 30 to 80 Hz.

Even, the sampling rate has been so chosen so that Nyquist rate exceeds maximum frequency component of the pulse.

I would be higly obliged if any one could kindly aid me on this. Thanks.

This is what is expected, Right ?.

Not at all. Why would you think this? The DFT represents a time domain signal as a weighted sum $$N$$ discrete complex exponentials, where $$N$$ is the DFT length. Frequency resolution is the sample rate divided by $$N$$

You perform a DFT of a sum of sine waves. Each individual sine wave will map most of it's energy to the DFT bin closest to it's frequency, but with significant spectral leakage. If your frequency spacing in the signal is closer than the DFT frequency resolution it will look sort-of continuous. If it's wider, you will see significant "combing", i.e. the bins that don't match well to a signal frequency will have low energy.

The peaks at the ends are a function of your choice of a zero phase for all sines. There is positive interference at the edges. If you choose a random phase instead the peaks will disappear but the whole thing will look more noise like.

I suggest starting with a single sine wave and varying the frequency. Make sure you fully understand spectral leakage. Then try to sine waves, vary frequency spacing and relative phase.

Once you understand these cases, the rest should be easier.

• I got your point but here, I have chosen additional phase factors to be 0 for all the waves. The only phase difference arises from difference in frequency. How, is then only at the end peaks arises ? Moreover, as I am putting df = 1 Hz the plot (FFT data) turns out to be a sort of rectange (i.e. continuous) accross 30 to 80 Hz band which I presume due to same amplitudes at all the sine waves in frequency range (30-80 Hz). Why is it instilling difference as I am considering, df <= 0.1 Hz ? Sep 28 '21 at 16:45
• For dF = 1 you should NOT get a square wave, instead you should be seeing the individual frequency as peaks. Tray an extreme case of dF = 25, you should only see individual peaks at 30, 55 and 80. If you get something difference something is wrong with your code. Sep 29 '21 at 19:12