以下是老师提供的代码:
%% BPSK
clear;
close all;
% 仿真参数
fc = 1000;
Rb = 100;
fs = 8000;
ts = 1/fs;
% 基带信号生成
info = randi([0,1],1,10000); % 产生待发送的二进制序列
info_map = info*2-1; % 转化成双极码
sm_rect = rectpulse(info_map,80); % 矩形脉冲
% BPSK 信号生成
t1 = (0:length(sm_rect)-1)*ts;
sc1 = cos(2*pi*fc*t1); % 高频载波
%% BPSK 信号生成
s_bpsk_rect=sm_rect.*sc1;
% Time-domain Waveforms
figure();
subplot(3,1,1);
plot(t1,sc1);
title('高频载波 ');
xlabel('时间 (s)');
axis([0.02 0.08 -1.5 1.5]);
subplot(3,1,2);
plot(t1,sm_rect);
title('矩形脉冲基带信号');
xlabel('时间 (s)');
axis([0.02 0.08 -1.5 1.5]);
subplot(3,1,3);
plot(t1,s_bpsk_rect);
title('BPSK调制信号');
xlabel('时间(s)');
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); % 带通滤波器设计
figure();
[h,w]=freqz(bpf_hn, 1, 4096);
subplot(2,1,1);
plot(w*fs/(2*pi),20*log10(abs(h)),'r');
title('BPF频率响应');
xlabel('频率 (Hz)');
axis([400 1600 -120 10]);
grid on;
s_bpsk_bpf = filter(bpf_hn, 1, s_bpsk_rect); % 滤波
subplot(2,1,2);
plot(t1,s_bpsk_bpf);
title('经过带通滤波器的BPSK信号');
xlabel('Time (s)');
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));
figure();
fHz=(-fft_n/2: fft_n/2-1)/fft_n*fs;
subplot(2,1,1);
plot(fHz,abs(SPSK_f));
% axis([400 1600 0 800]);
% axis([0 fs/2 0 800]);
grid on;
title('BPSK信号频率特性');
xlabel('频率 (Hz)');
SPSK_bpf_f = fftshift(fft(s_bpsk_bpf,fft_n));
fHz=(-fft_n/2: fft_n/2-1)/fft_n*fs;
subplot(2,1,2);
plot(fHz,abs(SPSK_bpf_f));
% axis([400 1600 0 800]);
% axis([0 fs/2 0 800]);
grid on;
title('经过带通滤波器的BPSK信号频率特性');
xlabel('频率 (Hz)');
%% 相干解调原理
s_mul = 2*s_bpsk_rect.*sc1
s_mul = 1×800000
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
figure()
wc = 2*2*pi*Rb/fs;
lbf_hn = fir1(32,wc/pi); % 低通滤波器
[h,w]=freqz(lbf_hn, 1, 4096);
plot(w*fs/(2*pi),20*log10(abs(h)),'r');
title('低通滤波器频率响应');
xlabel('频率(Hz)');
axis([0 1500 -120 10]);
grid on;
s_lpf = filter(lbf_hn, 1, s_mul); % 低通滤波
figure();
subplot(2,1,1);
plot(t1(1:1200),sm_rect(1:1200)/2,'-r','LineWidth',2);
axis([0 0.15 -0.8 0.8]);
title('矩形脉冲基带信号');
xlabel('Time (s)');
grid on;
subplot(2,1,2);
plot(t1(1:1200),s_lpf(1:1200),'-b');
axis([0 0.15 -1.2 1.2]);
title('解调的基带信号');
xlabel('Time (s)');
grid on;
%% QPSK
% close all;
% clear;
% --------QPSK Constellations
% clear all;
% close all;
x_qpsk = randi([0,1],1,10000)*2-1 + 1i*(randi([0,1],1,10000)*2-1); % case 1
scatterplot(x_qpsk);
xlabel('I');
ylabel('Q');
title('无噪声星座');
line([0 0],[-3 3],'Color','r');
line([-3 3],[0 0],'Color','r');
axis([-2 2 -2 2]);
grid on;
%% 请分别实现信噪比为30dB、10dB、0dB星座图
% 30dB
SNR = 30;
x_qpsk = awgn(x_qpsk, SNR);
scatterplot(x_qpsk);
xlabel('I');
ylabel('Q');
title('Es/N0=30dB星座');
line([0 0],[-3 3],'Color','r');
line([-3 3],[0 0],'Color','r');
axis([-2 2 -2 2]);
grid on;
% 10dB
SNR = 10;
x_qpsk = awgn(x_qpsk, SNR);
scatterplot(x_qpsk);
xlabel('I');
ylabel('Q');
title('Es/N0=10dB星座');
line([0 0],[-3 3],'Color','r');
line([-3 3],[0 0],'Color','r');
axis([-2 2 -2 2]);
grid on;
% 0dB
SNR = 0;
x_qpsk = awgn(x_qpsk, SNR);
scatterplot(x_qpsk);
xlabel('I');
ylabel('Q');
title('Es/N0=0dB星座');
line([0 0],[-3 3],'Color','r');
line([-3 3],[0 0],'Color','r');
axis([-2 2 -2 2]);
grid on;
以下是老师没有提供的代码
% 实验扩展1、试用其他脉冲滤波器生成BPSK(如滚降脉冲),并画出其时频域波形以及分析其带宽。
% 没做
% 实验扩展2、试比较和分析原始基带信号与解调信号的时频域波形的差异性。
figure;
subplot(2,2,1);
plot(t1(1:1200), sm_rect(1:1200)/2);
% axis([0 0.15 -0.8 0.8]);
title('基带信号时域');
xlabel('Time (s)');
grid on;
subplot(2,2,2);
[f, fs] = T2F(t1, sm_rect);
plot(f, abs(fs));
title('基带信号频域');
xlabel('f (Hz)');
subplot(2,2,3);
plot(t1(1:1200), s_lpf(1:1200), '-b');
% axis([0 0.15 -0.8 0.8]);
title('解调信号时域');
xlabel('Time (s)');
grid on;
subplot(2,2,4);
[f, fs] = T2F(t1, s_lpf);
plot(f, abs(fs));
title('解调信号频域');
xlabel('f (Hz)');
% 3、仿真QPSK系统的误码率与信噪比关系曲线,并与理论结果进行对比,
% 分析随着信噪比增大,QPSK系统的误码率如何变化?
clc, clear, clf;
snr_dB = 1:10;
snr = 10 .^ (snr_dB / 10);
delt_fa = 10 .^ (-snr_dB / 10);
delt = sqrt(delt_fa);
Pe = zeros(1, length(snr_dB));
for iter = 1:length(snr_dB)
N = 100000;
fa_bit = randi([0 1], [1 N]);
fa_key = randi([0 1], [1 N]);
fa_enc = bitxor(fa_bit, fa_key);
m_s = 2 * fa_enc - 1;
me = mean(fa_key);
av = var(fa_key);
n = delt(iter)*(randn(1,N) + sqrt(-1)*randn(1,N))/sqrt(2);
r = m_s + n;
es_fa = sign(real(r));
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;
end
figure;
semilogy(snr_dB, Pe, 'r-o', snr_dB, theory_Pe, '*-b');
xlabel('信噪比SNR (dB) ');
ylabel('误码率BER');
title('误码率曲线 SNR')
legend('BPSK仿真误码率', 'BPSK理论误码率');