0
0
Power Electronicsknowledge~5 mins

Total Harmonic Distortion (THD) in Power Electronics - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Total Harmonic Distortion (THD)
O(n)
Understanding Time Complexity

When calculating Total Harmonic Distortion (THD), we want to know how the work needed grows as we include more harmonics.

We ask: How does the calculation time increase when we analyze more harmonic frequencies?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


// Calculate THD by summing squares of harmonic amplitudes
function calculateTHD(harmonics) {
  let sumSquares = 0;
  for (let i = 1; i < harmonics.length; i++) {
    sumSquares += harmonics[i] * harmonics[i];
  }
  return Math.sqrt(sumSquares) / harmonics[0];
}
    

This code calculates THD by looping through all harmonic amplitudes except the fundamental, summing their squares, then dividing by the fundamental amplitude.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Looping through the harmonic amplitudes array once.
  • How many times: Exactly once for each harmonic beyond the first (fundamental).
How Execution Grows With Input

As the number of harmonics increases, the number of operations grows directly with it.

Input Size (n)Approx. Operations
109 additions and multiplications
10099 additions and multiplications
1000999 additions and multiplications

Pattern observation: The work grows in a straight line as more harmonics are added.

Final Time Complexity

Time Complexity: O(n)

This means the time to calculate THD increases proportionally with the number of harmonics analyzed.

Common Mistake

[X] Wrong: "Calculating THD takes the same time no matter how many harmonics we include."

[OK] Correct: Each additional harmonic adds more calculations, so the time grows with the number of harmonics.

Interview Connect

Understanding how calculation time grows with input size shows you can analyze and explain performance, a useful skill in many technical discussions.

Self-Check

"What if we only calculated THD using every other harmonic? How would the time complexity change?"