# Analog to Digital Converter Modelling with PySpice

Prerequisites: basic programming skills with Python and SPICE

# Intro

This is the second tutorial of the series dedicated to IC modelling with PySpice. Or how to not spend tons of money on CAD licenses for those half starving tech startups, so that the board members could keep their kidneys 😉 This tutorial will continue the discussion of modelling data converters and their components and will describe how to model an Analog-to-Digital Converter (ADC). Some of the DAC code and concepts covered in the previous article will be used here as well.

As was mentioned earlier, ideal models that consist of behavioural elements allow to analyze the performance of a mixed-signal circuit block within a reasonable amount of time. For example, if one has designed a DAC at the transistor level and want to simulate its operation under various temperatures and matching conditions, the one might want to apply a digital input code generated from an ideal ADC with a sine wave. The same way, if one has a digital signal processing system (DSP) and want to get an analog waveform output at any point where there is a digital word, the one can quickly drop an ideal DAC. (see Fig. 1)

Fig. 1. Digital Control System with Analog IO

An ideal ADC converts an input analog voltage or current to a certain number of bits N  proportional to the magnitude of the voltage or current. The sequence of bits represents a digital number and each bit has a double of the weight of the next one, starting from the most significant bit (MSB) to the less significant one (LSB).

So, the MSB has weight of Vref/2, the next one has weight of Vref/4, etc. Finally, the LSB has a weight of Vref/2^N.

The conventional implementation of the ideal ADC consists of an ideal Sample-and-Hold (S/H) followed by passing the output of the S/H (the held signal) through an algorithm to generate the output digital bits.

The following implementation is based on a pipeline ADC:

1. The input signal is sampled and held,
2. The held signal is fed into a comparator that compares the input value to the reference voltage,
3. if the input signal is greater then the reference voltage, the output bit is set to high, and the reference signal is subtracted from the input. The difference is multiplied by two and passed  to the output stage,
4. If the input signal is less then the reference voltage, the output bit is set to low. The input signal is then multiplied by two and passed to the output stage,
5. This output is used at the input to the next stage and steps 2, 3, and 4 above are repeated. This continues for N stages (where N is the number of bits in the ADC)

The common voltage Vcm can be determined by calculating the mid point between Vref+ and Vref- followed by subtracting Vref- so that Vcm is referenced around 0V.

One might also want to level shift the input signal so that it is referenced to 0V and the transfer curves to the left by 1/2 LSB. The value of 1/2 LSB is given by:

assuming Vref+>Vref- and N is the number of bits in the ADC. Again, level shifting the input and the common-mode voltage makes sense in order to make the model as flexible as possible (things are much simpler if Vref+ = VDD and Vref- = GND)

# PySpice Modelling Approach

As was mentioned before, in order to implement an ideal ADC one needs to build the following sub blocks: sample-and-hold, level shifter and the pipeline stages.

## Ideal Sample-and-Hold Circuit

The block diagram for an ideal S/H is shown below.The inputs are clock, Vin and the trip voltage, while the output is labeled as Vout.

Note, that both switches controlled by clk and clk_ can be closed momentarily at the same time for a period, which is approximately equal to one transient step. The charge sharing between the capacitors is affected by that, hence, the reasonably large ratio should be picked for the values (around 120 dB, i.e. a million-to-one). However, the shorter the time of both switches being closed, the smaller the ratio could be picked without affecting the circuit operation. Also note, that over a time set by GMIN (remember a resistor with a value of 1/GMIN is placed across every pn-junction in a SPICE simulation and GMIN default value is around 1E-12) and the capacitor values the charge on the capacitors will leak off causing a voltage drop. For example, for 0.1fF capacitor the associated RC time because of GMIN becomes 100us (increasing the capacitor to 10fF won’t affect the sampling operation and pushes RC time constant up to 10ms).

As one may see, the accuracy of the S/H is ultimately limited by the simulator tolerances (e.g. RELTOL, ABSTOL and VNTOL). However, this is not the topic of this tutorial and will be discussed later in more detail.

We now have to define a class for S/H, which incorporates a model for the switches, ideal caps and voltage controlled voltage sources acting as ideal voltage buffers. The code is given below

## Ideal Pipeline Stage

