Hi again,
Deleted what? I did post the wrong result for the 2nd harmonic though, it was 23 percent not 0.23 percent
If I read your results right, with a normalized fundamental of 1.00 and a normalized second harmonic of 2.256e-02, isn't that about 2.256%, not 23%?
Mr RB, I've tracked down the source of the second harmonic. The way you have generated the PWM is the cause.
As MrAL described:
"For the first pulse which is 50, you generate a '1' output for 50 instruction cycles, then output a '0' for 50 more instr cycles, then
for the second pulse which is 55 you output a '1' for 55 instr cycles, then output a '0' for 45 more instr cycles, then
for the third pulse which is 59 you output a '1' for 59 instr cycles, then output a '0' for 41 more instr cycles, then
etc., etc.,
until you get to the last and then repeat.
So in general you output a '1' for the table entry and then a '0' for 100 minus the table entry.
"
Doing this leads to a PWM wave which doesn't have half-wave symmetry; this then leads to some 2nd harmonic. I calculate the 2nd harmonic magnitude as about 3% of the fundamental. This means that in the later stages of your filter the 50 kHz will be essentially gone, but you will have almost the full amount of 2nd harmonic because your filter rolls off so slowly.
This means that you can't get less than about 3% distortion with your siimple filter. To do much better you would have to have a filter that can greatly attenuate the 2nd harmonic while not significantly attenuating the fundamental. That would be a much sharper cutoff filter than you can implement with R/C stages.
I've attached a couple of images that show the problem. In these images I've shown the first 1/2 millisecond of the PWM waveform overlaid on the second 1/2 millisecond of the waveform, and I've offset the two waveforms vertically a little bit for clarity. I've also removed the DC component of the waveform and made the peak-to-peak amplitude equal to 1.
In the first image, you can see the first half of the PWM waveform in blue and the second half in red. The left edge of each transition is the beginning of an interval. You can see that in the top of the image, the narrow red positive periods are not centered in the wide blue intervals; they are aligned with the left side of the wide blue intervals. In the bottom of the image, the narrow blue periods are not aligned with the left of the wide red intervals; they are aligned to the right. This is the problem.
Rather than using this method: "For a given value N in the sine table, output a 1 for the N instruction cycles, then output a zero for 100-N instruction cycles."
you should do this: "For a given value N in the sine table, output a zero for (100-N)/2 cycyles, output a 1 for N cycles, then output another zero for (100-N)/2 cycles."
The second image again shows a comparison of the first and second half of the PWM waveform generated with this method. Notice how the narrow pulses are centered in the wide pulses. When I calculate the 2nd harmonic of this waveform, it is essentially zero, so after filtering you would be able to get a distortion of well under 1%, I think.
I'm aware that there are problems with this solution to the 2nd harmonic problem, such as, for example, some of the values in the sine table are not evenly divisible by 2, and the PIC may not be fast enough to do all this, etc. But, to get distortion under 3%, you will have to come up with a PWM waveform that has half-wave symmetry by some method, if your filtering is going to be simple R/C stages.