def exercise_02(): filename = 'lab01/som_8_16_mono.wav' print('2. Reading sound file: {}'.format(filename)) fs, m = wav.read(filename) vmax = q.vmax(m) r = np.array([3, 5, 8]) snr = np.arange(len(r), dtype='float') for i in range(len(r)): delta_q = q.delta_q(vmax, r[i]) vj, tj = q.uniform_midrise_quantizer(vmax, delta_q) mq, idx = q.quantize(m, vmax, vj, tj) filename = 'lab02/som_8_16_quantize_{}bits.wav'.format(r[i]) print('2. r = {} [quantize]; Writing sound file {}'.format( r[i], filename)) wav.write(filename, fs, mq.astype('int16')) bin = c.pcm_encode(idx, r[i]) dec = c.pcm_decode(bin) filename = 'lab02/som_8_16_quantize_encode_decode_dequantize_{}bits.wav'.format( r[i]) print( '2. r = {} [quantize > encode > decode > dequantize]; Writing sound file {}' .format(r[i], filename)) wav.write(filename, fs, q.dequantize(vj, dec).astype('int16')) p = np.sum(m * m) / len(m) snr[i] = lib.metrics.snr_theoric(r[i], p, vmax) print('2. r = {:d}; SNR = {:>7.3f}'.format(r[i], snr[i]))
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)
def example(): # sample 3, page 85, midrise vmax = 1 delta_q = 2 * vmax / 8 vj, tj = q.uniform_midrise_quantizer(vmax, delta_q) n = np.arange(0, 8) m = np.round(np.sin(2 * np.pi * (np.float(1300) / 8000) * n), decimals=3) mq, idx = q.quantize(m, vmax, vj, tj) bin = c.pcm_encode(idx, 3) dec = c.pcm_decode(bin) xq = q.dequantize(vj, dec)
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))
def exercise_05(): # TODO # a) fs = 8000 f = 3500 t = np.arange(0, 5 * 1 / f, 1 / fs) m = np.sin(2 * np.pi * f * t) r = 3 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) x = c.pcm_encode(idx, r) print( '5. a) First 4 samples of the signal y(t) = sin(2 * pi * 3500 * t) with fs = {}: {}' .format(fs, m)) print('5. a) Midrise quantizer with r = 3 codification: {}'.format( np.ndarray.flatten(x)))
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]))
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('')
def exercise(): # hamming parameters n = 15 r = 11 # quantization m1, fs, filename = super_mario_intro('3sec') # m1, fs, filename = test_wav() # m1, fs, filename = sawtooth_signal() filename = 'lab04/{}'.format(filename) 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) # codification x2 = codification.pcm_encode(idx, r) # error control parity_matrix = 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') x3 = error_control.hamming(x2, parity_matrix, n, r) # digital modulation x4, coords_o, new_bits = digital_modulation.qam_encode(x3, p=8) # channel sigma_square = np.array([0.05, 0.1, 0.2, 0.3]) snr_channel = np.zeros(len(sigma_square)) snr_reception = np.zeros(len(sigma_square)) ber_bc = np.zeros(len(sigma_square)) ber_ac = np.zeros(len(sigma_square)) fig_signal = plt.figure(1) fig_received_signal = plt.figure(2, figsize=(12, 10)) fig_constellation = plt.figure(3, figsize=(12, 10)) for i in range(len(sigma_square)): y1 = channel.send_with_awgn(x4, sigma=np.sqrt(sigma_square[i])) # digital modulation y2, coords_r, coords_p = digital_modulation.qam_decode( y1, p=8, rm_bits=new_bits) # error control y3 = error_control.correction(y2, parity_matrix) # codification y4 = codification.pcm_decode(y3) # quantization m2 = quantization.dequantize(vj, y4) # output wav.write(filename.format(sigma_square[i]), fs, m2.astype('int16')) # metrics ax_received_signal = fig_received_signal.add_subplot(2, 2, i + 1) ax_constellation = fig_constellation.add_subplot(2, 2, i + 1) signal_graph(ax_received_signal, m2, sigma_square[i]) constellation_graph(ax_constellation, coords_o, coords_p, coords_r, sigma_square[i]) p_x4 = metrics.signal_power(x4) p_y1 = metrics.signal_power(y1) snr_channel[i] = metrics.snr(p_x4, p_y1) p_m1 = metrics.signal_power(m1) p_m2 = metrics.signal_power(m2) snr_reception[i] = metrics.snr(p_m1, p_m2) ber_bc[i] = metrics.ber(x3, y2) ber_ac[i] = metrics.ber(x2, y3) # metrics ax_signal = fig_signal.add_subplot(1, 1, 1) ax_signal.set_title('Transmitted signal') ax_signal.set_xlabel('time') ax_signal.set_ylabel('amplitude') ax_signal.plot(m1) fig_received_signal.suptitle('Received signal') fig_constellation.suptitle('16-QAM constellation') fig_ber_snr = plt.figure(4) ber_snr_graph(fig_ber_snr.add_subplot(1, 1, 1), ber_ac, ber_bc, snr_channel, snr_reception, sigma_square) plt.tight_layout() plt.show()