Discussion in 'Electronic Projects Design/Ideas/Reviews' started by Mr RB, Jun 9, 2011.

1. ### Mr RBWell-Known Member

Joined:
Jul 22, 2008
Messages:
4,716
Likes:
194
Location:
Out there
I'm definitely appreciative, and interested in your dual pin designs. But I don't have a Spice program or any way to even view the principle or schematic of your .ASC model (unfortunately).

I did have a think about how to manually generate dual complimentary PWM and I think I already covered a technique on 8th Dec 2009 that would work on low-end PICs seen here; http://www.romanblack.com/onesec/ZE_SineInverter.htm
It can do any manual PWM of any number of pins, but takes a lot of time to write and test the code... Again it comes down to an effort: performance decision.

Hmm, ok thank you very much for finding this 2nd harmonic issue! I was completely unaware of it, or had forgotten, because I do remember reading something years ago on the benefits of centre-aligned PWM. So to put it in "simple terms" although each 20uS PWM cycle must have the correct average voltage, the force that pushes the sine UP is always at the start of the 20uS period and the force that pulls the sine down is always at the END of the 20uS period.

So there is a result that the sine always ramps UP (about) 10uS earlier than it should, and ramping DOWN occurs about (10uS) later than it should.

The worst case is actually less than 10uS it would be the time between the centres of the energy pulses. At 50% PWM it self cancels the 2nd harmonic time error , and at wave peaks the 2nd harmonic time error is quite low at 14% of 20uS or 2.8uS. The worst case is near the 45 degree positions where the PWM is about 70% so the time difference in the up and down energy is about 35% of 20uS or 7uS. These figures are all halved compared to a perfect sine as they are +/-.

So the total time distortion caused by the PWM will be an average of around 0uS to 7uS halved, or about +/- 2uS on a total sine period of 1000uS. I just don't see how that can be equated to a total error of 3%. The max error at any point can only be +/-3.5uS on 1000uS or 0.35% so the average must be less than that.

Not entirely true! Even my crude filter (designed for max amplitude) attenuates 1kHz to 0.37 and 2kHz to 0.18 so that even if the 2kHz component was your stated 3% it would still be halved compared to the primary 1kHz.

And the more aggressive passive filters proposed by MrAl and yourself would result in a much greater reduction of the 2kHz component than my filter which was never designed to remove 2kHz!

Yep that's completely understood. Some of the more expensive PICs (I believe) have provision to generate centre-aligned PWM. Also on many mid-range 18F series PICs the PWM polarity can be easily inverted, so the same sine half table could be used for both halves, but the PWM polarity reversed at the wave peak, so the UP and DOWN slopes of the sine would be time balanced. And there's always the option of complete manual PWM generation which is centre-aligned.

For now the main thing I think would be to quantify the 2nd harmonic problem caused by the PWM! I just don't buy the 3% total error figure. How many PWM cycles did you use in your simulation? Your charts show 25 cycles per sinewave?

As this is a time distortion error it will be directly related to the number of PWM cycles per sine, so it needs to be calculated with 50 PWM cycles per sine as used in my hardware. Also I have a feeling that since the smoothing effect of the 3-stage RC filter effects both amplitude AND time there will be quite a significant reduction in this time error by the end of the 3 stage filter.

If you check Ericgibbs' simulation images he used actual PWM simulation and plotted the output of the filter vs a perfect sine, and there is practically no difference.

If someone has a simulator with the same feature can you provide the THD table for the OUTPUT of the filter? I think there has been way too much emphasis on analysing the source PWM error and not on analysing the actual error at the filter output.

I think that's a good idea that goes a long way to solving the 2nd harmonic issue.

Last edited: Jun 17, 2011
2. ### MrAlWell-Known MemberMost Helpful Member

Joined:
Sep 7, 2008
Messages:
11,045
Likes:
961
Location:
NJ
Hi again MrRB,

The reason why we were suggesting you try a different pattern is because that's the best place to start to reduce the overall distortion. To show just how much this can mean, here is the result of your original pattern with your original filter as you requested. You can see here that the 2nd harmonic is dominating the total harmonic distortion of around 1.8 percent. Reducing this 2nd harmonic to zero means reducing the THD to less than 1 percent, with your chosen filter.

Also note that when comparing these waveforms side by side it is very difficult to see the effects of the low order harmonics like the 2nd until you do a complete Fourier analysis. If you look at a scope waveform it looks really really clean, yet has significant 2nd harmonic, 2nd harmonic that isnt too hard to eliminate using a slightly different pattern.

Also, the way the pattern is generated has a lot to do with the harmonics of course, so to effectively analyze your pattern it has to be generated using pulse sources that perfectly generate your exact pattern pulse by pulse. Using pure PWM isnt exactly the same because you dont see the effects of the integer truncation that you do get using a micro controller.

The following analysis represents that pattern pulse by pulse with 1ns rise and fall times and filtered with your filter with the 330 ohm, 2.3mH, 0.33uf, 330 ohm , 0.068uf, 820 ohm, 0.068uf components. No shortcuts were taken this time.

If you dont feel up to a 2 pin pattern, then i would bet that a more symmetrical single pin pattern would help quite a bit too.

You also might want to note that LT Spice is free.

Code (text):

Fourier components of V(vo2)
DC component:2.50013

