import numpy as np from matplotlib import pyplot as plt from fpu_test_gen import reverse_endian, dtype_size def generate(fname, samples, dtype=np.float32): dsize = dtype_size(dtype) numbers = np.linspace(-4, 4, samples, dtype=dtype) data = numbers.tobytes() with open(fname, 'w') as f: for i in range(samples): f.write(f"{reverse_endian(data[i*dsize:i*dsize+dsize]).hex()} // {numbers[i]:0.6f}\n") def view_result(fname, dtype=np.float32): x_bytes = b'' y_bytes = b'' timing = [] with open(fname, 'r') as f: for line in f.readlines(): parts = line.split() x_bytes += reverse_endian(bytes.fromhex(parts[0])) y_bytes += reverse_endian(bytes.fromhex(parts[1])) timing.append(int(parts[2])) x = np.frombuffer(x_bytes, dtype=dtype) y = np.frombuffer(y_bytes, dtype=dtype) t = np.array(timing) / 10 fig, ax = plt.subplots() plt.title('Digital circuit sigmoid function test') ax2 = ax.twinx() ax.plot(x, y, '.', markersize=0.95) ax2.plot(x, t, '.', markersize=0.95, color='m') ax.set_xlabel('Function input') ax.set_ylabel('Function output') ax2.set_ylabel('Timing in cycles', color='m') plt.grid() plt.show() if __name__ == '__main__': # generate('sigmoid_test.hex', 5001) view_result('sigmoid_result.hex')