clear;
clf;
%%%%%%%%%%%%%%%%%%%%%%%%% Parameters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Rs=1200; % Symbol rate
Ts=1/Rs; % Symbol interval
T=50; % oversample rate = number of samples in a symbol
fs=Rs*T; % sampling rate
ts=1/fs;
N=30; % Length of the seq.
n_range=(0:N*T-1);
t=n_range*ts;
%%%%%%%%%%%%%%%%%%%%%%%%% Baseband signaling %%%%%%%%%%%%%%%%%%%%%%%%
bnOri=unifrnd(-1,+1,1,N)>0;
bn1ri=unifrnd(-1,+1,1,N)>0;
gTpulse=ones(1,T); % NRZ
n_dly=n_range+T/2+1;
B0=1.0*Rs;
OriImpulses=upsample(bnOri,T,T/2); % build the impluse train
mOri=conv(OriImpulses,gTpulse); % generate baseband (PAM) signal
mOri=mOri(n_dly);
m0=2.*mOri-1;
riImpulses=upsample(bn1ri,T,T/2); % build the impluse train
m1ri=conv(riImpulses,gTpulse); % generate baseband (PAM) signal
m1ri=m1ri(n_dly); % time alignment
m1=2.*m1ri-1;
%%%%%%%%%%%%%%%%%%%%%%%%% Bandpass signaling %%%%%%%%%%%%%%%%%%%%%%%%
fc=1800;
s1=(1/sqrt(2)).*m1.*cos(2*pi*fc*t);
s0=(1/sqrt(2)).*m0.*sin(2*pi*fc*t);
s=s1-s0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% plot %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sRng=[0,N*(T-1)*ts, -1.5,+1.5];
figure(1);
subplot(411),plot(t,m0,'r',t,s0,'b'); axis(sRng);
figure(2);
subplot(411),plot(t,m1,'r',t,s1,'b'); axis(sRng);
figure(3);
subplot(411),plot(t,s,'g'); axis(sRng);
%%%%%%%%%%%%%%%%%%%%%%%%% AWGN Channel %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N0=10^2.225/10^7;
sigma=sqrt(N0*fs/2);
w=normrnd(0,sigma,[1,N*T]); % 正态分布随机数
Es=sum(s.^2)*ts/N;
SNR=10*log10(Es/N0);
Baud_B0_EbN0=[Rs,B0,SNR]
%%%%%%%%%%%%%%%%%%%%%%%%%%% Receiver %%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%% BFP %%%%%%%%%%%%%%%%%%%%%%%%%%%
h1BPF=fir1(100,[fc-1200,fc+1200]/(fs/2)); % 考虑101阶
r1=filter(h1BPF,1,s); % BPF for f1
r0=filter(h1BPF,1,s); % BPF for f0
%%%%%%%%%%%%%%%%%%%%%%%%%%% LFP %%%%%%%%%%%%%%%%%%%%%%%%%%%
hLPF=fir1(100,[B0]/(fs/2)); %考虑101阶
r1_posi=r1.*cos(2*pi*fc*t);
r1_out=filter(hLPF,1,r1_posi);
r0_posi=r0.*cos(2*pi*fc*t);
r0_out=filter(hLPF,1,r0_posi);
r_out=r1_out.*2+r0_out;
sRng=[0,N*(T-1)*ts, -1.2,+1.2];
subplot(414),plot(t,r1_posi,'r',t,r1_out,'b',t,r_out,'k'); axis(sRng);
%%%%%%%%%%%%%%%%%%%%%%%%%%% PSD %%%%%%%%%%%%%%%%%%%%%%%%%%%%
Nfft=length(t);Nfft=2^ceil(log2(Nfft)+2);
df=fs/Nfft;
fk=(-Nfft/2:Nfft/2-1)*df;
Rxx=xcorr(s)/N;
sOri1_f=ts*fftshift(fft(Rxx,Nfft));
Rxx=xcorr(s)/N;
sOri0_f=ts*fftshift(fft(Rxx,Nfft));
Rxx=xcorr(s)/N;
sOri_f=ts*fftshift(fft(Rxx,Nfft));
Rxx=xcorr(s)/N;
sAns_f=ts*fftshift(fft(Rxx,Nfft));
Rxx=xcorr(s)/N;
s_f=ts*fftshift(fft(Rxx,Nfft));
figure(4);
f_range=[0,5000,-40,-10];
subplot(411),plot(fk,10*log10(abs(sOri1_f)),'b',fk,10*log10(abs(sOri0_f)),'r');axis(f_range);
subplot(412),plot(fk,10*log10(abs(sOri_f)));axis(f_range);
f_range=[0,5000,-40,-20];
subplot(413),plot(fk,10*log10(abs(sOri_f)),'b',fk,10*log10(abs(sAns_f)),'r');axis(f_range);
subplot(414),plot(fk,10*log10(abs(s_f)));axis(f_range);