Frequency Component Energy
Implement a function to calculate the energy of a signal from its Discrete Fourier Transform (DFT) magnitudes. This task involves understanding the relationship between a signal's time-domain representation and its frequency-domain representation. The Fourier Transform is a mathematical tool used to decompose a signal into its constituent frequencies, and the DFT is a discrete-time equivalent.
The energy of a signal can be computed using the time-domain representation, but it can also be calculated using the frequency-domain representation, thanks to Parseval's theorem. This theorem states that the energy of a signal is equal to the sum of the squared magnitudes of its frequency components.
To compute the energy, follow these steps:
- Square each magnitude of the DFT
- Sum the squared magnitudes
- Divide the sum by the total number of frequency components, N
This technique is widely used in image processing to analyze the frequency components of an image.
Example:
signal_energy([2, 0, 2, 0])
2.0
- The input list is interpreted as the DFT magnitudes: X=[2,0,2,0].
- Compute squared magnitudes and sum: ∣2∣2+∣0∣2+∣2∣2+∣0∣2=4+0+4+0=8.
- There are N=4 frequency bins, so energy is E=N1​∑k=03​∣X[k]∣2=41​⋅8=2.0.
- Thus,
signal_energy([2, 0, 2, 0])returns 2.0.
Constraints:
- Return energy rounded to 4 decimal places
To solve this problem, you mainly need to understand what the DFT represents and how Parseval’s theorem lets you compute signal energy from its frequency-domain magnitudes.
1. Background Knowledge (Key Concepts)
-
DFT and Magnitude A discrete-time signal x[n], n=0,…,N−1, has a Discrete Fourier Transform (DFT) X[k], k=0,…,N−1. Each X[k] is complex and can be written as X[k]=ak​+jbk​, and its magnitude is ∣X[k]∣=ak2​+bk2​​. The squared magnitude is ∣X[k]∣2, which corresponds to the power (or energy contribution) at frequency bin k.
-
Parseval’s Theorem (Discrete Form) For the DFT, Parseval’s theorem states that the total energy in time domain equals the total energy in frequency domain, up to a scaling factor that depends on the DFT normalization convention. For the common convention used in your problem:
E = N1​\sum_{k=0}^{N-1} |X[k]|^2
Do not just return the raw sum. - **Using magnitude instead of squared magnitude** You must use $|X[k]|^2$, not $|X[k]|$. Make sure to square the magnitude. - **Confusion about input type** - If the input is complex DFT coefficients, compute $|X[k]|^2$ via real/imag parts. - If the input is already magnitudes, do **not** take another square root; just square the given value. - **Integer overflow or precision issues** Use a floating-point type (double / float64) for the accumulator, especially if values can be large or $N$ is big. - **Off-by-one in loops** Make sure you cover indices from 0 to N-1 inclusive. ### 5. Time & Space Complexity - **Time Complexity** - One loop over all $N$ frequency bins, each doing $O(1)$ work. - Total time: **$O(N)$**. - **Space Complexity** - If you process the given array in-place or read-only, you only need a constant number of extra variables. - Extra space: **$O(1)$**.