# Is there a simple way to quantize an input waveform in LTspice?

How can a 1V sine-wave input be converted to a staircase sine-wave with an 8-bit resolution for instance in LTspice? Or is this even possible?

Can this be achieved without a complicated ADC circuitry? Does anybody have experience with that in LTspice?

Edit:

I found the following example so far:

Version 4
SHEET 1 920 680
WIRE -112 -16 -160 -16
WIRE 176 0 144 0
WIRE -160 32 -160 -16
WIRE 448 48 352 48
WIRE 592 48 544 48
WIRE 176 64 144 64
WIRE 544 80 544 48
WIRE -160 144 -160 112
WIRE -112 208 -160 208
WIRE 544 208 544 160
WIRE -160 256 -160 208
WIRE -160 368 -160 336
FLAG 544 208 0
FLAG 592 48 sq
IOPIN 592 48 Out
FLAG -160 144 0
FLAG -112 -16 s0
FLAG 144 0 s0
FLAG -160 368 0
FLAG -112 208 fs
FLAG 144 64 fs
FLAG 448 48 vs0
SYMBOL bv 544 64 R0
SYMATTR InstName B1
SYMATTR Value V=int(V(vs0))
SYMBOL voltage -160 16 R0
WINDOW 123 0 0 Left 0
WINDOW 39 0 0 Left 0
SYMATTR InstName V1
SYMATTR Value SINE(0 7.5 {f0})
SYMBOL SpecialFunctions\\sample 256 32 R0
WINDOW 3 0 0 Invisible 0
SYMATTR InstName A1
SYMATTR Value2 vhigh=1e6 vlow=-1e6
SYMATTR Value vt=0.5
SYMBOL voltage -160 240 R0
WINDOW 123 0 0 Left 0
WINDOW 39 0 0 Left 0
SYMATTR InstName V2
SYMATTR Value PULSE(0 1 0 1n 1n 10u {1/fs})
TEXT -176 -160 Left 0 !.tran 0 3m 0 1u
TEXT -176 -128 Left 0 !.options plotwinsize=0
TEXT -176 -96 Left 0 !.param f0=1k fs=20k


Copy and save the above code in notepad with .asc extension and run in LTspice.

I get the following result for 1V sine input amplitude:

It seems like a sample and hold. But I couldn't figure out the formula to set the quantization resolution for a given amplitude. For example for 10Vpp input the resolution increases.

• I don't know who drew that picture but whoever it was can't have meant it to be representative of proper quantization as performed by an ADC with a constant sampling rate in normal increasing time. Commented Nov 19, 2018 at 18:30
• @Andyaka I didn't get why it lacks the representation but if you find an accurate one I will update it for sure. Commented Nov 19, 2018 at 18:45
• @Andyaka: That shows a quantization map only, with sampling (presumably) left as an exercise to the reader. Or maybe with a sampling rate that's way faster than the sweep speed. Commented Nov 19, 2018 at 18:48
• Be careful when using B-sources: they only go so far with dynamic range. If you need more than 7-8bits, things start getting ugly. But then, even ith dedicated circuitry and .opt plotwinsize=0, more than 16bits are problematic. That's because LTspice, and SPICE, in general, works with a finite precision, and trying to represent a step of micro Volts or less over a signal that's Volts or more, is a potential for a recipe for errors. Commented Nov 19, 2018 at 19:29
• @aconcernedcitizen I tried user49628's answer with 256 intervals. I had to set the time interval to 100p second for a fine stair case I also used .opt plotwinsize=0. I didn't try more than 8 bit (256 intervals). I guess it is also input amplitude dependent. Commented Nov 19, 2018 at 19:35

You can accomplish this with a behavioral source and the "floor()" function. Here is an example:

.param vrange = 1
.param nlevels = 16

B1 VQUANT 0 V={floor(V(VIN)/vrange*nlevels)/nlevels*vrange}
V1 VIN 0 SINE(0.5 0.5 100)

.trans 10m

• Looks great, for 8-bit should nlevels = 256 or 255 ? (btw time step must be adjusted for an accurate output) Commented Nov 19, 2018 at 19:28

Make a dependent source and use the 'quant' function. I looked a bit for good answers, this is the best I found -- search on 'quant'. You'll need to find an example of how to make a dependent source with a formula in it, and then combine that with the 'quant' function.

When you do, please post what you find as an answer to your own question -- I'm making this an answer because it is, but I almost put it in as a comment because it's really quite scant.

• I found an example but I don't understand how it works. Commented Nov 19, 2018 at 18:59
• Post a link to the example. Commented Nov 19, 2018 at 19:02
• See the last edit. Commented Nov 19, 2018 at 19:03