info = randi([0,1],1,10000); % 产生待发送的二进制序列
info_map = info*2-1; % 转化成双极码
sm_rect = rectpulse(info_map,80); % 矩形脉冲
t1 = (0:length(sm_rect)-1)*ts;
sc1 = cos(2*pi*fc*t1); % 高频载波
s_bpsk_rect=sm_rect.*sc1;
axis([0.02 0.08 -1.5 1.5]);
axis([0.02 0.08 -1.5 1.5]);
axis([0.02 0.08 -1.5 1.5]);
wc_bpf = [2*pi*(fc-Rb)/fs, 2*pi*(fc+Rb)/fs];
bpf_hn = fir1(256, wc_bpf/pi); % 带通滤波器设计
[h,w]=freqz(bpf_hn, 1, 4096);
plot(w*fs/(2*pi),20*log10(abs(h)),'r');
axis([400 1600 -120 10]);
s_bpsk_bpf = filter(bpf_hn, 1, s_bpsk_rect); % 滤波
axis([0.02 0.08 -1.5 1.5]);
fft_n = 2^nextpow2(length(s_bpsk_bpf));
SPSK_f = fftshift(fft(s_bpsk_rect,fft_n));
fHz=(-fft_n/2: fft_n/2-1)/fft_n*fs;
% axis([400 1600 0 800]);
SPSK_bpf_f = fftshift(fft(s_bpsk_bpf,fft_n));
fHz=(-fft_n/2: fft_n/2-1)/fft_n*fs;
plot(fHz,abs(SPSK_bpf_f));
% axis([400 1600 0 800]);
title('经过带通滤波器的BPSK信号频率特性');
s_mul = 2*s_bpsk_rect.*sc1
2.0000 1.0000 0.0000 1.0000 2.0000 1.0000 0.0000 1.0000 2.0000 1.0000 0.0000 1.0000 2.0000 1.0000 0.0000 1.0000 2.0000 1.0000 0.0000 1.0000 2.0000 1.0000 0.0000 1.0000 2.0000 1.0000 0.0000 1.0000 2.0000 1.0000 0.0000 1.0000 2.0000 1.0000 0.0000 1.0000 2.0000 1.0000 0.0000 1.0000 2.0000 1.0000 0.0000 1.0000 2.0000 1.0000 0.0000 1.0000 2.0000 1.0000
lbf_hn = fir1(32,wc/pi); % 低通滤波器
[h,w]=freqz(lbf_hn, 1, 4096);
plot(w*fs/(2*pi),20*log10(abs(h)),'r');
s_lpf = filter(lbf_hn, 1, s_mul); % 低通滤波
plot(t1(1:1200),sm_rect(1:1200)/2,'-r','LineWidth',2);
plot(t1(1:1200),s_lpf(1:1200),'-b');
% --------QPSK Constellations
x_qpsk = randi([0,1],1,10000)*2-1 + 1i*(randi([0,1],1,10000)*2-1); % case 1
line([0 0],[-3 3],'Color','r');
line([-3 3],[0 0],'Color','r');
%% 请分别实现信噪比为30dB、10dB、0dB星座图
x_qpsk = awgn(x_qpsk, SNR);
line([0 0],[-3 3],'Color','r');
line([-3 3],[0 0],'Color','r');
x_qpsk = awgn(x_qpsk, SNR);
line([0 0],[-3 3],'Color','r');
line([-3 3],[0 0],'Color','r');
x_qpsk = awgn(x_qpsk, SNR);
line([0 0],[-3 3],'Color','r');
line([-3 3],[0 0],'Color','r');
% 实验扩展2、试比较和分析原始基带信号与解调信号的时频域波形的差异性。
plot(t1(1:1200), sm_rect(1:1200)/2);
% axis([0 0.15 -0.8 0.8]);
[f, fs] = T2F(t1, sm_rect);
plot(t1(1:1200), s_lpf(1:1200), '-b');
% axis([0 0.15 -0.8 0.8]);
[f, fs] = T2F(t1, s_lpf);
% 3、仿真QPSK系统的误码率与信噪比关系曲线,并与理论结果进行对比,
% 分析随着信噪比增大,QPSK系统的误码率如何变化?
snr = 10 .^ (snr_dB / 10);
delt_fa = 10 .^ (-snr_dB / 10);
Pe = zeros(1, length(snr_dB));
for iter = 1:length(snr_dB)
fa_bit = randi([0 1], [1 N]);
fa_key = randi([0 1], [1 N]);
fa_enc = bitxor(fa_bit, fa_key);
n = delt(iter)*(randn(1,N) + sqrt(-1)*randn(1,N))/sqrt(2);
es_bit = (1 + es_fa) / 2;
de_enc = bitxor(es_bit, fa_key);
Pe(iter) = sum(fa_bit ~= de_enc) / N;
theory_Pe = erfc(sqrt(snr)) / 2;
semilogy(snr_dB, Pe, 'r-o', snr_dB, theory_Pe, '*-b');
legend('BPSK仿真误码率', 'BPSK理论误码率');