Harmonic    Frequency     Fourier     Normalized     Phase      Normalized
Number       [Hz]       Component     Component    [degree]    Phase [deg]
1       1.000e+03    1.122e+00    1.000e+00      -78.12°        0.00°
2       2.000e+03    1.327e-02    1.183e-02       58.28°      136.40°
3       3.000e+03    4.932e-04    4.396e-04     -153.10°      -74.98°
4       4.000e+03    1.759e-05    1.568e-05      -13.33°       64.79°
5       5.000e+03    3.329e-04    2.967e-04      -15.55°       62.57°
6       6.000e+03    8.324e-05    7.419e-05      165.60°      243.73°
7       7.000e+03    1.140e-04    1.016e-04      148.35°      226.47°
8       8.000e+03    6.012e-05    5.358e-05      146.35°      224.47°
9       9.000e+03    2.387e-04    2.128e-04      117.28°      195.40°
10       1.000e+04    2.185e-05    1.947e-05     -150.04°      -71.92°
11       1.100e+04    1.100e-04    9.806e-05      100.35°      178.47°
12       1.200e+04    1.965e-05    1.751e-05     -158.72°      -80.60°
13       1.300e+04    4.561e-05    4.065e-05       90.36°      168.48°
14       1.400e+04    3.919e-05    3.493e-05     -136.32°      -58.20°
15       1.500e+04    4.448e-05    3.964e-05     -139.78°      -61.66°
16       1.600e+04    2.190e-05    1.952e-05       69.93°      148.05°
17       1.700e+04    2.577e-05    2.297e-05       54.54°      132.66°
18       1.800e+04    2.078e-05    1.852e-05     -164.73°      -86.61°
19       1.900e+04    4.791e-06    4.270e-06      145.96°      224.08°
20       2.000e+04    1.870e-05    1.667e-05     -171.29°      -93.16°
21       2.100e+04    1.709e-05    1.523e-05     -174.58°      -96.46°
22       2.200e+04    1.482e-06    1.320e-06      176.23°      254.35°
23       2.300e+04    4.215e-06    3.757e-06     -170.49°      -92.37°
24       2.400e+04    1.197e-05    1.067e-05      179.52°      257.64°
25       2.500e+04    8.342e-06    7.435e-06     -176.61°      -98.49°
26       2.600e+04    1.032e-05    9.198e-06      176.15°      254.27°
27       2.700e+04    9.694e-06    8.640e-06      176.18°      254.31°
28       2.800e+04    4.474e-06    3.987e-06     -147.18°      -69.05°
29       2.900e+04    5.990e-06    5.339e-06     -171.28°      -93.16°
30       3.000e+04    9.150e-06    8.155e-06      169.15°      247.27°
31       3.100e+04    7.626e-06    6.796e-06      172.91°      251.03°
32       3.200e+04    7.628e-06    6.798e-06      168.17°      246.29°
33       3.300e+04    7.236e-06    6.449e-06      167.42°      245.54°
34       3.400e+04    5.597e-06    4.988e-06     -140.00°      -61.88°
35       3.500e+04    6.083e-06    5.421e-06      177.98°      256.10°
36       3.600e+04    7.178e-06    6.397e-06      157.16°      235.28°
37       3.700e+04    5.968e-06    5.318e-06      174.29°      252.42°
38       3.800e+04    5.984e-06    5.334e-06      169.91°      248.03°
39       3.900e+04    5.766e-06    5.139e-06      163.76°      241.89°
40       4.000e+04    5.629e-06    5.017e-06     -179.25°     -101.13°
41       4.100e+04    5.116e-06    4.559e-06      168.51°      246.64°
42       4.200e+04    5.834e-06    5.199e-06     -164.10°      -85.97°
43       4.300e+04    4.211e-06    3.753e-06      173.73°      251.85°
44       4.400e+04    4.921e-06    4.386e-06     -166.94°      -88.82°
45       4.500e+04    4.140e-06    3.690e-06      143.85°      221.97°
46       4.600e+04    7.573e-06    6.749e-06       92.19°      170.32°
47       4.700e+04    2.692e-05    2.399e-05       65.14°      143.26°
48       4.800e+04    6.684e-05    5.957e-05       54.58°      132.70°
49       4.900e+04    9.341e-05    8.325e-05       46.62°      124.74°
50       5.000e+04    1.630e-04    1.453e-04       42.68°      120.80°
51       5.100e+04    7.654e-05    6.821e-05     -145.39°      -67.27°
52       5.200e+04    5.507e-05    4.908e-05       37.95°      116.07°
53       5.300e+04    2.864e-05    2.552e-05     -151.06°      -72.93°
54       5.400e+04    7.023e-06    6.259e-06       51.33°      129.45°
55       5.500e+04    7.391e-06    6.587e-06     -170.49°      -92.37°
Total Harmonic Distortion: 1.184466%

Last edited: Jun 17, 2011
3. ### MrAlWell-Known MemberMost Helpful Member

Joined:
Sep 7, 2008
Messages:
11,045
Likes:
961
Location:
NJ
Hi again MrRB,

The reason why we were suggesting you try a different pattern is because that's the best place to start to reduce the overall distortion. To show just how much this can mean, here is the result of your original pattern with your original filter as you requested. You can see here that the 2nd harmonic is dominating the total harmonic distortion of around 1.8 percent. Reducing this 2nd harmonic to zero means reducing the THD to less than 1 percent, with your chosen filter.

Also note that when comparing these waveforms side by side it is very difficult to see the effects of the low order harmonics like the 2nd until you do a complete Fourier analysis. If you look at a scope waveform it looks really really clean, yet has significant 2nd harmonic, 2nd harmonic that isnt too hard to eliminate using a slightly different pattern. Take a look at the attached diagram of two waves. Can you figure out which one has the 1.8 percent 2nd harmonic content and which one is a perfect sine wave?

Also, the way the pattern is generated has a lot to do with the harmonics of course, so to effectively analyze your pattern it has to be generated using pulse sources that perfectly generate your exact pattern pulse by pulse. Using pure PWM isnt exactly the same because you dont see the effects of the integer truncation that you do get using a micro controller.

The following analysis represents that pattern pulse by pulse with 1ns rise and fall times and filtered with your filter with the 330 ohm, 2.3mH, 0.33uf, 330 ohm , 0.068uf, 820 ohm, 0.068uf components. No shortcuts were taken this time.

If you dont feel up to a 2 pin pattern, then i would bet that a more symmetrical single pin pattern would help quite a bit too.

