import numpy as np from matplotlib import pyplot as plt from fpu_test_gen import reverse_endian, dtype_size WEIGTHS = [ [ [0.7095146, -0.41895103, -0.08075078, -0.5218736], [-0.4351325, 1.0214638, 0.14494987, -0.78134096], [0.38553882, -1.0607314, 0.01327306, -0.28972188], [0.84955347, 0.32464203, 0.8879888, 0.00756884], [-0.8693629, 0.8418823, 0.60206324, -0.78290594], [0.1586302, 0.01737848, 0.75329006, -0.57819647], [-0.16126093, 0.5317601, 0.34316933, -0.7074082], [0.09219088, -0.624525, -0.61903083, -0.87057704] ], [ [0.36770403, -0.78046024, 0.3979908, 0.5494289, -0.13859335, 0.40053025, 0.08249452, -0.32528356], [-0.17659009, 0.13901198, -0.45248222, -0.7894139, -0.81092286, -0.521815, 0.30632392, -0.3143816], [-0.04314173, 0.14361085, 0.6259473, 0.3571782, -0.38011226, 0.01378736, 0.05794358, 0.09667788], [-0.46864474, 0.36618456, -0.45595396, -0.39789405, 0.73964316, -0.30294785, 0.2482118, -0.2127953], [-0.37941265, 0.45330787, -0.12066315, 0.5636705, 0.68990386, 0.6543718, 0.86367106, -0.5707757], [-0.78606385, 0.24032554, -0.4472755, -0.24661142, -0.2698564, -0.8365823, -0.13674814, -0.39799848], [0.11138931, 0.48950365, 0.12998834, 0.4947537, 0.516593, 0.82281274, 0.04789656, 0.30206403], [0.23097174, 0.30290592, -0.596446, -0.40108407, 0.12246455, -0.47260976, -0.55030185, 0.44481543] ], [ [0.5724262, 0.5853241, 0.3748752, -0.892384, -1.0270239, 0.2170913, -0.07271451, 0.14661156], [0.30391088, -0.92324615, 0.8088594, -1.0522624, 0.07374455, -0.550893, 0.8194236, -0.62796086] ] ] BIAS = [ # L1 [0.01425434, -0.06219335, -0.0201127, -0.04791382, -0.04360008, -0.05311861, -0.01731363, -0.00014839], # L2 [0.03480967, 0.06208326, -0.01576317, -0.00037753, -0.03940378, 0.05157978, -0.02775403, 0.04540931], # L3 [0.03787775, -0.03655371], ] RESULT = [ 0x00000000, 0x3f800000, 0x3f800000, 0x00000000, 0x00000000, 0x00000000, 0x3f800000, 0x3f800000, # 0x3f030126, 0x3f800000, # 0x3e652010, 0x00000000, # 0x3d2d25da, 0x3efea470, # 0x3f1a4267, 0x3f06d0f4, ] def generate_nn_values(dtype=np.float32): dsize = dtype_size(dtype) def sigm(x): ex = 2.7182818 ** x return ex / (ex + 1) def linr(x): return x def process(value): if value > 3 or value < 0: raise ValueError("Value not in between 0 and 3") onehot_mat = [ [0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0], ] L0 = onehot_mat[value] L1_mult = [[L0[j] * WEIGTHS[0][i][j] for j in range(len(L0))] for i in range(8)] L1 = [linr(sum(L1_mult[i]) + BIAS[0][i]) for i in range(8)] L2 = [linr(sum([L1[j] * WEIGTHS[1][i][j] for j in range(len(L1))]) + BIAS[1][i]) for i in range(8)] L3 = [sum([L2[j] * WEIGTHS[2][i][j] for j in range(len(L2))]) + BIAS[2][i] for i in range(2)] # print(f"L0: {L0} \n\tL1mult: {L1_mult}\n\tL1: {L1} \n\tL2: {L2} \n\tL3: {L3}") return [sigm(v) for v in L3] def show_results(): sim_data = np.array([process(0), process(1), process(2), process(3)]).T hdl_data = np.frombuffer(b''.join([i.to_bytes(4, 'little') for i in RESULT]), dtype=np.float32).reshape(4, 2).T print(hdl_data) plt.plot(sim_data[0], sim_data[1], 'x', color='b') plt.plot(hdl_data[0], hdl_data[1], 'x', color='r') plt.xlabel('I') plt.ylabel('Q') # plt.xlim([0, 1]) # plt.ylim([0, 1]) plt.grid() plt.show() if __name__ == '__main__': show_results() # generate_nn_values()