A square wave is a binary signal that flips between two values for fixed fractions of each cycle. When the fractions are equal — the symmetric "50% duty" case — the result is the textbook square wave with the famous odd-only harmonic series. When the high and low fractions are unequal, you get a pulse wave with a richer spectrum that includes even harmonics and specific spectral nulls. Both come straight from the same Fourier-series formula.
The amplitude rule
For a bipolar square / pulse wave with duty D (fraction of the period spent at the high level), the n-th harmonic's magnitude is |Hn| = (4 / πn) · |sin(πnD)|. Three immediate consequences:
- The fundamental (n=1) has amplitude 4/π · |sin(πD)|, peaking at 4/π ≈ 1.273 when D = 0.5.
- At D = 0.5, sin(πn/2) = 0 for every even n → only odd harmonics, with amplitudes 4/(πn). H3 sits at −9.54 dB below H1, H5 at −13.98 dB, H7 at −16.90 dB, etc.
- For any duty D, the harmonics at integer multiples of 1/D are zero. At D = 0.25 you get nulls at H4, H8, H12, … At D = 0.10 nulls at H10, H20, H30.
The DC offset
If high = +1 and low = −1, the time-average value is DC = 2D − 1. So a 25% duty pulse sits at −0.5 V average. Most audio amplifiers are AC-coupled and will block this DC, but if you're driving anything DC-coupled (oscilloscope DC input, lab DUT) you'll see the offset. The tool reports the DC value; the audio output is always zero-mean because Web Audio's PeriodicWave never includes a DC component (the spec discards real[0] and imag[0]).
Why "band-limited" matters
An ideal square wave has infinite bandwidth — vertical edges hit DC to infinity. Any digital audio system has a finite sample rate (typically 44.1 or 48 kHz), so trying to play a "true" square at 440 Hz aliases higher harmonics down into the audible band, producing nasty inharmonic noise. This tool relies on band-limited synthesis: Web Audio's PeriodicWave constructs the wave from only the first 40 harmonics — the result is alias-free but shows the classic Gibbs ringing overshoot at each transition. That ringing is real and audible; it isn't a bug. To verify what the generator actually produces through your audio hardware, point the harmonic balance analyzer at your speaker — it will capture the live spectrum and compare it to these theoretical bar-chart values.
Practical uses for variable duty
- PWM audio — a variable duty cycle on a high-frequency square encodes amplitude information, the basis of class-D amplifiers and digital-to-analog conversion. The same pulse-width modulation principle underlies 1-bit DACs (sigma-delta converters) found in every modern audio interface.
- Subtractive synthesis — vintage analog synths (Minimoog, Prophet 5, Juno-60) use a duty-modulated pulse oscillator as one of their primary timbres. PWM through a voltage-controlled low-pass filter (VCF) produces the characteristic "fat" chorus-like sweep sound; the sawtooth wave generator shows the complementary oscillator waveform these synths pair with the pulse.
- Test signals — narrow-duty pulses (<10% duty) excite a wide harmonic band with nearly uniform energy, useful for impulse-response measurement of loudspeakers, rooms, and DSP filters. The harmonic amplitudes from the formula above let you correct for the non-flat spectral envelope in post-processing.
- Digital clock signals — most chip clocks are nominal-50%-duty squares; deviation from 50% causes duty-cycle distortion (DCD), leading to EMI and signal-integrity timing errors in high-speed logic.
Why does the waveform look round on the corners?
You're seeing the band-limited reconstruction. Real digital audio can't reproduce infinitely-fast edges; the tool sums only the first 40 harmonics, so the transitions take a finite time and exhibit Gibbs ringing (the small overshoot just past each step). This is what your speaker actually plays — drawing perfectly vertical edges would be lying about what you'll hear.
Why are even harmonics zero at exactly 50% duty?
The amplitude formula is (4/πn) · |sin(πnD)|. At D = 0.5, sin(πn · 0.5) = sin(nπ/2) which is 0 for any even n (n=2 gives sin π = 0; n=4 gives sin 2π = 0; etc.). It's a clean algebraic zero, not a numerical accident — symmetric square waves contain no even-harmonic content by exact mathematical identity.
Where do the spectral nulls move when I change duty?
Nulls land at every n that's an integer multiple of 1/D. So D = 0.5 → nulls at all even n; D = 0.33 → nulls at H3, H6, H9, …; D = 0.25 → nulls at H4, H8, H12; D = 0.10 → nulls at H10, H20. As D drifts away from a simple fraction, the nulls land at non-integer positions that aren't actual harmonics, so the spectrum becomes dense and even.
Why does the audio level barely change between 30% and 70% duty?
Because we set disableNormalization: false in the PeriodicWave constructor — Web Audio rescales the peak amplitude of each wave shape back to ±1 so you hear consistent loudness across duty cycles. That's a deliberate choice for usability; if you wanted the literal energy difference (narrow pulses are quieter than 50% squares at the same peak), you'd switch normalisation off.
Can I generate sub-audio (DC-ish) or ultrasonic squares?
The slider caps at 5 kHz because higher fundamentals start aliasing the 40-harmonic synthesis: H40 at 5 kHz is 200 kHz, well above the 24 kHz Nyquist of a 48 kHz audio context, so the cosine sum will fold back. At lower fundamentals (e.g. 20 Hz) the 40-harmonic span reaches 800 Hz — plenty of headroom, no aliasing. For ultrasonic or sub-audio applications you'd want a custom AudioWorklet that adjusts the harmonic count to fit Nyquist; outside this tool's scope.
Why is the spectrum bar chart not what I see on a real spectrum analyser?
Two reasons. (1) Real spectrum analysers measure the actual played signal after going through the speaker, ADC, mic, and room — each of which colours the spectrum. The bar chart here is the generated spectrum, exactly the harmonic coefficients fed to the oscillator. (2) The chart shows only 20 harmonics. Above H20 there's more content (we synthesise up to H40) that doesn't appear on screen.
Is this safe for my speakers / ears?
Square waves are louder per peak-volt than sine waves of the same amplitude because their RMS value is higher (1.0 vs 0.707), and their harmonic energy concentrates power into bands where ear and speaker response is most sensitive. Start at the default 10% volume and increase gradually. Avoid extended high-volume listening — even compared to harmless sine tones at the same setting, square waves can cause hearing fatigue much faster.
Does playing a square wave hurt my speakers?
Not at sensible volumes — speakers happily reproduce squares as they're just sums of sine harmonics. The risk is at very high volumes clipping the amplifier: the resulting flat-topped output is essentially a louder, more harmonic-rich square, dumping unusual amounts of high-frequency energy into the tweeter, which can damage it. As long as your amp isn't clipping, square-wave playback is no worse than any other complex signal.
What does the THD readout actually mean in practice?
Total harmonic distortion (THD) here is the RMS sum of harmonics H2 through H20 divided by H1. For a 50% duty square wave the theoretical THD is about 48% — that's not a flaw, it's the defining characteristic of a square wave (a rich harmonic series). The readout is most useful when you've moved away from 50%: it tells you how much of the signal's energy sits in overtones versus the fundamental, which directly affects perceived brightness and timbre. Audio equipment specs quoting THD <0.01% refer to how little a device distorts a pure sine; a square wave's own THD is structural, not a distortion artifact.
How is this different from PWM used in class-D amplifiers?
The principle is the same — a pulse wave whose duty cycle encodes a signal — but class-D amplifiers run at switching frequencies of 300 kHz to 1 MHz, far above the audio band, then use a low-pass output filter to recover only the audio-frequency component. This tool generates audible square waves at 20 Hz–5 kHz, so the fundamental and its harmonics are all within the listening range. Think of this as the "audio-rate" version of PWM, useful for understanding the spectral mechanics before applying the concept to ultrasonic switching circuits.
Can I use this to test the frequency response of an amplifier or filter?
Yes, with important caveats. A square wave is a convenient multi-frequency test signal: set a fundamental (say 1 kHz) and the output simultaneously exercises the device at 1 kHz, 3 kHz, 5 kHz, 7 kHz, and so on up to H20. By routing this through a device and comparing the output shape to this page's theoretical waveform, you can spot bandwidth limiting (rounded edges), phase shift (asymmetric ringing), or low-frequency droop (sagging flat portions). For a more detailed frequency sweep, use our
online tone generator to probe individual frequencies, or our
FFT frequency analyzer to capture the actual spectrum of the device output.