You also might want to note that LT Spice is free.

Code (text):

Fourier components of V(vo2)
DC component:2.50013

Harmonic    Frequency     Fourier     Normalized     Phase      Normalized
Number       [Hz]       Component     Component    [degree]    Phase [deg]
1       1.000e+03    1.122e+00    1.000e+00      -78.12°        0.00°
2       2.000e+03    1.327e-02    1.183e-02       58.28°      136.40°
3       3.000e+03    4.932e-04    4.396e-04     -153.10°      -74.98°
4       4.000e+03    1.759e-05    1.568e-05      -13.33°       64.79°
5       5.000e+03    3.329e-04    2.967e-04      -15.55°       62.57°
6       6.000e+03    8.324e-05    7.419e-05      165.60°      243.73°
7       7.000e+03    1.140e-04    1.016e-04      148.35°      226.47°
8       8.000e+03    6.012e-05    5.358e-05      146.35°      224.47°
9       9.000e+03    2.387e-04    2.128e-04      117.28°      195.40°
10       1.000e+04    2.185e-05    1.947e-05     -150.04°      -71.92°
11       1.100e+04    1.100e-04    9.806e-05      100.35°      178.47°
12       1.200e+04    1.965e-05    1.751e-05     -158.72°      -80.60°
13       1.300e+04    4.561e-05    4.065e-05       90.36°      168.48°
14       1.400e+04    3.919e-05    3.493e-05     -136.32°      -58.20°
15       1.500e+04    4.448e-05    3.964e-05     -139.78°      -61.66°
16       1.600e+04    2.190e-05    1.952e-05       69.93°      148.05°
17       1.700e+04    2.577e-05    2.297e-05       54.54°      132.66°
18       1.800e+04    2.078e-05    1.852e-05     -164.73°      -86.61°
19       1.900e+04    4.791e-06    4.270e-06      145.96°      224.08°
20       2.000e+04    1.870e-05    1.667e-05     -171.29°      -93.16°
21       2.100e+04    1.709e-05    1.523e-05     -174.58°      -96.46°
22       2.200e+04    1.482e-06    1.320e-06      176.23°      254.35°
23       2.300e+04    4.215e-06    3.757e-06     -170.49°      -92.37°
24       2.400e+04    1.197e-05    1.067e-05      179.52°      257.64°
25       2.500e+04    8.342e-06    7.435e-06     -176.61°      -98.49°
26       2.600e+04    1.032e-05    9.198e-06      176.15°      254.27°
27       2.700e+04    9.694e-06    8.640e-06      176.18°      254.31°
28       2.800e+04    4.474e-06    3.987e-06     -147.18°      -69.05°
29       2.900e+04    5.990e-06    5.339e-06     -171.28°      -93.16°
30       3.000e+04    9.150e-06    8.155e-06      169.15°      247.27°
31       3.100e+04    7.626e-06    6.796e-06      172.91°      251.03°
32       3.200e+04    7.628e-06    6.798e-06      168.17°      246.29°
33       3.300e+04    7.236e-06    6.449e-06      167.42°      245.54°
34       3.400e+04    5.597e-06    4.988e-06     -140.00°      -61.88°
35       3.500e+04    6.083e-06    5.421e-06      177.98°      256.10°
36       3.600e+04    7.178e-06    6.397e-06      157.16°      235.28°
37       3.700e+04    5.968e-06    5.318e-06      174.29°      252.42°
38       3.800e+04    5.984e-06    5.334e-06      169.91°      248.03°
39       3.900e+04    5.766e-06    5.139e-06      163.76°      241.89°
40       4.000e+04    5.629e-06    5.017e-06     -179.25°     -101.13°
41       4.100e+04    5.116e-06    4.559e-06      168.51°      246.64°
42       4.200e+04    5.834e-06    5.199e-06     -164.10°      -85.97°
43       4.300e+04    4.211e-06    3.753e-06      173.73°      251.85°
44       4.400e+04    4.921e-06    4.386e-06     -166.94°      -88.82°
45       4.500e+04    4.140e-06    3.690e-06      143.85°      221.97°
46       4.600e+04    7.573e-06    6.749e-06       92.19°      170.32°
47       4.700e+04    2.692e-05    2.399e-05       65.14°      143.26°
48       4.800e+04    6.684e-05    5.957e-05       54.58°      132.70°
49       4.900e+04    9.341e-05    8.325e-05       46.62°      124.74°
50       5.000e+04    1.630e-04    1.453e-04       42.68°      120.80°
51       5.100e+04    7.654e-05    6.821e-05     -145.39°      -67.27°
52       5.200e+04    5.507e-05    4.908e-05       37.95°      116.07°
53       5.300e+04    2.864e-05    2.552e-05     -151.06°      -72.93°
54       5.400e+04    7.023e-06    6.259e-06       51.33°      129.45°
55       5.500e+04    7.391e-06    6.587e-06     -170.49°      -92.37°
Total Harmonic Distortion: 1.184466%

File size:
10.6 KB
Views:
96

Joined:
Jan 12, 1997
Messages:
-
Likes:
0

5. ### The ElectricianActive Member

Joined:
Jan 13, 2009
Messages:
551
Likes:
70

I said about 3%, taking that number as the upper bound. In the audio world a reduction by 1/2 in an audio tone is generally accepted as the just barely detectable (by ear) change, so I usually consider such a reduction hardly worth while. I would try to reduce unwanted components by 20 or 30 dB.

Which of Ericgibbs' simulations are you referring to (which post#)? He was using a wrong PWM waveform for a while.

I used the exact sine table values you provided (except for the centered example, of course) and I used the full 50 PWM cycles. As I described, the images I posted show both the first half of the PWM waveform and the second half, superimposed and slightly offset from each other; each half has 25 cycles for a total of 50 cycles for the full waveform.