Along with the supply rails the inputs for the following implementation of a pipeline stage block are input signal (‘in’),  reference voltage (‘cm’), trip voltage for the switches (‘trip’). The outputs are the result output signal (‘out’) and the bit value (‘bitout’).

As was mentioned earlier, each stage compares an input signal with the reference voltage and assigns corresponding values to the bit output. Depending on the result of the comparison the stage then passes either a value of the input signal or its difference with Vref multiplied by two to the next stage.

The following code implements the ideal pipeline stage

## N-bit Analog-to-Digital Converter

Now we have all the main blocks required to build a model for an ADC.  The following implementation has positive/negative reference, clock and input signals as inputs along with the supply rails. The outputs are the digital bits for the corresponding number. The diagram is shown below. As was mentioned earlier, the the value of the input signal gets sampled by the S/H circuit and level shifted by LSB/2 value. The result then gets through the N number of pipeline stages producing corresponding digital bits.

The following code implements an ideal model of the N-bit pipeline ADC.

It is important to realize the usefulness of the simulation model we have just developed. Should there be any analog/mixed signal simulation (using SPICE) or general system level simulation (using Matlab or Python), one can use the ADC model to degenerate a digital signal as an input source. The one can also use the developed earlier DAC model to convert a digital word into an analog waveform, which can be used for a block characterization or figuring out some system level parameters.

For the first test bench lets have a an ideal ADC developed in this tutorial and an ideal DAC developed in the previous tutorial. The ADC will be clocked at 100 MHz and have an analog input. The digital word  generated by the ADC will be driving the DAC, which will generate an analog waveform. The diagram of the test bench is below.

Let’s begin by simply applying a ramp from Vref- to Vref+ to the input and use 3 bit ADC and DAC. The code is given below.

One can see how the digital bits of the ADC change according to the rising input voltage (000 corresponds to the lowest input voltage and 111 is for the highest one). Then the bits of the digital word control the DAC. Looking at the DAC output one can also see the LSB value of the system, which in this case is 0.125V.

Fig. 2. Transient Analysis. Input Voltage Ramp-up

### Viewing the ADC Quantization Noise Spectrum

At this point one should understand the sampling process and the operation of ideal ADC and DAC. Another useful thing is to explore a quantization noise (the effective noise added to a signal after passing through an ADC) and how it affects the spectrum of the signal.

Let’s consider a similar test bench but this time use 6-bit ADC and DAC and use a 10MHz sine wave as an input signal, which would swing from Vref- to Vref+. Minor modifications to the previous test bench are needed (change the bits and replace the input voltage source).

This is what the plots look like.

Fig. 3. Transient Analysis. Sampling a Sine Wave

Now, this quantization is not obvious after looking at the time domain response. However, looking at the spectrum of the ADC input and the DAC output should reveal the difference in the noise floor between the two. Transforming the signals from time to frequency domain can be done using FFT methods from Python libraries.

Fig. 4. ADC Input and DAC Output in Time and Frequency Domains

As it can be seen from the plots, the noise floors are different. The noise floor associated with the DAC output is related to the RMS quantization noise voltage, which can be found as

For example, for a case of an 8-bit ADC with 1 V range where 1LSB=3.9mV, Vqrms=1.1mV  or -59dB. And the higher the bit number, the closer the quantized noise floor gets to the noise floor of the input signal. For a 12-bit ADC, Vqrmr=70.5uV or -83dB.

# Conclusions

This tutorial explains how to model an analog to digital converter with PySpice. Some basic theory of the sampling process and the operation of an ideal ADC and a DAC was covered. Python classes implementing Ideal blocks for pipeline ADC (e.g. S/H and pipeline stage) and the N-bit ADC itself were built. Some examples of basic test benches for transient analysis were shown. It was also shown how to extract the data and post-process it with Python FFT packages for quantization noise analysis.

# General References

1. GitHub Repository with the complete code for this tutorial: https://github.com/OlzhasT/PySpiceCircuits-/blob/master/adc_dac_ideal_subcircuit_class_test.py,
2. Previous part (Part 1) of this tutorial series:
3. All the PySpice documentation and installation guidelines: https://pypi.python.org/pypi/PySpice/0.3.3,
4. PySpice  examples explaining how to get started with simple circuits and basic analysis and data processing: https://pyspice.fabrice-salvaire.fr/examples/index.html;