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
fft_out = np.fft.rfft(signal) * dttofft_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