# Fast Fourier Transform using numpy

I am a computer science student and didn't really have signal processing as a subject. Maybe I should be clear on the concepts of sampling rate and frequency of the signal but I am a little confused. I was trying to code and understand the process of finding MFCC filter banks on audio files. One intermediate step is to find FFT. I computed and plot the fft results of my audio but wasnt sure that I was doing things correct as I didnt know the component frequencies that may have been present on my audio.

So I decided to form a sample wave and find and plot the fft results of the test signal.(The np.fft.fftfreq functions return the frequencies corresponding to the fft computed by np.fft.fft()).So my questions are

1) I found out value of y for time at a separation of 1ms seconds( 0 to 1, 1000 values). So my sampling rate should be 1000 right?. Then according to the np.fft.fftfreq() function, the second parameter should be 1/sampling rate. Then it gives correct values for 1/1000. However if I give sampling rate as 100(T=1/100) and give the second parameter as 1/100. It gives wrong results. Why??

2) If I give the frequency of the sine wave as 1080 instead of 80 for T=1/1000. Then also it gives same results as for 80. Why is it so?

# EXAMPLE SIGNAL TAKEN TO VERIFY ABOVE FFT PROCEDURE IS RIGHT
NUM=1000
T=1/1000
tt=np.linspace(0,NUM*T,num=NUM)
#So I take readings every T seconds = 1ms

print(1/T)
y=np.sin(2*np.pi*1080*tt)#+np.sin(2*np.pi*90*tt)
#plt.plot(tt,y)
NFFT=1024
mag_frames = np.absolute(np.fft.rfft(y, NFFT))
xf = np.linspace(0.0, 1.0/(2.0*T), NUM/2)
freq=np.fft.rfftfreq(NFFT,1/1000)
plt.plot(freq,mag_frames)


3) What should be value for the second parameter when I perform fft on a audio data imported from a .wav file by librosa library with sampling rate = rate

I am sorry if the problem is very simple or basic. I am really a beginner in this area, and could not get much help from results in Google search

1. You are right that your time instants are spaced at an interval of $$T$$. Hence, the sampling rate is $$1/T$$. ($$T$$ is defined in line 3 of your code.) You are also right that the second parameter of np.fft.fftfreq() is the sampling interval $$T$$. I am, however, not sure why you get wrong results when setting $$T=100\,\text{Hz}$$.
2. This phenomenon is called aliasing. Nyquist's sampling theorem states that frequencies below $$f_\text{s}/2=\frac{1}{2T}$$ can be captured correctly by the sampling process. Frequencies above half of the sampling frequency are mirrored back to frequencies below half of the frequency. Therefore, a frequency of $$1080\text{Hz}$$ is too high to be captured correctly.
3. The choice for the second parameter of the FFT, the FFT length, depends on the specific application. You have to experiment what leads to the best results. Still, your choice of NFFT = 1024` is typically a good start.