Square Wave Generator
Free, browser-only square / pulse wave generator with an adjustable duty cycle (1 – 99%), live band-limited waveform, and a real-time H1 – H20 harmonic bar chart. Built on Web Audio's PeriodicWave for clean, alias-free synthesis from 20 Hz to 5 kHz.
Source
Spectral character
Square Waves, Duty Cycle & Harmonics
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 uses Web Audio's PeriodicWave which 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.
Practical uses for variable duty
- PWM audio — varying duty cycle on a high-frequency square encodes amplitude information, the basis of class-D amplifiers and digital-to-analog conversion.
- 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 low-pass filter produces the characteristic "fat" sweep sound.
- Test signals — narrow-duty pulses excite a wide harmonic band evenly, useful for impulse-response measurement of speakers, rooms and DSP filters.
- Digital clock signals — most chip clocks are nominal-50%-duty squares; deviation from 50% causes EMI and signal-integrity issues.
Frequently Asked Questions
Why does the waveform look round on the corners?
Why are even harmonics zero at exactly 50% duty?
Where do the spectral nulls move when I change duty?
Why does the audio level barely change between 30% and 70% duty?
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.