# Analog to digital conversion using Python

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?

• <s>Your signal frequency is too close to the Nyquist frequency. Signals need to be bandlimited below fs/2 before sampling.</s> Actually it looks ok, it's slightly below fs/2. The samples will move up and down like that, it's normal. The reconstructed signal will still be a sine wave. – endolith Aug 11 '16 at 21:27
• Please, change plt.plot (sampling_time, sampling_signal) to plt.plot (sampling_time, sampling_signal, '+'); and plt.plot (sampling_time, quantizing_signal); to plt.plot (sampling_time, quantizing_signal, 'o');. Then, please, explain what do you mean by " I got distorted signal ". – SergV Aug 12 '16 at 9:22
• @Gluttton, you are right. I'm sorry! – Ali Aug 12 '16 at 15:05
• @SergV, Thanks! I got your point and update graphs. explain what do you mean - envelope of the output signal has clearly expressed amplitude modulation. – Gluttton Aug 13 '16 at 7:01
• " envelope of the output signal has clearly expressed amplitude modulation" - it is problem of graphic representation. See my answer - stackoverflow link – SergV Aug 15 '16 at 7:47

## 1 Answer

You see this because you're sampling too close to Nyquist rate, but do not worry, although it seems to be aliased however, your sampling system works well and the signal is not aliased. To be more sure, just take a look at sampled signal in frequency domain: As you see, only 9th bin is active, all others are zero, so no aliasing is occurred in sampling.The code (MATLAB) for FFT calculation is:

Fs = 20;                    % Sampling frequency
T = 1/Fs;                     % Sample time
L = length(sampling_signal);  % Length of signal
t = (0:L-1)*T;                % Time vector
y = sampling_signal;     % Sinusoids plus quantization noise
NFFT = 2^nextpow2(L);
Y = fft(y,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')


Note! I've changed time_of_view to 10 secs, to avoid FFT leakage (the side lobes you see around 9th bin).

It is O.K to increase signal rate this close to Nyquist rate, as far as you're not intended to return the signal back to the analog domain, because then your DAC requires a very sharp Low-pass filter in output and its realization is too hard (if not impossible). This paper intuitively expalins sampling problems, Sampling: What Nyquist Didn't Say, and What to Do About It