Talking about distortion: I have some trouble here as the PWM output seems to be rather distorted. 2nd is about 30dB down compared to the wanted 1kHz output. PWM frequency is 62.5kHz. Are my sine tables wrong or does the PWM output exhibit such a nonlinearity (load resistance 10k)? Own experience anyone? Thanks

Mark

## PWM nonlinearity

On page 2 of your schematic, you need 27 pF in parallel with the crystal. :twisted:

Seriously, we need a lot more information. Which AVR, what table? Crystal or RC?

You do know, for example, that a sine table alone won't work when the PWM isn't a multiple of the output frequency? The discontinuity at the end of the table will give you lots of unexpected harmonics.

There has been a similar thread.

The observation was already made.

See the following thread:

https://www.avrfreaks.net/index.p...

Look at my posts in this thread, some contain measurements and

results from experiments, but no explenation.

I think people building digital-audio-amplifiers could immediately

explain to us what happens. Its some kind of

non-linear (amplitued and frequency dependent) distortion.

Actually, I doubt more details would help much - though they might help to rule out the obvious. It's a tiny26 with 16MHz xtal (though I used 64MHz PLL first), PWM value 127-112 to 127+112, the output connected to 2nd order sallen-key butterworth lowpass with ~1000Hz cutoff and the sine table length is correct (i.e. number_of_samples * spacing_angle = 2*pi, or rather 8*2*pi since 8 periods are stored). I'll have to replace the PWM with a function generator once again to check the lowpass - if the distortion changes it has to be the PWM.

I am pretty sure, that it is PWM, as in my case (see above).

In my case I checked it by replaycing the PWM by a "TRUE" 8-Bit-R-2R-DAC

and the signal was much (around 25dB) cleaner.

I read the old thread now ossi. Thanks for your input! I suspected the output driver being nonlinear first but according to the datasheet, both sink and source capabilities seem to be well matched. So this isn't a very good explanation. I could use AT90PWM3 instead - it has a DAC. But the errata sheet says there are glitches when changing value. Somebody tell me this doesn't suck.

PWM glitches a lot (0 to VCC). Much easier to filter the glitch from a DAC.

**gizmo wrote:**

Actually, I doubt more details would help much - though they might help to rule out the obvious. It's a tiny26 with 16MHz xtal (though I used 64MHz PLL first), PWM value 127-112 to 127+112, the output connected to 2nd order sallen-key butterworth lowpass with ~1000Hz cutoff and the sine table length is correct (i.e. number_of_samples * spacing_angle = 2*pi, or rather 8*2*pi since 8 periods are stored). I'll have to replace the PWM with a function generator once again to check the lowpass - if the distortion changes it has to be the PWM.

But yet you do not specify how many total points your sine table has. If it has a length of 16, you have stored square wave there. You really should use the table for only one full period instead of 8 periods of the same sinewave, as more points per wave gives you more accuracy if you can play it back.

- Jani

**mneary wrote:**

PWM glitches a lot (0 to VCC). Much easier to filter the glitch from a DAC.

Nope! The PWM concept per se is linear. With my filter the 0 to VCC "glitches" at 62.5kHz are 69dB below the generated 1kHz. Now compare this to a DAC that has some undefined glitches unless lower 4 bits are kept constant. This is inherently nonlinear behavior no matter what filter you do apply.

Mark

**Jepael wrote:**

But yet you do not specify how many total points your sine table has. If it has a length of 16, you have stored square wave there. You really should use the table for only one full period instead of 8 periods of the same sinewave, as more points per wave gives you more accuracy if you can play it back.

- Jani

But I said I was generating 1kHz. 62.5kHz * 8periods /1kHz = 500. Actually I have 507 sample points and 986Hz output.

BTW, even a sampled square wave wouldn't contain 2nd harmonic!

Once again - IT'S NOTHING OBVIOUS. The problem seems to be hidden in the pwm output pin driver.

I would not call a PWM-modulator "linear" (see above mentioned distortions).

It only has the property, that the average of the generated voltage is

a linear function of the duty-cycle. So only in the

very low frequency limit we can expect "linear" behaviour. At higher frequencies we may observe odd behaviour !

ok so my assumptions of linearity were false. :(

thinking about it further, this is a variable phase shift induced distortion. centered pulses should work better. of course, tiny26 only has fast PWM mode... the idea of dealing with predistortion makes me sick.

Numerical simulation says you're using non-phase-correct mode, and haven't compensated the values in your table.

-35dB is about right (20 dB per division). In non-phase-correct mode, the center of the pulse moves with amplitude. This 'phase modulation with itself' generates that strong second harmonic.

Either go phase correct, or do the math to compensate the samples (since you're basically working with a non-constant sample rate)

Edit: once again one of those slow postings.

Edit: this post is no longer relevant, skip to the next

If you can use both PWM outputs, invert one of them, and sum them with two (20k) resistors, and you have a phase-symmetric output that pulses +5V and 0V, with a 2.5V center (idle) level.

reloading goes something like

OCR1A = 127 +112*sin() = table[]

OCR1B = 127 -112*sin() = 254-table[]

I feel like I'm talking to myself by now.

if you calculate your table values as

table[i]=round( 127+120*(sin(i/perlen*2*pi*8 )+sin(i/perlen*2*pi*16)*0.0232+sin(i/perlen*pi*24)*0.001) )

where i=0..perlen-1, perlen=507

then you should get an output from your EXISTING circuit that looks like this:

The harmonics I added counter the harmonics generated by the phase modulation, and so you should have a _theoretical_ floor of -64dB. With 10k in series with the output, that's less than what I will expect from the output fet nonlinearity, so a measurement should be around -55dB (provided your supply is clean, and there are no heavy loads in the on-die power distribution making that un-clean).

That's impressive KKP. Let me see...

Quite a good job KKP! 2nd is now below the noise floor. Unfortunately some 3rd is now visible. How did you calculate the amplitudes of the predistortion terms? The 0.001 in the third term seems suspicious as it's far below one LSB.

correction!

sin(i/perlen*2*pi *8 )+ sin(i/perlen*2*pi *16)*0.0232 + sin(i/perlen*2*pi *24)*0.001

the 2*pi dropped out of the third order term during edit/post. With that incorrect, the third order rises to -60dB. Not fatal, but not optimal either.

I do the following:

First, compose the proposed table of 507 samples.

Second, generate a waveform buffer of length 507*256.That's one point per clockcycle, and emulate the PWM into this buffer

Third, run DFT on the buffer.

Now comes the fun part: Select the most annoying harmonic, and add a counter-waveform of approximately the same amplitude. Repeat. This requires a little fiddling, since there is, as you say, quantization. The third order 0.001 term pushes all the samples a little, some of them will flip to the next value, and with enough fiddling one can find an optimum solution.

Do not apply brain cells when brute force will suffice :-)

Ready to use value table: http://xxx.kasperkp.dk/gizmo4.txt

How much 3rd order do you get?

I realized this was a typo and had it corrected from the beginning... there must be some other problem. 3rd is at least 20dB more than you predicted.

Does that mean -44 or -34dB? And this is without the filter, I assume.

Ideas: Assume I mispredicted the output impedance change of the pin. What if you give it 100k in series, what does that do to the distortion factor? Did your calculated table match mine? Does your filter have 3rd order distortion? (I'm missing my favourite scratches-head smiley here)

What are you measuring with? You aren't, like one of my pimply faced youths did, measuring this with an 8 bit scope with a distorting input amplifier, are you?