Silver Code (4-QAM) 2X2

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% Silver code ( 4-QAM ) 2 X 2 %%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc
clear all;
close all;
N = 4*10^4;
M=4;
Xin = randint(1,N,M);
y=modulate(modem.qammod(M),Xin);
a=1/sqrt(7);
G1=[1 0 0 0 a -a -a -2*a;
    0 1 0 0 a a 2*a -a;
    0 0 1 0 -a 2*a -a -a;
    0 0 0 1 -2*a -a a -a;
    0 0 -1 0 -a 2*a -a -a;
    0 0 0 1 2*a a -a a;
    1 0 0 0 -a a a 2*a;
    0 -1 0 0 a a 2*a -a];
G=transpose(G1);
G=reshape(G,64,1);
mat1=ones(1,N/4);
G=kron(G,mat1);
G=reshape(G,8,2*N);
y=kron(y,[1;1]);
y(1,:)=real(y(1,:));
y(2,:)=imag(y(2,:));
y=reshape(y,8,N/4);
y=kron(y,[1 1 1 1 1 1 1 1]);
out=reshape(transpose(sum(G.*y,1)),8,N/4);
h11 = 1/sqrt(2)*(randn(1,N/4) + 1i*randn(1,N/4));
h12 = 1/sqrt(2)*(randn(1,N/4) + 1i*randn(1,N/4));
h21 = 1/sqrt(2)*(randn(1,N/4) + 1i*randn(1,N/4));
h22 = 1/sqrt(2)*(randn(1,N/4) + 1i*randn(1,N/4));
h=reshape([h11;h12;h21;h22],4,N/4);
h1=reshape(h,2,N/2);
h2=zeros(2,N);
h2(:,(1:2:end))=h1;
h2(:,(2:2:end))=h1;
h3(1,:)=h2(1,:);
h3(2,:)=h2(1,:);
h3(3,:)=h2(2,:);
h3(4,:)=h2(2,:);
h4=zeros(4,2*N);
h4(:,(1:8:end))=h3(:,(1:4:end));
h4(:,(5:8:end))=h3(:,(1:4:end));
h4(:,(2:8:end))=h3(:,(2:4:end));
h4(:,(6:8:end))=h3(:,(2:4:end));
h4(:,(3:8:end))=h3(:,(3:4:end));
h4(:,(7:8:end))=h3(:,(3:4:end));
h4(:,(4:8:end))=h3(:,(4:4:end));
h4(:,(8:8:end))=h3(:,(4:4:end));
h4=[h4;h4];
mat=[1 1 1 1 0 0 0 0;
     1 1 1 1 0 0 0 0;
     1 1 1 1 0 0 0 0;
     1 1 1 1 0 0 0 0;
     0 0 0 0 1 1 1 1;
     0 0 0 0 1 1 1 1;
     0 0 0 0 1 1 1 1;
     0 0 0 0 1 1 1 1;];
