📄 simulation_system.m
字号:
function simulation_system% This is simulation for the OFDM system.% ------- simulation1.m ------------------------------------% Black team% April-08-05% ----------------------------------------------------------sigma = input('write the value of the noise power in db (adviced range : [-20, 10]) ');%define global variable: knowm at the transmitter and receiverglobal nr_cyclic_bits % number of cyclic bitsglobal nr_subcarriers % number of subcarriersglobal size_training_seq % used to detect a incomming messageglobal Ex % normalized energy per subcarrierglobal gap % gap in the waterfilling algoglobal nr_pilots % number of pilots at the very beginningglobal nr_frames % number of frames per blockglobal encoder_order % encoder rate: 1/encoder_orderglobal nr_fft% just for initialization in transmitterglobal begin_flag % flag: synchronization sequence and 10 pilots at beginning global train_flag % flag: first message block, message with synchronization sequenceglobal start % flag: if start>0, transmit % just initialization for receiverglobal begin_flag_receiver % begin_flag for receiver global train_flag_receiver % train_flag for receiverglobal alfa; % temp variable to calculate H global belta; % temp variable to calculate H % just for saving these value, like a bufferglobal tempb; % Temp: bits repartitionglobal tempe; % Temp: energy repartitionglobal tempH; % Temp: channel responseglobal tempN; % Temp: noise varianceglobal Len; % Length of message % initializationEx = 30; gap = -6;nr_cyclic_bits = 40; % Size of cyclic prefix (in nr bits) size_training_seq = 50; % Size of training sequence (in bits)nr_frames = 30; % Number of parallel frames per blocknr_subcarriers = 127; % Number of subcarriers usednr_fft = 256; % Number of fft, = 2*nr_subcarriers + 2nr_pilots = 30; % Number of parallel pilots (empty or not)size_message = 10^4; encoder_order = 2;% initial for the beginning of allbegin_flag = 0;begin_flag_receiver = 0;train_flag = 0;train_flag_receiver = 0;alfa = 0;belta = 0;tempN = 0;bn = zeros(1,nr_subcarriers);en = zeros(1,nr_subcarriers);size_frame = 127; blk = 0;sumSNR = 0;start = 1; is_updated = 0; %if is_updated =0, the transmitter %has no info about the channel %if is_updated =1, there was a feedback% pilot, first ten_pilots and traning sequence pilot = rand(nr_subcarriers,1) + j*rand(nr_subcarriers,1);training_seq = rand(1, size_training_seq);pilots_ten=[];for i = 1:nr_pilots temp = pilot + i * 0.05; pilots_ten = [pilots_ten temp];end % train_message for synchronization (message)IFFTpilot2 = symifft(pilot);pilot_train2 = add_prefix(IFFTpilot2,nr_cyclic_bits);train_message = reshape(pilot_train2,1,(nr_fft+nr_cyclic_bits));uptrain_message = upfir(train_message);uptrain_message = uptrain_message(1:end-7);% train_pilots for synchronization (10_pilots)IFFTpilot1 = symifft(pilot + 0.05);pilot_train1 = add_prefix(IFFTpilot1,nr_cyclic_bits);train_pilots = reshape(pilot_train1,1,(nr_fft+nr_cyclic_bits));uptrain_pilots=upfir(train_pilots);uptrain_pilots=uptrain_pilots(1:end-7); % Error counter nr_errors = 0; % generation of the message message = generate(size_message); output=[]; % transmission.. disp('Transmission'); while (start>0) blk = blk+1; % total bit energy if(blk==1) sumEb = 0; % pilots block else sumEb = sumEb + Ex*nr_subcarriers / size_frame; end %Transmitter sends Training sequence, Pilot and then block of %frames, Pilot, block of frames, Pilots, etc.. m1 = transmitter(training_seq, pilot, pilots_ten, message, bn, en, size_frame, is_updated); m2=filter([1 0.8],1,m1); m3 = addnoise(m2,sigma); %adding some noise %Transmitter synchronizes at the beginning, then estimates the %channel thanks to the Pilots, receives blocks after blocks and %provides the channel information [m4, en, bn, size_frame] = receiver(training_seq, pilot,uptrain_message,uptrain_pilots,m3); output = [output m4]; is_updated = 1; end if(length(output)<size_message) output = [output zeros( 1,(size_message-length(output)) )]; end if(length(output)>size_message) output = output(1:size_message); end disp('transmission done'); % error count nr_errors = sum(xor(output,message)); SNR = sumEb / 10^(sigma/10) / (blk - 1); % Compute the BER.disp('The BER is equal to:');BER = nr_errors / length(message)disp('The SNR in db :');SNRdb = 10 * log10(SNR)% %semilogy(SNR2db, BER2)% subplot(311)% plot(abs(tempH));title('Channel estimation');% subplot(312)% plot(bn);title('Bit allocation');% subplot(313)% plot(abs(message-output));title('the "1" correspond to one error');subplot(313)plot(bn);title('Bit allocation');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -