0

In this question we have a mathematical proof that the DC component of normalized discrete Fourier transform should be the same as the signal's arithmetic mean. However, in the following example I print the signal mean and magnitude of DC component, but they are different. What is the reason behind this?

import numpy as np
import matplotlib.pyplot as plt

T = 1.95 dt = 0.001 Freq = 8.0

t = np.arange(0, T, dt) signal = 0.6 * np.sin(2.0 * np.pi * Freq * t) + 1.5

fft_out = np.fft.rfft(signal) * dt fft_freq = np.fft.rfftfreq(len(t), dt) fft_mag = np.abs(fft_out) fft_mag[0] /= 2.0

plt.figure() plt.subplot(2,1,1) plt.plot(t, signal) plt.subplot(2,1,2) plt.plot(fft_freq, fft_mag) plt.show()

print(signal.mean()) print(fft_mag[0])

Output:

1.511161712096421
1.4733826692940106
Cloudy
  • 111
  • 2

1 Answers1

1

The question you cite proofs this for Continuous Fourier Transform, your code however uses the Discrete Fourier Transform which has significantly different properties.

The DFT is typically defined as

$$X[k] = \sum_{n=0}^{N-1} x[n] e^{-j2\pi\frac{kn}{N}} \leftrightarrow x[n] = \frac{1}{N} \sum_{k=0}^{N-1} X[k] e^{j2\pi\frac{nk}{N}} $$

For the DC component we get

$$X[0] = \sum_{n=0}^{N-1} x[n] $$

so it's the sum of the input samples not the mean. There are different scaling conventions for which this is different, but the one I'm using here is most commonly used in textbooks and it's also what the Python function uses. To get the correct results

  1. Do NOT scale by dt (why ???)
  2. Do NOT scale by $1/2$ (why ???)
  3. DO scale by $1/N$ where N is the FFT length
Hilmar
  • 44,604
  • 1
  • 32
  • 63
  • Thank you! I get the correct result now. I only need to change fft_out = np.fft.rfft(signal) * dt to fft_out = np.fft.rfft(signal) * 2.0 / len(t). However, after this we still need to scale the magnitude of DC component by $1/2$. This is because FFT spreads half of the energy on negative frequency domain, and we are only calculating magnitude from the positive frequency domain, so we need to multiple the magnitude of other frequency by 2. For DC component, this is no spread, so we need to further scale it down by $1/2$. – Cloudy Nov 08 '23 at 11:19
  • Multiplying with 2 and then with $1/2$ seems unnecessarily complicated to me. $X[0] = \sum x[n]$, that's all there is to it. If you want the mean, divide by N. – Hilmar Nov 09 '23 at 10:50
  • Because I also need to make sure that the magnitude of other frequency components are correct. – Cloudy Nov 10 '23 at 11:17