clear;
clc;
%%set the initial value...
fd=65536;
fs=262144;
fc=262144;
f=1048576;
data_len=1200000;
sym_len=data_len/3;
A=100;
%% constellation value...
for theta=1:8
QI(1,theta)=cos((theta-1)*pi/4);
QI(2,theta)=sin((theta-1)*pi/4);
end
flt=rcosine(fd,fs,'sqrt',0.25);
seq=randerr(1,data_len,data_len/2);
%% P/S
seq_ind=1:3:data_len;
dec_seq=seq(seq_ind)*4+seq(seq_ind+1)*2+seq(seq_ind+2)+1;
seq_Q(1:sym_len)=ones(1,sym_len).*QI(1,dec_seq(1:sym_len));
seq_I(1:sym_len)=ones(1,sym_len).*QI(2,dec_seq(1:sym_len));
%%Modeling...
R=fs/fd;
up_seq_Q=upsample(seq_Q,R);
up_seq_I=upsample(seq_I,R);
rcos_Q=conv(up_seq_Q,flt);
rcos_I=conv(up_seq_I,flt);
rcos_Q_up=interp(rcos_Q,f/fs);
rcos_I_up=interp(rcos_I,f/fs);
t=0:length(rcos_I_up)-1;
high_freq_Q=rcos_Q_up .* cos(2*pi*fc*t/f);
high_freq_I=rcos_I_up .* sin(2*pi*fc*t/f);
transmit=high_freq_Q+high_freq_I;
figure;
%plot(transmit(1:1000:1200000));%显示发送信号的时域波形
%plot(abs(fft(transmit(1:100000))));%显示频谱
%% AWGN channe
SNR=10;
inoise=randn(1,length(transmit)).*sqrt(0.5*sum(transmit.*transmit)/length(transmit)*10.^(-SNR/10));
receive=transmit+inoise;
rc_length=length(receive);
t=0:rc_length-1;
rc_Q=receive .* cos(2*pi*fc*t/f);
rc_I=receive .* sin(2*pi*fc*t/f);
down_Q=downsample(rc_Q,f/fs);
down_I=downsample([0 rc_I],f/fs);
low_Q_rcos=conv(down_Q,flt);
low_I_rcos=conv(down_I,flt);
delay=3*R*2;
rc_Q_seq=(downsample(low_Q_rcos(delay+1:end-delay-1),R));
rc_I_seq=(downsample(low_I_rcos(delay+1:end-delay-1),R));
theta=(rc_Q_seq<0)*pi+((rc_Q_seq>=0).*(rc_I_seq<0))*2*pi+atan(rc_I_seq./rc_Q_seq);
dec=floor(theta/(pi/8));
b1=floor(dec/4);
b2=floor((dec-b1*8)/2);
b3=floor((dec-b1*4-b2*2));
decode=reshape([b1;b2;b3],1,data_len);
%huitu
plot(seq_Q,seq_I,'*r');
axis([-3 3 -3 3]);
hold;
plot([-5:5],zeros(1,11));
xlabel('Q'),ylabel('I');
title('发送星座图');
figure;
plot(rc_Q_seq,rc_I_seq,'.r');
axis([-3 3 -3 3]);
hold;
plot([-5:5],zeros(1,11));
xlabel('Q'),ylabel('I');
title('接收星座图');
%Pe
error=sum(abs(decode-seq));
errorrate=error/data_len;
fprintf('误比特数\t\t%d\t\n',error);
fprintf('误比特率\t\t%e\t\n',errorrate);