# Electronic Circuits and Projects Forum

1. Hello again MrRB, MRCecil, and Electrician,

I am replying to three different posts here...

MrRB:
With the 2 pin version i was assuming you would do a bang bang type output, not rely on any PWM peripheral built into the chip. I figured a bang bang output would not limit the chip type at all.

MRCecil:
What are you using to generate the pulse pattern?
Also, what is the exact configuration of your parts and their values?
Im not sure what you mean by a schematic because all i did was use MrRB's values for the filter and generated the pattern using a program that reads the numerical values of his published values list and makes a pulse pattern out of it. If our numbers dont agree we can look into this further and see why they dont agree as you suggest.
Im also not sure what you mean by "50 or 51 slices of the 1kHz fundamental". I took the numbers that MrRB published and used them, so that would mean 25 periods above zero and 25 periods below zero, if that helps.
Since we are seeing a difference in results that may differ very greatly (as with the 50th) i would suggest that we start with a much much simpler pattern with only a couple switch transitions per cycle and compare results with that first and go from there. The new pattern would be:

(see attached drawing)
Positive half cycle: 0 1 0 1 0
Negative half cycle: 0 -1 0 -1 0
Both: 0 1 0 1 0 0 -1 0 -1 0

The timing would be as:
t=0.0ms 0
t=0.1ms 1 (switches to 1)
t=0.2ms 0 (switches to 0)
t=0.3ms 1
t=0.4ms 0
t=0.5ms 0
t=0.6ms -1 (switches to -1)
t=0.7ms 0 (switches to 0)
t=0.8ms -1
t=0.9ms 0
t=1.0ms (repeat above from t=0ms)

This is a very simple pattern and is easy to evaluate.

Electrician:
The definitition i use is the harmonic mean, which is:
THD=sqrt(v2^2+v3^2+v4^2+...+vInf^2)/v1

however for the filtered and even unfiltered pattern it is common to ignore very high frequencies as i mentioned previously, giving:
THD=sqrt(v2^2+v3^2+v4^2+...+vN^2)/v1

and the reason this works is because for almost any filter (except one designed just to void this rule for some reason) we end up with a filtered THD that approaches some limit of THD as the frequencies under consideration exceed some harmonic N. In other words:
THD(N)=THD(N+1)=THD(N+2)=THD(N+3)=THD(inf) approximately.
I chose N=157 as N=257 for example produces almost the same results, but can go higher if necessary.

2. Originally Posted by The Electrician
What do you mean by "sum of the roots"? Do you mean "sum of the square roots"? And, the THD must surely be something more than just a sum; shouldn't it involve a ratio? Furthermore, "sum of the roots" isn't the same as "...harmonic mean of all the harmonics up to a given harmonic number" is it?

I am unable to find a source describing the calculation of THD to be: "The Total THD is the harmonic mean of all the harmonics up to a given harmonic number". Can either of you give me a link to such a description?
I think it's obvious that I wasn't clear so let me restate. THD can be determined using either the actual recorded value of the fundamental and n harmonics or by using their normalized values. In the first case, it is a matter finding the root of the sum of the squares of Vn2, Vn3, Vn4...Vn divided by Vn1. If one normalizes the columnar data to the fundamental, the process is the same with a division by Vn1, which would be equal to 1. Yes there is a ratio involved in the computation, but to be sure, every number can be divided by 1.

My humble apologies for misstating my intended message to another.

Cheers

3. Originally Posted by MrAl
(see attached drawing)
Positive half cycle: 0 1 0 1 0
Negative half cycle: 0 -1 0 -1 0
Both: 0 1 0 1 0 0 -1 0 -1 0
Pardon me if I'm missing something here, but if you're trying to create a sine wave (even a degenerate one), shouldn't your pattern be

0 1 2 1 0 -1 -2 -1 0

(or normalized to 1: 0 0.5 1 0.5 0 -0.5 -1 -0.5 0)

or was this just a throwaway example?

4. Originally Posted by carbonzit
Pardon me if I'm missing something here, but if you're trying to create a sine wave (even a degenerate one), shouldn't your pattern be

0 1 2 1 0 -1 -2 -1 0

(or normalized to 1: 0 0.5 1 0.5 0 -0.5 -1 -0.5 0)

or was this just a throwaway example?
hi,
Just in case Al is not around, the pattern he shows is a binary pattern of the base PWM.

5. Hi again,

MRCecil:
Please read post #31 where i posted a much simpler pattern so we can compare our analysis results.

