jbjjbjjbj / eittek651

Antenna diversity simulations.
Other
2 stars 2 forks source link

Sammenlign fejlprocent af simulation med teori #25

Closed jbjjbjjbj closed 3 years ago

jbjjbjjbj commented 3 years ago

Man kunne måske lave et modul der tegner de to grafer og så kan man selv sammenligne den.

Vi kan nok ikke gøre det ved alle modulationer, men vi kan jo gøre det ved nogle af dem.

jbjjbjjbj commented 3 years ago

Første test af vores kodes bit/sym error rate i forhold til den teoretiske værdi.

Symbol error probability

Her er do_bit sat til False i scriptet under:

image

Hmm der ser fint ud.

Bit error prob

Her er do_bit sat til True.

image

Her er noget galt, kan det måske være at vores gray encoder ikke virker.

Har i nogle ideer?

Script

Dette script bruger ændringer fra commit a70755b, dd091d5 og 12853cf i PR #26 .

Synes det var lidt for grimt til at commit.

from antenna_diversity.modulation import PAM
from antenna_diversity.encoding import SymbolEncoder
from antenna_diversity import error
from antenna_diversity.common import db_to_power, db_from_power
from antenna_diversity.channel import AWGN

import numpy as np
import matplotlib.pyplot as plt
import os

def eval_mod_bit(mod, snrs, n):
    print(mod.constellation)
    enc = SymbolEncoder(mod.M)

    byts = os.urandom(n)
    checker = error.BitErrorMeasure(byts)
    symbols = enc.encode_msb(byts)

    moded = mod.modulate(symbols)

    res = np.empty(len(snrs))
    for i, snr in enumerate(snrs):
        with_noise = moded + AWGN(len(moded), snr)
        demod = mod.demodulate(with_noise)

        byts_after = enc.decode_msb(demod)

        prob, faults, total = checker.check_against(byts_after)
        res[i] = prob
        print(f"snr={snr}, prob={prob}, faults={faults}, total={total}")

    return res

def eval_mod_sym(mod, snrs, n):
    print(mod.constellation)

    symbols = np.random.randint(0, M, size=n)
    checker = error.SymErrorMeasure(symbols)

    moded = mod.modulate(symbols)

    res = np.empty(len(snrs))
    for i, snr in enumerate(snrs):
        with_noise = moded + AWGN(len(moded), snr)
        demod = mod.demodulate(with_noise)

        prob, faults, total = checker.check_against(demod)
        res[i] = prob
        print(f"snr={snr}, prob={prob}, faults={faults}, total={total}")

    return res

M = 4
do_bit = False

theo_x_db = np.linspace(-100, 100, 1000)
# We devide by four to get the SNR per bit, instead of per sumbol
theo_x = db_to_power(theo_x_db) / (M if do_bit else 1)
theo_x_db = db_from_power(theo_x)

prob = (PAM.theo_bitprob if do_bit else PAM.theo_symprob)(M, theo_x)

real_x_db = np.linspace(-100, 100, 50)

func = eval_mod_bit if do_bit else eval_mod_sym
real_prob = func(PAM(M, use_gray=True), real_x_db, 10000)

plt.plot(theo_x_db, prob, label="theo")
plt.plot(real_x_db, real_prob, label="sim")
plt.legend()
plt.savefig("hej.png")
jbjjbjjbj commented 3 years ago

Snakkede lige med Carles, og fejlen er bare i det dumme script. Har smidt den nye herunder.

Ved ikke lige hvordan vi skal tilføje dette til repo på en god måde.

from antenna_diversity.modulation import PAM
from antenna_diversity.encoding import SymbolEncoder
from antenna_diversity import error
from antenna_diversity.common import db_to_power, db_from_power
from antenna_diversity.channel import AWGN

import numpy as np
import matplotlib.pyplot as plt
import os
import math

def eval_mod_bit(mod, snrs, n):
    print(mod.constellation)
    enc = SymbolEncoder(mod.M)

    byts = os.urandom(n)
    checker = error.BitErrorMeasure(byts)
    symbols = enc.encode_msb(byts)

    moded = mod.modulate(symbols)

    res = np.empty(len(snrs))
    for i, snr in enumerate(snrs):
        with_noise = moded + AWGN(len(moded), snr)
        demod = mod.demodulate(with_noise)

        byts_after = enc.decode_msb(demod)

        prob, faults, total = checker.check_against(byts_after)
        res[i] = prob
        print(f"snr={snr}, prob={prob}, faults={faults}, total={total}")

    return res

def eval_mod_sym(mod, snrs, n):
    print(mod.constellation)

    symbols = np.random.randint(0, M, size=n)
    checker = error.SymErrorMeasure(symbols)

    moded = mod.modulate(symbols)

    res = np.empty(len(snrs))
    for i, snr in enumerate(snrs):
        with_noise = moded + AWGN(len(moded), snr)
        demod = mod.demodulate(with_noise)

        prob, faults, total = checker.check_against(demod)
        res[i] = prob
        print(f"snr={snr}, prob={prob}, faults={faults}, total={total}")

    return res

M = 4
do_bit = True

theo_x_db = np.linspace(-15, 15, 1000)
# We devide by four to get the SNR per bit, instead of per sumbol
theo_x = db_to_power(theo_x_db) / (math.log2(M) if do_bit else 1)
# theo_x_db = db_from_power(theo_x)

prob = (PAM.theo_bitprob if do_bit else PAM.theo_symprob)(M, theo_x)

real_x_db = np.arange(-15, 15 + 1, 1)

func = eval_mod_bit if do_bit else eval_mod_sym
real_prob = func(PAM(M, use_gray=True), real_x_db, 100000)

plt.plot(theo_x_db, prob, label="theo")
plt.plot(real_x_db, real_prob, label="sim")
plt.legend()
plt.xlabel("noise (db)")
plt.ylabel(("bit" if do_bit else "sym") + " error probability")
plt.yscale("log")
plt.savefig("hej.png")