| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import matplotlib.pyplot as plt
- import numpy as np
- from sklearn.metrics import accuracy_score
- from models import basic
- from models.basic import AWGNChannel, BPSKDemod, BPSKMod, BypassChannel, MaryMod, MaryDemod
- import misc
- def show_constellation(mod, chan, demod, samples=1000):
- x = misc.generate_random_bit_array(samples)
- x_mod = mod.forward(x)
- x_chan = chan.forward(x_mod)
- x_demod = demod.forward(x_chan)
- x_mod_rect = misc.polar2rect(x_mod)
- x_chan_rect = misc.polar2rect(x_chan)
- plt.plot(x_chan_rect[:, 0][x], x_chan_rect[:, 1][x], '+')
- plt.plot(x_chan_rect[:, 0][~x], x_chan_rect[:, 1][~x], '+')
- plt.plot(x_mod_rect[:, 0], x_mod_rect[:, 1], 'ro')
- axes = plt.gca()
- axes.set_xlim([-2, +2])
- axes.set_ylim([-2, +2])
- plt.grid()
- plt.show()
- print('Accuracy : ' + str())
- def get_ber(mod, chan, demod, samples=1000):
- x = misc.generate_random_bit_array(samples)
- x_mod = mod.forward(x)
- x_chan = chan.forward(x_mod)
- x_demod = demod.forward(x_chan)
- return 1 - accuracy_score(x, x_demod)
- def get_AWGN_ber(mod, demod, samples=1000, start=-8, stop=5, steps=30):
- ber_x = np.linspace(start, stop, steps)
- ber_y = []
- for noise in ber_x:
- ber_y.append(get_ber(mod, AWGNChannel(noise), demod, samples=samples))
- return ber_x, ber_y
- if __name__ == '__main__':
- plt.plot(*get_AWGN_ber(MaryMod(6, 10e6, gray=True), MaryDemod(6, 10e6), samples=12000, start=-15), '-', label='64-QAM')
- plt.plot(*get_AWGN_ber(MaryMod(5, 10e6, gray=True), MaryDemod(5, 10e6), samples=12000, start=-15), '-', label='32-QAM')
- plt.plot(*get_AWGN_ber(MaryMod(4, 10e6, gray=True), MaryDemod(4, 10e6), samples=12000, start=-15), '-', label='16-QAM')
- plt.plot(*get_AWGN_ber(BPSKMod(10e6), BPSKDemod(10e6, 10e3), samples=12000), '-', label='BPSK')
- plt.yscale('log')
- plt.gca().invert_xaxis()
- plt.grid()
- plt.xlabel('Noise dB')
- plt.ylabel('BER')
- plt.legend()
- plt.show()
- pass
|