mat = repmat(mat,[1 ,N/4]);
H=mat.*h4;
H(1,(1:8:end))= real(H(1,(1:8:end)));H(2,(2:8:end))= real(H(2,(2:8:end)));
H(3,(3:8:end))= real(H(3,(3:8:end)));H(4,(4:8:end))= real(H(4,(4:8:end)));
H(5,(5:8:end))= real(H(5,(5:8:end)));H(6,(6:8:end))= real(H(6,(6:8:end)));
H(7,(7:8:end))= real(H(7,(7:8:end)));H(8,(8:8:end))= real(H(8,(8:8:end)));
H(2,(1:8:end))=real(1i*H(2,(1:8:end)));H(2,(3:8:end))=real(1i*H(2,(3:8:end)));
H(4,(1:8:end))=real(1i*H(4,(1:8:end)));H(4,(3:8:end))=real(1i*H(4,(3:8:end)));
H(6,(5:8:end))=real(1i*H(6,(5:8:end)));H(6,(7:8:end))=real(1i*H(6,(7:8:end)));
H(8,(5:8:end))=real(1i*H(8,(5:8:end)));H(8,(7:8:end))=real(1i*H(8,(7:8:end)));
H(3,(1:8:end))=real(H(3,(1:8:end)));H(4,(2:8:end))=real(H(4,(2:8:end)));
H(1,(3:8:end))=real(H(1,(3:8:end)));H(2,(4:8:end))=real(H(2,(4:8:end)));
H(5,(7:8:end))=real(H(5,(7:8:end)));H(6,(8:8:end))=real(H(6,(8:8:end)));
H(7,(5:8:end))=real(H(7,(5:8:end)));H(8,(6:8:end))=real(H(8,(6:8:end)));
H(1,(2:8:end))=real(1i*(-H(1,(2:8:end))));H(1,(4:8:end))=real(1i*(-H(1,(4:8:end))));
H(3,(2:8:end))=real(1i*(-H(3,(2:8:end))));H(3,(4:8:end))=real(1i*(-H(3,(4:8:end))));
H(5,(6:8:end))=real(1i*(-H(5,(6:8:end))));H(5,(8:8:end))=real(1i*(-H(5,(8:8:end))));
H(7,(6:8:end))=real(1i*(-H(7,(6:8:end))));H(7,(8:8:end))=real(1i*(-H(7,(8:8:end))));
X=kron(out,[1 1 1 1 1 1 1 1]);
yout=reshape(sum(H.*X,1),8,N/4);
snr=linspace(0,16,17);
ser=zeros(1,length(snr));
for jj=1:length(snr)
    ynoisy=awgn(yout,snr(jj),'measured');
    ynoisy1=reshape(ynoisy,8,1,N/4);
    xoutput=[];
    ydemod=[];
    for kk=1:N/4
    %%%%%%%%%%%%%%% Forming CHannel Matrix %%%%%%%%%%%%%%%%
        H11 = h11(kk);  
        H12 = h12(kk);
        H21 = h21(kk);
        H22 = h22(kk);
        H=[ real(H11) -imag(H11) real(H12) -imag(H12) 0 0 0 0;
            imag(H11)  real(H11) imag(H12)  real(H12) 0 0 0 0;
            real(H21) -imag(H21) real(H22) -imag(H22) 0 0 0 0;
            imag(H21)  real(H21) imag(H22)  real(H22) 0 0 0 0;
            0 0 0 0 real(H11) -imag(H11) real(H12) -imag(H12);
            0 0 0 0 imag(H11)  real(H11) imag(H12)  real(H12);
            0 0 0 0 real(H21) -imag(H21) real(H22) -imag(H22);
            0 0 0 0 imag(H21)  real(H21) imag(H22)  real(H22);];
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        BChannel=H*G1;
        [Q R]=qr(BChannel);
        ydash=transpose(Q)*ynoisy1(:,:,kk);
        i=8;c0=5;dc=c0;
        T=zeros(1,8);x=zeros(1,8);
        B=zeros(1,8);A=zeros(1,8);
        E=zeros(1,8);
        ii=2;
        path=[];
        came=0;
    %%%%%%%%%%%%%%%%%%%%%%% Sphere Decoder %%%%%%%%%%%%%%%%%%%%%%
    while ii<7
            if ii==2
                if dc<T(i)
                    ii=4;  
                else
                    A(i)=max(0,ceil(((1/R(i,i))*(ydash(i)-E(i)-sqrt(dc-T(i)))+1)/2));
                    B(i)=min(1,floor(((1/R(i,i))*(ydash(i)-E(i)+sqrt(dc-T(i)))+1)/2));
                    x(i)=A(i)-1;
                    ii=3;
                end
            end
            if ii==3   
                x(i)=x(i)+1;
                if x(i)<=B(i)
                    ii=5;
                else
                    ii=4;
                end
            end
            if ii==4
                if i==8
                    ii=7;
                    if came==0
                        dc=dc+5;
                        ii=2;
                    end
                else
                    i=i+1;
                    ii=3;
                end
            end
            if ii==5
                if i>1
                    T(i-1)=0;
                    E(i-1)=0;
                    for j=i:8
                        E(i-1)=R(i-1,j)*(2*x(j)-1)+E(i-1);
                    end
                    T(i-1)=T(i)+(ydash(i)-E(i)-R(i,i)*(2*x(i)-1))^2;
                    i=i-1;
                    ii=2;
                else
                    ii=6;
                end
            end
            if ii==6
                dcap=T(1)+(ydash(1)-E(1)-R(1,1)*(2*x(1)-1))^2;
                if dcap<dc
                    dc=dcap;
                    xcap=2*x-1;
                    came=1;
                    for k=1:8
                        B(k)=min(1,floor((1/R(k,k))*(ydash(k)-E(k)+sqrt(dc-T(k)))));
                    end
                    ii=3;
                else
                    ii=7;
                end
            end
        end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        xcap=reshape(xcap,2,4);
        recvd=xcap(1,:)+1i*xcap(2,:);
        xdemod=qamdemod(recvd,M);
        xoutput=[xoutput,xdemod];
    end
    [num ty]=symerr(xoutput,Xin);
    ser(jj)=ty;
end
semilogy(snr,ser,'r-*');
grid on;hold on;
title('Silver Code nTx=2, nRx=2, (4-QAM)','Color','k','FontSize',12);
legend('sim (nTx=2, nRx=2, (4-QAM)) Using Sphere Decoder','location','southwest');
xlabel('SNR(dB) ---->','Color','k','FontSize',11);Ylabel('Symbol Errorrate ---->','Color','k','FontSize',11);