jakevdp / nfft

Lightweight non-uniform Fast Fourier Transform in Python
MIT License
201 stars 29 forks source link

Frequency resolution of f_hat #17

Open UDEPWollny opened 3 years ago

UDEPWollny commented 3 years ago

Hi,

I have a question regarding the frequency resolution for the vector of Fourier coefficients. No matter what I do, whether I use equal M and N, or one being a multiple of the other, it doe's not change the frequency resolution of the coefficients given in f_hat. For equidistant FFT, there is is a relationship for df, which is the reciprocal of M * dt. This obviously does not apply here, since df stays always at df = 1 Hz. Is there a chance to change this behavior, or am I doing something wrong in principle? Here is my little test to check the right frequencies and amplitudes. I can see that the 200 Hz, for example, is always at index 200.

import numpy as np
import nfft as nf

n = 2**13
dt = 1.0e-4

x = np.linspace(0.0, n*dt, n)
y = 2. * np.sin(2. * np.pi * 2.5 * x) + 0.5 * np.sin(2. * np.pi * 50. * x) + 1.25 * np.sin(2. * np.pi * 200. * x)

f_hat = nf.nfft_adjoint(x, y, n)

import matplotlib.pyplot as plt
plt.plot(2.0/n * np.abs(f_hat[n//2:]))

plt.grid()
plt.xlim(195, 205)

plt.savefig('test.png')

THX! Patrick

UDEPWollny commented 3 years ago

I'll now answer my own question in case someone had the same issue. The thing is, that df = 1/(M dt) holds. One simply has to scale the total sampling time T = Mdt to unity and apply this to the sampling location vector "x". The frequency is then index * df (with df = 1 / T).