def compute_nco(fin, fout): d1 = (2 * fin // fout) d2 = d1 + 1 f1 = 2 * fin // d1 f2 = 2 * fin // d2 high_inc = 0x80000000 * (fout - f2) // (f1 - f2) low_inc = 0x80000000 - high_inc print(low_inc, high_inc) got = 2 * ((fin / d1) * high_inc + (fin / d2) * low_inc) / (high_inc + low_inc) print(f"Desired: {fout}") print(f"Got: {got} ({got / 256})")