How does Pulse Density Modulation (PDM) actually work? - Page 1
Forum section: electronics / beginners

author: matrixofdynamism
author_rank: Regular Contributor
timestamp: July 18, 2022, 11:28:40 pm
content: I am confused between Pulse Width Modulation (PWM) and Pulse Density Modulation (PDM).From my understanding, both are used to transmit a digital signal into an analogue domain. It is basically doing a digital to analogue coversion. Is this correct?In PWM, we generate an on-off signal at a fixed frequency. The On-period of the signal in each time period is what controls the amplitude of the resulting signal in the analogue domain. In this way, a digital circuit can output an analogue signal.Now lets come to the PDM. Here also we have an on-off signal but there is no fixed frequency. How do we drive the PDM from lets say a sinusoid i.e when to turn it on and when to turn it off?From my university days more than 10 years ago, I remember there being PCM, PWM, PAM, QPSK but not PDM.

author: langwadt
author_rank: Super Contributor
timestamp: July 18, 2022, 11:42:14 pm
content: PDM does have a fixed frequency, it is the length of a pulse. Each pulse can be one or zero, and the number of ones/zeros over some period set the amplitude

author: T3sl4co1l
author_rank: Super Contributor
timestamp: July 19, 2022, 01:40:58 am
content: I suppose PDM can be more freeform, but it usually means a sigma-delta bitstream, which is to say, exactly that. ^^For example, to convert PDM to PCM, count up the number of '1's in a given period. Or to convert back, to a certain extent you can simply set a PWM timer to suitable settings -- but the exact settings needed are not obvious, for example PDM could have a pattern of alternating 1/0 for 3-5-4-5-(repeating) clock cycles, which you will not reconstruct exactly by taking the flat ratio (7/17) because it has a higher spectrum than by grouping all those pulses together.Also, if PDM is defined as synchronous to a bit clock, then PWM generated by a timer is a strict subset of PDM.There are also kinds of PDM (which may or may not be time-quantized i.e. clocked/synchronous) for very specific cases, like for the sine wave example, there are "magic sinewaves" such as Don Lancaster was very interested in, back in the day; this is a sequence of pulses which has been tuned to not just minimize but indeed eliminate harmonics up to some cutoff frequency (N harmonics for N pulses). They're not very useful though, because they're not controllable: you have to recalculate the whole thing (simultaneous solutions over trig functions!) for other amplitude, or if you need some harmonics anyway (say as predistortion for a nonlinear load?). And obviously, the computational effort goes up considerably (polynomial? I forget what order), making it rather impractical on all but the most powerful systems (but, when you have say a 300MHz DSP handy -- who really cares?).As for PCM, the code that's being modulated, is any kind of code. Usually it's the analog level on the line (DAC output), or a digital count corresponding to such (less some quantization noise) (ADC output, WAV data, etc.) A pretty archaic term, I suppose -- but then, back in the day, it was new, the digital way of doing things, not the default and given it is today.Ed: related reading:https://electronics.stackexchange.com/questions/627934/how-is-pulse-density-module-pdm-signal-drivenTim

