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.

The edge or level triggered reset input can be enabled in the integrator’s block properties. The block is then wired to integrate the frequency value and be reset whenever the output crosses the value of one. This can be realized by subtracting one and comparing the result to zero.

The resulting sawtooth signal then looks like the following, assuming a constant frequency of 1Hz:

By subtracting the sawtooth from the duty cycle “factor” and taking the sign, the rectangular signal is created. Since the sign block creates values in the range of -1..1, a shift and scale is applied to bring it to the range of 0..1.

This is what the result looks like with a frequency of 1Hz and a duty cycle of 25% (like in the first picture):

When applying a frequency ramp – 1Hz/s in the following example – with the same duty cycle of 25%, a chirp-like output signal is created:

And this is what it looks like when keeping a constant frequency (1Hz again) while ramping down the duty cycle by 25%/s from 100%:

That last example is a bit counter-intuitive though as the duty cycle is reduced continually (as opposed to every second), which is – for example – why there is no segment of 100% duty cycle; but you’ll get the idea.

Thank you for this model, I found it very useful. With my solver settings however, I was getting into an algebraic loop at the trigger.

I added a memory block on the integrator feedback loop, and it worked well for me.

Thanks again!

Hi there,

Thanks for useful information. I have a question about amount of frequency. How can I calculate it? for example I want frequency about 1MHz, What shall I do?

I’m afraid I don’t understand your question, since for this model the frequency is a required parameter. You can (re-)calculate the frequency from the output, if that is what you mean, by either counting all rising edges (low-to-high jumps) per given time (100 rising edges per second makes 100 Hz) or determine the time between two rising edges, as this is the period length. Is that what you mean?

The algebraic loop can be avoided when the feedback is done from the state port of the integrator instead of the output of the integrator. To have the state port available you need to set “Show state port” in the block parameters.

if I have total time and duty cycle as input then how to generate PWM signal without using generator block/signal simply using mathematical operational block in Simulink.

As Ulrich pointed out, there will be an algebraic loop when using simple operators, because you’ll have to feed back the result on the operation itself (think x=x+1, which is invalid), so you will need at least some sort of delay. That said, the simplest form of integration is addition.

If you divide your duty cycle by the timeframe, you’ll obtain the frequency. The rest would be as shown.

Hi,

Can you tell me how can I generate a sawtooth wave whose frequency/time is changing in every cycle.

The sawtooth is the core trick in this model and is shown in the first plot. The right half of the model is about converting the sawtooth into a square wave under consideration of the duty cycle. Just leave that out and you should be good to go.

Hi,

I couldn’t able to find integrator block,please help me

Hi,

This code is quite helpful. Thanks. I have a question. I have to create PWM signal with variable frequency. I am applying input with external USB joystick device. But when i apply, some error occurs. Joystick’s output is analog (not exactly ramp function, it looks like step input). What can i do? Thanks.

Hello! Your model is awesome! Thank you very much! I adopted Ulrich’s solution and it works fine

How can I generate a variable for this kind duty cycle.

I got a block like this for a specific hardware. A simple constant is not enough. I want to generate a sine wave out of the hardware withe this pwm-block.

Can you help me please?