main.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from sklearn.metrics import accuracy_score
  4. from models import basic
  5. from models.basic import AWGNChannel, BPSKDemod, BPSKMod, BypassChannel, AlphabetMod, AlphabetDemod
  6. import misc
  7. import math
  8. from models.autoencoder import Autoencoder, view_encoder
  9. def show_constellation(mod, chan, demod, samples=1000):
  10. x = misc.generate_random_bit_array(samples)
  11. x_mod = mod.forward(x)
  12. x_chan = chan.forward(x_mod)
  13. x_demod = demod.forward(x_chan)
  14. x_mod_rect = misc.polar2rect(x_mod)
  15. x_chan_rect = misc.polar2rect(x_chan)
  16. plt.plot(x_chan_rect[:, 0][x], x_chan_rect[:, 1][x], '+')
  17. plt.plot(x_chan_rect[:, 0][~x], x_chan_rect[:, 1][~x], '+')
  18. plt.plot(x_mod_rect[:, 0], x_mod_rect[:, 1], 'ro')
  19. axes = plt.gca()
  20. axes.set_xlim([-2, +2])
  21. axes.set_ylim([-2, +2])
  22. plt.grid()
  23. plt.show()
  24. print('Accuracy : ' + str())
  25. def get_ber(mod, chan, demod, samples=1000):
  26. if samples % mod.N:
  27. samples += mod.N - (samples % mod.N)
  28. x = misc.generate_random_bit_array(samples)
  29. x_mod = mod.forward(x)
  30. x_chan = chan.forward(x_mod)
  31. x_demod = demod.forward(x_chan)
  32. return 1 - accuracy_score(x, x_demod)
  33. def get_AWGN_ber(mod, demod, samples=1000, start=-8, stop=5, steps=30):
  34. ber_x = np.linspace(start, stop, steps)
  35. ber_y = []
  36. for noise in ber_x:
  37. ber_y.append(get_ber(mod, AWGNChannel(noise), demod, samples=samples))
  38. return ber_x, ber_y
  39. def get_SNR(mod, demod, samples=1000, start=-8, stop=5, steps=30):
  40. ber_x, ber_y = get_AWGN_ber(mod, demod, samples, start, stop, steps)
  41. x_mod = mod.forward(misc.generate_random_bit_array(samples*mod.N))
  42. sig_amp = x_mod[:, 0]
  43. sig_power = [A ** 2 for A in sig_amp]
  44. av_sig_pow = np.mean(sig_power)
  45. av_sig_pow = math.log(av_sig_pow, 10)
  46. SNR = (ber_x * -1) + av_sig_pow
  47. return SNR, ber_y
  48. if __name__ == '__main__':
  49. # show_constellation(BPSKMod(10e6), AWGNChannel(-1), BPSKDemod(10e6, 10e3))
  50. # get_ber(BPSKMod(10e6), AWGNChannel(-20), BPSKDemod(10e6, 10e3))
  51. # mod = MaryMod('8psk', 10e6)
  52. # misc.display_alphabet(mod.alphabet, a_vals=True)
  53. # mod = MaryMod('qpsk', 10e6)
  54. # misc.display_alphabet(mod.alphabet, a_vals=True)
  55. # mod = MaryMod('16qam', 10e6)
  56. # misc.display_alphabet(mod.alphabet, a_vals=True)
  57. # mod = MaryMod('64qam', 10e6)
  58. # misc.display_alphabet(mod.alphabet, a_vals=True)
  59. # aenc = Autoencoder(4, -25)
  60. # aenc.train(samples=5e5)
  61. # plt.plot(*get_AWGN_ber(aenc.get_modulator(), aenc.get_demodulator(), samples=12000, start=-15), '-',
  62. # label='AE 4bit -25dB')
  63. # aenc = Autoencoder(5, -25)
  64. # aenc.train(samples=2e5)
  65. # plt.plot(*get_AWGN_ber(aenc.get_modulator(), aenc.get_demodulator(), samples=12000, start=-15), '-',
  66. # label='AE 5bit -25dB')
  67. # view_encoder(aenc.encoder, 5)
  68. # plt.plot(*get_AWGN_ber(AlphabetMod('32qam', 10e6), AlphabetDemod('32qam', 10e6), samples=12000, start=-15), '-',
  69. # label='32-QAM')
  70. # show_constellation(AlphabetMod('32qam', 10e6), AWGNChannel(-1), AlphabetDemod('32qam', 10e6))
  71. # mod = AlphabetMod('32qam', 10e6)
  72. # misc.display_alphabet(mod.alphabet, a_vals=True)
  73. # pass
  74. # aenc = Autoencoder(5, -15)
  75. # aenc.train(samples=2e6)
  76. # plt.plot(*get_AWGN_ber(aenc.get_modulator(), aenc.get_demodulator(), samples=12000, start=-15), '-',
  77. # label='AE 5bit -15dB')
  78. #
  79. # aenc = Autoencoder(4, -25)
  80. # aenc.train(samples=6e5)
  81. # plt.plot(*get_AWGN_ber(aenc.get_modulator(), aenc.get_demodulator(), samples=12000, start=-15), '-',
  82. # label='AE 4bit -20dB')
  83. #
  84. # aenc = Autoencoder(4, -15)
  85. # aenc.train(samples=6e5)
  86. # plt.plot(*get_AWGN_ber(aenc.get_modulator(), aenc.get_demodulator(), samples=12000, start=-15), '-',
  87. # label='AE 4bit -15dB')
  88. # aenc = Autoencoder(2, -20)
  89. # aenc.train(samples=6e5)
  90. # plt.plot(*get_AWGN_ber(aenc.get_modulator(), aenc.get_demodulator(), samples=12000, start=-15), '-',
  91. # label='AE 2bit -20dB')
  92. #
  93. # aenc = Autoencoder(2, -15)
  94. # aenc.train(samples=6e5)
  95. # plt.plot(*get_AWGN_ber(aenc.get_modulator(), aenc.get_demodulator(), samples=12000, start=-15), '-',
  96. # label='AE 2bit -15dB')
  97. # aenc = Autoencoder(4, -10)
  98. # aenc.train(samples=5e5)
  99. # plt.plot(*get_AWGN_ber(aenc.get_modulator(), aenc.get_demodulator(), samples=12000, start=-15), '-',
  100. # label='AE 4bit -10dB')
  101. #
  102. # aenc = Autoencoder(4, -8)
  103. # aenc.train(samples=5e5)
  104. # plt.plot(*get_AWGN_ber(aenc.get_modulator(), aenc.get_demodulator(), samples=12000, start=-15), '-',
  105. # label='AE 4bit -8dB')
  106. for scheme in ['64qam', '32qam', '16qam', 'qpsk', '8psk']:
  107. plt.plot(*get_SNR(
  108. AlphabetMod(scheme, 10e6),
  109. AlphabetDemod(scheme, 10e6),
  110. samples=100e3,
  111. steps=40,
  112. start=-15
  113. ), '-', label=scheme.upper())
  114. plt.yscale('log')
  115. # plt.gca().invert_xaxis()
  116. plt.grid()
  117. plt.xlabel('SNR dB')
  118. plt.ylabel('BER')
  119. plt.legend()
  120. plt.show()
  121. pass