📄 simulation2.asv
字号:
%simulation chain
% This is simulation for the OFDM system.
% ------- simulation1.m ------------------------------------
% Black team
% April-08-05
% ----------------------------------------------------------
clear;
%define global variable: knowm at the transmitter and receiver
global nr_cyclic_bits % number of cyclic bits
global nr_subcarriers % number of subcarriers
global size_training_seq % used to detect a incomming message
global Ex % normalized energy per subcarrier
global gap % gap in the waterfilling algo
global nr_pilots % number of pilots at the very beginning
global nr_frames % number of frames per block
global encoder_order % encoder rate: 1/encoder_order
global nr_fft
% just for initialization in transmitter
global begin_flag % flag: synchronization sequence and 10 pilots at beginning
global train_flag % flag: first message block, message with synchronization sequence
global start % flag: if start>0, transmit
% just initialization for receiver
global begin_flag_receiver % begin_flag for receiver
global train_flag_receiver % train_flag for receiver
global alfa; % temp variable to calculate H
global belta; % temp variable to calculate H
% just for saving these value, like a buffer
global tempb; % Temp: bits repartition
global tempe; % Temp: energy repartition
global tempH; % Temp: channel response
global tempN; % Temp: noise variance
global Len; % Length of message
global last_t;
% initialization
SNR = 7; % Noise power values to simulate (in dB)
Ex = 1;
gap = -3;
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 block
nr_subcarriers = 127; % Number of subcarriers used
nr_fft = 256; % Number of fft, = 2*nr_subcarriers + 2
nr_pilots = 30; % Number of parallel pilots (empty or not)
size_message = 10^4;
encoder_order = 2;
is_updated = 0; %if is_updated =0, the transmitter
%has no info about the channel
%if is_updated =1, there was a feedback
last_t = 0; % t_samp of last time. Use it if
% t_samp is bad this time
% 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);
% Loop over different values of Noise power(in dB).
nr_errors = zeros(1, length(SNR)); % Error counter
for snr_point = 1:length(SNR)
snr_point
%generation of the message
message = generate(size_message);
output=[];
% initial for the beginning of all
begin_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;
is_updated = 0
start = 1;
while (start>0)
start
%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);
%modelisation of the channel
%
m2 = filter( [1 0 0 0 0 0.5 0 0.3],1,m1); %channel fading
m3 = addnoise2(m2,SNR(snr_point)); %adding some noise
m3 = [zeros(1,30) m3]; %delay
%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,train_message,train_pilots,m3);
output = [output m4];
is_updated = 1;
end
% let length(output) = size_message, for comparision in Matlab
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
% error count
nr_errors(snr_point) = sum(xor(output,message));
end
% Compute the BER.
BER = nr_errors / length(message)
semilogy(SNR, BER)
% figure(2)
% plot(abs(H))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -