🔲

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

Logarithmic from 20 Hz to 5 kHz so the slider is uniform across decades.
50% = textbook square (odd harmonics only). Anything off-50% generates even harmonics too.
Start low — square waves are loud and harsh. The displayed waveform and spectrum are unaffected by volume.
Idle — press Play.

Spectral character

Waveform
DC offset
= 2D − 1
Strongest harmonic
excluding DC
Odd harmonics
H3, H5, H7… of total
Even harmonics
H2, H4, H6… of total
Total harmonic distortion (THD)
|H₂..H₂₀| RMS / |H₁|
Harmonic amplitude rule
|Hn| = (4 / πn) · |sin(πnD)|
Zeros land at n = 1/D and its multiples. At D = 0.5: sin(πn/2) = 0 for even n → only odd harmonics.
Band-limited waveform (3 cycles)
Harmonic spectrum — H1 through H20 (dB relative to H1)

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?
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.