# Energy calculation in frequency domain

I was just wondering... The formula I learned to calculate the energy of the signal is expressed in the time domain:

$E_x^{\text{time}} = \sum_{n=-\infty}^{\infty} |x[n]|^2$

Then, what does the amount of energy gotten from the magnitude spectrum mean?

$E_x^{\text{frequency}} = \sum_{f=0}^{fs/2} |X[f]|^2$

Supposing that both amplitudes are expressed in decibels, $fs/2$ is the maximum representable frequency (by Nyquist theorem), and $E_x^{\text{frequency}}$ is taken over the same time frame as $E_x^{\text{time}}$. Should the quantities be the same?

• First of all second equation is not applicable to logarithmic scale as addition does not hold. Secondly Assuming that $X(f)$ is correctly scaled magnitude (taking windowing function into account, i.e. division by number of samples and doubling the energy from negative frequencies), then yes - these to should be the same. – jojek Jan 27 '15 at 17:54
• Quick example in Python (I am not taking amplitudes up to $f_s/2$ because it is bit pointless. If you want, then don't forget that DC coefficient is not multiplied by 2 when restoring energy. import numpy as np;  # Number of samples N=100;  # Generate the time domain signal  x=np.random.randn(N); # Get the magnitude  X=np.abs(np.fft.fft(x));  # Calculate the energies  Et=np.sum(x**2);  Ef=np.sum(X**2)/N;  print "Et = %.1f" % Et;  print "Ef = %.1f" % Ef; – jojek Jan 27 '15 at 18:16
• One thing to keep in mind: if $x[n]$ is periodic, then its energy is infinite, and it's better to measure its power. The FFT assumes the signal is periodic. You need to be careful and make sure you understand what you're measuring. – MBaz Jan 27 '15 at 18:35

$$\sum_{n=-\infty}^{\infty}|x[n]|^2=\int_{-1/2}^{1/2}|X(f)|^2df\tag{1}$$
where $f$ is normalized by the sampling frequency. For the DFT you have
$$\sum_{n=0}^{N-1}|x[n]|^2=\frac{1}{N}\sum_{k=0}^{N-1}|X[k]|^2$$