How to increase FFT frequency bin resolution without reducing frequency range in scipy

Viewed 30

I am practicing using the fft function in scipy with the following code below in which I define a sinusoidal wave of frequency Fr and then calculate the fft. If I reduce the sample time, t, then the resulting fft plot has a much larger maximum frequency but a lower resolution. I understand that this is caused by the fact that frequency is 1 over time and that the fft range must encompass all possible frequencies. However, is there anyway to increase the number of datapoints in ‘yf’ so that I get a higher resolution output. This is important to me because in my application I will have a wide range of frequencies (about 50 – 10,000 Hz).

import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import fft, fftfreq

N=1000
Fr=3
t=0.1 #sample time

x=np.linspace(0,N*t,N)
y=np.sin(2*np.pi*Fr*x)

yf=fft(y)
xf = fftfreq(n=N, d=t)[:N//2]
plt.plot(xf, 2.0/N * np.abs(yf[0:N//2]))
0 Answers
Related