I recently came across the problem that I needed a PWM (i.e. pulse-width modulated rectangular signal) generator block in Simulink with variable frequency and duty cycle inputs. I couldn’t find one in the library, so I wrote one.
The rather simple implementation basically consists of three parts: An integrator block with an external reset input is used to create a sawtooth signal. The sawtooth is then compared to a threshold value given by means of the duty cycle to create a rectangular signal. Lastly that signal is scaled to a standard output range of 0..1.