%qpsk.m
t=0:1/1000:1;
E=2;
Ts=1;
nc=4;
fc=nc/Ts;
TX=[];
BINSEQ=abs(round(rand(1,200)*2-1));
phi1=sqrt(2*E/Ts)*cos(2*pi*fc*t);
phi2=sqrt(2*E/Ts)*sin(2*pi*fc*t);
n=1;
for m=1:1:100
if(((BINSEQ(n)==0)& (BINSEQ(n+1)==0)))
TX=[TX -1*sqrt(1/2)*phi1-sqrt(1/2)*phi2];
n=n+2;
hold on
plot([(m-1)*1001+1:1:(m-1)*1001+1001],-1*sqrt(1/2)*phi1-1*sqrt(1/2)*phi2 ,'r')
elseif(((BINSEQ(n)==0)& (BINSEQ(n+1)==1)))
TX=[TX -1*sqrt(1/2)*phi1+sqrt(1/2)*phi2];
n=n+2;
hold on
plot([(m-1)*1001+1:1:(m-1)*1001+1001],-1*sqrt(1/2)*phi1+1*sqrt(1/2)*phi2 ,'b')
elseif(((BINSEQ(n)==1)& (BINSEQ(n+1)==0)))
TX=[TX 1*sqrt(1/2)*phi1-sqrt(1/2)*phi2];
n=n+2;
hold on
plot([(m-1)*1001+1:1:(m-1)*1001+1001],1*sqrt(1/2)*phi1-1*sqrt(1/2)*phi2 ,'g')
else
TX=[TX 1*sqrt(1/2)*phi1+sqrt(1/2)*phi2];
n=n+2;
hold on
plot([(m-1)*1001+1:1:(m-1)*1001+1001],1*sqrt(1/2)*phi1+sqrt(1/2)*phi2,'m')
end
end
%Adding noise to the band pass signal
RX=TX+sqrt(40)*randn(1,length(TX));
figure
subplot(2,1,1)
plot(TX)
title('QPSK signal')
subplot(2,1,2)
plot(RX)
title('QPSK with noise ')
%Coherent detection
LO1=sqrt(2*E/Ts)*cos(2*pi*fc*t);
LO2=sqrt(2*E/Ts)*sin(2*pi*fc*t);
BINSEQDET=[];
COROUT1=[];
COROUT2=[];
for n=1:1:100
temp=RX([(n-1)*1001+1:1:(n-1)*1001+1001]);
S1=sum(temp.*LO1);
S2=sum(temp.*LO2);
COROUT1=[COROUT1 S1];
COROUT2=[COROUT2 S2];
if((S1<0)&(S2<0))
BINSEQDET=[BINSEQDET 0 0];
elseif ((S1<0)&(S2>0))
BINSEQDET=[BINSEQDET 0 1];
elseif ((S1>0)&(S2<0))
BINSEQDET=[BINSEQDET 1 0];
else
BINSEQDET=[BINSEQDET 1 1];
end
end
figure
scatter(COROUT1,COROUT2)
figure
subplot(2,2,1)
stem(COROUT1)
subplot(2,2,2)
stem(COROUT2)
subplot(2,2,3)
stem(BINSEQ)
subplot(2,2,4)
stem(BINSEQDET)


