# Formula/mathematical expression for PWM

For a simple AM, we multiply a low frequency message signal ( $\sin(t)$) with a high frequency carrier ($\sin(10^6t)$) to produce two high frequency signals :
$$\sin(t)*\sin(10^6t) = \dfrac{1}{2}\left(\cos[(10^6+1)t] + \cos[(10^6-1)t] \right)$$

I'm hoping there exists a similar formula when we do PWM, but wiki and other sources talk about many other things except a formula like above. So I'm posting this question here. How to represent the output of the comparator waveform mathematically ? Like, the input modulating signal is $\sin(t)$, and the input triangular waveform may be some piecewise linear function ? Then what will be the expression for the comparator output ? I mean exactly what frequencies does the output contain ?

• On time domain this is difficult, there is no standard math notation for "rectangular waveform with varying duty-cycle". A comparator function can be generically written as sgn of a difference. But on frequency domain you can get a more general understanding of the spectral peaks. Look after "PWM Bessel peaks" on google. This is a good hit:pdfs.semanticscholar.org/7e5b/… . Another one: bib.irb.hr/datoteka/… – Vicente Cunha Jun 7 '18 at 1:40
• @VicenteCunha Thank you. I'm going through that pdf.. It seems they're using $sgn$ for the polarity of output, nice.. – AgentS Jun 7 '18 at 1:45

When modulation Vpp matches triangle Vpp you get 0 to 100% duty cycle and both are within the input Vcm range.

The challenge for fine tuning, if needed , comes from having precise input levels and null or low input offset.

The output frequency is chosen just high enough to prevent LED flicker or motor aliasing with commutation. Usually around 1kHz to 10kHz in some uC with built in PWM or 20kHz to 5MHz for SMPS buck regulators or say >=50kHz for class D audio.

A square wave has only odd harmonics and a narrow pulse has every harmonic up to the period of the pulse and then repeating harmonics up to the period matching the rise time.

• Oh so for the frequency of triangle wave, we have to also consider the devices that we connect the PWM signal output to. Good to know. As of now, I don't know fourier much but if I understand correctly, a narrow pulse width contains the maximum sinusoid frequency whose period equals the pulse width. Is that right ? Thank you :) – AgentS Jun 7 '18 at 1:54
• If the output frequency depends on the pulse width, then it contains all the frequencies, because the comparator output can contain any pulse width. This seems like an issue for bandwidth ? – AgentS Jun 7 '18 at 1:57
• Yes the Bandwidth is very high but in an unlimited channel, no sweat, so just limited by rise time so not efficient for BW but cheap and efficient for power – Tony Stewart Sunnyskyguy EE75 Jun 7 '18 at 3:13

I was looking for an answer to the same question. With help of your picture with the carrier and comparator, I might have a solution:

In time domain, a sawtooth wave can be described as: $$sawtooth (t) = A \cdot \biggl( \dfrac{t} {T} - \text{floor} \Bigl( \dfrac{t} {T} \Bigr) \biggr)$$

where $$\t\$$ is the time, $$\T\$$ the period and $$\A\$$ the amplitude.

(Probably superfluous, $$\\text{floor}(x)\$$ rounds $$\x\$$ down to the closest integer, e.g. $$\\text{floor}(2.75)=2\$$ ).

For the purpose of comparison (the comparator of your picture), both signals should between the same values, let's choose 0 and 1. (You could also choose -1 and 1).

Therefore, for the carrier signal, choose amplitude $$\A = 1\$$ and period $$\ T = \dfrac{1} {F_{carrier}} \$$

$$carrier(t) = \biggl( F_{carrier} \cdot t - \text{floor} \bigl( F_{carrier} \cdot t \bigr) \biggr)$$

Next, the message signal needs to be fit between 0 and 1. In case of $$\\sin(t)\$$ this becomes

$$message(t) = \dfrac{1} {2} + \dfrac{1} {2} \sin(t)$$

The comparator function can mathematically be expressed using $$\\text{sgn}(x)\$$, so the pulse width modulated wave becomes

$$pwm(t) = \text{sgn} \Bigl( message(t)-carrier(t) \Bigr)$$

I used this to describe an Arbitrary Behavioral Voltage Source in Spice. I needed to adjust the simulation step size to 1/100 of the the carried period time to get decent results.

You can also express a sawtooth wave as function of arctan and cot, check:

I'm hoping there exists a similar formula when we do PWM

Yes there is. Look at the upper waveform in the picture below: -

It has a mathematical expression for the harmonics produced based on pulse width k and period T. Note that d = k/T. Now look at the formula below (taken from that picture): -

$$a_n = \dfrac{2A}{n\pi}\cdot \sin(n\pi d)$$

Note that $a_0$ is just the dc content of the signal.

So your modulation signal amplitude (instantaneous) defines the value of d hence it defines the spectrum of the resulting PWM signal.

• "I'm hoping there exists ... Yes there is" --- No there isn't (in any case, not that I'm aware of, and not what you explained in this answer). The spectrum of a square wave with duty cycle d shows the spectrum of a DC signal modulated. The intuition is that if the signal changes value very slowly compared to the PWM frequency, then it will exhibit the behaviour of a DC signal (but careful: this is similar to the argument that (historically) led engineers to believe that FM modulation would have extra-low bandwidth, when it turned out to be exactly the opposite!) – Cal-linux Oct 10 '18 at 12:23
• @Cal-linux and what historical evidence about FM do you have that shows engineers to be unaware about the spectrum of FM. I think you are being needlessly harsh here. – Andy aka Oct 10 '18 at 13:19
• Perhaps I was. Regarding the FM history: after posting I actually thought about that; in my Analog Communications course, our teacher told us that (that the motivating intuition for FM was the "arbitrarily low" bandwidth), but true: for all I know, that may be just an "engineering urban legend". Maybe a better phrasing would be: your argument is akin to thinking that FM would produce a signal with arbitrarily low bandwidth, just by looking at the effect when considering an "instantaneous" signal value. – Cal-linux Oct 10 '18 at 23:52
• Please don't paraphrase me by talking about FM. I am stating reality about a PWM signal and you are arguing about the maths. You are actually wrong. – Andy aka Oct 11 '18 at 7:29
• I'm not paraphrasing anyone --- I'm just presenting an analogy to indicate why your answer (as an answer to this thread's question) is incorrect; notice the emphasis on "as an answer to this question" --- as far as the specific statements you make, nothing in what you say is factually incorrect, of course. Not sure why you seem bothered that I'm talking about the maths, when the subject of this thread specifically asks for a mathematical expression. – Cal-linux Oct 11 '18 at 14:20