Square wave osc and pulse width duty cycle

Hi all,

I was wondering, when looking at the square wave when using the osc, i was wondering if there was a way to set the width of the square wave. Example i’m at frequency X but would like the pulse to only be X% wide at that frequency.

Thanks for any thoughts you have on this.


you can easily write your own square oscillator.

an oscillator cycle is
(1.0 / frequency)
seconds long, and therefore lasts
n = sampleRate / frequency
blocks in the output buffer.

of those n blocks you set you desired percentage to 1.0 and the rest to -1.0.

1 Like

Another way to create a PW-variable rectangle wave: subtract two phase-shifted sawtooth waveforms.

1 Like

@Firesledge, @xavierxeon,

Thank you for the info, this is super helpful.



I like this idea, i’m just trying to picture why this works in my head because I don’t quite understand how subtracting these two makes a square wave. Do I need to do something else such as if above 0 it’s a 1?

Sorry, if this feels like a noob question, I just keep learning as I bring up my new hardware. :slight_smile:

Thanks for the feedback and help.


oh - got it!


That is super cool!

Thanks! :smiley:

The image comes from this link for refence:
Sawtooth waves and symmetry (ucsd.edu)


Thanks for the help.

I ended up using this info plus I borrowed from the DaisySP \ Source \ Synthesis \ oscillator.h/.cpp and put the two together. I now have a square wave that can have a duty cycle from 0-1 (0-100%), it can have an amplitude (0-1), and have the phase set.

I just got it setup and running last night and wanted to say thank you for the help.

I hope to share the code back to the community here after a while.