Carbon:
The pattern i suggested:
0p0p00n0n0 (p=1, n=-1)

is simply a very simple pattern that can be used to compare our analysis methods. This pattern was chosen because it was a little more complicated than a square wave but yet simple enough to allow a quite simple proof that the amplitudes of the harmonics analysis results are the true amplitudes. We can generate an approximate pattern using the harmonics once we know what they are. If we dont get the harmonics right, we will get a very different pattern, but if we get them right, using some of the lower order harmonics we can generate a pattern that gets closer and closer to that original test pattern as we use higher and higher harmonics for the pattern generation (instead of using pulses as the real pattern is generated). It's a graphical technique that can prove whether we are doing this right or not. It has nothing to do with a sine wave however, it's just a simple test pattern that's all.

6. Originally Posted by MrAl
Hello again MrRB, MRCecil, and Electrician,

I am replying to three different posts here...

Electrician:
The definitition i use is the harmonic mean, which is:
THD=sqrt(v2^2+v3^2+v4^2+...+vInf^2)/v1
This expression doesn't seem to be the same as the common definition of the harmonic mean: http://mathworld.wolfram.com/HarmonicMean.html

Can you show me how your expression is the same as the harmonic mean given at the above URL?

Originally Posted by MrAl
however for the filtered and even unfiltered pattern it is common to ignore very high frequencies as i mentioned previously, giving:
THD=sqrt(v2^2+v3^2+v4^2+...+vN^2)/v1

and the reason this works is because for almost any filter (except one designed just to void this rule for some reason) we end up with a filtered THD that approaches some limit of THD as the frequencies under consideration exceed some harmonic N. In other words:
THD(N)=THD(N+1)=THD(N+2)=THD(N+3)=THD(inf) approximately.
I chose N=157 as N=257 for example produces almost the same results, but can go higher if necessary.
In post #15, you said: "Without filter:
THD=0.3 (up to 157th harmonic)"

I think the OP's pattern gives much higher THD than 0.3 (percent) even with harmonics only up to the 157th taken into account.

7. Hello again Electrician,

I have to apologize as i often call that calculation a "harmonic mean" because it's similar to and invert hyperharmonic mean, but strictly speaking those definitions are not universal as you noticed. A better definition is simply the Norm/Fundamental, where the Norm is the norm of harmonics not including the fundamental:
THD=sqrt(v2^2+v3^2+v4^2+...+vN^2)/v1

To clear things up please refer to that formula above.
Also, i dont often worry about the pattern harmonics too much because they have little bearing on the final output which always gets some filtering as well as rise and fall time considerations which take the higher ones down too. You're right in that the true raw pattern is higher than that and i'll update my formula to reflect that. All of these kinds of patterns will have very nasty non filtered harmonics.

I was hoping that anyone here that wanted to test their methods would use that much simpler pattern i had posted so that we can compare notes. An even simpler pattern is to just use the first pulse in each half cycle, which gives only one positive pulse from 0.1ms to 0.2ms and one negative pulse from 0.6ms to 0.7ms. Would be nice to compare calculations using both of these patterns.

8. Originally Posted by Mr RB
MRCecil, could you please re-run the LTSpice THD figures with the exact filter I showed in post #16 but WITHOUT the inductor and with no load? That gave a very nice result on the scope, and removes any distortion that may be added in the simulator from LC resonance, and removes distortion from the 1k load. Maybe that could act as a "base point" for comparing THD figures with MrAl too, as it removes any complexity added by the inductor.
Here you go. The table in the scroll box below is the result of the configuration you noted.

