how to use
dtsonov opened this issue · comments
Hello,
I installed NFFT and tried forward and backward transform, as is shown on the examples.
After backward transform the input data I got is very different from the original input data.
Probably I'm not using the transform correctly. Would some one help with it.
Here is my code:
import nfft
import numpy as np
import matplotlib.pyplot as plt
data = np.array([1.431, 1.4312, 1.4314, 1.4315, 1.4314, 1.4309, 1.4309, 1.4306, 1.4303,
1.4299, 1.4299, 1.4297, 1.43, 1.4299, 1.4302, 1.4299, 1.4297, 1.4296,
1.4299, 1.4298, 1.43, 1.4298, 1.4298, 1.43, 1.4301, 1.4301, 1.4304,
1.43, 1.43, 1.4303, 1.4304, 1.4305, 1.4303, 1.4303, 1.4306, 1.4304,
1.4309, 1.4305, 1.4306, 1.4306, 1.4296, 1.4283, 1.4288, 1.4295, 1.4298,
1.4294, 1.4291, 1.4288, 1.4284, 1.4287, 1.4289, 1.4289, 1.429, 1.4288,
1.4286, 1.4283, 1.4276, 1.4276, 1.4279, 1.428, 1.4279, 1.4281, 1.4274,
1.4273])
x = -0.5 + data
f = np.sin(10 * 2 * np.pi * x)
k = -20 + np.arange(40)
#forward Fourier
f_hat = nfft.nfft_adjoint(x, f, len(k))
plt.title('fft transform')
plt.plot(k, f_hat.real, label='real')
plt.plot(k, f_hat.imag, label='imag')
plt.show()
#backward Fourier
recf = nfft.nfft(x, f_hat)
plt.title('backward transform')
plt.plot(np.abs(recf))
plt.show()
#input data
plt.title('input data')
plt.plot(data)
plt.show()
Yes, the documentation is not great, and I'm not sure I'll have time to address that any time soon. One thing: it's strange that you're subtracting 0.5 from your data. The reason I do that in the example is to get data that lies in the range [-0.5, 0.5]. Your data don't lie between 0 and 1, so I can't see any reason why you'd want to subtract 0.5.
Also, if you plot f vs x, you'll see that your data is effectively a straight line... I wouldn't expect an fft to pull out meaningful frequency information from a non-periodic signal.
When you plot your input data, you use plot.plot(data)
. That is not your input data: your input data is plt.plot(x, f)
Here's how you can approximate the input data for an NFFT by doing a round-trip: note that it takes a LOT of frequencies to get a close approximation: this is because the effective Nyquist frequency is very large when data are not equally spaced.
import nfft
import numpy as np
import matplotlib.pyplot as plt
x = 100 * np.random.rand(1000)
f = np.sin(x / 10)
N = 1000000
k = -N / 2 + np.arange(N)
f_hat = nfft.nfft_adjoint(x, f, N)
f_reconstructed = nfft.nfft(x, f_hat) / N
plt.plot(x, f, '.', color='blue')
plt.plot(x, f_reconstructed, '.', color='red')