# Evaluate Fourier coefficients at arbitrary point using Python

Lets say I have a sinusoidal function $$s$$ that looks like

import numpy as np
import matplotlib.pyplot as plt

s = lambda t: np.sin(2 * np.pi * t / 13)
s_sampled = np.array([s(i) for i in range(50)])
plt.plot(s_sampled)


Now I want to take the Discrete Fourier transform of the signal and reconstruct the whole original signal. As the signal only has a period of 13. It should be enough to Fourier transform the first 13 points.

fourier_coefficients = np.fft.fft(s_sampled[:13])


However, using the numpy's inverse transform I can only get back 13 points.

plt.plot(np.fft.ifft(fourier_coefficients))


How can I get the rest of the (now just repeating) signal back? I have tried to build a function myself, but it does not quite work out...

def inverse_fourier(x, t):
"""Evaluate Fourier coefficients at point t.

Args:
x: The Fourier coefficients.
t: The time point at which to evaluate the function.
"""
x = x.flatten()
n = len(x)
k = np.arange(n)
y = x @ np.exp(2j * np.pi * t * k / n)

return y / n

plt.plot(np.array([inverse_fourier(fourier_coefficients, t) for t in range(50)]))


To give a bit more detail on the problem. I want to take the approximate difference between to signals that vary between different sinusoids over time, but are inherently phase shifted. So my idea was to take the last $$k$$ timepoints, estimate the Fourier coefficient, take the difference in Fourier space and evaluate the resulting function represented by the coefficients at time point $$t$$. Therefore, I want to know how I can evaluate the coefficients at $$t$$. I know the periods of the signals that I am varying between, so can go back enough $$k$$ points to capture all relevant frequencies.

• Compute the DFT of the complete signal (instead of the first 13 samples) and then you'll be able to retrieve the complete signal using the inverse DFT. Commented Mar 3, 2019 at 13:46
• I don't want to do this for some reasons. Basically, taking the difference of two changing, but phase shifted signals. Commented Mar 3, 2019 at 21:35
• Do you think you can add a bit more detail about what are you trying to achieve here? The DFT will return the frequency alright and with a bit more work you could even recover the initial phase. Once you have these two components, you can generate a sinusoid "for ever". The other option would be to have a PLL (or PLL-like mechanism) lock on to your incoming frequency which would recover frequency and phase too but in a slightly different way.
– A_A
Commented Mar 4, 2019 at 8:05
• Ok I tried to give a bit of background, hope that helps. Commented Mar 4, 2019 at 15:27
• "approximate difference between to signals that vary between different sinusoids over time, but are inherently phase shifted." You want to take the difference of their amplitudes? Or compare their phases? Or what? Commented Mar 4, 2019 at 15:44

Maybe I'm missing something. In your code, set

ic =  np.fft.ifft(fourier_coefficients)


and test

np.linalg.norm(ic - s_sampled[0:13])
# Answer is really small, indicating that these two vectors are equal


This shows that you got the first 13 elements accurately.

Further, test

np.linalg.norm(ic - s_sampled[13:26])
# These two vectors are also equal


Thus, s_sampled consists of ic, repeated multiple times.

What exactly is missing?

PS: There is a bug in the code in your question.

s_sampled = np.array([p1(i) for i in range(50)])


should be

s_sampled = np.array([s(i) for i in range(50)])

• Looking back at this question, I have to admit that I have actually no idea, what I was trying to do here... Commented Jan 26, 2022 at 6:15