author: Nominal Animal
author_rank: Super Contributor
timestamp: July 19, 2022, 06:34:27 am
content: I've described PDM inhereandhere.If you look at a PWM signal, it consists of regular periods, during which there is either zero or two transitions (so there is either one pulse, or the signal stays low (0% duty) or high (100% duty) for the entire period). To determine the value it represents, you take the average over exactly one period.If you look at a PDM signal, it can change state every clock cycle. It contains much higher frequency components. To determine the value at any clock cycle, you average over a fixed number of clock cycles; that number is up to you.PWM duty can change at most once per period, but PDM duty can change every clock cycle.PWM is implemented using a simple counter. The period of the counter determines the resolution of the signal.PDM is implemented using an addition every clock cycle, with the overflow/carry determining the output state. The addition is done using modular (wraparound) arithmetic, and the register width determines the precision, but also the lowest frequency components that can be produced. Just like in PWM, full resolution is achieved only when averaging over the wraparound number of samples. (So, if you use an n-bit register, over 2nsamples.)Let's say we sample the sine wave at 1024 regular points using 8-bit precision in PWM, PDM, and PCM, all using 256 clock cycles per sample, and then do a discrete Fourier transform to obtain the amplitude spectrum of these three signals. In Python:Code:#!/usr/bin/env python3# -*- coding: utf-8 -*-import scipy as spimport numpy as npfrom math import sin, pifrom sys import stdout, stderrstderr.write("Generating data ... ")stderr.flush()pwm = np.array(range(0, 256 * 1024), dtype = np.float)pdm = np.array(range(0, 256 * 1024), dtype = np.float)pcm = np.array(range(0, 256 * 1024), dtype = np.float)pdmstate = 0for i in range(0, 1024):sample = round(128.0 + 112.0 * sin(i * pi / 512.0))pwm[256*i:256*i+sample] = 1.0pwm[256*i+sample:256*i+256] = 0.0pcm[256*i:256*i+256] = samplefor k in range(i*256, i*256+256):pdmstate += round(128.0 + 112.0 * sin(k * pi / 131072.0))pdm = pdmstate // 256pdmstate = pdmstate & 255stderr.write("Done.\n")stderr.write("Calculating FFT ... ")stderr.flush()pwmfft = np.fft.rfft(pwm, norm='ortho')pdmfft = np.fft.rfft(pdm, norm='ortho')pcmfft = np.fft.rfft(pcm, norm='ortho')stderr.write("Done.\n")stderr.flush()# pwmfft = np.fft.fftshift(pwmfft)# pdmfft = np.fft.fftshift(pdmfft)freq = np.fft.rfftfreq(len(pwm))pcmamp = np.abs(pcmfft) # **2 for power spectrum instead of amplitude spectrumpwmamp = np.abs(pwmfft) # **2 for power spectrum instead of amplitude spectrumpdmamp = np.abs(pdmfft) # **2 for power spectrum instead of amplitude spectrumfor i in range(0, 1024*128):stdout.write("%14.12f %18.12f %18.12f %18.12f\n" % (freq, pcmpower, pwmpower, pdmpower))The output contains the frequency in the first column, the amplitude spectrum for the PCM signal in the second column, the amplitude spectrum for the PWM signal in the third column, and the amplitude spectrum for the PDM signal in the fourth column.If we plot these using logarithmic axes, ignoring the very lowest frequencies (longest wavelengths), we get the following graph:(I saved the above output in a file namedout, and then ranset term png enhanced size 1024,384 ; set output 'plot.png' ; unset xtic ; unset ytics ; set xlabel 'Frequency, logarithmic' ; set ylabel 'Magnitude, logarithmic' ; set logscale xy ; set xrange [0.0001:0.5] ; plot 'out' u 1:3 t 'PWM' w lines lc rgb '#ff0000', 'out' u 1:4 t 'PDM' w lines lc rgb '#0000ff', 'out' u 1:2 t 'PCM' w lines lc -1in Gnuplot.)The PCM signal has a lot of spread-out quantization noise. (It stays constant for 256 cycles, as it has 256 different amplitudes, whereas the two others have only amplitudes 0 or 1. This causes the spectral effects.) Anyway, PCM/DAC signal only needs a lowpass filter to yield acceptable results.The PWM signal has a HUGE spike at the 256-sample wavelength, because that's its period. This is very difficult to filter out.The PDM signal, on the other hand, has most of its spectral content in the higher frequencies. If you look carefully, you'll see that its first spike is about one quarter from the right edge (specifically, at the 16th PWM spike counting from left), corresponding to the original sine wave frequency.This means that there isn't much low-frequency noise, and a low-pass filter can easily remove the quantization noise since it is at those very high frequencies, and spread out even there.

