def scenario(prob=0.02, frame_len=20): "Main method to simulate a noisy scenario using Convolutional codes" # Stats vars start_time = time.time() # Scenario behaviour print("Simulating Convolutional code scenario (Viterbi)...\n") # Class Notes example sec: #bitsTx = npy.array([1, 1, 0, 1, 0, 1, 0, 0]) bitsTx = randBits(frame_len) code_Tx = ConvolutionalCode(L=2, n=2, k=1).ConvolutionalEncoder(bitsTx) code_Rx = Channel(prob).run(code_Tx) bitsRx, memory = ConvolutionalCode(L=2, n=2, k=1).viterbiDecoder(code_Rx) # Print results print('------------------------------------') print('Time elapsed: '+str((time.time() - start_time)*1000) + ' ms') print('Frame length: '+str(frame_len)) print('Error probability: '+str(prob)) print('\n------------ Results ---------------') print('[+] Memory usage '+str(memory/1000)+' KB') print('[+] Total bits: '+str(frame_len)) print('[+] Good: '+str(getGood(bitsTx, bitsRx))) print('[+] Errors (Total): '+str(code_Tx.size - getGood(code_Tx, code_Rx))) print('[+] Error probability (calculated): '+str((code_Tx.size - getGood(code_Tx, code_Rx))/code_Tx.size))
def scenario(q=3, prob=0.02): "Main method to simulate a noisy scenario using Hamming codes" # Stats vars start_time = time.time() # Scenario behaviour print("Simulating Hamming code scenario...\n") HammingBlock = HammingCode(q=3) bitsTx = prepareBitStreamFromFile('data/data.txt') code_Tx = HammingBlock.hammingEncoder(bitsTx) code_Rx = Channel(prob).run(code_Tx) bitsRx, fixed = HammingBlock.hammingDecoder(code_Rx) # Print results print('------------------------------------') print('Time elapsed: ' + str((time.time() - start_time) * 1000) + ' ms') print('Frame length: ' + str(len(bitsTx))) print('Repetition factor: ' + str(q)) print('Error probability: ' + str(prob)) print('\n------------ Results ---------------') print('[+] Total bits: ' + str(len(bitsTx))) print('[+] Good: ' + str(getGood(npy.array(bitsTx), bitsRx))) print('[+] Errors: ' + str(code_Tx.size - getGood(code_Tx, code_Rx))) print('[+] Fixed: ' + str(fixed)) print('[+] Error probability (calculated): ' + str((code_Tx.size - getGood(code_Tx, code_Rx)) / code_Tx.size)) # Save Received file saveIntoFile(bitsRx, 'data/data_out.txt')
def scenario(n=3, prob=0.02, frame_len=10000): "Main method to simulate a noisy scenario using Repetition codes" # Stats vars start_time = time.time() # Scenario behaviour print("Simulating Repetition code scenario...\n") bitsTx = randBits(frame_len) code_Tx = RepetitionCode.repetitionEncoder(bitsTx, n) code_Rx = Channel(prob).run(code_Tx) bitsRx = RepetitionCode.repetitionDecoder(code_Rx, n) # Print results print('------------------------------------') print('Time elapsed: '+str((time.time() - start_time)*1000) + ' ms') print('Frame length: '+str(frame_len)) print('Repetition factor: '+str(n)) print('Error probability: '+str(prob)) print('\n------------ Results ---------------') print('[+] Total bits: '+str(frame_len)) print('[+] Good: '+str(getGood(bitsTx, bitsRx))) print('[+] Errors: '+str(code_Tx.size - getGood(code_Tx, code_Rx))) print('[+] Fixed: '+str(code_Rx.size - getGood(code_Rx, npy.repeat(npy.array(bitsRx), n)))) print('[+] Error probability (calculated): '+str((code_Tx.size - getGood(code_Tx, code_Rx))/code_Tx.size))
def scenario(n=3, prob=0.02, frame_len=10000): "Main method to simulate a noisy scenario using Parity codes" # Stats vars start_time = time.time() # Scenario behaviour print("Simulating Parity code scenario...\n") bitsTx = randBits(frame_len) code_Tx = ParityCode.parityEncoder(bitsTx, n) code_Rx = Channel(prob).run(code_Tx) bitsRx, err_detected = ParityCode.parityDecoder(code_Rx, n) # Print results print('------------------------------------') print('Time elapsed: ' + str((time.time() - start_time) * 1000) + ' ms') print('Frame length: ' + str(frame_len)) print('Repetition factor: ' + str(n)) print('Error probability: ' + str(prob)) print('\n------------ Results ---------------') print('[+] Total bits: ' + str(frame_len)) print('[+] Good: ' + str(getGood(bitsTx, bitsRx))) print('[+] Errors (Total): ' + str(code_Tx.size - getGood(code_Tx, code_Rx))) print('[+] Errors (Data): ' + str(frame_len - getGood(bitsTx, bitsRx))) print('[+] Errors (Parity bits): ' + str((code_Tx.size - getGood(code_Tx, code_Rx)) - (frame_len - getGood(bitsTx, bitsRx)))) print('[+] Errors detected: ' + str(err_detected)) print('[+] Error probability (calculated): ' + str((code_Tx.size - getGood(code_Tx, code_Rx)) / code_Tx.size)) print('[+] Non error detection probability (calculated): ' + str(n * (n - 1) * 0.5 * ((code_Tx.size - getGood(code_Tx, code_Rx)) / code_Tx.size)**2))
def scenario(n=3, prob=0.02): "Main method to simulate a noisy scenario using Repetition codes" # Stats vars start_time = time.time() # Scenario behaviour print("Simulating Repetition code scenario (Audio)...\n") wave_obj = sa.WaveObject.from_wave_file('data/brahms_mono.wav') play_obj = wave_obj.play() play_obj.wait_done() print("Settintg up data into binary stream... \n") bitsTx = prepareBitStreamFromWaW(wave_obj) print("Encoding binary stream...\n") code_Tx = RepetitionCode.repetitionEncoder(bitsTx, n) print("Tx encoded data (usually takes time)...\n") code_Rx = Channel(prob).run(code_Tx) print("Decode data (usually takes time)...\n") bitsRx = RepetitionCode.repetitionDecoder(code_Rx, n) wave_obj.audio_data = bitsRx.tobytes() play_obj = wave_obj.play() play_obj.wait_done() # Print results print('------------------------------------') print('Time elapsed: ' + str((time.time() - start_time) * 1000) + ' ms') print('Frame length: ' + str(len(bitsTx))) print('[+] Errors: ' + str(code_Tx.size - getGood(code_Tx, code_Rx))) print('[+] Fixed: ' + str(code_Rx.size - getGood(code_Rx, npy.repeat(npy.array(bitsRx), n)))) print('[+] Error probability (calculated): ' + str((code_Tx.size - getGood(code_Tx, code_Rx)) / code_Tx.size))