% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Alamouti code STBC-SM
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Script for computing the BER for M-QAM modulation in a
% Rayleigh fading channel with Alamouti Space Time Block Coding spatial modulation
% 4 transmit antenna, 4 Receive antenna
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc
clear all
Nt=4;
Nr=4;
M=4;
E=1;
k_QAM=sqrt(E)*sqrt(2);
k_b=log2(M); % 信号位数
small=0.7;
big=sqrt(2-small^2);
angle1=pi/8;
Nt1=(Nt*(Nt-1)/2);
ss=0;
while(2^ss<Nt1)
ss=ss+1;
end
ss=ss-1; %ss为天线对选择位数 ss:total number of STBC-SM codewords
sss=2^ss;
no=k_b*2+ss; %总数
N =1000; % number of bits or symbols
Es_N0_dB = 0:2:13; % multiple different Eb/N0 values
BER = zeros(1,length(Es_N0_dB));
bit_errors = zeros(1,length(Es_N0_dB));
sq=sqrt(-1); %虚数单位i
%% 以下为核心代码 %%
for ii = 1:length(Es_N0_dB)
for aa=1:N
%% transmitter %%
active_antenna = round(rand(1,ss)); %选择被激活天线,激活的用1表示
ip1 = round(rand(1,k_b)); %等概率生成0和1
ip2 = round(rand(1,k_b));
ip3 = round(rand(1,k_b));
bin_ip=[ip1;ip2]; %调制符号对信息
bin_ip1=[active_antenna,ip1,ip2]; %生成要传输的信息比特,包含激活天线对信息和调制符号对信息
dec_ip=zeros(1,3);
for a=1:ss %选天线参数十进制化
if active_antenna(a)==1 %哪一个天线被选中
dec_ip(1,1) = dec_ip(1,1) + 2^(ss-a); %将其十进制化
end
end
bin_ip3=zeros(1,k_b);
for jj=1:2
bin_ip3=bin_ip(jj,:);
for a=1:k_b % Convert to Decimal Input 两个传输信号十进制化
if bin_ip3(a)==1
dec_ip(1,jj+1) = dec_ip(1,jj+1) + 2^(k_b-a);
end
end
end
sin=zeros(1,2);
for i=1:2
sin(1,i)=pskmod(dec_ip(i+1),M); %产生x1和x2
end
pairs=[floor(rand(1,16)*4+1);floor(rand(1,16)*4+1)].'; %生成天线组合矩阵
dec_choose=dec_ip(1,1); %dec_choose是随机的
tx_ant_no=dec_choose+1;
tx=pairs(tx_ant_no,:); %从pairs中选取行参数,即选出了激活天线对
x=zeros(1,Nt);
x(tx(1))=(sin(1,1)); % 输出信号 和给定的程序相比少了一个+1,如果加1了就不在0到M-1范围内了吧
x(tx(2))=(sin(1,2));
% Alamouti STBC编码过程
sCode=zeros(2,Nt);
sCode(1,:)=x;
sCode(2,tx(1))=(conj(x(tx(2)))*(-1)); % 双时间间隔传输信号矩阵
sCode(2,tx(2))=(conj(x(tx(1))));
if tx_ant_no>2
scode=sCode*exp(angle1*sq)*big; %乘以一个优化旋转角
else
scode=sCode*small;
end
%% 经过信道传输
H = 1/sqrt(2)*(randn(Nt,Nr) + sq*randn(Nt,Nr)); %瑞利信道矩阵
sigma=10^(-Es_N0_dB(ii)/20)*k_QAM;
n = sigma*1/sqrt(2)*(randn(2,Nr) + sq*randn(2,Nr)); %高斯白噪声, 0dB variance
y=scode*H+n; %不能用hMod代替x进行Alamouti变换,因为天线对很多
%% ML Detection Scheme %%
symbolsDetect = zeros(2 , N) ;
dml = zeros(M ,M ,N) ;
%得到M*M种组合
for i1 = 1:M
for i2 = 1:M
dml(i1, i2, aa) = ( norm( y - sCode * H , 'fro') ) ; % 共16种组合
end
end
dml_min = min( reshape(dml( :, :, aa ) .' , 1, M*M) ) ;
%找到最合适的 然后译码
for j1 = 1: M
for j2 = 1:M
if dml(j1 , j2, aa) ==dml_min
symbolsDetect(1, aa) = j1;
symbolsDetect(2, aa) = j2;
end
end
end
end
%计算误码率
samp=10000;
snr(ii)=Es_N0_dB(ii)+10*log10(k_b)-10*log10(samp); %信噪比
yn=awgn(y,snr(ii),'measured'); %加入高斯白噪声
yd=qamdemod(yn,M); %此时解调出来的是64进制信号
z=de2bi(yd,'left-msb'); %转化为对应的二进制比特流
z=reshape(z.',numel(z),1');
% [num_of_err(ii),sym_err_rate(ii)]=symerr(xsym,yd.');
[number_of_errors(ii),bit_error_rate(ii)]=biterr(x,z.');
% 一个信噪比结束 统计SER
SER(ii) = sum ( reshape(symbolsDetect , 1, N * Nt) ~= reshape (symbols , 1, N *Nt) )/ (N * Nt);
BER(ii)=bit_errors(ii)/(no*N) % BER
BER(ii)=bit_errors(ii)/(N); % SER
end
%%
semilogy(Es_N0_dB,BER,'bd-.','LineWidth',2);
hold on
grid on
xlabel('Es/No, dB')
ylabel('Bit Error Rate')