Mimic A/D conversion

Mimic A/D conversion#

# from https://dsp.stackexchange.com/questions/33596/analog-to-digital-conversion-using-python

import numpy as np
import matplotlib.pyplot as plt

time_of_view        = 1.; # s.
analog_time         = np.linspace (0, time_of_view, num=100); # s.

sampling_rate       = 21.; # Hz
sampling_period     = 1. / sampling_rate; # s
sample_number       = time_of_view / sampling_period;
sampling_time       = np.linspace (0, time_of_view, int(sample_number));

carrier_frequency   = 9.; # Hz
amplitude           = 1;  # V
phase               = 0;  # deg

quantizing_bits     = 4;
quantizing_levels   = 2 ** quantizing_bits / 2;
quantizing_step     = 1. / quantizing_levels;

def analog_signal (time_point):
    return amplitude * np.cos (2 * np.pi * carrier_frequency * time_point + phase);

sampling_signal     = analog_signal (sampling_time);
quantizing_signal   = np.round (sampling_signal / quantizing_step) * quantizing_step;



fig = plt.figure ()
# plt.plot (analog_time,   analog_signal (analog_time) );
#plt.stem (sampling_time, sampling_signal);
plt.stem (sampling_time, quantizing_signal, linefmt='r-', markerfmt='rs', basefmt='r-');
plt.title("Analog to digital signal conversion")
plt.xlabel("Time")
plt.ylabel("Amplitude")

plt.show()
../_images/c18cdfef34225fea5e1b7b69629905aeb4de07ed2eabfeb370beaa3a2d665f87.png
fig = plt.figure()
plt.plot (analog_time, analog_signal (analog_time) );
#plt.stem (sampling_time, sampling_signal);
plt.stem(sampling_time, quantizing_signal, linefmt='r-', markerfmt='rs', basefmt='r-');
plt.title("Analog to digital signal conversion")
plt.xlabel("Time")
plt.ylabel("Amplitude");
../_images/eca4250930b0c477a891f03509a01c20d49de5ef3eae3cb4fb0f531a40375e9c.png