When you are designing a PWM motor controller, debugging an EMI issue on a high-speed clock line, or sizing an inverter output filter, you need more than just a visual of a square wave on your oscilloscope. You need the exact math. The fundamental formula for a square wave in the time domain relies on its period ($T$) and duty cycle ($D$), while its power delivery is calculated using $V_{RMS} = V_{peak} \sqrt{D}$. For harmonic and EMI analysis, the Fourier series formula defines the wave as an infinite sum of odd sinusoidal harmonics: $V(t) = \frac{4V_p}{\pi} \sum \frac{\sin(n\omega t)}{n}$.

Below is the complete mathematical framework for square waves, including symbol definitions, rearranged forms for quick bench calculations, and fully worked examples with strict unit tracking.

The Core Square Wave Formulas and Symbol Definitions

A square wave is defined by its amplitude, timing, and harmonic content. Depending on whether you are calculating power dissipation (RMS), DC equivalent (Average), or frequency spectrum (Fourier), you will use a different variation of the formula. The table below defines the standard equations used in electrical engineering for a unipolar square wave (switching between $0V$ and $V_p$) and a bipolar square wave (switching between $-V_p$ and $+V_p$).

Table 1: Square Wave Formula Parameters and Definitions
Parameter Formula Symbol Definition Standard Unit
Frequency $f = \frac{1}{T}$ $f$ Number of complete cycles per second Hertz (Hz)
Period $T = t_{on} + t_{off}$ $T$ Total time for one complete cycle Seconds (s)
Duty Cycle $D = \frac{t_{on}}{T}$ $D$ Ratio of high-time to total period (expressed as a decimal) Dimensionless
RMS Voltage (Unipolar) $V_{RMS} = V_p \sqrt{D}$ $V_{RMS}$ Root Mean Square voltage (equivalent DC heating value) Volts (V)
Average Voltage (Unipolar) $V_{avg} = V_p \times D$ $V_{avg}$ DC equivalent voltage over one period Volts (V)
Fourier Series (Bipolar) $V(t) = \frac{4V_p}{\pi} \sum_{k=1}^{\infty} \frac{\sin((2k-1)\omega t)}{2k-1}$ $\omega$ Angular frequency ($2\pi f$), $k$ is the harmonic index (1, 2, 3...) Volts (V), Rad/s

Note: For a detailed breakdown of how these waveforms behave in AC circuits, refer to the Electronics Tutorials guide on electrical waveforms.

Rearranged Forms for Bench Troubleshooting

On the bench, you rarely have all the variables. You might know your target RMS voltage and your supply rail, and need to find the required PWM duty cycle. Here are the rearranged forms of the square wave formulas, optimized for solving for each specific variable:

  • Solve for Period: $T = \frac{1}{f}$
  • Solve for High-Time ($t_{on}$): $t_{on} = D \times T$
  • Solve for Duty Cycle (from time): $D = \frac{t_{on}}{T}$
  • Solve for Duty Cycle (from voltage): $D = \left( \frac{V_{RMS}}{V_p} \right)^2$
  • Solve for Peak Voltage: $V_p = \frac{V_{RMS}}{\sqrt{D}}$
  • Solve for Angular Frequency: $\omega = 2\pi f$

Worked Examples with Unit Tracking

Abstract formulas are useless if you drop a decimal or mix up microseconds and milliseconds. Here are two real-world scenarios with strict unit tracking.

Problem 1: PWM Motor Drive RMS and Average Voltage

Scenario: You are driving a 24V DC motor using a microcontroller PWM signal at a switching frequency of $20 \text{ kHz}$. Your oscilloscope measures the high-time ($t_{on}$) at $15 \text{ \mu s}$. Calculate the duty cycle, the RMS voltage delivered to the motor, and the average voltage.

Step 1: Calculate the Period ($T$)
$$T = \frac{1}{f} = \frac{1}{20,000 \text{ Hz}} = 0.00005 \text{ s} = 50 \text{ \mu s}$$

Step 2: Calculate the Duty Cycle ($D$)
$$D = \frac{t_{on}}{T} = \frac{15 \text{ \mu s}}{50 \text{ \mu s}} = 0.30 \text{ (or 30\%)}$$

Step 3: Calculate the RMS Voltage ($V_{RMS}$)
$$V_{RMS} = V_p \sqrt{D} = 24\text{V} \times \sqrt{0.30} = 24\text{V} \times 0.5477 = 13.14\text{V}$$

Step 4: Calculate the Average Voltage ($V_{avg}$)
$$V_{avg} = V_p \times D = 24\text{V} \times 0.30 = 7.20\text{V}$$

Bench Insight: The motor's inductance will smooth the current, making it respond closer to the 7.2V average, but the resistive heating in the windings is dictated by the 13.14V RMS value.

Problem 2: Fourier Harmonic Magnitude for EMI Filtering

Scenario: You are debugging radiated EMI on a 5V bipolar square wave clock signal ($\pm 2.5\text{V}$ swing, so $V_p = 2.5\text{V}$) running at $10 \text{ MHz}$. You need to calculate the theoretical peak amplitude of the 3rd harmonic to size an LC notch filter.