Code :
```Fourier components of V(out)						     DC component:0.98498

Harmonic	Frequency	          Fourier 		Normalized	Normalized
Number 	[Hz]   		Component	         Component	          Phase [deg]
1	1.00E+03		4.76E-01		1.00E+00		    0.00°
2	2.00E+03		1.92E-04		4.04E-04		  -97.99°
3	3.00E+03		8.55E-05		1.80E-04		 -241.81°
4	4.00E+03		6.14E-05		1.29E-04		   15.04°
5	5.00E+03		4.03E-05		8.46E-05		  -89.47°
6	6.00E+03		3.63E-06		7.64E-06		 -191.11°
7	7.00E+03		5.69E-07		1.20E-06		  -52.12°
8	8.00E+03		1.87E-06		3.93E-06		 -190.44°
9	9.00E+03		1.38E-06		2.91E-06		   46.96°
10	1.00E+04		1.55E-06		3.26E-06		  -20.51°
11	1.10E+04		1.33E-07		2.79E-07		 -274.98°
12	1.20E+04		4.73E-07		9.95E-07		   39.57°
13	1.30E+04		4.21E-07		8.85E-07		   47.61°
14	1.40E+04		7.70E-07		1.62E-06		 -284.49°
15	1.50E+04		5.36E-07		1.13E-06		 -261.24°
16	1.60E+04		9.96E-08		2.10E-07		 -100.10°
17	1.70E+04		5.28E-07		1.11E-06		 -261.31°
18	1.80E+04		4.41E-07		9.26E-07		   69.65°
19	1.90E+04		5.49E-07		1.15E-06		 -281.09°
20	2.00E+04		9.79E-08		2.06E-07		   63.39°
21	2.10E+04		3.55E-07		7.46E-07		 -181.31°
22	2.20E+04		4.95E-07		1.04E-06		 -183.90°
23	2.30E+04		4.17E-07		8.77E-07		 -174.50°
24	2.40E+04		1.85E-07		3.88E-07		 -233.47°
25	2.50E+04		1.21E-07		2.54E-07		  -27.73°
26	2.60E+04		2.27E-08		4.77E-08		  -81.27°
27	2.70E+04		2.28E-07		4.79E-07		 -140.83°
28	2.80E+04		3.39E-07		7.14E-07		 -176.03°
29	2.90E+04		2.73E-07		5.74E-07		 -137.35°
30	3.00E+04		3.32E-07		6.97E-07		 -178.64°
31	3.10E+04		3.24E-07		6.81E-07		 -154.40°
32	3.20E+04		3.83E-07		8.06E-07		  -46.50°
33	3.30E+04		4.84E-07		1.02E-06		    2.57°
34	3.40E+04		4.55E-07		9.57E-07		  -61.33°
35	3.50E+04		3.19E-07		6.71E-07		 -122.73°
36	3.60E+04		3.47E-07		7.30E-07		  -97.41°
37	3.70E+04		2.10E-07		4.42E-07		 -269.62°
38	3.80E+04		2.53E-07		5.32E-07		  -22.59°
39	3.90E+04		7.32E-07		1.54E-06		    5.85°
40	4.00E+04		4.25E-07		8.94E-07		    5.82°
41	4.10E+04		1.25E-07		2.62E-07		 -285.14°
42	4.20E+04		3.13E-07		6.58E-07		 -170.18°
43	4.30E+04		1.32E-07		2.78E-07		 -260.56°
44	4.40E+04		2.73E-07		5.74E-07		 -242.86°
45	4.50E+04		2.80E-07		5.89E-07		   34.11°
46	4.60E+04		2.65E-07		5.56E-07		  -47.02°
47	4.70E+04		2.39E-07		5.03E-07		 -110.84°
48	4.80E+04		4.88E-06		1.03E-05		 -284.67°
49	4.90E+04		1.21E-04		2.54E-04		 -105.16°
50	5.00E+04		2.76E-04		5.80E-04		  -11.09°
51	5.10E+04		1.06E-04		2.22E-04		 -284.30°
52	5.20E+04		3.97E-06		8.34E-06		   73.81°
53	5.30E+04		5.00E-07		1.05E-06		    2.27°
54	5.40E+04		2.97E-07		6.24E-07		 -162.45°
55	5.50E+04		3.98E-07		8.36E-07		 -207.15°
56	5.60E+04		1.52E-07		3.20E-07		 -284.45°
57	5.70E+04		2.86E-07		6.01E-07		   44.05°
58	5.80E+04		3.44E-07		7.23E-07		  -35.49°
59	5.90E+04		1.42E-07		2.99E-07		  -33.77°
60	6.00E+04		2.27E-07		4.78E-07		 -120.41°
61	6.10E+04		1.04E-07		2.18E-07		 -214.94°
62	6.20E+04		4.23E-07		8.90E-07		 -252.88°
63	6.30E+04		5.22E-07		1.10E-06		 -241.35°
64	6.40E+04		1.62E-07		3.39E-07		 -239.86°
65	6.50E+04		1.08E-07		2.27E-07		  -19.82°
66	6.60E+04		1.38E-07		2.89E-07		 -159.60°
67	6.70E+04		7.29E-08		1.53E-07		  -39.95°
68	6.80E+04		3.52E-07		7.40E-07		 -230.28°
69	6.90E+04		2.10E-07		4.42E-07		 -121.18°
70	7.00E+04		2.45E-08		5.14E-08		 -208.48°
71	7.10E+04		2.21E-07		4.64E-07		 -120.21°
72	7.20E+04		2.45E-07		5.16E-07		  -61.12°
73	7.30E+04		2.56E-07		5.38E-07		  -47.63°
74	7.40E+04		1.10E-07		2.32E-07		 -280.13°
75	7.50E+04		1.72E-07		3.62E-07		 -130.86°
76	7.60E+04		1.04E-07		2.18E-07		    7.57°
77	7.70E+04		9.69E-08		2.04E-07		  -14.78°
78	7.80E+04		3.24E-07		6.81E-07		  -68.26°
79	7.90E+04		3.47E-07		7.29E-07		  -90.04°
80	8.00E+04		4.71E-07		9.90E-07		  -94.82°
81	8.10E+04		2.94E-07		6.18E-07		 -277.88°
82	8.20E+04		1.67E-07		3.51E-07		   44.93°
83	8.30E+04		4.86E-07		1.02E-06		   46.66°
84	8.40E+04		1.05E-07		2.21E-07		   36.53°
85	8.50E+04		2.77E-07		5.82E-07		   11.38°
86	8.60E+04		1.52E-07		3.19E-07		  -37.62°
87	8.70E+04		3.72E-07		7.83E-07		   26.20°
88	8.80E+04		3.42E-07		7.18E-07		   69.91°
89	8.90E+04		4.60E-07		9.67E-07		 -250.81°
90	9.00E+04		2.36E-07		4.96E-07		 -229.73°
91	9.10E+04		2.11E-07		4.44E-07		 -120.33°
92	9.20E+04		4.29E-07		9.02E-07		 -185.40°
93	9.30E+04		2.68E-07		5.63E-07		 -139.48°
94	9.40E+04		4.06E-07		8.53E-07		 -220.02°
95	9.50E+04		1.74E-07		3.66E-07		   39.78°
96	9.60E+04		1.21E-07		2.54E-07		   37.46°
97	9.70E+04		1.30E-07		2.74E-07		 -262.13°
98	9.80E+04		3.06E-07		6.43E-07		 -278.42°
99	9.90E+04		1.49E-06		3.14E-06		 -275.48°
100	1.00E+05		3.38E-06		7.10E-06		 -105.36°
101	1.01E+05		2.15E-06		4.53E-06		   36.23°
102	1.02E+05		3.60E-07		7.57E-07		 -119.16°
103	1.03E+05		2.22E-07		4.66E-07		 -164.77°
104	1.04E+05		1.14E-07		2.39E-07		   17.24°
105	1.05E+05		6.82E-08		1.43E-07		  -30.60°
106	1.06E+05		3.40E-07		7.14E-07		  -86.75°
107	1.07E+05		7.31E-08		1.54E-07		 -183.06°
108	1.08E+05		4.13E-07		8.67E-07		 -194.47°
109	1.09E+05		2.03E-07		4.27E-07		 -245.27°
110	1.10E+05		2.96E-07		6.22E-07		 -268.99°
111	1.11E+05		2.03E-07		4.27E-07		 -246.89°
112	1.12E+05		1.83E-07		3.85E-07		 -109.94°
113	1.13E+05		3.67E-07		7.71E-07		 -103.99°
114	1.14E+05		1.13E-07		2.37E-07		 -195.83°
115	1.15E+05		3.17E-07		6.67E-07		 -148.74°
116	1.16E+05		2.70E-07		5.69E-07		 -264.47°
117	1.17E+05		1.16E-07		2.44E-07		 -150.38°
118	1.18E+05		2.78E-07		5.84E-07		  -55.77°
119	1.19E+05		4.80E-07		1.01E-06		 -119.71°
120	1.20E+05		2.99E-07		6.29E-07		  -68.12°
121	1.21E+05		2.15E-07		4.52E-07		   70.51°
122	1.22E+05		2.73E-07		5.74E-07		   68.76°
123	1.23E+05		2.27E-08		4.78E-08		 -245.33°
124	1.24E+05		3.16E-07		6.65E-07		  -40.48°
125	1.25E+05		2.02E-07		4.26E-07		 -273.85°
126	1.26E+05		2.17E-07		4.55E-07		  -55.64°
127	1.27E+05		2.23E-07		4.70E-07		  -20.90°
128	1.28E+05		2.38E-07		5.00E-07		  -30.93°
129	1.29E+05		1.37E-07		2.88E-07		   28.30°
130	1.30E+05		8.84E-08		1.86E-07		   70.71°
131	1.31E+05		2.79E-07		5.86E-07		    1.79°
132	1.32E+05		3.22E-07		6.78E-07		   63.94°
133	1.33E+05		1.78E-07		3.74E-07		 -150.81°
134	1.34E+05		3.21E-07		6.75E-07		 -277.35°
135	1.35E+05		3.38E-07		7.11E-07		   62.71°
136	1.36E+05		3.25E-07		6.83E-07		   48.13°
137	1.37E+05		1.06E-07		2.24E-07		 -205.34°
138	1.38E+05		1.05E-07		2.22E-07		 -148.08°
139	1.39E+05		3.89E-07		8.18E-07		 -168.19°
140	1.40E+05		2.34E-07		4.91E-07		 -224.53°
141	1.41E+05		2.83E-07		5.95E-07		 -163.44°
142	1.42E+05		5.24E-08		1.10E-07		   -2.45°
143	1.43E+05		7.99E-08		1.68E-07		  -35.96°
144	1.44E+05		3.64E-07		7.66E-07		  -15.65°
145	1.45E+05		1.02E-07		2.13E-07		  -87.86°
146	1.46E+05		1.34E-07		2.82E-07		 -167.42°
147	1.47E+05		1.77E-07		3.73E-07		 -135.64°
148	1.48E+05		2.01E-07		4.22E-07		  -21.60°
149	1.49E+05		5.90E-07		1.24E-06		 -122.84°
150	1.50E+05		4.83E-06		1.02E-05		  -36.82°
151	1.51E+05		2.79E-06		5.87E-06		   60.94°
152	1.52E+05		5.69E-07		1.20E-06		 -282.52°
153	1.53E+05		5.91E-07		1.24E-06		 -240.02°
154	1.54E+05		9.92E-08		2.09E-07		  -19.07°
155	1.55E+05		2.52E-07		5.30E-07		  -67.14°
156	1.56E+05		8.47E-08		1.78E-07		 -128.93°
157	1.57E+05		3.37E-07		7.09E-07		 -140.71°
158	1.58E+05		1.89E-07		3.98E-07		 -170.23°
Total Harmonic Distortion: 0.081863%```

