def change_filter(self, b0, b1, x1init=0, *args, **kwargs): self.b0 = fixed_point(b0, self.int_bits, self.frac_bits) if self.quantize else b0 self.b1 = fixed_point(b1, self.int_bits, self.frac_bits) if self.quantize else b1 self.x1 = x1init print("!!!!!!!!!1 x1init=", x1init) if self.quantize and self.verbose and self.quant_filt: print( "\n* Finite word representations - int bits = %d, frac bits = %d" % (self.int_bits, self.frac_bits)) print("\tb0 = %E, error = %E" % (self.b0, -b0 + self.b0)) print("\tb1 = %E, error = %E" % (self.b1, -b1 + self.b1)) print("\tb0+b1 = %E, error=%E" % (self.b0 + self.b1, b0 + b1 - self.b0 - self.b1))
def __init__(self, a0, a1, b1, b2, a2=0.0, init_out=0, init_clk=0, int_bits=None, frac_bits=None, verbose=True, ignore_clk=False, quant_filt=True, out_bits=32, *args, **kwargs): """ Loop filter with tunable pole, zero """ if int_bits and frac_bits: self.quantize = True else: self.quantize = False self.int_bits = int_bits self.frac_bits = frac_bits self.verbose = verbose self.ignore_clk = ignore_clk self.max_int = 2**out_bits - 1 self.y1 = init_out self.y2 = init_out self.x1 = 0 self.a0 = fixed_point(a0, int_bits, frac_bits) if self.quantize else a0 self.a1 = fixed_point(a1, int_bits, frac_bits) if self.quantize else a1 self.a2 = fixed_point(a2, int_bits, frac_bits) if self.quantize else a2 self.b1 = fixed_point(b1, int_bits, frac_bits) if self.quantize else b1 self.b2 = fixed_point(b2, int_bits, frac_bits) if self.quantize else b2 self.clk_last = init_clk if self.quantize and verbose and quant_filt: print( "\n* Finite word representations - int bits = %d, frac bits = %d" % (int_bits, frac_bits)) print("\ta0 = %E, error = %E" % (self.a0, -a0 + self.a0)) print("\ta1 = %E, error = %E" % (self.a1, -a1 + self.a1)) if a2: print("\ta2 = %E, error = %E" % (self.a2, -a2 + self.a2)) print("\tb1 = %E, error = %E" % (self.b1, -b1 + self.b1)) print("\tb2 = %E, error = %E" % (self.b2, -b2 + self.b2)) print("\ta0+a1 = %E, error=%E" % (self.a0 + self.a1, a0 + a1 - self.a0 - self.a1)) print("\t-b1-b2 = %E, error=%E" % (-self.b1 - self.b2, -b1 - b2 + self.b1 + self.b2))
def __init__(self, b0, b1, init_out=0, init_clk=0, int_bits=None, frac_bits=None, verbose=True, ignore_clk=False, quant_filt=True, out_bits=32, *args, **kwargs): """ b0 is assumed to be 0 """ if int_bits and frac_bits: self.quantize = True else: self.quantize = False self.quant_filt = quant_filt self.int_bits = int_bits self.frac_bits = frac_bits self.verbose = verbose self.ignore_clk = ignore_clk self.max_int = 2**out_bits - 1 self.out_bits = out_bits self.y1 = init_out self.x1 = 0 self.b0 = fixed_point(b0, int_bits, frac_bits) if self.quantize else b0 self.b1 = fixed_point(b1, int_bits, frac_bits) if self.quantize else b1 self.clk_last = init_clk if self.quantize and verbose and quant_filt: print( "\n* Finite word representations - int bits = %d, frac bits = %d" % (int_bits, frac_bits)) print("\tb0 = %E, error = %E" % (self.b0, -b0 + self.b0)) print("\tb1 = %E, error = %E" % (self.b1, -b1 + self.b1)) print("\tb0+b1 = %E, error=%E" % (self.b0 + self.b1, b0 + b1 - self.b0 - self.b1))
def change_filter(self, a0, a1, b1, b2, a2=0.0, *args, **kwargs): self.a0 = fixed_point(a0, self.int_bits, self.frac_bits) if self.quantize else a0 self.a1 = fixed_point(a1, self.int_bits, self.frac_bits) if self.quantize else a1 self.a2 = fixed_point(a2, self.int_bits, self.frac_bits) if self.quantize else a2 self.b1 = fixed_point(b1, self.int_bits, self.frac_bits) if self.quantize else b1 self.b2 = fixed_point(b2, self.int_bits, self.frac_bits) if self.quantize else b2 if self.quantize and self.verbose: print( "\n* Finite word representations - int bits = %d, frac bits = %d" % (self.int_bits, self.frac_bits)) print("\ta0 = %E, error = %E" % (self.a0, -a0 + self.a0)) print("\ta1 = %E, error = %E" % (self.a1, -a1 + self.a1)) print("\tb1 = %E, error = %E" % (self.b1, -b1 + self.b1)) print("\tb2 = %E, error = %E" % (self.b2, -b2 + self.b2)) print("\ta0+a1 = %E, error=%E" % (self.a0 + self.a1, a0 + a1 - self.a0 - self.a1)) print("\t-b1-b2 = %E, error=%E" % (-self.b1 - self.b2, -b1 - b2 + self.b1 + self.b2))
def update(self, xin, clk): """ args: xin - filter input clk - clk of filter returns: filtered version of input """ if is_edge_phase(clk, self.clk_last) or self.ignore_clk: ynew = self.b0 * xin + self.b1 * self.x1 + self.y1 if self.quantize: ynew = fixed_point(ynew, self.out_bits, self.frac_bits) self.x1 = xin self.y1 = ynew self.clk_last = clk if self.y1 < 0: return 0 elif self.y1 > self.max_int: return self.max_int else: return np.floor(self.y1)
def quant_lf_params(lf_params, int_bits, frac_bits): _lf_params = copy(lf_params) for key in ["a0", "a1", "b0", "b1", "b2"]: if lf_params[key] is not np.inf: _lf_params[key] = fixed_point(lf_params[key], int_bits, frac_bits) return _lf_params
print("\tBased on inital df=%E Hz, and a tolerance band of +/- %E Hz" % (SETTLE_DF_TDC, SETTLE_DF_BBPD)) if OPT_KBBPD: _int_bits_bbpd, _frac_bits_bbpd = opt_lf_num_bits( lf_params_bbpd, LF_MIN_BITS, LF_MAX_BITS, rms_filt_error=LF_RMS_FILT_ERROR, noise_figure=LF_NOISE_FIGURE, mode="bbpd") FRAC_BITS = max(_frac_bits, _frac_bits_bbpd) _int_bits = max(_int_bits, _int_bits_bbpd) #make sure kbbpd != 0 when quantized print("* Kbb = ", KBBPD) while fixed_point(KBBPD, INT_BITS, FRAC_BITS) == 0.0: print( "KBBPD is quantized to zero with current data word resolution, incrementing fractional bits by 1." ) FRAC_BITS += 1 KBBPD = fixed_point(KBBPD, INT_BITS, FRAC_BITS) print("* Quantized Kbb = ", KBBPD) # M_BBPD = 1/(FCLK*(BBPD_TH+BBPD_TSU)) # else: KBBPD = 0 #TDC_STEPS = int(TDC_STEPS/float(KBBPD)) print( "\n* Final resolution of data words: %d frac bits, %d int bits, 1 sign bit" % (FRAC_BITS, _int_bits)) print("\n* Digitized ilter coeffients in TDC mode") binary_lf_coefs(lf_params, _int_bits, FRAC_BITS)
from libpll.tools import binary_lf_coefs, fixed_point int_bits = 5 frac_bits = 8 # lf_params = dict(b0=3.1975968494E+01, b1=-2.8851627388E+01) lf_params = dict(b0=24.746023869534508, b1=-22.328113694243058) binary_lf_coefs(lf_params, int_bits, frac_bits) print("\nQuantized Equivalent") for k, v in lf_params.items(): x = fixed_point(v, int_bits, frac_bits) print("%s\t->\t%.10E\tError = %.10E" % (k, x, x - v))