I mentioned in an earlier post that I had measured the distortion of the PWM output from a function generator. I got 4.2% distortion at the output of the first stage of the filter, 1.9% at the second stage and 1.6% at the third stage (with no inductor). At the third stage the residual is almost entirely second harmonic.

I've attached a couple of scope captures. The first image shows the PWM wave input to your filter. The function generator output impedance is 50 ohms and the input impedance of your filter loads the generator and causes a 1 kHz "wavyness". The first resistor input to your filter was reduced to 280 ohms (instead of 330 ohms) to account for the 50 ohm output impedance of the generator. The blue trace shows the filter input. The purple trace is the output of the third stage of your filter (no inductor on the input); the second harmonic content is not readily visible to the eye, but it's there. The yellow trace is the sync output from the generator.

The second image shows the residual output from the distortion analyzer. This is what remains after the fundamental has been nulled out. You will notice that it's almost entirely 2nd harmonic; this is the 1.6% distortion. There is just a tiny bit of the 50 kHz ripple visible. Just imagine how low the distortion would be if all of that second harmonic were gone and just the tiny 50 kHz ripple were all that remained!

Note that if there is indeed 3% second harmonic in the raw PWM, then the attenuation by a factor of 2 due to your filter would be expected to leave 1.5% second harmonic. My distortion analyzer says there is 1.6% second harmonic, which is good agreement of measurement with theory; the second harmonic is really there.

#### Attached Files:

File size:
75 KB
Views:
103
• ###### Residual_Out.jpg
File size:
80.7 KB
Views:
105
Last edited: Jun 17, 2011
6. ### MrAlWell-Known MemberMost Helpful Member

Joined:
Sep 7, 2008
Messages:
11,045
Likes:
961
Location:
NJ
Hi there Electrician,

Just a quick question: why does the pulsed pattern in your pictures look like it is itself riding on some sort of sine wave? Can it do true DC coupling or is it something else?

7. ### The ElectricianActive Member

Joined:
Jan 13, 2009
Messages:
551
Likes:
70
I was trying to explain this with my comment about "wavyness". The output impedance of the generator is 50 ohms, about the same order of magnitude as the input impedance of the filter. The generator output is therefore "a little bit" filtered at the input to the filter. When I disconnect the filter from the generator, this "wavyness" goes away and the generator output waveform looks like it should.

8. ### MrAlWell-Known MemberMost Helpful Member

Joined:
Sep 7, 2008
Messages:
11,045
Likes:
961
Location:
NJ
Hi again Electrician,

Oh, that explains it. Makes us appreciate the perfectness of the spice models a little more
The generator i have has 600 ohms output which had always been a problem with testing things without some sort of external amplification. Always meant to build in a little amp but never got around to it.
So i guess you can just use a slightly less value resistor for R1 i guess. Nice to see some real world results though after working so much in spice and general theory.

9. ### The ElectricianActive Member

Joined:
Jan 13, 2009
Messages:
551
Likes:
70
Yep. The very next sentence after I mentioned the "wavyness" was:

"The first resistor input to your filter was reduced to 280 ohms (instead of 330 ohms) to account for the 50 ohm output impedance of the generator."

10. ### MrAlWell-Known MemberMost Helpful Member

Joined:
Sep 7, 2008
Messages:
11,045
Likes:
961
Location:
NJ
Hi again,

Oh ok great. Probably not too big of a deal here but what the heck, if it's not hard to do why not do it right?
I see you're up tonight too

11. ### Mr RBWell-Known Member

