DSPRelated.com
Forums

generate sin wave with PWM

Started by ivan October 10, 2006
hi all,

How do I generate a sin wave using PWM?
The way I have thought is just to create a triangular wave with equal steps
of the PWM duty cycle (depending on freq) and then filter out the harmonics
with a hw filter.
But what if I want to generate a sin wave instead of a triangular wave, how
do I calculate the steps for the duty cycle of the PWM? These steps are not
equal, so I will need to store them in a table.
Any ideas?

thanks
Ivan


In comp.arch.embedded ivan <wolfsafety@wolfsafety.it> wrote:

> How do I generate a sin wave using PWM? The way I have thought is > just to create a triangular wave with equal steps of the PWM duty > cycle (depending on freq) and then filter out the harmonics with a hw > filter. But what if I want to generate a sin wave instead of a > triangular wave, how do I calculate the steps for the duty cycle of > the PWM? These steps are not equal, so I will need to store them in a > table.
Well, the steps are not equal, so you will need to store them in a table :) Using sine tables is common for generating sine waves, since calculating sines is a very expensive operation.
> Any ideas?
You could use something like the following snippet: #include <stdio.h> int main(int argc, char **argv) { int x = 0, y = 64; while(1) { x += y/8; y -= x/8; printf("%d %d\n", x, y); } return 0; } -- :wq ^X^Cy^K^X^C^C^C^C
Write a program on your favorite computer in your favorite language
that emits a bunch of .pword assembler directives with the sine values.


Note that the table only needs to cover 1/4 cycle. At run time you use
symmetry to get the values for other parts of the sine wave. All 4
combinations of indexing into the table forwards and backwards, and
negating the result or not yield the full sine wave.


ivan wrote:
> hi all, > > How do I generate a sin wave using PWM? > The way I have thought is just to create a triangular wave with equal steps > of the PWM duty cycle (depending on freq) and then filter out the harmonics > with a hw filter. > But what if I want to generate a sin wave instead of a triangular wave, how > do I calculate the steps for the duty cycle of the PWM? These steps are not > equal, so I will need to store them in a table. > Any ideas? > > thanks > Ivan
"ivan" <wolfsafety@wolfsafety.it> wrote in message
news:egg63k$ekg$1@news.flashnet.it...
> hi all, > > How do I generate a sin wave using PWM? > The way I have thought is just to create a triangular wave with equal
steps
> of the PWM duty cycle (depending on freq) and then filter out the
harmonics
> with a hw filter. > But what if I want to generate a sin wave instead of a triangular wave,
how
> do I calculate the steps for the duty cycle of the PWM? These steps are
not
> equal, so I will need to store them in a table. > Any ideas?
Or calculate them on the fly. Neither method seems challenging. After all if the PWM is clocking at the CPU frequency you've got lots of cycles to the next PWM update unless you got no resolution from the PWM. Now, is the sin wave you want at a fixed frequency or fixed amplitude? As to calculating you might want to apply you mind to the trig formula: sin(A+B) = sin(A)*cos(B) + cos(A)*sin(B) Peter
ivan wrote:
> hi all, > > How do I generate a sin wave using PWM? > The way I have thought is just to create a triangular wave with equal steps > of the PWM duty cycle (depending on freq) and then filter out the harmonics > with a hw filter. > But what if I want to generate a sin wave instead of a triangular wave, how > do I calculate the steps for the duty cycle of the PWM? These steps are not > equal, so I will need to store them in a table. > Any ideas?
You can output widths that are samples of a sine. they can be generated directly as you need them or taken from a table. The derivative of a sine is a cosine. The difference between the widths of successive pulses that simulate a sine follow a cosine. A pair of DDAs, like a pair of op-amps, connected back to back can generate sine and cosine waves simultaneously. (DDA stands for "digital differential analyzer". Nobody builds them any more, but they are easily created in software.) Software DDAs are often used for circle generation, and they can supply pulse widths for uni- or bipolar PWM. The software version is related to Bresenham&#4294967295;s algorithm. Look it up. Jerry -- "The rights of the best of men are secured only as the rights of the vilest and most abhorrent are protected." - Chief Justice Charles Evans Hughes, 1927 &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
Ico wrote:

