@@ -36,6 +36,8 @@ class SAR(ADC):
3636 Sets or returns the capacitor weighting of the array. Default is binary weighting.
3737 mismatch : float
3838 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.
3941 offset : tuple of float
4042 Sets the (mean, stdev) of the offset of the converter. Default is no offset.
4143 gain_error : tuple of float
@@ -57,25 +59,28 @@ def __init__(self, nbits=8, fs=1, vref=1, seed=1, **kwargs):
5759 super ().__init__ (nbits , fs , vref , seed )
5860
5961 self ._mismatch = None
62+ self ._comp_noise = 0
6063
6164 # Get keyword arguments
6265 self ._weights = kwargs .get ("weights" , None )
63-
66+
6467 @property
6568 def weights (self ):
6669 """Returns capacitor unit weights."""
6770 if self ._weights is None :
68- self ._weights = np .flip (2 ** np .linspace (0 , self .nbits - 1 , self .nbits ))
71+ self ._weights = np .flip (2 ** np .linspace (0 , self .nbits - 1 , self .nbits ))
6972 return np .array (self ._weights )
7073
7174 @weights .setter
7275 def weights (self , values ):
7376 """Sets the capacitor unit weights."""
7477 self ._weights = np .array (values )
7578 if self ._weights .size < self .nbits :
76- print (f"WARNING: Capacitor weight array size is { self ._weights .size } for { self .nbits } -bit ADC." )
79+ print (
80+ f"WARNING: Capacitor weight array size is { self ._weights .size } for { self .nbits } -bit ADC."
81+ )
7782 self .mismatch = self .err ["mismatch" ]
78-
83+
7984 @property
8085 def mismatch (self ):
8186 """Return noise stdev."""
@@ -90,24 +95,34 @@ def mismatch(self, stdev):
9095 self ._mismatch = np .random .normal (0 , stdev , self .weights .size )
9196 self ._mismatch /= np .sqrt (self .weights )
9297
98+ @property
99+ def comp_noise (self ):
100+ """Returns the noise of the comparator."""
101+ return self ._comp_noise
102+
103+ @comp_noise .setter
104+ def comp_noise (self , value ):
105+ """Sets the noise of the comparator."""
106+ self ._comp_noise = value
107+
93108 def run_step (self ):
94109 """Run a single ADC step."""
95110 vinx = self .vin
96-
111+
97112 cweights = self .weights * (1 + self .mismatch )
98113 cdenom = sum (cweights ) + 1
99-
100- comp_noise = np .random .normal (0 , self .err [ "noise" ] , cweights .size )
101-
114+
115+ comp_noise = np .random .normal (0 , self .comp_noise , cweights .size )
116+
102117 # Bit cycling
103118 vdac = vinx
104- for n in range ( len ( cweights ) ):
105- vcomp = vdac - self .vref / 2
119+ for n , _ in enumerate ( cweights ):
120+ vcomp = vdac - self .vref / 2 + comp_noise [ n ]
106121 compout = vcomp * 1e6
107122 compout = - 1 if compout <= 0 else 1
108123 self .dbits [n ] = max (0 , compout )
109124 vdac -= compout * self .vref / 2 * cweights [n ] / cdenom
110-
125+
111126 # Re-scale the data
112127 scalar = 2 ** self .nbits / cdenom
113- self .dval = min (2 ** self .nbits - 1 , scalar * sum (self .weights * self .dbits ))
128+ self .dval = min (2 ** self .nbits - 1 , scalar * sum (self .weights * self .dbits ))
0 commit comments