I'm a beginner. I try emulate analog signal conversion to digital (including sampling by time and quantizing by level) using Python. Here is my code:
import numpy as np import matplotlib.pyplot as plt time_of_view = 1.; # s. analog_time = np.linspace (0, time_of_view, 10e5); # s. sampling_rate = 20.; # Hz sampling_period = 1. / sampling_rate; # s sample_number = time_of_view / sampling_period; sampling_time = np.linspace (0, time_of_view, sample_number); carrier_frequency = 9.; amplitude = 1; phase = 0; 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()
In general I satisfied with it: if I play with variables the result is expected for me. Except one thing: according to the listing
sampling_rate = 20 and
carrier_frequency = 9 since
sampling_rate / carrier_frequency > 2 I expect successful conversion but Instead I got distorted signal (look at picture: envelope of the output signal has clearly expressed amplitude modulation). Where is my mistake? Is my implementation correct?