|
| 1 | +"""SAR ADC models""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from adc_eval.adcs.basic import ADC |
| 5 | + |
| 6 | + |
| 7 | +class SAR(ADC): |
| 8 | + """ |
| 9 | + SAR ADC Class. |
| 10 | +
|
| 11 | + Parameters |
| 12 | + ---------- |
| 13 | + nbits : int, optional |
| 14 | + Number of bits for the ADC. The default is 8. |
| 15 | + fs : float, optional |
| 16 | + Sample rate for the ADC in Hz. The default is 1Hz. |
| 17 | + vref : float, optional |
| 18 | + Reference level of the ADC in Volts ([0, +vref] conversion range). The default is 1. |
| 19 | + seed : int, optional |
| 20 | + Seed for random variable generation. The default is 1. |
| 21 | + **kwargs |
| 22 | + Extra arguments. |
| 23 | + weights : list, optional |
| 24 | + List of weights for SAR capacitors. Must be >= nbits. Defaults to binary weights. |
| 25 | + MSB weight should be in index 0. |
| 26 | +
|
| 27 | + Attributes |
| 28 | + ------- |
| 29 | + vin : float |
| 30 | + Sets or returns the current input voltage level. Assumed +/-vref/2 input |
| 31 | + vlsb : float |
| 32 | + LSB voltage of the converter. vref/2^nbits |
| 33 | + noise : float, default=0 |
| 34 | + Sets or returns the stdev of the noise generated by the converter. |
| 35 | + weights : list |
| 36 | + Sets or returns the capacitor weighting of the array. Default is binary weighting. |
| 37 | + mismatch : float |
| 38 | + Sets or returns the stdev of the mismatch of the converter. Default is no mismatch. |
| 39 | + comp_noise : float |
| 40 | + Sets or returns the stdev of the comparator noise. Default is no noise. |
| 41 | + offset : tuple of float |
| 42 | + Sets the (mean, stdev) of the offset of the converter. Default is no offset. |
| 43 | + gain_error : tuple of float |
| 44 | + Sets the (mean, stdev) of the gain error of the converter. Default is no gain error. |
| 45 | + distortion : list of float |
| 46 | + Sets the harmonic distortion values with index=0 corresponding to HD1. |
| 47 | + Example: For unity gain and only -30dB of HD3, input is [1, 0, 0.032] |
| 48 | + dout : int |
| 49 | + Digital output code for current vin value. |
| 50 | +
|
| 51 | + Methods |
| 52 | + ------- |
| 53 | + run_step |
| 54 | +
|
| 55 | + """ |
| 56 | + |
| 57 | + def __init__(self, nbits=8, fs=1, vref=1, seed=1, **kwargs): |
| 58 | + """Initialization function for Generic ADC.""" |
| 59 | + super().__init__(nbits, fs, vref, seed) |
| 60 | + |
| 61 | + self._mismatch = None |
| 62 | + self._comp_noise = 0 |
| 63 | + |
| 64 | + # Get keyword arguments |
| 65 | + self._weights = None |
| 66 | + self.weights = kwargs.get("weights", self.weights) |
| 67 | + |
| 68 | + @property |
| 69 | + def weights(self): |
| 70 | + """Returns capacitor unit weights.""" |
| 71 | + if self._weights is None: |
| 72 | + self._weights = np.flip(2 ** np.linspace(0, self.nbits - 1, self.nbits)) |
| 73 | + return np.array(self._weights) |
| 74 | + |
| 75 | + @weights.setter |
| 76 | + def weights(self, values): |
| 77 | + """Sets the capacitor unit weights.""" |
| 78 | + self._weights = np.array(values) |
| 79 | + self.dbits = np.zeros(len(values)) |
| 80 | + if self._weights.size < self.nbits: |
| 81 | + print( |
| 82 | + f"WARNING: Capacitor weight array size is {self._weights.size} for {self.nbits}-bit ADC." |
| 83 | + ) |
| 84 | + self.mismatch = self.err["mismatch"] |
| 85 | + |
| 86 | + @property |
| 87 | + def mismatch(self): |
| 88 | + """Return noise stdev.""" |
| 89 | + if self._mismatch is None: |
| 90 | + self._mismatch = np.zeros(self.weights.size) |
| 91 | + return self._mismatch |
| 92 | + |
| 93 | + @mismatch.setter |
| 94 | + def mismatch(self, stdev): |
| 95 | + """Sets mismatch stdev.""" |
| 96 | + self.err["mismatch"] = stdev |
| 97 | + self._mismatch = np.random.normal(0, stdev, self.weights.size) |
| 98 | + self._mismatch /= np.sqrt(self.weights) |
| 99 | + |
| 100 | + @property |
| 101 | + def comp_noise(self): |
| 102 | + """Returns the noise of the comparator.""" |
| 103 | + return self._comp_noise |
| 104 | + |
| 105 | + @comp_noise.setter |
| 106 | + def comp_noise(self, value): |
| 107 | + """Sets the noise of the comparator.""" |
| 108 | + self._comp_noise = value |
| 109 | + |
| 110 | + def run_step(self): |
| 111 | + """Run a single ADC step.""" |
| 112 | + vinx = self.vin |
| 113 | + |
| 114 | + cweights = self.weights * (1 + self.mismatch) |
| 115 | + cdenom = sum(cweights) + 1 |
| 116 | + |
| 117 | + comp_noise = np.random.normal(0, self.comp_noise, cweights.size) |
| 118 | + |
| 119 | + # Bit cycling |
| 120 | + vdac = vinx |
| 121 | + for n, _ in enumerate(cweights): |
| 122 | + vcomp = vdac - self.vref / 2 + comp_noise[n] |
| 123 | + compout = vcomp * 1e6 |
| 124 | + compout = -1 if compout <= 0 else 1 |
| 125 | + self.dbits[n] = max(0, compout) |
| 126 | + vdac -= compout * self.vref / 2 * cweights[n] / cdenom |
| 127 | + |
| 128 | + # Re-scale the data |
| 129 | + scalar = 2**self.nbits / cdenom |
| 130 | + self.dval = min(2**self.nbits - 1, scalar * sum(self.weights * self.dbits)) |
0 commit comments