3
$\begingroup$

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)

enter image description here

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))

enter image description here

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.

$\endgroup$
6
  • $\begingroup$ 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. $\endgroup$
    – anpar
    Commented Mar 3, 2019 at 13:46
  • $\begingroup$ I don't want to do this for some reasons. Basically, taking the difference of two changing, but phase shifted signals. $\endgroup$
    – Jarno
    Commented Mar 3, 2019 at 21:35
  • 2
    $\begingroup$ 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. $\endgroup$
    – A_A
    Commented Mar 4, 2019 at 8:05
  • $\begingroup$ Ok I tried to give a bit of background, hope that helps. $\endgroup$
    – Jarno
    Commented Mar 4, 2019 at 15:27
  • $\begingroup$ "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? $\endgroup$
    – endolith
    Commented Mar 4, 2019 at 15:44

1 Answer 1

1
$\begingroup$

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)])
$\endgroup$
1
  • $\begingroup$ Looking back at this question, I have to admit that I have actually no idea, what I was trying to do here... $\endgroup$
    – Jarno
    Commented Jan 26, 2022 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.