| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- 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, AlphabetMod, AlphabetDemod
- 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__':
- # show_constellation(BPSKMod(10e6), AWGNChannel(-1), BPSKDemod(10e6, 10e3))
- # get_ber(BPSKMod(10e6), AWGNChannel(-20), BPSKDemod(10e6, 10e3))
- # mod = MaryMod('8psk', 10e6)
- # misc.display_alphabet(mod.alphabet, a_vals=True)
- # mod = MaryMod('qpsk', 10e6)
- # misc.display_alphabet(mod.alphabet, a_vals=True)
- # mod = MaryMod('16qam', 10e6)
- # misc.display_alphabet(mod.alphabet, a_vals=True)
- # mod = MaryMod('64qam', 10e6)
- # misc.display_alphabet(mod.alphabet, a_vals=True)
- plt.plot(*get_AWGN_ber(AlphabetMod('64qam', 10e6), AlphabetDemod('64qam', 10e6), samples=12000, start=-15), '-', label='64-QAM')
- plt.plot(*get_AWGN_ber(AlphabetMod('16qam', 10e6), AlphabetDemod('16qam', 10e6), samples=12000, start=-15), '-', label='16-QAM')
- plt.plot(*get_AWGN_ber(AlphabetMod('qpsk', 10e6), AlphabetDemod('qpsk', 10e6), samples=12000, start=-15), '-', label='QPSK')
- plt.plot(*get_AWGN_ber(AlphabetMod('8psk', 10e6), AlphabetDemod('8psk', 10e6), samples=12000, start=-15), '-', label='8PSK')
- 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
|