import numpy as np import matplotlib.pyplot as plt from fixed_point import fixed, W from fixed_point import ROUND_MODES format = W(4, 0, 3) for rm in ROUND_MODES: xf = np.arange(-1200, 1200) / 1000. xq, xe = ([], []) for ff in xf: fx = float(fixed(ff, format=format, round_mode=rm)) xq.append(fx) # back to float for plotting, preserves value xe.append(ff - fx) # also plot error # plot the quantized values fig, ax = plt.subplots(1) tax = ax.twinx() ax.plot(xf, xf, label='real number value') ax.plot(xf, xq, label='quantized value') ax.grid(True) ax.set_xlim(-1.2, 1.2) ax.set_ylim(-1.2, 1.2) ax.set_title("Quantization\nfixed(format=W%s," "round_mode='%s'" "overflow_mode='saturate')" % (rm, W.format)) ax.set_ylabel('') ax.set_xlabel('') tax.plot(xf, xe, 'm:', alpha=.6, label='error') xf = np.array(xf)
def test_math(): x = fixed(0.5, format=W(16, 0)) y = fixed(0.25, format=W(16, 0)) w = x + y # typical modeling print(repr(w), repr(x), repr(y)) # pre-declared z = fixed(0, format=x + y) z[:] = x + y # hardware transfer (type is declared) print(repr(z), repr(x), repr(y)) assert float(w) == 0.75 assert float(z) == 0.75 # test non-aligned additions x = fixed(0.5, min=-4, max=4, res=.125) y = fixed(0.25, min=-16, max=16, res=.0625) print(repr(z), repr(x), repr(y)) z = x + y print(repr(z), repr(x), repr(y)) assert float(z) == 0.75 # test non-aligned additions and overflow x = fixed(0.5, min=-4, max=4, res=2**-32) y = fixed(1.5, min=-16, max=16, res=2**-32) z = x + y print(repr(z), repr(x), repr(y)) assert float(z) == 2.0 # @todo: negative iwl and fwl # test subtraction x = fixed(0.5, format=W(16, 0)) y = fixed(0.25, format=W(16, 0)) z = x - y print('S', repr(z), repr(x), repr(y)) assert float(z) == 0.25 x = fixed(0.5, min=-4, max=4, res=.125) y = fixed(0.25, min=-16, max=16, res=.0625) z = x - y print('S', repr(z), repr(x), repr(y)) assert float(z) == 0.25 z = y - x print('S', repr(z), repr(x), repr(y)) assert float(z) == -0.25 x = fixed(10.33, min=-16, max=16, res=.125) y = fixed(10.33, min=-16, max=16, res=.125) z = x - y print('S', repr(z), repr(x), repr(y)) assert float(z) == 0. # The resolution of x is .125, it means 0.33 will be # rounded to .25 or .375 (it should be .375). For # y the resolution is 2**-8 and the actual value should # be .328125 or .33203125 (.32 is closer?) x = fixed(0.33, min=-1, max=1, res=.125) y = fixed(0.33, min=-1, max=1, res=2**-8) z = x + y print('A', repr(z), repr(x), repr(y)) assert float(z) == 0.375 + 0.328125 x = fixed(0.33, min=-1, max=1, res=.125) y = fixed(0.33, min=-1, max=1, res=2**-8) z = x - y print('S', repr(z), repr(x), repr(y)) assert float(z) == 0.375 - 0.328125 x = fixed(-1, format=W(8, 0, 7)) + .5 assert float(x) == -.5
import numpy as np import matplotlib.pyplot as plt from fixed_point import fixed, W from fixed_point import ROUND_MODES formats = (W(4, 0, 3), W(4, 1, 2), W(4, 2, 1), W(4, 3, 0)) for format in formats: for rm in ROUND_MODES: xf = np.arange(-1200, 1200) / 1000. xq, xe = ([], []) for ff in xf: fx = float(fixed(ff, format=format, round_mode=rm)) xq.append(fx) # back to float for plotting, preserves value xe.append(ff - fx) # also plot error # plot the quantized values fig, ax = plt.subplots(1) tax = ax.twinx() ax.plot(xf, xf, label='real number value') ax.plot(xf, xq, label='quantized value') ax.grid(True) ax.set_xlim(-1.2, 1.2) ax.set_ylim(-1.2, 1.2) ax.set_title("Quantization\nfixed(format=(4,0,3)," "round_mode='%s'" "overflow_mode='saturate')" % (rm)) ax.set_ylabel('') ax.set_xlabel('') tax.plot(xf, xe, 'm:', alpha=.6, label='error')