📄 systems.m
字号:
function n_ber = systems (SNR,n_mod_type,G,N_SUI,encode,samples,BW,channel);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %
%% Name: systems.m %
%% %
%% Description: All the different stages of the system are %
%% called from this function. These stages are the encoder, %
%% the transmitter, the channel, the receiver and the decoder. %
%% The channel, through which the information and noise travel, %
%% is also simulated here. %
%% %
%% Parameters: Each simulation depends on the signal to noise %
%% ratio of the system, type of modulation, size of the cyclic %
%% prefix, the channel that we are simulating, if the data is %
%% encoded or not and the number of bits sent; not to forget %
%% the bandwidth that we have available. %
%% %
%% Result: When the whole simulation is finished, the value of %
%% the BER is returned, with which a graph is shown that is of %
%% interest. %
%% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if n_mod_type == 1 % As the rate is not one of the things that interfere at first, I define it here.
rate = 1/2; % It might be treated as a parameter and the value might be set manually.
end
rate = 3/4;
Nfft = 256;
data_total_rx = [];
data_total = [];
data_out = [];
data_in = [];
n_symbols = samples; % Total symbols to transmit
% Depending on the input data (modulation, rate, number of symbols and if i encode or not)
% i generate the necessary data to carry out the simulation
switch n_mod_type
case 1
amount = 11;
amount_coded = 192;
template = [1];
codeRS=[12 12];
case 2
if rate == (1/2)
amount = 23;
template = [1 0 1 1]; % These templates are those that are used in the
codeRS=[32 24]; % Convolutional encoding and later in the Virerbi algorithm.
elseif rate == (3/4)
amount = 35;
template = [1 0 1 0 1];
codeRS=[40 36];
end
amount_coded = 384;
case 4
if rate == (1/2)
amount = 47;
template = [1 0 1 1];
codeRS=[64 48]; % The variable codeRS indicates the form in which
elseif rate == (3/4) % data must be encoded in the Reed-Solomon encoding.
amount = 71;
template = [1 0 1 0 1];
codeRS=[80 72];
end
amount_coded = 768;
case 6
if rate == (2/3)
amount = 95; % Logically, "amount" marks how many bytes i must generate in each case.
template = [1 1 0];
codeRS=[108 96];
elseif rate == (3/4)
amount = 107;
template = [1 0 1 0 1];
codeRS=[120 108];
end
amount_coded = 1152;
end
% Once the bytes needed to generate are found, they are generated for each case
data_total = randint (1,amount*8*n_symbols);
n_symbols = n_symbols;
rate = amount_coded/(amount*8);
if encode==0
% If encoding is not employed, the necessary bits are generated.
n_symbols = floor(length(data_total)/amount_coded);
amount = amount_coded/8;
% Again, once the bytes needed to generate are found, they are generated.
data_total = randint (1,amount*8*n_symbols);
end
bits_ofdm = amount * 8;
for i=1:n_symbols
% We combine only the groups of bits necessary to form an OFDM symbol.
data_in = data_total( 1+(i-1)*bits_ofdm : i*bits_ofdm );
% When the bits of the symbol to transmit are ready,the transmitter is called so that it forms the complete OFDM symbol
[pilot_mapping,data_mapping] = encoder(data_in,codeRS,template,n_mod_type,encode);
pilots=pilot_mapping;
data=data_mapping;
guard1 = complex (0,0) * ones (1,28);
DC = complex (0,0);
guard2 = complex (0,0) * ones (1,27);
% The pilot and guard subcarriers are placed according to the standard.
symbol= [guard1 data(1:12) pilots(1) data(13:36)...
pilots(2) data(37:60) pilots(3) data(61:84) pilots(4)...
data(85:96) DC data(97:108) pilots(5) data(109:132) pilots(6)...
data(133:156) pilots(7) data(157:180) pilots(8) data(181:192) guard2];
symbol_ofdm = sqrt(Nfft) .* ifft(symbol,Nfft);
margin = length(symbol_ofdm)*G;
symbolTx = [symbol_ofdm((end-margin+1):end) symbol_ofdm];
% The channel is simulated:
if N_SUI == 0 % AWGN channel
symbol_channel = symbolTx;
elseif N_SUI~=0 % Channel with fading
NextSymbol = 8 .* sqrt(Nfft) .* ifft(randint(1,Nfft));
symbolTx = [NextSymbol NextSymbol symbolTx];
symbol_channel = filter(channel,1,symbolTx);
symbol_channel = symbol_channel(end-(256*(1+G))+1:end);
end
% The receiver is simulated:
margin = length(symbol_channel)*G;
margin = margin/(1+G);
symbol_ofdm_rx = symbol_channel(margin+1:end);
symbol_channel_rx = fft(symbol_ofdm_rx,Nfft) ./ sqrt(Nfft);
if encode == 1
Eb = 1;
elseif encode == 0
Eb = rate;
end
% Calculation of the variance of the noise.
sigma = (Eb*10^(-SNR/10)) / n_mod_type / 2;
% Finally, the noise to be added is calculated.
noise_channel = sqrt(sigma)*(randn(1,length(symbol_channel_rx)) + j*randn(1,length(symbol_channel_rx)));
symbol_channel_rx = symbol_channel_rx + noise_channel;
% Decode the received bits:
data_out = decoder (pilot_mapping,data_mapping,symbol_channel_rx,n_mod_type,codeRS,template,SNR,encode,bits_ofdm,channel,N_SUI);
% The results are accumulated to finally verify the error.
data_total_rx = [data_total_rx data_out];
end
% Now the rate of error of the transmission is verified.
n_ber = mean( data_total_rx(:) ~= data_total(:));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -