Ejemplo n.º 1
0
def exercise_01():
    signal = lab01.sawtooth_signal()
    vmax = q.vmax(signal)
    r = 3

    delta_q = q.delta_q(vmax, r)
    vj, tj = q.uniform_midrise_quantizer(vmax, delta_q)
    mq, idx = q.quantize(signal, vmax, vj, tj)

    bin = c.pcm_encode(idx, r)
    dec = c.pcm_decode(bin)
Ejemplo n.º 2
0
def exercise_03():
    # Hamming parameters
    n = 15
    r = 11

    # Signal quantization
    m = lab01.sawtooth_signal()

    vmax = q.vmax(m)
    delta_q = q.delta_q(vmax, r)
    vj, tj = q.uniform_midrise_quantizer(vmax, delta_q)
    mq, idx = q.quantize(m, vmax, vj, tj)

    # Parity matrix
    P = np.array([
        [1, 1, 1, 1],
        [0, 1, 1, 1],
        [1, 0, 1, 1],
        [1, 1, 0, 1],
        [1, 1, 1, 0],
        [0, 0, 1, 1],
        [0, 1, 0, 1],
        [0, 1, 1, 0],
        [1, 0, 1, 0],
        [1, 0, 0, 1],
        [1, 1, 0, 0],
    ],
                 dtype='uint8')

    # Encode and codify with Hamming(15, 11)
    bin = c.pcm_encode(idx, r)
    x = error_control.hamming(bin, P, n, r)

    # Simulate channel communication
    y = channel.send_with_binomial_noise(x, 0.01)

    # Measure the elapsed time to correct errors
    from time import time
    start_time = time()

    # Detect and correct error/noise
    y = error_control.correction(y, P)

    elapsed_time = time() - start_time
    print(
        '3.    Error correction elapsed time: {:4.3f} s'.format(elapsed_time))
Ejemplo n.º 3
0
def exercise_06():
    # Hamming parameters
    n = 15
    r = 11

    # Quantization
    from lab01 import lab01
    m1 = lab01.sawtooth_signal()

    vmax = quantization.vmax(m1)
    delta_q = quantization.delta_q(vmax, r)
    vj, tj = quantization.uniform_midrise_quantizer(vmax, delta_q)
    x1, idx = quantization.quantize(m1, vmax, vj, tj)

    # Encoder
    P = np.array([
        [1, 1, 1, 1],
        [0, 1, 1, 1],
        [1, 0, 1, 1],
        [1, 1, 0, 1],
        [1, 1, 1, 0],
        [0, 0, 1, 1],
        [0, 1, 0, 1],
        [0, 1, 1, 0],
        [1, 0, 1, 0],
        [1, 0, 0, 1],
        [1, 1, 0, 0],
    ],
                 dtype='uint8')

    x2 = codification.pcm_encode(idx, r)

    # Error control
    x3 = error_control.hamming(x2, P, n, r)

    # Digital modulation
    a = 1
    x4 = modulation.manchester_enconde(x3, a)

    # Channel
    sigma_squared = np.array([0.5, 1, 2, 4])

    for s in sigma_squared:
        y1 = channel.send_with_awgn(x4, np.sqrt(s))

        # Digital modulation
        y2 = modulation.machester_decode(y1, lambda_=0)

        # Error control
        y3 = error_control.correction(y2, P)

        # BER calculation
        # TODO: tb = len(x4[0]) ?
        tb = len(x4)
        eb = metrics.eb_manchester(a, tb)
        n0 = s * 2

        ber_pratic = metrics.ber(x3, y2)
        ber_theoric = metrics.ber_manchester(eb, n0)

        # Decoder
        y4 = codification.pcm_decode(y3)

        # Quantization
        m2 = quantization.dequantize(vj, y4)

        # Metrics
        # TODO: SNR?
        plt.plot(m1)
        plt.plot(m2)
        plt.show()

    # Metrics
    # snr = np.zeros(len(sigma_squared))

    px = metrics.signal_power(m1)
    snr_theoric = lib.metrics.snr_theoric(r, px, vmax)

    error = m1 - x1
    pe = metrics.signal_power(error)
    snr_pratic = lib.metrics.snr_db(px, pe)

    print('')
Ejemplo n.º 4
0
def exercise_04():
    # Parity matrix: http://michael.dipperstein.com/hamming/

    # Hamming parameters
    n = 15
    r = 11

    # Signal quantization
    m = lab01.sawtooth_signal()

    vmax = q.vmax(m)
    delta_q = q.delta_q(vmax, r)
    vj, tj = q.uniform_midrise_quantizer(vmax, delta_q)
    mq, idx = q.quantize(m, vmax, vj, tj)

    # Parity matrix
    P = np.array([
        [1, 1, 1, 1],
        [0, 1, 1, 1],
        [1, 0, 1, 1],
        [1, 1, 0, 1],
        [1, 1, 1, 0],
        [0, 0, 1, 1],
        [0, 1, 0, 1],
        [0, 1, 1, 0],
        [1, 0, 1, 0],
        [1, 0, 0, 1],
        [1, 1, 0, 0],
    ],
                 dtype='uint8')

    # Encode and codify with Hamming(15, 11)
    bin = c.pcm_encode(idx, r)
    x = error_control.hamming(bin, P, n, r)

    ber_theoric = np.array([.01, .05, .1, .5, .75, 1])
    ber_pratic = np.zeros(shape=(len(ber_theoric), 2))
    snr = np.zeros(len(ber_theoric))

    for i in range(len(ber_theoric)):
        # Channel simulation
        y = channel.send_with_binomial_noise(x, ber_theoric[i])
        ber_pratic[i, 0] = metrics.ber(x, y)

        # Error correction
        y = error_control.correction(y, P)
        ber_pratic[i, 1] = metrics.ber(bin, y)

        # Decode and Dequantize
        idx = c.pcm_decode(y)
        mq = q.dequantize(vj, idx)

        # SNR
        e = m - mq
        p_error = metrics.signal_power(e)
        p = metrics.signal_power(m)

        snr[i] = lib.metrics.snr_db(p, p_error)

        print(
            '4.    BERt = {:>6.2f}; BER = {:>6.3f}; BER\' = {:>6.3f}; SNR = {:>6.3f}'
            .format(ber_theoric[i], ber_pratic[i, 0], ber_pratic[i, 1],
                    snr[i]))
Ejemplo n.º 5
0
def sawtooth_signal():
    from lab01 import lab01
    m1 = lab01.sawtooth_signal()
    return m1, 0, 'sawtooth_{}.wav'