5
$\begingroup$

How to obtain the correct amplitude after the numerical Fourier transform of a signal?

Example: consider an exponential decaying wave $y(x)=e^{-x}\sin(100\pi x)$ with Fourier transform $y_f(x_f)$ which should be a peak with some broadening. I am searching for the appropriate approach to numerically approximate the continuous function $y_f(x_f)$ as close as possible. The calculation of the approximation of the function $y_f(x_f)$ should thus be independent of the number of samples and the range that y(x_i) is defined.

However, when I calculated the Fourier transform for several x-ranges (N in the code), I obtain different results:

fig, ax = plt.subplots()
N=[600,1200,2400,4800]
for i in N:
    N = i
    # sample spacing
    T = 1.0 / 800.0
    x = np.linspace(0.0, N*T, N)
    y = np.exp(-x/1)*np.sin(50 * 2.0*np.pi*x) 

    #Fourier
    yf = scipy.fftpack.fft(y)
    xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
    yp = 2.0/N * np.abs(yf[:N//2])

    label = "N="+str(N)
    ax.plot(xf, yp, label=label)

ax.legend()
plt.show()

The result gives different peak amplitudes for the different N-cases. The more sample points are taken (higher N), the lower the fft peak value... So, if you take N really high, then the Fourier transform amplitude decreases?

I would expect that the peaks have the same amplitude because $y(x)$ remains the same function in all cases. If the function does not change, the Fourier transform does also not change...

How to obtain the correct peak amplitude, such that the peak value is the same in the three cases?

enter image description here

$\endgroup$
5
  • 1
    $\begingroup$ y(x) does not remain the same in all cases. because in each loop, number of samples and the range that y(x) is defined is changing. $\endgroup$ Jul 26, 2020 at 10:19
  • $\begingroup$ I'm confused over why you're confused. You have a signal with decreasing amplitude (decaying as you say), when you take a longer average, the average height gets lower. Okay, so.....why is that surprising? The DFT is going to give you, in essence, the values of the best fit steady sinusoidal. As your interval gets longer, the bins are going to need a little help from their neighbors to fit the sloping shape. $\endgroup$ Jul 26, 2020 at 11:43
  • $\begingroup$ @m-sh-shokouhi, in continuous space, there is only one solution of the Fourier transform of $y(x)$. I am searching for a numerical approximation, as close as possible, to this continuous one. How do you get rid of the influence of the number of samples and the range over which $y(x)$ is defined? How do you obtain the correct amplitude of the Fourier transform of the function $y(x)$? How do you properly take into account the number of samples and the range over which $y(x)$ is defined? $\endgroup$
    – Frederic
    Jul 26, 2020 at 11:59
  • 1
    $\begingroup$ @Frederic: If you haven't yet, please read this answer. You cannot get rid of the errors introduced by sampling a (non-bandlimited) continuous time function, and you can't get rid of the error introduced by truncating a function with infinite support. You can just try to make these errors small by choosing a sufficiently large sampling frequency, and by considering a sufficiently large time interval. $\endgroup$
    – Matt L.
    Jul 26, 2020 at 12:09
  • $\begingroup$ I'm not sure how that code even works with N as your array and as a variable. Poor practice to do that, even if scoping rules allow it. $\endgroup$ Jul 26, 2020 at 20:33

2 Answers 2

2
$\begingroup$

There are two factors involved here. One has been mentioned in a comment: your truncation error is larger for smaller values of $N$ because you truncate the time domain function at $NT$, where the sampling interval $T$ is constant.

The other factor - which is the more important one here - is that you divide the FFT result by $N$. That's why you see a decreasing amplitude for larger values of $N$.

If you do things right you actually see the opposite effect: with increasing $N$ the amplitude increases as the approximation to the CTFT becomes better. The maximum value at $\omega_0=100\pi$ gets closer to the value of the CTFT, which is very close to $\frac12$:

$$|F(j\omega_0)|=\frac{\omega_0}{\sqrt{1+4\omega_0^2}}\approx\frac12,\qquad \omega_0\gg 1\tag{1}$$

enter image description here

$\endgroup$
3
  • $\begingroup$ Typically, people divide by N to get the correct amplitude (see for example stackoverflow.com/questions/19975030/…). So, you are now saying that I should not divide by N to get the correct amplitude in reciprocal space? $\endgroup$
    – Frederic
    Jul 26, 2020 at 11:54
  • 1
    $\begingroup$ @Frederic: Of course you shouldn't, why would you? As already mentioned in the comments to another question of yours, you should multiply by the sampling interval $T$. If that sampling interval is proportional to $1/N$ then that's just the way it happens to be, but in your case you have a fixed $T$ which is independent of $N$. $\endgroup$
    – Matt L.
    Jul 26, 2020 at 12:02
  • $\begingroup$ So, in general, you have to multiply the Fourier spectrum by the sampling interval T. In some specific cases, T is proportional to 1/N and that's why you see a lot of people doing this! I think I get it now. Thank you a lot Matt L.! $\endgroup$
    – Frederic
    Jul 26, 2020 at 12:14
4
$\begingroup$

Relating the DFT to the FT (CTFT) is a big issue. Let's start with the basic definitions without any domain specification using a $\frac{1}{N}$ normalization on the DFT.

$$ FT(x(t))(f) = \int x(t) e^{-i2\pi t f } dt $$

$$ DFT(x[n])(k) = \frac{1}{N} \sum x[n] e^{-i 2\pi \frac{n}{N} k } $$

They are obviously similar. Assuming they cover the same interval, we can map the domain variables to each other.

$$ \begin{aligned} t &= \frac{n}{N} = \frac{1}{N} \cdot n \\ f &= k \\ ( \Delta t &=\frac{1}{N} ) \to dt \end{aligned} $$

[Edit: Does anybody want to argue the (1/N) doesn't belong in the DFT definition?]

The units of $f$ and $k$ are cycles per the interval.

Of course, we are assuming the sequence of values matches the function at every point.

$$ x[n] = x(t) $$

The FT is defined on a domian of $-\infty$ to $\infty$, and the the DFT is defined on a domain of $0$ to $N-1$. The corresponding $t$ values are $0$ and $\frac{N-1}{N}$. We can now include the domains in the definitions for a direct comparison on the same interval.

$$ FT_{DFT}(x(t))(f) = \int_{0}^{\frac{N-1}{N}} x(t) e^{-i2\pi t f } dt $$

$$ DFT(x[n])(k) = \frac{1}{N} \sum_{n=0}^{N-1} x[n] e^{-i 2\pi \frac{n}{N} k } $$

To make the $FT_{DFT}$ transform fit the $FT$ transform, we have to introduce a rectangular window function on the interval.

$$ w(t) = \begin{cases} = 0 & t < 0 \\ = 1 & 0 \le t < 1 \\ = 0 & 1 \le t \\ \end{cases} $$

$$ FT_{DFT}(x(t))(f) = \int_{-\infty}^{\infty} w(t) x(t) e^{-i2\pi t f } dt $$

Please notice that there is no window function in the DFT! The window function is in the FT.

From these definitions, it is very clear that the FT is the limit of the DFT with a $\frac{1}{N}$ normalization factor as $N$ goes to infinity.

$$ \begin{aligned} \lim_{N \to \infty} DFT(x[n]) &= FT_{DFT}(x(t)) \\ \lim_{N \to \infty} \frac{1}{N} \sum_{n=0}^{N-1} x[n] e^{-i 2\pi \frac{n}{N} k } &= \int_{-\infty}^{\infty} w(t) x(t) e^{-i2\pi t f } dt \\ \end{aligned} $$

This is the "right" way to view it. An integral is the limit of a summation.

It is possible to to view the DFT as a sampled version of the FT by using a second window "function" (loosely speaking) of a train of Dirac deltas. But this is not the definition of the DFT, requires advanced Real Analysis understanding, and you are merely getting back to the definition by working backwards.

Some EE professors neglect to mention this, some just don't know it.

So, just like you can approximate an intergral with a summation, you can approximate an FT on a finite interval with a $\frac{1}{N}$ normalized DFT.

To get intervals other than $[0,1)$ on $t$, call your new time $\tau$ and rescale it.

$$ t = \frac{\tau}{T} = \frac{n}{N} $$

Feel free to shift it too.

$$ t = \frac{\tau- \tau_0}{T} = \frac{n}{N} $$

This makes the sequence definition equivalent to

$$ x[n] = x_{\tau}(\tau) = x_{\tau}\left( \tau_0 + \frac{T}{N}n \right) $$

The DFT does not care what your sampling rate is. aka $\frac{N}{T}$ with these names nor where you place the frame, $\tau_0$.


Followup:

Questions from the OP's comment under the post question:

(All answers assume you are dealing with a pure real tone with a slighlty varying envelope.)

"How do you get rid of the influence of the number of samples and the range over which y(x) is defined? "

By framing your interval on a whole number of cycles. This is the principal justification in my mind for a $\frac{1}{N}$ normalization factor of the DFT. On a whole number of cycles (for a pure steady tone) a single conjugate pair of bins are non-zero. The value in the bin is independent of N when normalized. Thus the reading you get matches the continuous case exactly and is independent of the sample count. Also, there is no need to calculate the whole DFT (take a FFT), a single bin calculation will do. With a consistent frequency having a set of basis vectors is most efficient. With a varying frequency, a Goertzel calculation yields the equivalent more efficiently.

"How do you obtain the correct amplitude of the Fourier transform of the function y(x)? "

With a single bin, the magnitude of the bin is half the amplitude of the tone. A small DFT frame size of 2 to 3 cycles in length ensures that the reading is fairly accurate on that interval. The reading will be that of the best fit sinusoid on that frame.

"How do you properly take into account the number of samples and the range over which y(x) is defined? "

Not sure what you mean by this. The sample rate and signal frequency define how many samples per cycle you will have. Decide the number of cycles you want, multiply that by the number of samples in a cycle and you get the sample count (N) for that frame. If you are calculating a single bin, there is no special advantage to powers of two or any other factor.

$\endgroup$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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