News for hackers. Stuff that matters.
Re:Variable duty cycle square waves with the Web Audio API by timewizard on Monday April 07, 2025 @02:13PM (#43614287)
"Variable duty cycle?"

So just PWM?

Also, if you needed to exceed 50%, you could have just combined two different square waves, out of phase, and you'd be there.

Re:Variable duty cycle square waves with the Web Audio API by iamdan on Monday April 07, 2025 @04:10PM (#43615443)
That's true, I'm coming into the audio and signals world as a beginner so I'm still picking up the lingo but totally! For my purposes I only want to keep track of a single source for a channel so I've gone down this wave transformation path rather than composition but that also would get the job done
Re:Variable duty cycle square waves with the Web Audio API by dimatura on Monday April 07, 2025 @03:36PM (#43615052)
Yeah, at least in the context of music synthesis, everyone just says PWM. (The term even being subject of a meme with a certain youtuber who seems to be fond of it ;)).
Re:Variable duty cycle square waves with the Web Audio API by nayuki on Monday April 07, 2025 @06:11PM (#43616520)
> if you needed to exceed 50%, you could have just combined two different square waves

I don't think this is possible. A balanced square wave has no even harmonics in the frequency domain. Anytime the duty cycle is not 0%, 50%, or 100%, you will have non-zero even harmonics.

A linear combination (scaled sum or difference) of two balanced square waves will necessarily still have all even harmonics at zero, and thus cannot emulate a square wave with a duty cycle different from 50%.

Re:Variable duty cycle square waves with the Web Audio API by timewizard on Monday April 07, 2025 @08:34PM (#43617286)
> Anytime the duty cycle is not 0%, 50%, or 100%, you will have non-zero even harmonics.

The article is specifically trying to achieve those harmonics. This is the initial problem.

> A linear combination (scaled sum or difference) of two balanced square waves will necessarily still have all even harmonics at zero

Yea, that's why you need the phase offset, as I mentioned.

> and thus cannot emulate a square wave with a duty cycle different from 50%.

You can /synthesize/ a square wave at any duty cycle you like. We're still doing Fourier just without the whole transform.