Exemple #1
0
def test_evaluate():
    amplitude = 1.2
    frequency = 42.
    t = 3.
    sinusodial = Sinusodial(amplitude, frequency)
    assert sinusodial.evaluate(t) == (amplitude *
                                      math.sin(2 * math.pi * frequency * t))
Exemple #2
0
def test_evaluate_with_offset_and_phase():
    amplitude = 1.2
    frequency = 42.
    phase = 7.5 * math.pi
    offset = 4.5321
    t = 3.
    sinusodial = Sinusodial(amplitude, frequency, phase, offset)
    assert sinusodial.evaluate(t) == (
        amplitude * math.sin(2 * math.pi * frequency * t + phase) + offset)
Exemple #3
0
def test_properties():
    amplitude = 1.2
    frequency = 42.
    phase = 7.5 * math.pi
    offset = 4.5321
    sinusodial = Sinusodial(amplitude, frequency, phase, offset)
    assert sinusodial.amplitude == amplitude
    assert sinusodial.frequency == frequency
    assert sinusodial.phase == phase
    assert sinusodial.offset == offset
Exemple #4
0
def main():
    # Analog system
    analog_system = ChainOfIntegrators(betaVec, rhoVec, kappaVec)

    # Initialize the digital control.
    digital_control = DigitalControl(T, M)

    # Instantiate the analog signal
    analog_signal = Sinusodial(amplitude, frequency, phase, offset)

    # Instantiate the simulator.
    simulator = StateSpaceSimulator(analog_system,
                                    digital_control, [analog_signal],
                                    t_stop=end_time)
    # Depending on your analog system the step above might take some time to
    # compute as it involves precomputing solutions to initial value problems.

    # Construct byte stream.
    byte_stream = control_signal_2_byte_stream(simulator, M)
    write_byte_stream_to_file("./temp.adc", byte_stream)
    os.remove("./temp.adc")
# -------------
#
# We will also need an analog signal for conversion.
# In this tutorial we will use a Sinusodial signal.

# Set the peak amplitude.
amplitude = 1.0
# Choose the sinusodial frequency via an oversampling ratio (OSR).
frequency = 1.0 / (T * OSR * (1 << 0))

# We also specify a phase an offset these are hovewer optional.
phase = 0.0
offset = 0.0

# Instantiate the analog signal
analog_signal = Sinusodial(amplitude, frequency, phase, offset)

print(analog_signal)


###############################################################################
# Simulating
# ----------
#
# Each estimator will require an independent stream of control signals.
# Therefore, we will next instantiate several digital controls and simulators.

# Set simulation precision parameters
atol = 1e-6
rtol = 1e-12
max_step = T / 10.
Exemple #6
0
B = np.zeros((N, 1))
B[0, 0] = beta
# B[0, 1] = -beta
C = np.eye(N)
Gamma_tilde = np.eye(N)
Gamma = Gamma_tilde * (-beta)
Ts = 1/(2 * beta)


eta2 = 1e6
K1 = 1 << 12
K2 = 1 << 12
size = K2 << 4
analogSystem = AnalogSystem(A, B, C, Gamma, Gamma_tilde)
digitalControl = DigitalControl(Ts, N)
analogSignals = [Sinusodial(0.5, 1)]


def controlSequence():
    while True:
        yield np.ones(N, dtype=np.uint8)


def test_filter_computation_parallel_estimator_algorithm(benchmark):
    def setup():
        ParallelEstimator(
            analogSystem, digitalControl, eta2, K1, K2)
    benchmark(setup)


def test_filter_computation_digital_estimator_algorithm(benchmark):
C = np.eye(N)
Gamma_tilde = np.eye(N)
Gamma = Gamma_tilde * (-beta)
Ts = 1 / (2 * beta)

amplitude = 1.0
frequency = 10.
phase = 0.

eta2 = 1e6
K1 = 1 << 12
K2 = 1 << 12
size = K2 << 4
analogSystem = AnalogSystem(A, B, C, Gamma, Gamma_tilde)
digitalControl = DigitalControl(Ts, N)
analogSignals = [Sinusodial(amplitude, frequency, phase)]


def iterate_through(iterator):
    count = 0
    for _ in range(size):
        next(iterator)
        count = count + 1
    return count


def test_benchmark_state_space_simulation_algorithm(benchmark):
    est = StateSpaceSimulator(analogSystem, digitalControl, analogSignals)
    result = benchmark(iterate_through, est)
    assert (result == size)
Exemple #8
0
def test_initialization():
    amplitude = 1.0
    frequency = 42.
    Sinusodial(amplitude, frequency)
Exemple #9
0
def test_estimation_with_circuit_simulator():
    eta2 = 1e12

    K1 = 1 << 10
    K2 = 1 << 10
    size = K2 << 2
    window = 1000
    size_2 = size // 2
    window_2 = window // 2
    left_w = size_2 - window_2
    right_w = size_2 + window_2

    analogSystem = AnalogSystem(A, B, CT, Gamma, Gamma_tildeT)
    analogSignals = [Sinusodial(amplitude, frequency, phase)]
    digitalControl1 = DigitalControl(Ts, M)
    digitalControl2 = DigitalControl(Ts, M)
    digitalControl3 = DigitalControl(Ts, M)
    digitalControl4 = DigitalControl(Ts, M)

    tf_abs = np.abs(
        analogSystem.transfer_function_matrix(np.array([2 * np.pi * frequency
                                                        ])))
    print(tf_abs, tf_abs.shape)

    simulator1 = StateSpaceSimulator(analogSystem, digitalControl1,
                                     analogSignals)
    simulator2 = StateSpaceSimulator(analogSystem, digitalControl2,
                                     analogSignals)
    simulator3 = StateSpaceSimulator(analogSystem, digitalControl3,
                                     analogSignals)
    simulator4 = StateSpaceSimulator(analogSystem, digitalControl4,
                                     analogSignals)
    estimator1 = DigitalEstimator(analogSystem, digitalControl1, eta2, K1, K2)
    estimator2 = ParallelEstimator(analogSystem, digitalControl2, eta2, K1, K2)
    estimator3 = FIRFilter(analogSystem, digitalControl3, eta2, K1, K2)
    estimator4 = IIRFilter(analogSystem, digitalControl4, eta2, K2)
    estimator1(simulator1)
    estimator2(simulator2)
    estimator3(simulator3)
    estimator4(simulator4)

    tf_1 = estimator1.signal_transfer_function(
        np.array([2 * np.pi * frequency]))[0]

    e1_array = np.zeros(size)
    e2_array = np.zeros(size)
    e3_array = np.zeros(size)
    e4_array = np.zeros(size)
    e1_error = 0
    e2_error = 0
    e3_error = 0
    e4_error = 0

    for index in range(size):
        e1 = estimator1.__next__()
        e2 = estimator2.__next__()
        e3 = estimator3.__next__()
        e4 = estimator4.__next__()
        e1_array[index] = e1
        e2_array[index] = e2
        e3_array[index] = e3
        e4_array[index] = e4
        t = index * Ts
        u = analogSignals[0].evaluate(t)
        u_lag = analogSignals[0].evaluate(t - estimator4.filter_lag() * Ts)
        if (index > left_w and index < right_w):
            print(
                f"Time: {t: 0.2f}, Input Signal: {u * tf_1}, e1: {e1}, e2: {e2}, e3: {e3}, e4: {e4}"
            )
            e1_error += np.abs(e1 - u * tf_1)**2
            e2_error += np.abs(e2 - u * tf_1)**2
            e3_error += np.abs(e3 - u_lag * tf_1)**2
            e4_error += np.abs(e4 - u_lag * tf_1)**2
    e1_error /= window
    e2_error /= window
    e3_error /= window
    e4_error /= window
    print(f"""Digital estimator error:        {e1_error}, {10 *
        np.log10(e1_error)} dB""")
    print(f"""Parallel estimator error:       {e2_error}, {10 *
        np.log10(e2_error)} dB""")
    print(f"""FIR filter estimator error:     {e3_error}, {10 *
        np.log10(e3_error)} dB""")
    print(f"""IIR filter estimator error:     {e4_error}, {10 *
        np.log10(e4_error)} dB""")

    assert (np.allclose(e1_error, 0, rtol=1e-6, atol=1e-6))
    assert (np.allclose(e2_error, 0, rtol=1e-6, atol=1e-6))
    assert (np.allclose(e3_error, 0, rtol=1e-6, atol=1e-6))
    assert (np.allclose(e4_error, 0, rtol=1e-6, atol=1e-6))
# For this tutorial, we will choose a
# :py:class:`cbadc.analog_signal.Sinusodial`. Again, this is one of several
# possible choices.

# Set the peak amplitude.
amplitude = 0.5
# Choose the sinusodial frequency via an oversampling ratio (OSR).
OSR = 1 << 9
frequency = 1.0 / (T * OSR)

# We also specify a phase an offset these are hovewer optional.
phase = np.pi / 3
offset = 0.0

# Instantiate the analog signal
analog_signal = Sinusodial(amplitude, frequency, phase, offset)
# print to ensure correct parametrization.
print(analog_signal)

###############################################################################
# Simulating
# -------------
#
# Next, we set up the simulator. Here we use the
# :py:class:`cbadc.simulator.StateSpaceSimulator` for simulating the
# involved differential equations as outlined in
# :py:class:`cbadc.analog_system.AnalogSystem`.
#

# Simulate for 2^18 control cycles.
end_time = T * (1 << 18)