Step 1: Identify the Harmonic Frequency and Index
Fundamental frequency $f_1 = 10 \text{ MHz}$.
3rd harmonic frequency $f_3 = 30 \text{ MHz}$.
In the Fourier formula $\frac{4V_p}{\pi} \sum \frac{\sin((2k-1)\omega t)}{2k-1}$, the harmonic number $n = (2k-1)$. For the 3rd harmonic, $n = 3$.

Step 2: Apply the Fourier Amplitude Formula
The peak amplitude of the $n$-th harmonic is $V_n = \frac{4V_p}{n\pi}$
$$V_3 = \frac{4 \times 2.5\text{V}}{3 \times \pi} = \frac{10\text{V}}{9.4247} = 1.061\text{V}_{peak}$$

Bench Insight: While the fundamental is $3.18\text{V}_{peak}$, the 3rd harmonic at $1.06\text{V}_{peak}$ is still massive and will easily couple onto adjacent high-impedance traces. For a deeper dive into how these harmonics sum together, review the All About Circuits Fourier Series chapter.

Assumptions, Unit Traps, and Realistic Magnitudes

When the Formula Applies (and When It Doesn't)

The formulas above assume an ideal square wave with zero rise and fall times. In reality, every physical square wave is a trapezoid. The rise time ($t_r$) creates a high-frequency roll-off in the Fourier spectrum. The harmonic amplitudes will follow the ideal $1/n$ curve up to a breakpoint frequency of $f_{break} = \frac{1}{\pi t_r}$, after which they roll off at -20dB/decade, and eventually -40dB/decade. If you are designing an EMI filter for a fast logic gate (like a 74LVC series with 2ns edges), the ideal Fourier formula will overestimate the amplitude of the 100th harmonic by several orders of magnitude.

Unit Mistakes That Break the Math

  1. Using Percentage instead of Ratio for $D$: If your duty cycle is 50%, you must use $D = 0.5$ in the RMS formula. Plugging in $50$ will result in $V_{RMS} = V_p \sqrt{50}$, yielding a mathematically impossible voltage 7 times higher than your supply rail.
  2. Confusing Peak ($V_p$) with Peak-to-Peak ($V_{pp}$): If your oscilloscope reads a 10Vpp bipolar square wave (swinging from -5V to +5V), $V_p$ is 5V. Using 10V in the RMS formula will double your calculated power dissipation.
  3. Timebase Collisions: Mixing milliseconds and microseconds. Always convert $t_{on}$ and $T$ to the exact same base unit (usually seconds or microseconds) before dividing them to find $D$.

What Realistic Answer Magnitudes Look Like

To sanity-check your calculations, keep these benchmarks in mind:

  • 5V Logic (Unipolar, 50% Duty): $V_{avg}$ should be exactly 2.5V. $V_{RMS}$ should be $5 \times \sqrt{0.5} \approx 3.53\text{V}$.
  • 120V Inverter Output (Bipolar, 50% Duty): A modified sine wave inverter outputting a $\pm 170\text{V}$ square wave ($V_p = 170\text{V}$) at 50% duty cycle yields an RMS of $170 \times \sqrt{0.5} \approx 120\text{V}_{RMS}$. This is how cheap inverters fake a 120V RMS sine wave using square waves.
  • Fourier Fundamental: The fundamental sine wave component of a bipolar square wave is always exactly $\frac{4}{\pi}$ (approx 1.27) times the peak voltage of the square wave. If your $V_p$ is 10V, the fundamental cannot exceed 12.7V.

Frequently Asked Questions

What is the mathematical formula for a square wave in MATLAB or Python?

If you are generating a square wave programmatically for simulation, you cannot use the infinite Fourier series due to computational limits and Gibbs phenomenon (ringing at the edges). Instead, use the signum function applied to a sine wave. In Python (using NumPy) or MATLAB, the formula is implemented as y = A * np.sign(np.sin(2 * np.pi * f * t)). This instantly evaluates to $+A$ when the sine wave is positive, and $-A$ when negative, creating a perfect mathematical square wave without calculating infinite harmonics.

How does the formula for a square wave change if it has a DC offset?

When a square wave does not switch between $0V$ and $V_p$, but rather between a minimum voltage ($V_{min}$) and a maximum voltage ($V_{max}$), you must account for the offset. The Average Voltage formula becomes $V_{avg} = V_{min} + D(V_{max} - V_{min})$. The RMS formula requires calculating the root-mean-square of both states: $V_{RMS} = \sqrt{D(V_{max}^2) + (1-D)(V_{min}^2)}$. For example, a signal switching between -2V and +8V at a 50% duty cycle has an average of 3V, but an RMS of $\sqrt{0.5(8^2) + 0.5(-2)^2} = \sqrt{32 + 2} = 5.83\text{V}$.

Why does my multimeter read a different RMS value than the square wave formula predicts?

This is one of the most common bench frustrations. If your formula predicts 3.53V RMS for a 5V 50% duty cycle square wave, but your multimeter reads 2.5V, you are using an average-responding multimeter. Cheap multimeters measure the average voltage and multiply it by 1.11 (the form factor of a pure sine wave) to display RMS. This calibration is completely invalid for square waves. To get a reading that matches the $V_p \sqrt{D}$ formula, you must use a True-RMS multimeter with a bandwidth sufficient to capture the square wave's harmonics (typically at least 3 to 5 times the fundamental frequency to capture the bulk of the signal's energy).