clear all;
%main
%general parameters
Tx=2; % the number of transmit antennas
Rx=2; % the number of receive anntennas
NumberOfSubfreq=512; % the number of sub_carrier wave in OFDM subsystem
NumberOfBurst=100; % the number of bursts
NumberOfSymbols=10; % the number of ofdm symbols per burst
NumberOfbits=952; % the number of bits per ofdm symbol. NumberOfbits=(NumberOfSubfreq-2*number_of_pilot)*Tx*modulate_type*1/2
% channel parameters
pathnumber=1; % the number of multipath
jakes_fd =100; % Hz
jakes_fs = 5000000; % Hz
in_number= NumberOfSymbols*NumberOfBurst*(512+128);
disp('please select modulate type: 1 BPSK; 2 QPSK; 3 8PSK; 4 16QAM')
%modulate_type=input('modulate type=:'); % 1 BPSK 2 QPSK 3 8PSK 4 16QAM
modulate_type=2;
begin=-8;
step=2;
for s=1:8 % loop of iteration
SNR(s)=begin+(s-1)*step; %generate SNR
No = power(10,-SNR(s)/10)/modulate_type; %calculate No
sigma=sqrt(No/2);
% loop of each burst
for Number_Of_burst=1:NumberOfBurst
% generate original_bit
rand('state',0);
original_bit=round(rand(NumberOfSymbols,NumberOfbits));% dimension NumberOfSymbols*NumberOfbits=10*952; about 10k bits
% loop of each ofdm symbol
for NumberOfLoops=1:NumberOfSymbols
%-------------------------transmit part ---------------------------------------%
input_bits=original_bit(NumberOfLoops,:);% dimension 1*952
%modulate
temp_modulate_symbol=modulate(modulate_type,input_bits,NumberOfbits);%dimension 1*476
%transmit power normal
modulate_symbol=temp_modulate_symbol./sqrt(Tx); % dimension 1*476
%serial to paralle
length_of_column=NumberOfbits/(Tx*modulate_type);% 238
vector=reshape(modulate_symbol,Tx,length_of_column); % dimension 2*238
% MIMO Alamouti coding
for k=1:length_of_column
X=vector(:,k); % dimension 2*1
Alamouti_coding=Alamouti(X); % dimension 2*2
MIMO_coding(:,(2*k-1):(2*k))=Alamouti_coding; % dimension 2*476
end %end of Alamouti coding
tx1_symbol=MIMO_coding(1,:);% the transmited data of tx1 dimension 1*476
tx2_symbol=MIMO_coding(2,:);% the transmited data of tx2 dimension 1*476
%insert pilot
[ofdm_input_tx1,ofdm_input_tx2]=add_pilot_fast_fading(tx1_symbol,tx2_symbol);% dimension 1*512
%OFDM
guard_interval=128;
output_ofdm_tx1=ifft_cp_tx_blk(ofdm_input_tx1,NumberOfSubfreq,guard_interval);% dimension 1*(512+128)
output_ofdm_tx2=ifft_cp_tx_blk(ofdm_input_tx2,NumberOfSubfreq,guard_interval);% dimension 1*(512+128)
%-------------------- end of transmit part--------------------------------------------------------%
%-------------------- Start of channel part--------------------------------------------------------%
% read channel weighs
rayleigh_I=load('D:\MIMO\MIMO+OFDM\all\rayleigh_I.txt');
rayleigh_Q=load('D:\MIMO\MIMO+OFDM\all\rayleigh_Q.txt');
position=Number_Of_burst*NumberOfLoops;
% extrat data of n_th ofdm symbol
rayleigh=rayleigh_I(2560*position-2559:2560*position)+1i*rayleigh_Q(2560*position-2559:2560*position);
for i=1:640
H(1,1,i)=rayleigh(i);
H(1,2,i)=rayleigh(i+640);
H(2,1,i)=rayleigh(i+1280);
H(2,2,i)=rayleigh(i+1920);
end
%input channel
for i=1:640
X=[output_ofdm_tx1(i);output_ofdm_tx2(i)];
h=H(:,:,i);
y=h*X;
channel_output(:,i)=y;
end
%add AWGN noise
noise=randn(2,640)*sigma/sqrt(NumberOfSubfreq); %生成高斯白噪声
%received data
rx1=channel_output(1,:)+noise(1,:);% 维数 1×(512+128)
rx2=channel_output(2,:)+noise(2,:);% 维数 1×(512+128)
%-------------------- end of channel part--------------------------------------------------------%
%------------------ receive part---------------------------------------------------------------%
% OFDM demodulate
ofdm_demodulate_rx1=fft_cp_rx_blk(rx1,NumberOfSubfreq,guard_interval);% dimension 1*512
ofdm_demodulate_rx2=fft_cp_rx_blk(rx2,NumberOfSubfreq,guard_interval);% dimension 1*512
% channel estimat
[H_est,rx1_symbols,rx2_symbols]=est_fast_fading(ofdm_demodulate_rx1,ofdm_demodulate_rx2);% rx1_symbols dimension 1*476, H_est dimension 2*952
% MIMO decoding
for i=1:length_of_column
H_MIMO=( H_est(:,4*i-3:4*i-2)+H_est(:,4*i-1:4*i))/2; % H dimension 2*2
receiver_symbol(1,:)=rx1_symbols( (2*i-1) : 2*i);% dimension 1*2
receiver_symbol(2,:)=rx2_symbols( (2*i-1) : 2*i);% dimension 1*2 receiver_symbol的dimension 2*2
output_decoding=DeAlamouti(H_MIMO,receiver_symbol,Rx); % dimension 1*2
MIMO_decoding((2*i-1) : 2*i,1)=output_decoding; % dimension 476*1;
end %end of MIMO decoding
% demodulate
[NumberOfRows,NumberOfcolumns]=size(MIMO_decoding);
temp_receive_bits=demodulate(MIMO_decoding,modulate_type,NumberOfRows);% dimension2*476;
receive_bit(NumberOfLoops,:)=reshape(temp_receive_bits,1,NumberOfbits);% dimension NumberOfSymbols*952
end % end of 每个帧的循环
% calculated the number of error bits per burst
compare_matrix=(receive_bit ~= original_bit);
error_bits(Number_Of_burst)=sum(sum(compare_matrix,2));
end % end of 每个迭代的主循环,即若干帧的循环
% calculate BER
total_error_bits=sum(error_bits);
total_bits=NumberOfBurst*NumberOfSymbols*NumberOfbits;
BER(s)=total_error_bits/total_bits;
end % end of 每个仿真点的主循环
% drwa figure
draw_figure(SNR,BER,modulate_type,Rx,Tx);