function [ ED_SMsequence,H_ED ] = ED_SM_BitMapping( psudo_bit_sequence,H,N_total,Nt,Nr,M1,R,k )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
psudo_bit_sequence_length = length(psudo_bit_sequence);%比特序列长度
table1=[-3+5i,-1+5i,-1-5i,-3-5i,-5+3i,-5+1i,-5-1i,-5-3i,-3+3i,-3+1i,-3-1i,-3-3i,-1+3i,-1+1i,-1-1i,-1-3i,1+3i,1+1i,1-1i,1-3i,3+3i,3+1i,3-1i,3-3i,5+3i,5+1i,5-1i,5-3i,3+5i,1+5i,1-5i,3-5i]/sqrt(20);%32QAM
table2 = [1+1i,1+3i,3+1i,3+3i,1-1i,1-3i,3-1i,3-3i,-1+1i,-1+3i,-3+1i,-3+3i,-1-1i,-1-3i,-3-1i,-3-3i]/sqrt(10);%16QAM
table3=[-3+1i,-3-1i,-1+1i,-1-1i,1+1i,1-1i,3+1i,3-1i]/sqrt(6);%矩形8QAM
table4=[1+1i,1-1i,-1+1i,-1-1i]/sqrt(2);%4QAM
table5=[1+1i,-1-1i]/sqrt(2);%BPSK
switch log2(M1)
case 1
table=table5;
case 2
table=table4;
case 3
table=table3;
case 4
table=table2;
case 5
table=table1;
end
% xx=zeros(16,4);%产生所有可能的发送向量
% mm=1;
% for j=1:size(table,2)
% for w=1:size(xx,2)
% xx(mm,w)=table(j);
% mm=mm+1;
% end
% end
xx = [ 0.7071 + 0.7071i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.7071 + 0.7071i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.7071 + 0.7071i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.7071 + 0.7071i
0.7071 - 0.7071i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.7071 - 0.7071i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.7071 - 0.7071i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.7071 - 0.7071i
-0.7071 + 0.7071i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i -0.7071 + 0.7071i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i -0.7071 + 0.7071i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.7071 + 0.7071i
-0.7071 - 0.7071i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i -0.7071 - 0.7071i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i -0.7071 - 0.7071i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.7071 - 0.7071i];
%*********************************************************************************************
%天线选择部分
Nt_index=[1:1:N_total];
candi=nchoosek(Nt_index,4);
for i=1:size(candi,1)
nn=1;
H_cand=H(:,candi(i,:));%在大H中取出每个候选组合进行ED计算
for j=1:size(xx,1)
for w=1:size(xx,1)
if j~=w
db(nn)=norm(H_cand*(xx(j,:)-xx(w,:))')^2;
nn=nn+1;
end
end
end
d(i)=min(db);%先找出每个候选组合中最小的ED
end
[PI,POS]=max(d);
H_ED=H(:,candi(POS,:));%将最小ED最大的组合作为最优选择
% for i=1:size(candi1,1)
% destance(i)=+inf;
% HH(:,1)=H(:,candi1(i,1));
% HH(:,2)=H(:,candi1(i,2));
% HH(:,3)=H(:,candi1(i,3));
% HH(:,4)=H(:,candi1(i,4));
% for j=1:size(tablexx,1)
% for m=1:size(tablexx,1)
% if(j~=m)
% tmp=norm(HH*(tablexx(j,:)-tablexx(m,:)).')^2;
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% end
% end
% end
% end
% for i=1:size(candi1,1)
% destance(i)=+inf;
% HH(:,1)=H(:,candi1(i,1));
% HH(:,2)=H(:,candi1(i,2));
% HH(:,3)=H(:,candi1(i,3));
% HH(:,4)=H(:,candi1(i,4));
% for j=1:size(table,2)
% for m=1:size(table,2)
% if(j==m)
% tmp=norm((HH(:,1)*table(j)-HH(:,2)*table(m)),'fro');
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% tmp=norm((HH(:,1)*table(j)-HH(:,3)*table(m)),'fro');
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% tmp=norm((HH(:,1)*table(j)-HH(:,4)*table(m)),'fro');
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% tmp=norm((HH(:,2)*table(j)-HH(:,3)*table(m)),'fro');
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% tmp=norm((HH(:,2)*table(j)-HH(:,4)*table(m)),'fro');
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% tmp=norm((HH(:,3)*table(j)-HH(:,4)*table(m)),'fro');
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% else
% tmp=norm(HH(:,1)*(table(j)-table(m)),'fro');
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% tmp=norm(HH(:,2)*(table(j)-table(m)),'fro');
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% tmp=norm(HH(:,3)*(table(j)-table(m)),'fro');
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% tmp=norm(HH(:,4)*(table(j)-table(m)),'fro');
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% tmp=norm((HH(:,1)*table(j)-HH(:,2)*table(m)),'fro');
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% tmp=norm((HH(:,1)*table(j)-HH(:,3)*table(m)),'fro');
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% tmp=norm((HH(:,1)*table(j)-HH(:,4)*table(m)),'fro');
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% tmp=norm((HH(:,2)*table(j)-HH(:,3)*table(m)),'fro');
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% tmp=norm((HH(:,2)*table(j)-HH(:,4)*table(m)),'fro');
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% tmp=norm((HH(:,3)*table(j)-HH(:,4)*table(m)),'fro');
% if(tmp<=destance(i))
% destance(i)= tmp;
% end
% end
% end
% end
% end
%*********************************************************************************************
if mod(psudo_bit_sequence_length,R) == 0
q1 = psudo_bit_sequence(1:R:end);
q2 = psudo_bit_sequence(2:R:end);
q3 = psudo_bit_sequence(3:R:end);
q4 = psudo_bit_sequence(4:R:end);
qx = [q1,q2,q3,q4];
bit_to_transform=qx(k,:);
SMbit1=bit_to_transform(1:2);%天线索引比特
SMbit2=bit_to_transform(3:4);%传输符号比特
SMpos=zeros(1,Nt);
SMbit1_str=num2str(SMbit1);
SMbit1_num=bin2dec(SMbit1_str);
SMTx=SMbit1_num+1;
SMbit2_str=num2str(SMbit2);
SMbit2_num=bin2dec(SMbit2_str);
SMx=table(SMbit2_num+1);
SMpos(SMTx)=SMx;
ED_SMsequence=SMpos;
else
disp('Please input a sequence with even members');
sequence = 0;
end
end