示例#1
0
    def setUp(self):
        self.directory = tempfile.mkdtemp()
        self.path_context = data.PathContext.cwd()

        meta = gen.StageMeta(3.5, 3)

        ideal = gen.PipeParameters([meta.generate_ideal()], gen.compute_thres(5, *meta.fsr))
        real_stage = meta.generate_gaussian(s_eff=0.1, s_cap=0.1, s_refs=0.1, s_thres=0, s_cm=0.1)
        real = gen.PipeParameters([real_stage], gen.compute_thres(5, *meta.fsr))

        self.meta = meta
        self.ideal = ideal
        self.real = real
示例#2
0
def create_single(args, metas, modify):
    stages = [
        meta.generate_gaussian(args.seff, args.scap, args.srefs, args.sthres,
                               args.scm) for meta in metas
    ]
    tail = np.random.normal(
        gen.compute_thres(args.tailbits, *stages[-1].meta.fsr), args.sthres)
    adc = gen.PipeParameters(stages, tail)
    adc = adc.create_modified(modify)
    return adc
示例#3
0
    def test_stage_thres_code_consistency(self):
        for n_bits in range(1, 12):
            meta = gen.StageMeta(n_bits, n_refs=2)
            meta_h = gen.StageMeta(n_bits, n_refs=3, half_bit=True)

            for m in (
                    meta,
                    meta_h,
            ):
                thres = gen.compute_thres(m.n_bits,
                                          *m.fsr,
                                          half_bit=m.half_bit)
                self.assertEqual(m.n_codes - 1, len(thres))
示例#4
0
def run(args):
    path_context = data.PathContext.relative()
    stages_location = [
        data.DataLocation(path_context, args.location, source, "json")
        for source in getattr(args, "stages-sources")
    ]

    NestedStages = data.nested_lists_of(gen.StageParameters)

    all_stages = []
    for stage_loc in stages_location:
        print(" loading {}...".format(stage_loc.computed_path))
        stage = data.load(NestedStages, stage_loc)
        all_stages.append(stage)

    assert len(all_stages) > 0
    shape = np.shape(all_stages[0].data)
    assert all(shape == np.shape(stages.data) for stages in all_stages[1:])

    adcs = np.full(shape, None)
    for idx in all_stages[0].iter_idx():
        stages = tuple(stages[idx] for stages in all_stages)
        thresholds = [stage.thres for stage in stages]
        meta0 = stages[0].meta
        thresholds = [
            gen.compute_thres(
                meta0.n_bits, *meta0.fsr, half_bit=meta0.half_bit)
        ] + thresholds
        thresholds, tail = thresholds[:-1], thresholds[-1]

        assert all(stage.meta.n_codes == np.size(thres) + 1
                   for stage, thres in zip(stages, thresholds))
        stages = [
            gen.StageParameters(s.meta, s.eff, s.caps, s.refs, thres,
                                s.common_mode)
            for s, thres in zip(stages, thresholds)
        ]

        adcs[idx] = gen.PipeParameters(stages, tail)

    adcs = data.nested_lists_of(gen.PipeParameters)(adcs.tolist(),
                                                    len(np.shape(adcs)))

    adcs_location = data.DataLocation(path_context, args.location, args.dest,
                                      "json")
    print(" saving {}...".format(adcs_location.computed_path))
    data.save(adcs, adcs_location)
示例#5
0
def save_stages(tb, path_context, location, dest):
    shape = tb.conf_shape + tb.shape
    array = len(shape) > 0

    stages = np.full(shape, None)
    stages_ideal = np.full(shape, None)

    for idx in tb.iter_idx():
        conf_idx, stage_idx = idx
        idx = conf_idx + stage_idx

        real = tb.stages[stage_idx]
        stages[idx] = real

        ideal = real.meta.generate_ideal()
        tail_bits, tail_half = gen.infer_thres_bits(real.thres)
        thres_ideal = gen.compute_thres(tail_bits,
                                        *real.meta.fsr,
                                        half_bit=tail_half)
        ideal._thres = thres_ideal
        stages_ideal[idx] = ideal

    NestedStages = data.nested_lists_of(gen.StageParameters)
    dims = len(shape)
    stages = NestedStages(stages.tolist() if array else stages[tuple()], dims)
    stages_ideal = NestedStages(
        stages_ideal.tolist() if array else stages_ideal[tuple()], dims)

    stages_location = data.DataLocation(path_context, location,
                                        dest + ".stages", "json")
    print(" saving {}...".format(stages_location.computed_path))
    data.save(stages if array else stages[tuple()], stages_location)

    ideal_location = data.DataLocation(path_context, location,
                                       dest + ".stages.ideal", "json")
    print(" saving {}...".format(ideal_location.computed_path))
    data.save(stages_ideal if array else stages_ideal[tuple()], ideal_location)
示例#6
0
    def test_snr(self):
        TOLERANCE = 0.03
        for bits in range(2, 10):
            for half_bit in (
                    False,
                    True,
            ):
                n_refs = 3 if half_bit else 2
                meta = gen.StageMeta(bits,
                                     n_refs,
                                     eff=1,
                                     fsr=(
                                         -1,
                                         1,
                                     ),
                                     half_bit=half_bit)
                stage = meta.generate_ideal()
                adc = gen.PipeParameters([stage],
                                         gen.compute_thres(1, *meta.fsr))
                snr = an.snr(adc, sampling_factor=40)
                enob = an.snr_to_enob(snr)

                self.assertTrue(
                    np.abs((bits + 1) - (enob + 0.293)) - TOLERANCE <= 0)
示例#7
0
    def test_convert_rebuild_cycle(self):
        N_TEST = 3
        SAMPLES_FACT = 50
        for n_bits_0, n_bits_1, n_bits_tail in \
            zip(range(3, 3 + N_TEST), range(2, 2 + N_TEST), range(1, 1 + N_TEST)):

            tot_bits = n_bits_0 + n_bits_1 + n_bits_tail
            samples = int((2**tot_bits + 1) * SAMPLES_FACT)

            for half_bit in (
                    False,
                    True,
            ):
                n_refs = 3 if half_bit else 2
                meta_0 = gen.StageMeta(n_bits_0,
                                       n_refs=n_refs,
                                       half_bit=half_bit,
                                       eff=1)
                meta_1 = gen.StageMeta(n_bits_1,
                                       n_refs=n_refs,
                                       half_bit=half_bit,
                                       eff=1)

                stages = [meta_0.generate_ideal(), meta_1.generate_ideal()]
                thres_tail = gen.compute_thres(n_bits_tail,
                                               *meta_1.fsr,
                                               half_bit=half_bit)

                adc = gen.PipeParameters(stages, thres_tail)
                data = np.linspace(*meta_0.fsr, samples)  # TODO:randomize

                conversion, tail_code = adc.convert(data[..., np.newaxis])
                re_data = adc.rebuild(conversion, tail_code)
                lsb = (meta_0.fsr[1] - meta_0.fsr[0]) / 2**tot_bits

                self.assertTrue((np.abs(data - re_data) <= lsb / 2).all())