DeltaSigma.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * DeltaSigma.h - Pulse-density modulation
8  *
9  * @author Sept 2019 mikee47 <mike@sillyhouse.net>
10  *
11  * https://en.wikipedia.org/wiki/Delta-sigma_modulation
12  *
13  ****/
14 
15 #pragma once
16 
17 #include <cstdint>
18 
20 {
21 public:
22  /*
23  * Produce 32 bits of modulated output for an input sample
24  */
25  uint32_t update(int16_t sample)
26  {
27  uint16_t DAC = 0x8000 + sample;
28  uint32_t acc = 0;
29  for(unsigned i = 0; i < 32; ++i) {
30  acc <<= 1;
31  if(DAC >= err) {
32  acc |= 1;
33  err += 0xFFFF - DAC;
34  } else {
35  err -= DAC;
36  }
37  }
38  return acc;
39  }
40 
41 private:
42  uint16_t err = 0;
43 };
Definition: DeltaSigma.h:19
uint32_t update(int16_t sample)
Definition: DeltaSigma.h:37