OFDM adding repetition coding

23 visualizzazioni (ultimi 30 giorni)
Jose Iglesias
Jose Iglesias il 3 Mag 2023
Commentato: Jose Iglesias il 1 Giu 2023
Need assistance in correcting an error in my OFDM transmitter and receiver Matlab code shown below. The following error appeared when I tried to run a repetition coding of 3 repetitions.
Error using reshape
Product of known dimensions, 192, not divisible into total number of elements, 64.
Error in ofdm_master (line 392)
rx_equalized_reshaped = reshape(rx_equalized, M, rep, []);
It seems this is an error of dimensions not being divisible by number of elements. Please let me know how I can correct this. I was simply trying to implement repetition coding for 1, 3, and 5 repetitions to the BPSK, QPSK, and 16-QAM modulation.in reference to SNR range -10dB:1:10dB, plotting the probability of error vs the receive SNR.
nsymb = 100;
M = 64; %Number of subcarrier channel
block_size = 64; %Size of each OFDM block to add cyclic prefix
cp_len = round(0.25*block_size); %Length of cyclic prefix
% Initiate SNR range and error count array
SNR_range = -10:1:10;
% BER = zeros(size(SNR_range));
errors = zeros(size(SNR_range));
errors1 = zeros(size(SNR_range));
errors2 = zeros(size(SNR_range));
symbols = 0;
symbols1 = 0;
symbols2 = 0;
% Read the video file
video = VideoReader('test_video.mp4');
% Calculate the total number of bits in the video
total_bits = 0;
% Initialize error count for all frames
error_count = 0;
% Loop through each frame of the video and convert it to a binary data stream
while hasFrame(video)
frame = readFrame(video);
decimal_data = reshape(frame,numel(frame),1);
binary_data = reshape(de2bi(decimal_data,8)',[],1);
% Increment total_bits
% total_bits = total_bits + numel(binary_data);
% Loop over SNR range
for i = 1:length(SNR_range)
snr = SNR_range(i);
disp(snr)
mod = 2;
no_of_data_bits = 64 * log2(mod);
% Loop through each symbol
for j = 1:numel(binary_data)/no_of_data_bits
% Use the binary data as the input source data
data = binary_data((j-1)*no_of_data_bits+1:j*no_of_data_bits);
data_reshaped = reshape(data, log2(mod), no_of_data_bits / log2(mod));
% Perform BPSK modulation on the input source data
modulated_data = qammod(data,mod);
% Repeat the modulated data
rep = 3; % repetitions
modulated_data_rep = repmat(modulated_data, rep, 1);
% Converting the series data stream into parallel data stream to form subcarriers
S2P = reshape(modulated_data,no_of_data_bits/M,M);
% Generate channel
channel_len = 10;
channel = (randn(1, channel_len) + 1i * randn(1, channel_len)) / sqrt(2);
% Zero-padding
channel_padded = [channel, zeros(1, block_size - length(channel))];
% IFFT of subcarriers and add cyclic prefix
number_of_subcarriers = M;
% IFFT does parallel to serial conversion
ifft_Subcarrier = ifft(S2P, block_size);
% Add cyclic prefix
cyclic_prefix = ifft_Subcarrier(:,end-cp_len+1:end);
Append_prefix = [cyclic_prefix ifft_Subcarrier];
% Multipath channel
rcvd_symbol = filter(channel,1,Append_prefix);
% Calculate noise variance for current SNR
noise_var = 1 / (10^(snr/10));
% Generate noisy channel coefficients
noisy_symbol = rcvd_symbol + sqrt(noise_var/2)*(randn(1,80)+1i*randn(1,80));
% Remove cyclic prefix at the receiver
rx_no_cp = noisy_symbol(cp_len+1:end);
% Perform FFT on received data
rx_fft = fft(rx_no_cp, block_size);
% FFT on the channel
channel_fft = fft(channel_padded);
% Equalize channel effect
rx_equalized = rx_fft ./ channel_fft;
% Reshape the received data to apply the repetition decoding
rx_equalized_reshaped = reshape(rx_equalized, M, rep, []);
% Decode the repeated data (majority rule)
rx_equalized_decoded = sum(rx_equalized_reshaped, 2) > rep/2;
% Perform BPSK demodulation
rx_demod = qamdemod(rx_equalized(:), mod); % (:) used to create vectors
% Calculate bit error rate
rx_demod_bits = dec2bin(rx_demod, log2(mod)) - '0';
% Calculate bit error rate
errors(i) = errors(i) + sum(rx_demod_bits(:) ~= data(:));
symbols = symbols + numel(data);
if j > nsymb
break
end
end
mod = 4;
no_of_data_bits = 64 * log2(mod);
% Loop through each symbol
for j = 1:numel(binary_data)/no_of_data_bits
% Use the binary data as the input source data
data = binary_data((j-1)*no_of_data_bits+1:j*no_of_data_bits);
% data_reshaped2 = reshape(data, no_of_data_bits / log2(mod), log2(mod));
data_reshaped = reshape(data, length(data) / log2(mod), log2(mod));
data_char = char(data_reshaped + '0');
data_dec = bin2dec(data_char);
% Perform BPSK modulation on the input source data
modulated_data = qammod(data_dec,mod);
% Converting the series data stream into parallel data stream to form subcarriers
% S2P = reshape(modulated_data,no_of_data_bits/M,M);
S2P = transpose(modulated_data);
% Generate channel
channel_len = 10;
channel = (randn(1, channel_len) + 1i * randn(1, channel_len)) / sqrt(2);
% Zero-padding
channel_padded = [channel, zeros(1, block_size - length(channel))];
% IFFT of subcarriers and add cyclic prefix
number_of_subcarriers = M;
% IFFT does parallel to serial conversion
ifft_Subcarrier = ifft(S2P, block_size);
% Add cyclic prefix
cyclic_prefix = ifft_Subcarrier(:,end-cp_len+1:end);
Append_prefix = [cyclic_prefix ifft_Subcarrier];
% Multipath channel
rcvd_symbol = filter(channel,1,Append_prefix);
% Calculate noise variance for current SNR
noise_var = sqrt(2) / (10^(snr/10));
% Generate noisy channel coefficients
noisy_symbol = rcvd_symbol + sqrt(noise_var/2)*(randn(1,80)+1i*randn(1,80));
% Remove cyclic prefix at the receiver
rx_no_cp = noisy_symbol(cp_len+1:end);
% Perform FFT on received data
rx_fft = fft(rx_no_cp, block_size);
% FFT on the channel
channel_fft = fft(channel_padded);
% Equalize channel effect
rx_equalized = rx_fft ./ channel_fft;
% Perform BPSK demodulation
rx_demod = qamdemod(rx_equalized(:), mod); % (:) used to create vectors
rx_demod_bits = dec2bin(rx_demod, log2(mod)) - '0';
% Calculate bit error rate
errors1(i) = errors1(i) + sum(rx_demod_bits(:) ~= data(:));
symbols1 = symbols1 + numel(data);
if j > nsymb
break
end
end
mod = 16;
%%%%%%%%%%
no_of_data_bits = M * log2(mod);
% Loop through each symbol
for j = 1:numel(binary_data)/no_of_data_bits
% Use the binary data as the input source data
data = binary_data((j-1)*no_of_data_bits+1:j*no_of_data_bits);
% data_reshaped2 = reshape(data, no_of_data_bits / log2(mod), log2(mod));
data_reshaped = reshape(data, length(data) / log2(mod), log2(mod));
data_char = char(data_reshaped + '0');
data_dec = bin2dec(data_char);
% Perform BPSK modulation on the input source data
modulated_data = qammod(data_dec,mod);
% Converting the series data stream into parallel data stream to form subcarriers
% S2P = reshape(modulated_data,no_of_data_bits/M,M);
S2P = transpose(modulated_data);
% Generate channel
channel_len = 10;
channel = (randn(1, channel_len) + 1i * randn(1, channel_len)) / sqrt(2);
% Zero-padding
channel_padded = [channel, zeros(1, block_size - length(channel))];
% IFFT of subcarriers and add cyclic prefix
number_of_subcarriers = M;
% IFFT does parallel to serial conversion
ifft_Subcarrier = ifft(S2P, block_size);
% Add cyclic prefix
cyclic_prefix = ifft_Subcarrier(:,end-cp_len+1:end);
Append_prefix = [cyclic_prefix ifft_Subcarrier];
% Multipath channel
rcvd_symbol = filter(channel,1,Append_prefix);
% Calculate noise variance for current SNR
noise_var = sqrt(10) / (10^(snr/10));
% Generate noisy channel coefficients
noisy_symbol = rcvd_symbol + sqrt(noise_var/2)*(randn(1,80)+1i*randn(1,80));
% Remove cyclic prefix at the receiver
rx_no_cp = noisy_symbol(cp_len+1:end);
% Perform FFT on received data
rx_fft = fft(rx_no_cp, block_size);
% FFT on the channel
channel_fft = fft(channel_padded);
% Equalize channel effect
rx_equalized = rx_fft ./ channel_fft;
% Perform BPSK demodulation
rx_demod = qamdemod(rx_equalized(:), mod); % (:) used to create vectors
rx_demod_bits = dec2bin(rx_demod, log2(mod)) - '0';
% Calculate bit error rate
errors2(i) = errors2(i) + sum(rx_demod_bits(:) ~= data(:));
symbols2 = symbols2 + numel(data);
if j > nsymb
break
end
end
% rx_demod_bits, data = ofdm_loop(2, binary_data, M, nsymb, ...)
% errors(i) = errors(i) + sum(rx_demod_bits(:) ~= data(:));
% symbols = symbols + numel(data);
% rx_demod_bits, data = ofdm_loop(4, binary_data, M, nsymb, ...)
% errors1(i) = errors1(i) + sum(rx_demod_bits(:) ~= data(:));
% symbols1 = symbols1 + numel(data);
% rx_demod_bits, data = ofdm_loop(16, binary_data, M, nsymb, ...)
% errors2(i) = errors2(i) + sum(rx_demod_bits(:) ~= data(:));
% symbols2 = symbols2 + numel(data);
end
break
end
% Divide the accumulated BER by the total number of symbols to get the average BER for this SNR
% BER = BER / (total_bits/no_of_data_bits);
% Calculate BER for each SNR
BER_2 = errors ./ symbols;
BER_4 = errors1 ./ symbols1;
BER_16 = errors2 ./ symbols2;
% Plot BER vs SNR
figure(8);
semilogy(SNR_range, BER_16, 'b-o', SNR_range, BER_4, 'r-s',SNR_range, BER_2, 'g-*');
% semilogy(SNR_range, BER_2, 'g-*');
% legend( '16QAM','QPSK','BPSK');
xlabel('SNR (dB)');
ylabel('BER');
grid on;
% xlabel('SNR (dB)');
% ylabel('Bit Error Rate');
% >>>>>>> Stashed changes
title('Probability of Error vs SNR');

Risposta accettata

Santosh Fatale
Santosh Fatale il 9 Mag 2023
Hi Jose,
I investigated the code shared by you and below are my findings/observation:
  • The variable modulated_data_rep is initialised but never used anywhere in the code.
  • I would recommend referring to the documentation on the repmat function. The repmat function repeats the input vector/matrix copy as per the specification. For example: repmat([1; 2; 3],3,1) creates a 9-by-1 column vector using 3-by-1 input vector. The output vector would be [1; 2; 3;1; 2; 3;1; 2; 3]. Kindly ensure that the repetition coding you are implementing is the same as the one in the communication standards.
  • The error message you are encountering is since input constraints to reshape functions are not satisfying. You could reshape the m-by-n matrix to p-by-q only if . The conditions assures that the number of elements in the original matrix and the reshaped matrix are same.
In the code, the original matrix is of size 64-by-1() and reshaped matrix would be 64-by-3 ().
Refer to the following documentation link to learn more about reshape and repmat functions.
I would request you to share all essential files while posting a query on the forum to understand it precisely. It is also recommended to utilize code option available while posting a query.

Più risposte (0)

Prodotti


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by