author: RoGeorge
author_rank: Super Contributor
timestamp: July 19, 2022, 08:23:03 am
content: This animation might help visualising the differences. Yellow is the PDM, blue is PWM. The duty factor is varied back and forth between 10% and 90% simultaneously for both types of modulation. The resulting averaged DC (moving back and forth between 0.1Vcc and 0.9Vcc with the duty factor) will be the same for both, but it is not shown in this animation.The animated gif is fromhttps://hackaday.io/project/6356-delta-sigma-versus-pwmIn the same project there is a code example of how to implement PDM in software.The PDM algorithm is very simple:- keep adding the same number to an accumulator of a given size, in an infinite loop- after each add, write the status of the overflow flag to a digital output pinThe number to add is proportional with the desired duty factor.

author: Picuino
author_rank: Super Contributor
timestamp: July 19, 2022, 09:43:07 am
content: As far as I know, PDM is typically used in switching power supplies when the load is very low.Imagine you have a step-down converter with PWM and the minimum pulse is 0.2us every 20us and this is too much for the output current requirement (e.g. standby).Then you switch to PDM modulation. You can switch on the power supply with a 0.2us pulse and wait for the output voltage to drop, then switch on the power supply again for 0.2us. The time between pulses can be between 20us and 500us depending on the current consumption. You don't have a fixed frequency, but you do have a fast response to the output voltage drop due to power consumption.

author: langwadt
author_rank: Super Contributor
timestamp: July 19, 2022, 08:57:24 pm
content: Quote from: Nominal Animal on July 19, 2022, 06:34:27 amthat is basically a first order delta-sigma modulator, you can have higher orders, but above second order figuring out if it will stable gets "interesting"Quote from: Nominal Animal on July 19, 2022, 06:34:27 amif you take a delta-sigma modulator and "model" the quantizer as an adder that adds noise and write out the transfer functions for signal and noise, you'll see that the transfer function for the signal from input to output is a delay and that the transfer function for the the added quantization noise to the output is a high pass filter, the higher the order of the modulator the steeper the filter.iow the error gets pushed to high frequencies where they can be removed by the reconstruction filter

author: Nominal Animal
author_rank: Super Contributor
timestamp: July 20, 2022, 12:51:10 am
content: Exactly, langwadt! (Apologies for not using the correct terminology, and not mentioning delta-sigma modulation, too.)If one considers e.g. signal 10110100, one way it can be reconstructed as a waveform is╱╲╱╲╱╲╱ ╲where 1 corresponds to a rise and 0 to a fall in the signal, which is obviously a summing-changes (sigma-delta) modulation scheme.I know this can be combined with ann-bit DAC, having the DAC encode theslope, yielding a signal that ought to be easier to filter than a purely dithered one, but I haven't examined it. (Difference between dithering and encoding the slope is one of those where the simplest of each yields the same results, but the results start behaving differently when you add further details, like how you generate the dithering pattern, and whether you use a predictor-corrector (higher degree) for the slope or just the instant (first degree) slope.) I am not sure, but I think it is one of the cheap techniques one can use to make a high-performance audio DAC without requiring uncanny precision resistors. As usual, it's really the amplifier stage that is harder to do.Quote from: RoGeorge on July 19, 2022, 08:23:03 amIn addition to the unwanted "noise" being at much higher frequencies in PDM than in PWM, PDM can also be modulated at every single clock cycle. That is, the duty cycle does not need to stay constant, but can vary at every clock cycle. The effect this has on the resulting waveform is a bit more complicated than just summing or averaging, because the changes do directly affect the highest frequencies, but as an intuitive approximation it works well.For things like switchmode supplies at low duty, this lets them react quickly to changes in the demand. The downside, of course, is that you have many, many more transitions, which aren't instant in real life, during which e.g. MOSFETs are in their linear region, causing them to generate more heat than they would at similar duty PWM (which minimizes the minimum number of transitions needed).

author: RoGeorge
author_rank: Super Contributor
timestamp: July 20, 2022, 07:31:12 am
content: Quote from: Nominal Animal on July 20, 2022, 12:51:10 am-----------------------( That's a sleeping cat )-----------------------o ^__^o (oo)\_______(__)\ )\/\||----w ||| ||cat's ears -> ╱╲╱╲cat's tail -> ╱╲╱ 00 ╲