def block_error_ratio_hamming_awgn(snr_db, block_size): mapping_k_m = { 4: 3 } # Mapping from k (block size) to m. m = 3 implies (7,4) code m = mapping_k_m[block_size] '''Hamming encoder and decoder instance''' hamm = itpp.comm.hamming_code(m) n = pow(2, m) - 1 # channel use rate = float(block_size) / float(n) '''Generate random bits''' nrof_bits = 10000 * block_size source_bits = itpp.randb(nrof_bits) '''Encode the bits''' encoded_bits = hamm.encode(source_bits) '''Modulate the bits''' modulator_ = itpp.comm.modulator_2d() constellation = itpp.cvec('-1+0i, 1+0i') symbols = itpp.ivec('0, 1') modulator_.set(constellation, symbols) tx_signal = modulator_.modulate_bits(encoded_bits) '''Add the effect of channel to the signal''' noise_variance = 1.0 / (rate * pow(10, 0.1 * snr_db)) noise = itpp.randn_c(tx_signal.length()) noise *= itpp.math.sqrt(noise_variance) rx_signal = tx_signal + noise '''Demodulate the signal''' demodulated_bits = modulator_.demodulate_bits(rx_signal) '''Decode the received bits''' decoded_bits = hamm.decode(demodulated_bits) '''Calculate the block error ratio''' blerc = itpp.comm.BLERC(block_size) blerc.count(source_bits, decoded_bits) return blerc.get_errorrate()
def propagate_transmit_bits_over_channel(transmit_bits, modulation_order, nrof_resource_blocks, channel_coefficients, noise_variance): nrof_constellation_symbols = int(itpp.math.pow2(modulation_order)) modulator = itpp.comm.QAM(nrof_constellation_symbols) nrof_subcarriers = nrof_resource_blocks * CONFIG.SUBCARRIERS_PER_PRB nrof_symbols = CONFIG.NROF_OFDM_SYMBOLS_PER_SUBFRAME modulated_symbols = modulator.modulate_bits(transmit_bits) if (modulated_symbols.length() != nrof_subcarriers * nrof_symbols): print('Mismatched number of modulated symbols: %d and resource elements: %d'%(modulated_symbols.length(), nrof_subcarriers * nrof_symbols)) ofdm_symbols = itpp.cmat(nrof_subcarriers, nrof_symbols) for i in range(nrof_symbols): temp = modulated_symbols.mid(i * nrof_subcarriers, nrof_subcarriers) ofdm_symbols.set_col(i, itpp.signal.ifft(temp, nrof_subcarriers))#.left(nrof_subcarriers)) noise = itpp.randn_c(nrof_subcarriers, nrof_symbols) * (0.5 * itpp.math.sqrt(noise_variance)) block_channel_coefficients = itpp.repmat(channel_coefficients, 1, nrof_symbols, True) received_symbols = itpp.elem_mult_mat(ofdm_symbols, block_channel_coefficients) + noise compensated_symbols = itpp.elem_div_mat(received_symbols, block_channel_coefficients) # Receiver processing demultiplexed_symbols = itpp.cvec(nrof_subcarriers * nrof_symbols) for i in range(nrof_symbols): temp = compensated_symbols.get_col(i) demultiplexed_symbols.set_subvector(i * nrof_subcarriers, itpp.signal.fft(temp, nrof_subcarriers))#.left(nrof_subcarriers)) # receive_soft_values = modulator.demodulate_soft_bits(demultiplexed_symbols, channel_coefficients, noise_variance, itpp.comm.Soft_Method.LOGMAP) receive_soft_values = modulator.demodulate_soft_bits(demultiplexed_symbols, noise_variance, itpp.comm.Soft_Method.LOGMAP) return receive_soft_values
def de_multiplex_symbols(nrof_ofdm_symbols_per_frame, nrof_subcarriers, frame_symbols): nrof_frames = frame_symbols.cols() constellation_symbols = cvec(nrof_subcarriers * nrof_ofdm_symbols_per_frame * nrof_frames) constellation_symbols.clear() for frame_index in range(nrof_frames): for ofdm_symbol_index in range(nrof_ofdm_symbols_per_frame): write_index = frame_index * nrof_subcarriers * nrof_ofdm_symbols_per_frame + ofdm_symbol_index * nrof_subcarriers constellation_symbols.set_subvector( write_index, (1.0 / (nrof_subcarriers**0.5)) * fft( frame_symbols.get_col(frame_index).mid( ofdm_symbol_index * nrof_subcarriers, nrof_subcarriers))) return constellation_symbols
def block_error_ratio_uncoded_awgn(snr_db, block_size): '''Generate random bits''' nrof_bits = 3 * 10000 * block_size source_bits = itpp.randb(nrof_bits) rate = 1.0 '''Modulate the bits''' modulator_ = itpp.comm.modulator_2d() constellation = itpp.cvec('-1+0i, 1+0i') symbols = itpp.ivec('0, 1') modulator_.set(constellation, symbols) tx_signal = modulator_.modulate_bits(source_bits) '''Add the effect of channel to the signal''' noise_variance = 1.0 / (rate * pow(10, 0.1 * snr_db)) noise = itpp.randn_c(tx_signal.length()) noise *= itpp.math.sqrt(noise_variance) rx_signal = tx_signal + noise '''Demodulate the signal''' demodulated_bits = modulator_.demodulate_bits(rx_signal) '''Calculate the block error ratio''' blerc = itpp.comm.BLERC(block_size) blerc.count(source_bits, demodulated_bits) return blerc.get_errorrate()
def multiplex_symbols(nrof_ofdm_symbols_per_frame, nrof_subcarriers, constellation_symbols): nrof_frames = int(constellation_symbols.length() / (nrof_subcarriers * nrof_ofdm_symbols_per_frame)) frame_symbols = cmat(nrof_subcarriers * nrof_ofdm_symbols_per_frame, nrof_frames) frame_symbols.clear() # Pre-allocate vector to store frame symbols for efficiency current_frame_symbols = cvec(nrof_subcarriers * nrof_ofdm_symbols_per_frame) for frame_index in range(nrof_frames): current_frame_symbols.clear() for ofdm_symbol_index in range(nrof_ofdm_symbols_per_frame): read_index = frame_index * nrof_subcarriers * nrof_ofdm_symbols_per_frame + ofdm_symbol_index * nrof_subcarriers current_frame_symbols.set_subvector( ofdm_symbol_index * nrof_subcarriers, (nrof_subcarriers**0.5) * ifft(constellation_symbols.mid(read_index, nrof_subcarriers))) frame_symbols.set_col(frame_index, current_frame_symbols) return frame_symbols