Chosto Do you mean that there is a 1.38kHz frequency component in the output frequency spectrum - that does not exist in the input signal ? Is this because the switching frequency is fixed ?
This is correct. You would see this directly on an oscilloscope. To exactly meet the conditions of the Nyquist Sampling theorem, a brick-wall filter would be applied at 1/2 the PWM frequency and you would be left with the pure tone. In the real-world you are expecting the speaker to take off this carrier noise, but in your case the 1.38 kHz sampling period is within the band of the coil, so you either have to add steep analog filtering at the output to pull it down if it is a problem in your application.
Chosto So a 1-bit DAC is the same as PDM, but a PWM with 1-bit resolution would give nothing but a two-state analog output after being filtered by a low-pass filter ?
PDM and PWM can both be considered 1-bit DACs. PDM takes advantage of noise to evenly spread the spectral density of the quantization error over a wider frequency band. PWM focuses the quantization noise into a narrow band. You can think of PWM as putting all of the pulses of an averaging period into adjacent time slots, where PDM spreads all of those pulses at random intervals. In both cases you can increase resolution by decreasing bandwidth, even though it may initially seem counter-intuitive that you can get 16-bit resolution from a 1-bit DAC.
A 1-bit DAC like the high-end stuff uses feedback so it adjusts the average output high frequency noise power to match the low-bandwidth signal power. It's sort of like amplitude modulation where the carrier is wide-band high frequency noise. The output pulse train is filtered to remove the high frequency noise The lower frequency cut-off used, the more points used in the averaging, the higher the effective resolution. With a 44.1 kHz bit rate, you're pretty much limited to 2 or 3 bits if you want to reproduce something up to 1 kHz.
Chosto I have read that 1-bit DAC are very common nowadays, even high-end devices. Why is there a lot of quantization noise in that case ?
I think my comment above might have addressed this one too. Basically if you look at something changing between 1 and 0 at a really high rate (usually > 20 MHz) you can average it over a much longer period of time.
Say you have a sequence 10101101. Averaged over 8-samples gives you the value 0.625. As you can see this has 8-bit resolution. To change this sequence into PWM, you have 11111000, which averages to the same level. If you output this sequence repeatedly, you will get one particularly dominant frequency in the output. If you output the PDM sequence 10101101, you will get the same average value but the noise moves to a wider band at higher frequencies.
Suppose you have a sample at 0.75, 44.1 kHz base sample rate. You can output a sequence of '1' and '0' pulses that average out to 0.75 over the duration of 1/44.1k = 22.68us averaging interval. To get 16-bit resolution you need a total of 216 pulses during that interval, so you need 44.1k*216 2.89GHz pulse rate -- at least, that is what you would need for PWM.
How does a sigma-delta modulator get 16-bit resolution with only a 20MHz bit rate? This is related to noise-shaping and post-filtering -- the quantization noise is pushed up into the band where it can be more easily filtered off (think of the difference between 11111000 and 10101101). It's hard for me to explain it without pointing you to a paper on sigma-delta modulators, but basically this is focusing the noise into a band that will be naturally rejected by the physical system (output filters and/or speaker).
Chosto As this is what a class D amplifier does, I think it is suited for this, am I right ?
Not the way you are thinking. A Class D pulse modulation is usually >500 kHz. You will be feeding it a 1.38 kHz square wave. The class D amp will chop that up, reconstruct it, and output an amplified version of what you put in to your speakers.
For whatever it is worth, the industry has many names for switch-mode amplifiers (like Class D). These trademark names often refer to some form of PDM/Noise Shaping as compared to classical PWM. I think some variation on PWM is still dominant in higher power amps because switching losses in the FETs would become important, lowering the efficiency of the amp. Fortunately an analog PWM circuit does not quantize the input signal so the resolution trade-off is not an issue as long as the switching frequency is high enough to be rejected by the speakers.
Chosto Also, thanks a lot for your code. I'll test it as soon as I receive the Bela.
I'm not certain my code does what you want to do. It's more of a proof-of concept regarding PDM vs PWM and the potential to spread the quantization noise. If you really want to reproduce certain wave shapes this needs to evolve into a some form of a sigma delta modulator because we can push a lot of the noise up >10 kHz.
If all you want is a variable frequency output with a noise spectrum generally centered around any arbitrary point then my code might do what you want. I'm not very familiar with haptics, nor with your particular application so I'm not sure how elegant of a system is required to do what you actually need.
I need more time to figure out what does a biquad IIR filter.
In my code it implements a band-pass function. Noise is used as the signal input, filtered noise is the signal output.
IIR stands for "infinite impulse response", which means it recirculates some of the output back to the input. Neglecting digital resolution and noise floor it would hypothetically recirculate all inputs from all time forever as the history decays toward zero. In reality the past history decays to <2-32 (or whatever resolution) pretty quickly.
"Biquad" refers to the filter order, in this case order 2. It means the magnitude @ angle vector associated with the transfer function occupies two quadrants (phase shift limited between 0 to 180 degrees is one way to think of it). In contrast a first-order filter has phase response contained in the first quadrant (0 to 90 degrees).