> In comp.arch.embedded ivan <wolfsafety@wolfsafety.it> wrote: > > >>How do I generate a sin wave using PWM? The way I have thought is >>just to create a triangular wave with equal steps of the PWM duty >>cycle (depending on freq) and then filter out the harmonics with a hw >>filter. But what if I want to generate a sin wave instead of a >>triangular wave, how do I calculate the steps for the duty cycle of >>the PWM? These steps are not equal, so I will need to store them in a >>table. > > > Well, the steps are not equal, so you will need to store them in a table :) > > Using sine tables is common for generating sine waves, since > calculating sines is a very expensive operation. >
Not necessarily. You could, for example, run a square wave into a wave digital LP filter. If you use Chebychev or elliptic it won't consume much in terms of MIPS. One can play with the coefficients a bit to reduce the number of shift-adds as long as the stop band attenuation remains sufficient. And use CSD. -- Regards, Joerg http://www.analogconsultants.com
ivan wrote:

> But what if I want to generate a sin wave instead of a triangular wave, how > do I calculate the steps for the duty cycle of the PWM? These steps are not > equal, so I will need to store them in a table. > Any ideas?
Here are some ideas: http://www.dattalo.com/technical/theory/sinewave.html
ivan wrote:

> hi all, > > How do I generate a sin wave using PWM? > The way I have thought is just to create a triangular wave with equal > steps of the PWM duty cycle (depending on freq) and then filter out the > harmonics with a hw filter. > But what if I want to generate a sin wave instead of a triangular wave, > how do I calculate the steps for the duty cycle of the PWM? These steps > are not equal, so I will need to store them in a table. > Any ideas?
Others will tell you of all sorts of formulae and about look-up tables but one technique that many forget is called a "Digital Differential Analyser". With some very simple maths and some logic these can be implemented and produce sine and cosine streams of data which could be used more-or-less directly by the PWM generation logic. Put the term "Digital Differential Analyser" and Sizer into Google's search string and you will get a number of references that may prove useful (especially if you register for the IEEE and CiteSeer sites. -- ******************************************************************** Paul E. Bennett ....................<email://peb@amleth.demon.co.uk> Forth based HIDECS Consultancy .....<http://www.amleth.demon.co.uk/> Mob: +44 (0)7811-639972 Tel: +44 (0)1235-811095 Going Forth Safely ..... EBA. www.electric-boat-association.org.uk.. ********************************************************************
In comp.arch.embedded Joerg <notthisjoergsch@removethispacbell.net> wrote:
> Ico wrote: > >> In comp.arch.embedded ivan <wolfsafety@wolfsafety.it> wrote: >> >> >>>How do I generate a sin wave using PWM? The way I have thought is >>>just to create a triangular wave with equal steps of the PWM duty >>>cycle (depending on freq) and then filter out the harmonics with a hw >>>filter. But what if I want to generate a sin wave instead of a >>>triangular wave, how do I calculate the steps for the duty cycle of >>>the PWM? These steps are not equal, so I will need to store them in a >>>table. >> >> >> Well, the steps are not equal, so you will need to store them in a table :) >> >> Using sine tables is common for generating sine waves, since >> calculating sines is a very expensive operation. >> > > Not necessarily. You could, for example, run a square wave into a wave > digital LP filter. If you use Chebychev or elliptic it won't consume > much in terms of MIPS. One can play with the coefficients a bit to > reduce the number of shift-adds as long as the stop band attenuation > remains sufficient. And use CSD.
That's what I said, very expensive :) -- :wq ^X^Cy^K^X^C^C^C^C
ivan wrote:

> hi all, > > How do I generate a sin wave using PWM? > The way I have thought is just to create a triangular wave with equal steps > of the PWM duty cycle (depending on freq) and then filter out the harmonics > with a hw filter. > But what if I want to generate a sin wave instead of a triangular wave, how > do I calculate the steps for the duty cycle of the PWM? These steps are not > equal, so I will need to store them in a table. > Any ideas?
Don Lancaster got carried away with this awhile ago: http://www.tinaja.com/magsn01.asp