Skip to content

Commit

Permalink
Improved output of main_FAR.m
Browse files Browse the repository at this point in the history
  • Loading branch information
robmaunder committed Jan 23, 2018
1 parent aef4920 commit e160205
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 164 deletions.
245 changes: 124 additions & 121 deletions main_BLER_vs_SNR.m
Expand Up @@ -13,12 +13,12 @@ function main_BLER_vs_SNR(code, A, E, L, min_sum, target_block_errors, target_BL
% functions for explanations of their inputs and outputs. Suitable values
% for code include 'PBCH', 'PDCCH, 'PUCCH' and 'custom1'.
%
% A should be an integer scalar. It specifies the number of bits in each
% simulated information bit sequence, before CRC and other redundant bits
% are included.
% A should be an integer row vector. Each element specifies the number of
% bits in each set of simulated information bit sequences, before CRC and
% other redundant bits are included.
%
% E should be an integer row vector. Each element of E specifies one
% encoded block length to simulate, where E is the number of bits in each
% E should be an integer row vector. Each element of E specifies one
% encoded block length to simulate, where E is the number of bits in each
% encoded bit sequence.
%
% L should be a scalar integer. It specifies the list size to use during
Expand Down Expand Up @@ -57,19 +57,19 @@ function main_BLER_vs_SNR(code, A, E, L, min_sum, target_block_errors, target_BL
%
% See also MAIN_SNR_VS_A and MAIN_FAR
%
% Copyright © 2017 Robert G. Maunder. This program is free software: you
% can redistribute it and/or modify it under the terms of the GNU General
% Public License as published by the Free Software Foundation, either
% version 3 of the License, or (at your option) any later version. This
% program is distributed in the hope that it will be useful, but WITHOUT
% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
% Copyright © 2017 Robert G. Maunder. This program is free software: you
% can redistribute it and/or modify it under the terms of the GNU General
% Public License as published by the Free Software Foundation, either
% version 3 of the License, or (at your option) any later version. This
% program is distributed in the hope that it will be useful, but WITHOUT
% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
% more details.

% Default values
if nargin == 0
code = 'PUCCH';
A = 32;
A = [16 32 64 128 256 512 1024];
E = [54 108 216 432 864 1728 3456 6912 13824];
L = 1;
min_sum = true;
Expand All @@ -83,129 +83,132 @@ function main_BLER_vs_SNR(code, A, E, L, min_sum, target_block_errors, target_BL
% Seed the random number generator
rng(seed);

% Create a figure to plot the results.
figure
axes1 = axes('YScale','log');
title([code, ' polar code, A = ',num2str(A),', L = ',num2str(L),', minsum = ',num2str(min_sum),', errors = ',num2str(target_block_errors),', QPSK, AWGN']);
ylabel('BLER');
xlabel('E_s/N_0 [dB]');
ylim([target_BLER,1]);
hold on
drawnow

% Consider each encoded block length in turn
for E_index = 1:length(E)

% Create the plot
plot1 = plot(nan,'Parent',axes1);
legend(cellstr(num2str(E(1:E_index)', 'E=%d')),'Location','southwest');
% Consider each information block length in turn
for A_index = 1:length(A)

% Counters to store the number of bits and errors simulated so far
block_counts=[];
block_error_counts=[];
EsN0s = [];

% Open a file to save the results into.
filename = ['results/BLER_vs_SNR_',code,'_',num2str(A),'_',num2str(E(E_index)),'_',num2str(L),'_',num2str(min_sum),'_',num2str(target_block_errors),'_',num2str(seed)];
fid = fopen([filename,'.txt'],'w');
if fid == -1
error('Could not open %s.txt',filename);
end
% Create a figure to plot the results.
figure
axes1 = axes('YScale','log');
title([code, ' polar code, A = ',num2str(A(A_index)),', L = ',num2str(L),', minsum = ',num2str(min_sum),', errors = ',num2str(target_block_errors),', QPSK, AWGN']);
ylabel('BLER');
xlabel('E_s/N_0 [dB]');
ylim([target_BLER,1]);
hold on
drawnow

% Initialise the BLER and SNR
BLER = 1;
EsN0 = EsN0_start;

found_start = false;

% Skip any encoded block lengths that generate errors
try
% Loop over the SNRs
while BLER > target_BLER

% Convert from SNR (in dB) to noise power spectral density
N0 = 1/(10^(EsN0/10));

% Start new counters
block_counts(end+1) = 0;
block_error_counts(end+1) = 0;
EsN0s(end+1) = EsN0;

keep_going = true;

% Continue the simulation until enough block errors have been simulated
while keep_going && block_error_counts(end) < target_block_errors

% Generate a random block of bits
a = round(rand(1,A));

% Perform polar encoding
f = feval([code,'_encoder'], a, E(E_index));

% QPSK modulation
f2 = [f,zeros(1,mod(-length(f),2))];
tx = sqrt(1/2)*(2*f2(1:2:end)-1)+1i*sqrt(1/2)*(2*f2(2:2:end)-1);

% Simulate transmission
rx = tx + sqrt(N0/2)*(randn(size(tx))+1i*randn(size(tx)));

% QPSK demodulation
f2_tilde = zeros(size(f2));
f2_tilde(1:2:end) = -4*sqrt(1/2)*real(rx)/N0;
f2_tilde(2:2:end) = -4*sqrt(1/2)*imag(rx)/N0;
f_tilde = f2_tilde(1:length(f));
% Consider each encoded block length in turn
for E_index = 1:length(E)

% Create the plot
plot1 = plot(nan,'Parent',axes1);
legend(cellstr(num2str(E(1:E_index)', 'E=%d')),'Location','southwest');

% Counters to store the number of bits and errors simulated so far
block_counts=[];
block_error_counts=[];
EsN0s = [];

% Open a file to save the results into.
filename = ['results/BLER_vs_SNR_',code,'_',num2str(A(A_index)),'_',num2str(E(E_index)),'_',num2str(L),'_',num2str(min_sum),'_',num2str(target_block_errors),'_',num2str(seed)];
fid = fopen([filename,'.txt'],'w');
if fid == -1
error('Could not open %s.txt',filename);
end

% Initialise the BLER and SNR
BLER = 1;
EsN0 = EsN0_start;

found_start = false;

% Skip any encoded block lengths that generate errors
try
% Loop over the SNRs
while BLER > target_BLER

% Perform polar decoding
a_hat = feval([code, '_decoder'],f_tilde,A,L,min_sum);
% Convert from SNR (in dB) to noise power spectral density
N0 = 1/(10^(EsN0/10));

% Start new counters
block_counts(end+1) = 0;
block_error_counts(end+1) = 0;
EsN0s(end+1) = EsN0;

if found_start == false && ~isequal(a,a_hat)
keep_going = false;
BLER = 1;
else
found_start = true;
keep_going = true;

% Determine if we have a block error
if ~isequal(a,a_hat)
% Continue the simulation until enough block errors have been simulated
while keep_going && block_error_counts(end) < target_block_errors

% Generate a random block of bits
a = round(rand(1,A(A_index)));

% Perform polar encoding
f = feval([code,'_encoder'], a, E(E_index));

% QPSK modulation
f2 = [f,zeros(1,mod(-length(f),2))];
tx = sqrt(1/2)*(2*f2(1:2:end)-1)+1i*sqrt(1/2)*(2*f2(2:2:end)-1);

% Simulate transmission
rx = tx + sqrt(N0/2)*(randn(size(tx))+1i*randn(size(tx)));

% QPSK demodulation
f2_tilde = zeros(size(f2));
f2_tilde(1:2:end) = -4*sqrt(1/2)*real(rx)/N0;
f2_tilde(2:2:end) = -4*sqrt(1/2)*imag(rx)/N0;
f_tilde = f2_tilde(1:length(f));

% Perform polar decoding
a_hat = feval([code, '_decoder'],f_tilde,A(A_index),L,min_sum);


if found_start == false && ~isequal(a,a_hat)
keep_going = false;
BLER = 1;
else
found_start = true;

% Determine if we have a block error
if ~isequal(a,a_hat)
block_error_counts(end) = block_error_counts(end) + 1;
end

% Accumulate the number of blocks that have been simulated
% so far
block_counts(end) = block_counts(end) + 1;

% Calculate the BLER and save it in the file
BLER = block_error_counts(end)/block_counts(end);

% Plot the BLER vs SNR results
set(plot1,'XData',EsN0s);
set(plot1,'YData',block_error_counts./block_counts);
drawnow
end

% Accumulate the number of blocks that have been simulated
% so far
block_counts(end) = block_counts(end) + 1;

% Calculate the BLER and save it in the file
BLER = block_error_counts(end)/block_counts(end);

% Plot the BLER vs SNR results
set(plot1,'XData',EsN0s);
set(plot1,'YData',block_error_counts./block_counts);
drawnow
end

if BLER < 1
fprintf(fid,'%f\t%e\n',EsN0,BLER);
end

% Update the SNR, ready for the next loop
EsN0 = EsN0 + EsN0_delta;

end

if BLER < 1
fprintf(fid,'%f\t%e\n',EsN0,BLER);
catch ME
if strcmp(ME.identifier, 'polar_3gpp_matlab:UnsupportedBlockLength')
warning('polar_3gpp_matlab:UnsupportedBlockLength','%s does not support the combination of block lengths A=%d and E=%d. %s',code,A(A_index),E(E_index), getReport(ME, 'basic', 'hyperlinks', 'on' ));
continue
else
rethrow(ME);
end

% Update the SNR, ready for the next loop
EsN0 = EsN0 + EsN0_delta;

end
catch ME
if strcmp(ME.identifier, 'polar_3gpp_matlab:UnsupportedBlockLength')
warning('polar_3gpp_matlab:UnsupportedBlockLength','%s does not support the combination of block lengths A=%d and E=%d. %s',code,A,E(E_index), getReport(ME, 'basic', 'hyperlinks', 'on' ));
continue
else
rethrow(ME);
end

% Close the file
fclose(fid);
end

% Close the file
fclose(fid);
end





0 comments on commit e160205

Please sign in to comment.