As you can see, the THD is ~0.082% unloaded. With a 10k load it's ~0.089%; 5k it's ~0.093%; at 1k it's ~0.143%. The two issues I see are the filter is very lossy and the 50khz "carrier" at 50kHz and "sidebands" at 49 & 51kHz. It appears to me there is not enough roll-off to whack down those harmonics.

I'll post the schematic to display the mock PIC used to generate the input as I would like you to confirm that is a close approximation to the PIC output you have in the plot of the output I'll also post, and because Mr AL was interested in how I was generating the input too.

Also, I will post the FFT of the output so you can see the actual spectrum.

9. Hi Mr Al,

Yes, I'm very familiar with the basics. What I was curious about was the pulse rate of the "carrier" that "slices" the 1kHz. The first time I encountered something similar to this was about 40 years ago in my tech days on a US Army time diversity phone Rx/Tx field terminal from the 1950's era;
I was out of the NAVY then and working at an Army depot repair facility; Go Navy!

I was asking because you mentioned the 51st harmonic a few times and I was not getting that same response with a 50kHz square wave. In my post #38 I think I included everything you asked for. If I left anything out, let me know.

10. Hello MRCecil,

What do you mean you are not getting the 51st harmonic with a 50kHz square wave? I dont think i said that you would :-)
What i did say is that you would see the 49th and the 51st harmonic if you generate the pattern that MrRB was talking about. Using a pulsing carrier and sine wave is not the same thing as using an actual pulsing pattern that goes from 0 to 1 or from 0 to 1 and from -1 to 0 for the negative half cycle. It is possible to completely cancel the 50th harmonic even though the pattern has 50 switching periods in it. I was going to show this same behavior with another wave too which is much simpler to generate and analyze. If you like, i can post an asc file for you to look at that generates the simple pattern and you can see for yourself.

While im waiting for you guys to reply again (and i'd like to hear some results on the simple pattern too eventually) im going over my own program and calculations just to make sure. One thing i did find already is that i was using a pattern that goes from 0 to 0.5 and from -0.5 to 0, rather than the one that goes from 0 to 1. In other words, i was assuming a 2 pin output pattern, which can be used to cancel many of the harmonics and make a really nice pattern.

« First 12345678 ... Last »
 Tags