On Thu, 8 Jan 2004, Russell McMahon wrote: > > > Google on "magic sine waves" and then let your jaw flop to the > > > ground. This is sort of what you are proposing. > > > > Actually, Koen's proposal looked like straight PWM; I'm sure the > > PICLIST archives contain numerous examples of PWM sine-wave > > generation using a quarter-period lookup table. Lancaster's > > "magic sine waves" are different in that they cancel harmonics. > > Lancaster's magic sine waves aren't magic and they aren't Lancaster's, but > he has publicised them well enough that he deserves some mileage from the > concept. > > A MSW with a given number of transitions is the minimum error PWM > approximation that you can achieve to a perfect sine wave. I'm told (and > haven't yet tried it)(but intend to) that you get the same sequences by > setting a predefined error limit, setting your output to zero, summing the > accumulated error wrt to a sine wave with respect to time and then flipping > the output every time the error reaches the limit. Someone may wish to try > this. The mean error overall will be less than the error limit that you use > to toggle the output. This makes intuitive sense, Russell. However, it's theoretically possible to construct a stream of pulses that exactly cancel the first N harmonics, where N is an arbitrarily large number. However, the N+1'st and higher harmonics will contain a significant amount of energy. The idea is that a simple low pass filter can be constructed to eliminate these higher harmonics. The problem with the theory is that in order to cancel the first N harmonics, something on the order of N factorial pulses are needed for just a single cycle of the sine wave. This becomes practically difficult when N is 10 or so. You can check out some of the mathematics here: http://www.dattalo.com/technical/theory/sqwave.html I haven't found any other analysis of this kind of stuff. I found Lancaster's page lacking in mathematical detail. Other's have also suggest differential pulse-code modulation somewhat like something Roman Black had written about a few years ago on the list. In this scheme it's assumed that the impedance of the device being driven by the square wave is known. (This can be a simple RC low-pass filter that is buffered with an opamp). A model of this impedance is created in the PIC. A square wave stream is created and it's output is adjusted based on the feedback from the output of the simulated impedance. Of course, if one had a really fast A/D then the output signal of the real impedance could be fed back and servo'd to the sine wave (or whatever analog signal one wishes to produce). But for simple things like tone generators, this open looped approach (or simulated closed loop) can generate a reasonable sound. Here are some graphs I created from an Octave script to investigate this: http://www.dattalo.com/p2.png http://www.dattalo.com/p3.png http://www.dattalo.com/p4.png Here's the Octave script: http://www.dattalo.com/swsd.m Roman's page is here: http://centauri.ezy.net.au/~fastvid/picsound.htm Scott -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics