def test_iterator(chain_of_integrators): analogSignals = [ConstantSignal(0.1)] digitalControl = DigitalControl(Ts, M) statespacesimulator = StateSpaceSimulator(chain_of_integrators["system"], digitalControl, analogSignals, t_stop=Ts * 1000) for control_signal in statespacesimulator: pass
def test_estimation_with_circuit_simulator(): eta2 = 1e12 K1 = 1000 K2 = 0 analogSystem = AnalogSystem(A, B, CT, Gamma, Gamma_tildeT) # analogSignals = [Sinusoidal(0.5, 10)] analogSignals = [ConstantSignal(0.25)] digitalControl = DigitalControl(Ts, M) circuitSimulator = StateSpaceSimulator(analogSystem, digitalControl, analogSignals, t_stop=Ts * 1000) estimator = DigitalEstimator(analogSystem, digitalControl, eta2, K1, K2) estimator(circuitSimulator) for est in estimator: print(est)
def test_large_integrator(): N = 20 M = N A = np.eye(N) * rho + np.eye(N, k=-1) * beta B = np.zeros((N, 1)) B[0] = beta CT = np.zeros((N, 1)).transpose() CT[-1] = 1.0 Gamma_tildeT = np.eye(N) Gamma = Gamma_tildeT * (-beta) analogSystem = AnalogSystem(A, B, CT, Gamma, Gamma_tildeT) analogSignals = [ConstantSignal(0.1)] digitalControl = DigitalControl(Ts, M) statespacesimulator = StateSpaceSimulator(analogSystem, digitalControl, analogSignals, t_stop=Ts * 1000) for control_signal in statespacesimulator: pass
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")
# Set the peak amplitude. amplitude = 2.5 # 2.5 V is the theoretical limit for the hardware prototype # Choose the sinusoidal frequency via an oversampling ratio (OSR). OSR = 1 << 8 frequency = 1.0 / (T * OSR) # Instantiate the analog signal analog_signal = Sinusoidal(amplitude, frequency) # print to ensure correct parametrization. print(analog_signal) # simulate for 1024 cycles n_cycles = 1 << 10 end_time = T * n_cycles # Instantiate the simulator. simulator = StateSpaceSimulator(pcb.analog_system, pcb.digital_control, [analog_signal], t_stop=end_time) ############################################################################## # Finally we extract the bitstream and store it in a numpy array. ctrl_stream = np.zeros((n_cycles, M)) for index, s in enumerate(simulator): ctrl_stream[index, :] = np.array(s) ############################################################################## # ------------------- # Barcode Style # ------------------- # We want to display the individual control bit signals in a barcode manner. # We achieve this by filling the area between the rectangular control signals
from cbadc.simulator import StateSpaceSimulator # Set simulation precision parameters atol = 1e-6 rtol = 1e-12 max_step = T / 10. # Instantiate digital controls digital_control1 = DigitalControl(T, M) digital_control2 = DigitalControl(T, M) print(digital_control1) # Instantiate simulators. simulator1 = StateSpaceSimulator(analog_system, digital_control1, [analog_signal], atol=atol, rtol=rtol, max_step=max_step) simulator2 = StateSpaceSimulator(analog_system, digital_control2, [analog_signal], atol=atol, rtol=rtol, max_step=max_step) print(simulator1) ############################################################################### # Default and Mid point FIR Filter # -------------------------------- # # Next we instantiate the quadratic and default estimator
def test_initialization(chain_of_integrators): analogSignals = [ConstantSignal(0.1)] digitalControl = DigitalControl(Ts, M) StateSpaceSimulator(chain_of_integrators["system"], digitalControl, analogSignals)
def test_benchmark_state_space_simulation_algorithm(benchmark): est = StateSpaceSimulator(analogSystem, digitalControl, analogSignals) result = benchmark(iterate_through, est) assert (result == size)
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))
def setup(): StateSpaceSimulator(analogSystem, digitalControl, analogSignals)
############################################################################### # 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) # 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. # Let's print the first 20 control decisions. index = 0 for s in simulator: if (index > 19): break print(f"step:{index} -> s:{np.array(s)}") index += 1 # To verify the simulation parametrization we can print(simulator)