Joined:
Jul 22, 2008
Messages:
4,716
Likes:
194
Location:
Out there
MrAl, thanks for running the actual values in the PWM table through the actual filter values I used in hardware! So that is the definitive table of results now, (your table in post #62). It's amazing that a total averaged distortion of almost 1.2% is practically invisible, I would have expected to see at least PART of the waveform at 1.2% phase error or amplitude error (or part of the wave at MORE than 1.2% and part of the wave accurate), not the miniscule differences we are seeing!

I was impressed before with Ericgibbs' PWM simulation showing almost perfect overlapping sines (post #30) and would not have imagined that error would be 1.2% or 1.6% THD! It seems almost nonsensical that such a small difference would affect anything I'm going to do with sines...

You mentioned the sine table integer truncation again, thanks for implementing that in the testing too. I'm not sure that it will have much significant difference as I averaged the integer truncation when making the table so the errors canceled. That truncation is unavoidable really with a PWM resolution of 100 (actually of 73).

TheElectrician, I apologise, if the 2nd harmonic is 3% at the source then it's 3%.

Now it looks like you and MrAl are getting matching figures (his sim at 1.2% your hardware at 1.6%) they are much easier to believe than some of those early figures.

I played with a draw program to get some diagrams to better help me understand the mechanism there.

The top row shows the "perfect" centre-aligned PWM as suggested by TheElectrician, but there seems to be a problem with that in concept? The diagram shows the volage vaveform (green) after the first RC filter. On the right shows the average voltage over time, shown (for simplicity) for the first and second half of the PWM 20uS period.

So the top 3 diagrams show the "perfect" PWM waveform as suggested actually produces the WORST time imbalance of the energy of the waveform at the filter.

The middle 3 diagrams show the standard start-aligned PWM which performs better, and has less time imbalance of the voltage integrated at the filter.

The bottom diagrams show the best case, which is where the centre of the PWM waveform has been moved to 1/4 wave from the centre of the PWM period. Funny enough this worst case distortion for the PWM period actually produces the best case for the voltage at the RC filter where the first half of the PWM period has the exact same average voltage as the second half of the PWM period.

So there seems to be a distinct inverse relationship between the THD of the source PWM and the THD of that PWM period once integrated by the RC filter. This is why I'm thinking that modelling the THD of the source PWM is a mistake, and if the figures are generated by modelling THD of the source PWM then just "adjusting" the amounts based on the filter attenuation that will not mimic the real world result!

#### Attached Files:

• ###### sinetest.png
File size:
31.9 KB
Views:
119
Last edited: Jun 17, 2011
12. ### The ElectricianActive Member

Joined:
Jan 13, 2009
Messages:
551
Likes:
70
I'm not sure how to connect "energy balance" with THD, but if linear network theory is correct, then calculating the result of the attenuation of the filter on the various spectral components should work just fine.

Even though MrAL showed just how difficult it is to discern small amounts of 2nd harmonic, here's another image showing a sine wave free of distortion (blue) superimposed on a sine wave with 2% of second harmonic(red).

You can see that the positive half cycle of the distorted sine (red) leans to the left, whereas the negative half cycle of the distorted sine leans to the right. That is what lack of half-wave symmetry is. In order to have half-wave symmetry, each half cycle should lean in the same direction so that if we flip the negative half cycle so that it's positive, it would have the same shape as the positive half cycle.

When a wave has half-wave symmetry, there are no even harmonics.

File size:
19.4 KB
Views:
111
13. ### MrAlWell-Known MemberMost Helpful Member

Joined:
Sep 7, 2008
Messages:
11,045
Likes:
961
Location:
NJ
Hi again,

Yes you can see a tiny difference when you overlap them like that. It still might be hard to tell which one was which if we were not told.

I guess there is a sort of 'energy balance', but it's an inverted relationship. The energy in the first 180 degrees has to match the 'inverted' energy in the 180 to 360 degree portion of the wave. This next diagram illustrates this, with the red arrow pointing to the axis of 'inverted' symmetry. A pattern such as this is void of even harmonics.
There are three distinct features in this wave.
1. The first half contains one or more positive pulses (only one pulse in this particular wave)
2. The second half contains the same pulses with the same spacing and widths, but they are inverted (only one in this wave)
3. Directly in the center of the two halves, the pattern switches from a low (0) to a high (1).

Although this is just a simple pulsed wave, a more complex pattern that follows those rules could be created to form a sine pattern void of all even harmonics. I dont believe it will ever be as good as tri level pulsed wave (2 pin pattern) however.

The main idea with the creation of a sine pattern is to eliminate the lower harmonics because they dont filter well. If we leave in only one low harmonic that's enough to force the THD up above 1 percent, so might as well get rid of it with the pattern and let the filter do the rest on the higher harmonics that cant be eliminated with the pattern itself. So in other words, we eliminate the lower order harmonics with the pattern and kill the higher order harmonics with the filter.

Using the above guidelines here are the results for a single port pin pattern that eliminates the even harmonic content and results in a THD of well under 1 percent with the original filter (with the 2.3mH inductor and 3 resistors and 3 capacitors):
Code (text):

Relative harmonic content:
1: +1.00000
2: +0.00000     3: +0.00298     4: +0.00000     5: +0.00287
6: +0.00000     7: +0.00634     8: +0.00000     9: +0.01367
10: +0.00000    11: +0.01671    12: +0.00000    13: +0.01568
14: +0.00000    15: +0.02012    16: +0.00000    17: +0.03027
18: +0.00000    19: +0.04502    20: +0.00000    21: +0.03445
22: +0.00000    23: +0.04692    24: +0.00000    25: +0.05018
26: +0.00000    27: +0.05494    28: +0.00000    29: +0.06307
30: +0.00000    31: +0.06133    32: +0.00000    33: +0.08204
34: +0.00000    35: +0.07894    36: +0.00000    37: +0.08015
38: +0.00000    39: +0.08974    40: +0.00000    41: +0.11090
42: +0.00000    43: +0.13378    44: +0.00000    45: +0.15970
46: +0.00000    47: +0.22224    48: +0.00000    49: +0.40970
50: +0.00000    51: +0.35104    52: +0.00000    53: +0.21223
54: +0.00000    55: +0.31318    56: +0.00000    57: +0.14672
58: +0.00000    59: +0.07978    60: +0.00000    61: +0.04899
62: +0.00000    63: +0.03174    64: +0.00000    65: +0.01729
66: +0.00000    67: +0.00657    68: +0.00000    69: +0.00140
70: +0.00000    71: +0.00227    72: +0.00000    73: +0.01823
74: +0.00000    75: +0.01332    76: +0.00000    77: +0.01695
78: +0.00000    79: +0.02032    80: +0.00000    81: +0.01801
82: +0.00000    83: +0.02699    84: +0.00000    85: +0.01469
86: +0.00000    87: +0.02252    88: +0.00000    89: +0.02708
90: +0.00000    91: +0.02797    92: +0.00000    93: +0.02122
94: +0.00000    95: +0.01760    96: +0.00000    97: +0.02401
98: +0.00000    99: +0.00402   100: +0.00000   101: +0.17959
102: +0.00000   103: +0.21560   104: +0.00000   105: +0.20849
106: +0.00000   107: +0.23618   108: +0.00000   109: +0.07305
110: +0.00000   111: +0.03374   112: +0.00000   113: +0.02565
114: +0.00000   115: +0.02752   116: +0.00000   117: +0.02851
118: +0.00000   119: +0.02458   120: +0.00000   121: +0.01739
122: +0.00000   123: +0.02063   124: +0.00000   125: +0.00475
126: +0.00000   127: +0.01219   128: +0.00000   129: +0.00926
130: +0.00000   131: +0.00452   132: +0.00000   133: +0.00698
134: +0.00000   135: +0.00482   136: +0.00000   137: +0.00378
138: +0.00000   139: +0.00337   140: +0.00000   141: +0.00549
142: +0.00000   143: +0.01042   144: +0.00000   145: +0.01351
146: +0.00000   147: +0.02328   148: +0.00000   149: +0.05934
150: +0.00000   151: +0.09863   152: +0.00000   153: +0.04481
154: +0.00000   155: +0.15440   156: +0.00000   157: +0.12190

Total harmonic distortion (up to Nth harmonic only):
0.9317936967

Total Harmonic Distortion of Filtered Output:
0.001328458063

As these numbers show, the THD of the filtered pattern (0.13 percent) has been improved 10 fold by simply changing the pattern somewhat. This is really 0.13 percent and not 13 percent or 1.3 percent.

#### Attached Files:

• ###### PatternOdd-01.gif
File size:
4.9 KB
Views:
120
Last edited: Jun 18, 2011
14. ### Mr RBWell-Known Member

Joined:
Jul 22, 2008
Messages:
4,716
Likes:
194
Location:
Out there
No I don't think it will work just fine! The digital PWM wave is so different in form to a curved "analog" wave that the change is much more than amplitude reduction of harmonics. There are also changes in phase and time caused by the PWM energy being distributed later.

I think a first proof of this is my diagram above in post #70, the PWM form (top line) with zero 2nd harmonic, once integrated by the RC shifts the "energy" to the second half of the PWM cycle, so you can clearly see after RC integration the second half of the PWM cycle has a MUCH higher average voltage than the first half. What is the "best case" in the PWM math comes out as "worst case" actual 2nd harmonic after integration. The hardware example I used with standard PWM results in some 2nd harmonic, but only about half the 2nd harmonic that you would get from centre aligned PWM. Likewise to get zero second harmonic after integration (so the second half of any PWM cycle has the same average voltage as the first half) you need to generate the PWM so that the centre of the PWM wave is 1/4 wavelength displaced from the centre of the PWM period. This would give the greatest 2nd THD calculated at the source PWM!

I'm really glad you posted the sines #71 showing what actual 2% 2nd harmonic looks like, because that matches almost EXACTLY what I would expect it to look like! You can see there is around a 2% phase error and 2% amplitude error. But that is NOT what is actually happening in the RC hardware simulation (compare your sines to Ericgibbs' sines post #30).

I think this method is fundamentally flawed, you cannot measure the THD of the digital PWM and then just assume the "filter" will attenuate all those harmonics accordingly without any phase shift or redistribution of energy over time.

The only method that I think will work is to mathematically model the PWM->RC integrator and get the stage1 analog voltage waveform, then calc the THD figures based on that analog waveform. Or better still model the analog waveform at the end of stage3 and run the THD calc on the final waveform!

I had quite a bit of experience math modelling 1bit and 2bit PWM into an RC integrator when I was producing the BTc sound encoder software (see thumbnail) and it is never as simple as "PWM = output".

I could re-write some of that code to math model the RC integration but it seems a time waste as LTspice should already be doing the RC integration modelling, and you can just run the THD measurement on it's output.

I agree absolutely. If it is easy enough to compensate for 2nd harmonic effects at source then it would be very worthwhile. Personally I am leaning towards the same PIC 1pin PWM using the (easy) hardware module and just using 50 "compensated" sine values!

I modelled the RC integration of your proposed "symmetrical" PWM pattern (see below). It does have symmetrical average voltage for the first and last halves of the total PWM period. BUT it also has 50% PWM duty, which is easy to get symmety with.

How on earth would you use that pattern but make it have wide changes in duty cycle? I did an RC integration of your pattern but at 75% duty and it does not hold up (below), there is still a difference between the average voltage of each PWM half after integration.

We can't fall into that same THD mistake of thinking because the PWM pattern has zero 2nd harmonic that it will produce an RC integration that has zero second harmonic. We have to start at the RC RESULT, and then see what the PWM needs to be to make that result!

I'm thinking we might need to go back to the methodology I used in the BTc encoder; which was to actually generate the desired RC result, then backwards generate the PWM table to give that RC result...

File size:
25.4 KB
Views:
106
File size:
6.7 KB
Views:
102
File size:
7.3 KB
Views:
104
15. ### Mr RBWell-Known Member

Joined:
Jul 22, 2008
Messages:
4,716
Likes:
194
Location:
Out there
While I had the draw program open, I tested inverting the PWM waveform to remove the 2nd harmonic issue after integration.

The top 2 waveforms show centre-aligned PWM, and the 30% PWM value is inverted (to make a "centre-hole"). This inverts the second harmonic after RC so it could be used to compensate half the sine to balance the two halves.

The bottom 2 waveforms show typical edge-aligned PWM, and the RC integrated result as before is about half the 2nd harmonic distorion compared to centre aligned. And like the centre-aligned example if half the sine uses the inverted PWM it is just as good for cancelling the 2nd harmonic.

So neither of these systems gives as good an integrated result as the "phase tuned" system on the bottom of the diagram in post #70. However with the phase tuned system the phase offset is different for each PWM duty, so it is a pain to implement compared to using a worse system and just inverting each half cycle...

When to invert?
Is it at the 50% PWM position or at the sine peak? Ie; does the pwm effect that causes the 2nd harmonic occur because of the voltage bing over halfway, or does it occur because the voltage is ramping up and ramping down?

#### Attached Files:

• ###### sinetes5.gif
File size:
39.7 KB
Views:
106
Last edited: Jun 18, 2011
16. ### MrAlWell-Known MemberMost Helpful Member

Joined:
Sep 7, 2008
Messages:
11,045
Likes:
961
Location:
NJ

Hi there MrRB,

Some interesting stuff there, but i have to say based on your reply to Electrician i think you are over thinking this just a little bit when it comes to the filtering of the harmonics. The filter characteristic for any linear filter will be such that it can always be looked at as if it were filtering each individual harmonic independently, and if there is a certain harmonic missing (like the 2nd for our example) then there will be a missing harmonic in the output too simply because a linear filter can not add any harmonic content to a signal, it can only attenuate it or if and only if it is already present it can amplify it. In other words, if we start with zero 2nd then we end up with zero 2nd unless there is some non linearity present in the filter characteristic, and this is usually only possible if one of the circuit elements happens to be nonlinear such as a voltage dependent capacitance or current dependent inductance. To state this in simple terms, unless you have very non linear components i think you can stop worrying about this.
If you think you do have nonlinear components such as the inductor then you would have to do a real world test on the output similar to what Electrician has been doing, and if you think you can characterize that inductor with at least some accuracy then yes we can figure that non linearity into the equation for the sine pattern generation. If we dont know what it is though we cant do anything about it.
What this means is that we design the pattern first, then the filter. If you find a great non linearity, then we design the filter first and the pattern second. Assuming we dont have a great non linearity, we go with the previous method of doing the pattern first and filter second.

The simple pattern i posted was to show what a pattern with all even harmonics eliminated would look like in its basic character. It does in fact have a 0.5 DC component, half the peak, and a Fourier analysis proves this to be true too.
The generation of this kind of pattern for a sine with very low distortion comes from generating a pattern for the first half cycle, then inverting that and shifting that pattern by Tp/2 and forcing the first time pulse in the second half cycle to go high at Tp/2. The result is a pattern with no even harmonics. That's the way i did the pattern who's results i showed in my last post and it checks out mathematically. If you would like to try this pattern i would be happy to post it, and it would be interesting to see the two patterns generating sine waves superimposed over each other like Electricians example.

Maybe what i'll do next is get that zero even harmonic pattern into LT Spice form and compare it side by side with the other pattern that does have 2nd harmonic, both after filtering, so we can see them together on the same screen.

Last edited: Jun 18, 2011
17. ### The ElectricianActive Member

Joined:
Jan 13, 2009
Messages:
551
Likes:
70
With all due respect I must tell you that you have a fundamental misconception about how voltage waveforms are changed by filters.

A passive filter, one composed of resistors, capacitors and inductors, cannot produce at its output harmonics in a waveform if those harmonics didn't exist in the input waveform. This is because such passive filters are linear devices.

The fact that the waveform is composed of rectangular pulses doesn't change anything. If you zoom in on the corners of the waveform with a high bandwidth scope, you would see that the corners are in fact curved. All voltage waveforms are analog in the final analysis.

Passive filters can only change the amplitude and phase of the various harmonics, not their frequency. Passive filters can't create new frequencies that aren't already present. To create new frequencies the signal must be processed by non-linear circuit elements, such as diodes, transistors, saturable inductors, clipping amplifiers, etc.

It is a perfectly valid method to decompose a waveform into its harmonics, attenuate and phase shift those harmonics individually (by the same amount as the filter would do) and then recombine the harmonics. This will give the waveform at the output of the filter. It's done all the time.

18. ### MrAlWell-Known MemberMost Helpful Member

Joined:
Sep 7, 2008
Messages:
11,045
Likes:
961
Location:
NJ
Hello again,

Electrician:
As you probably know, many inductors are actually non linear that's why i was asking MrRB about his components. I am assuming it is not for now however, until i see some concrete data that says otherwise.

MrRB:
Here is a pattern that has all of the characteristics that i was talking about. As you can see from the numbers, the output THD is very good, less than 0.2 percent as expected. You can see how the pulses are quarter wave and half wave symmetrical and that helps a lot.
BTW if some of the pulses are too short to generate easily with the PIC i can lower the sine modulation factor in the generation of the pattern.

Here are the numbers, and the pic of the pattern follows...

Code (text):

Fourier components of V(vo2)
DC component:2.50011

Harmonic    Frequency     Fourier     Normalized     Phase      Normalized
Number       [Hz]       Component     Component    [degree]    Phase [deg]
1       1.000e+03    1.514e+00    1.000e+00      -74.53°        0.00°
2       2.000e+03    1.148e-04    7.584e-05     -157.50°      -82.97°
3       3.000e+03    1.453e-03    9.598e-04     -136.43°      -61.90°
4       4.000e+03    6.052e-05    3.998e-05     -168.66°      -94.13°
5       5.000e+03    5.074e-04    3.352e-04       15.58°       90.11°
6       6.000e+03    4.028e-05    2.661e-05     -172.22°      -97.69°
7       7.000e+03    5.926e-04    3.915e-04      -25.83°       48.69°
8       8.000e+03    3.078e-05    2.034e-05     -174.61°     -100.08°
9       9.000e+03    7.308e-04    4.828e-04      -33.39°       41.14°
10       1.000e+04    2.441e-05    1.613e-05     -175.06°     -100.54°
11       1.100e+04    5.471e-04    3.614e-04      -55.33°       19.20°
12       1.200e+04    2.027e-05    1.339e-05     -176.33°     -101.80°
13       1.300e+04    3.264e-04    2.157e-04      -60.42°       14.11°
14       1.400e+04    1.758e-05    1.162e-05     -176.71°     -102.19°
15       1.500e+04    2.848e-04    1.881e-04      -78.70°       -4.18°
16       1.600e+04    1.548e-05    1.023e-05     -176.96°     -102.44°
17       1.700e+04    2.959e-04    1.955e-04      -80.49°       -5.96°
18       1.800e+04    1.366e-05    9.023e-06     -177.49°     -102.97°
19       1.900e+04    3.160e-04    2.088e-04      -92.03°      -17.51°
20       2.000e+04    1.248e-05    8.244e-06     -177.97°     -103.44°
21       2.100e+04    1.763e-04    1.165e-04      -95.47°      -20.95°
22       2.200e+04    1.096e-05    7.240e-06     -178.54°     -104.01°
23       2.300e+04    1.806e-04    1.193e-04     -105.37°      -30.84°
24       2.400e+04    1.051e-05    6.943e-06     -176.83°     -102.31°
25       2.500e+04    1.463e-04    9.669e-05     -106.83°      -32.30°
26       2.600e+04    9.474e-06    6.259e-06     -178.75°     -104.23°
27       2.700e+04    1.249e-04    8.250e-05     -115.33°      -40.80°
28       2.800e+04    9.060e-06    5.986e-06      177.18°      251.70°
29       2.900e+04    1.120e-04    7.397e-05     -116.01°      -41.48°
30       3.000e+04    8.155e-06    5.388e-06      178.89°      253.41°
31       3.100e+04    8.755e-05    5.784e-05     -123.43°      -48.91°
32       3.200e+04    7.759e-06    5.126e-06     -179.58°     -105.06°
33       3.300e+04    9.325e-05    6.161e-05     -123.33°      -48.80°
34       3.400e+04    6.821e-06    4.507e-06     -177.33°     -102.80°
35       3.500e+04    7.443e-05    4.917e-05     -129.02°      -54.49°
36       3.600e+04    6.653e-06    4.395e-06      179.27°      253.79°
37       3.700e+04    6.261e-05    4.137e-05     -129.48°      -54.96°
38       3.800e+04    6.574e-06    4.343e-06     -173.86°      -99.34°
39       3.900e+04    5.741e-05    3.793e-05     -133.70°      -59.17°
40       4.000e+04    5.783e-06    3.821e-06     -176.04°     -101.51°
41       4.100e+04    5.901e-05    3.899e-05     -133.09°      -58.57°
42       4.200e+04    5.616e-06    3.711e-06     -176.32°     -101.80°
43       4.300e+04    5.770e-05    3.812e-05     -135.86°      -61.33°
44       4.400e+04    5.136e-06    3.394e-06      179.36°      253.88°
45       4.500e+04    5.991e-05    3.958e-05     -137.86°      -63.33°
46       4.600e+04    5.351e-06    3.536e-06      175.84°      250.37°
47       4.700e+04    6.932e-05    4.580e-05     -135.44°      -60.92°
48       4.800e+04    4.947e-06    3.268e-06      175.43°      249.96°
49       4.900e+04    1.061e-04    7.007e-05     -139.87°      -65.34°
50       5.000e+04    4.967e-06    3.282e-06      172.23°      246.75°
51       5.100e+04    8.659e-05    5.721e-05     -140.20°      -65.68°
52       5.200e+04    5.241e-06    3.463e-06      177.23°      251.76°
53       5.300e+04    3.270e-05    2.160e-05       39.21°      113.74°
54       5.400e+04    4.569e-06    3.018e-06     -169.42°      -94.90°
55       5.500e+04    4.946e-05    3.268e-05       43.75°      118.27°
Total Harmonic Distortion: 0.134259%

#### Attached Files:

• ###### PNEW-1.gif
File size:
30.9 KB
Views:
110
Last edited: Jun 18, 2011
19. ### Mr RBWell-Known Member

Joined:
Jul 22, 2008
Messages:
4,716
Likes:
194
Location:
Out there
Ok, I have been trying to do some very simple models to understand why the PWM produces the 2nd harmonic.

I reduced the model to the simplest possible form, a series of 70% PWM cycles followed by a series of 30% PWM cycles. This makes a crude model of a top half and bottom half of a sine. I did not bother modelling the middle parts of the sine as integration means they will always follow the midpoint between what the top and bottom halves are doing.

The diagram below uses the same PWM->RC voltage models as these show the average voltage of each half of any PWM cycle.

There is an obvious second harmonic in the RC voltage seen in the top waveform, because for the top half of the sine the PWM voltages are higher later in time, so this obviously shifts the top half of the sine to the right. On the bottom half of the sine the PWM averages are lower on the left, this shifts the bottom half of the sine to the left.

The surprise was when I modelled the standard PWM in the bottom chart. I knew this would have less shift than the centre-alined PWM as the average voltage difference in the PWMs was lower (from my earlier testing).

But it also became obvious the top hald AND the bottom half of the sine are both shifted right, and by the same amount! Unless I have done something very wrong it looks a bit like the 2nd harmonic issue is busted. It appears to be absorbed into the integration?

I'm open to suggestions and comments!

File size:
37.3 KB
Views:
105
20. ### Mr RBWell-Known Member

Joined:
Jul 22, 2008
Messages:
4,716
Likes:
194
Location:
Out there
(edit) Text removed as I was wrong and wasting people's time.

Last edited: Jun 18, 2011
21. ### MrAlWell-Known MemberMost Helpful Member

Joined:
Sep 7, 2008
Messages:
11,045
Likes:
961
Location:
NJ
Hi again MrRB,

Im not entirely sure why you are 'integrating' here. I also cant see how you can get 2nd harmonics output from a filter that is perfectly linear with an input of no 2nd harmonic. That just doesnt happen. An RC filter is not an integrator anyway, it's a little different than that because it has a sort of 'droop' to it that a true integrator doesnt have. In the frequency domain an integration is a/s (a a constant) while an RC filter is a/(s+b) (a and b constants). In the time domain an integration is a ramp, while an RC network is an exponential rise or decay. The two are very different, but they are still linear, so any non existent harmonic on the input should not appear on the output unless something is wrong. How are you measuring the 2nd harmonic you are seeing?

The pulsed wave coming out of the PIC is non linear because it's digital but that's not the same as a non linear filter, and the digital output is made linear by using the Fourier components. It's not linear in the time domain, but it is linear in the frequency domain, for each component.

Oh looking again im not sure your patterns shown there are free from 2nd harmonics anyway. I'll have to check that.
Also, how can you integrate up to level 1 in time t1 yet you can integrate down by the same amount in a shorter time? Are you changing the constant of integration?

If i rem right if you start your waveform with a 'zero' level for say 100us (just some random time interval) and then it switches high, then the second half cycle has to start with a 'one' level for 100us, just for a quick example. Everything in the second half cycle will have to be the same as the first half except the opposite level, on a moment by moment basis. For example, if you start the first half cycle with:
1 for 100us, 0 for 200us, 1 for 200us,
then you have to start the second half cycle with:
0 for 100us, 1 for 200us, 0 for 200us.
Note the times are the same but the levels are opposite.

Last edited: Jun 18, 2011