clear;clc;
close all;
%% 系统仿真参数
A = 1; %载波信号振幅
fc = 2; %载波信号频率
snr = 15; %信噪比
N_Sample = 8; %基带信号中每个码元的采样点数
N = 10000; %码元个数
Ts = 1; %码元宽度
df = 0.001; %频率分辨率
B = 1/Ts; %码元带宽
f_start = fc - B; %滤波器起始频率
f_cutoff = fc + B; %滤波器截止频率
fs = fc * N_Sample; %系统采样频率,即考虑载波后,一个码元内的采样点数
ts = Ts/fs; %系统采样间隔
t = 0:ts:N*Ts-ts;
Lt = length(t);
%% 调制信号及其功率谱密度
%% 单极性不归零信号
figure(1);
d = sign(randn(1,N));
dd = sigexpand((d+1)/2,fc*N_Sample);
gt = ones(1,fc*N_Sample);%NRZ波形
d_NRZ = conv(dd,gt);
d_NRZ1 = d_NRZ(1:Lt);
%% 双极性不归零信号
d_sjx = 2 * d_NRZ - 1;
d_sjx1 = d_sjx(1:Lt);
subplot(221);
plot(t,d_NRZ1);
axis([min(t),10,min(d_NRZ)-0.2,max(d_NRZ)+0.2]);
xlabel('t');title('单极性NRZ信号');
subplot(222);
[d_NRZ1f,d_NRZ1,df1,f] = T2F_new(d_NRZ1,ts,df,fs);
plot(f,10*log10(abs(fftshift(d_NRZ1f).^2/length(f))));
axis([-5*B,5*B,-50,0]);
xlabel('f');title('单极性NRZ信号功率谱密度');
%% 2FSK
s_2fsk = A * cos(2*pi*fc*t+2*pi*d_sjx(1:Lt).*t);%生成2FSK信号
subplot(223);
plot(t,s_2fsk);
axis([min(t),10,min(s_2fsk)-0.2,max(s_2fsk)+0.2]);
xlabel('t');title('2FSK信号');
subplot(224);
[s_2fskf,s_2fsk,df1,f] = T2F_new(s_2fsk,ts,df,fs);
plot(f,10*log10(abs(fftshift(s_2fskf).^2/length(f))));
axis([-fc-3*B,fc+3*B,-50,0]);
xlabel('f');title('2FSK信号功率谱密度');
%% 生成信道加性高斯白噪声
snr_lin = 10^(snr/10);
signal_energy = 0.5 * A^2 * Ts;%接收信号的平均能量
noise_power = (signal_energy * fs) / (snr_lin * 4);%求出噪声方差(噪声均值为0)
noise_std = sqrt(noise_power);%求出噪声均方差
noise = noise_std .* randn(1,Lt);%以噪声均方差作为幅度产生高斯白噪声
figure(2);
subplot(221);
plot(t,noise(1:Lt));
xlabel('t');title('信道噪声');
axis([min(t),10,min(noise)-1,max(noise)+1]);
[noisef,noise,df1,f] = T2F_new(noise,ts,df,fs);
subplot(222);
plot(f,10*log10(abs(fftshift(noisef).^2/length(f))));
axis([-fc-3*B,fc+3*B,-50,0]);
xlabel('f');title('信道噪声功率谱密度');
%% 将2FSK信号送入信道
r = s_2fsk(1:Lt) + noise(1:Lt);%叠加了噪声的已调信号,相当于将已调信号送入理想信道
subplot(223);
plot(t,r);%画出叠加噪声后的已调2FSK信号
xlabel('t');title('加燥2FSK信号');
axis([min(t),10,min(r),max(r)]);
[rf,r,df1,f] = T2F_new(r,ts,df,fs);
subplot(224);
plot(f,10*log10(abs(fftshift(rf).^2/length(f))));
axis([-fc-3*B,fc+3*B,-50,0]);
xlabel('f');title('加燥2FSK信号功率谱密度');
figure(3);
subplot(311);
plot(f,fftshift(abs(rf)));
xlabel('f');title('加燥2FSK信号频谱');
axis([-fc-2*B,fc+2*B,min(rf),max(rf)+10]);
%% 在接收端准备进行解调,先通过带通滤波器
%% 带通滤波器
%生成上下支路两路两个滤波器
%低频载频
[H_low,f_low] = bp_f(length(r),f_start-0.76*B,f_start+0.76*B,df1,fs,1);
subplot(323);
plot(f_low,fftshift(abs(H_low))); %画出带通滤波器
xlabel('f');title('下支路理想BPF');
axis([-4,4,min(H_low)-0.2,max(H_low)+0.2]);
%高频载频
[H_high,f_high] = bp_f(length(r),f_cutoff-0.76*B,f_cutoff+0.76*B,df1,fs,1);
subplot(324);
plot(f_high,fftshift(abs(H_high))); %画出带通滤波器
xlabel('f');title('上支路理想BPF');
axis([-4,4,min(H_high)-0.2,max(H_high)+0.2]);
subplot(325);
rf_lowf = H_low .* rf;
[rf_low] = F2T_new(rf_lowf,fs); %滤波器输出信号的波形
plot(t,rf_low(1:Lt));
axis([min(t),30,min(rf_low)-0.1,max(rf_low)+0.1]);
title('下支路信号');
subplot(326);
rf_highf = H_high .* rf;
[rf_high] = F2T_new(rf_highf,fs); %滤波器输出信号的波形
plot(t,rf_high(1:Lt));
axis([min(t),20,min(rf_high)-0.1,max(rf_high)+0.1]);
title('上支路信号');
%%--------------和本地载波相乘,即混频
%下支路混频信号
figure(4);
subplot(421);
f_low = 1;
f_high = 3;
wave_low = A * cos(2*pi*f_low*t);
plot(t,wave_low(1:Lt));
axis([min(t),10,min(wave_low)-0.2,max(wave_low)+0.2]);
xlabel('t');title('下支路载波');
subplot(423); %频谱载波
[wave_lowf,wave_low,df1,f] = T2F_new(wave_low,ts,df,fs);
plot(f,fftshift(abs(wave_lowf)/5)); %画出载波频谱
xlabel('f');title('下支路载波频谱');
axis([-fc-2*B,fc+2*B,0,6]);
%再画出混频后信号及其频谱
der_low = rf_low(1:Lt).*wave_low(1:Lt); %混频
subplot(425); %画出混频后的信号
plot(t,der_low);
axis([min(t),30,min(der_low)-0.2,max(der_low)+0.2]);
xlabel('t');title('混频后的信号');
subplot(427);
[der_lowf,der_low,df1,f] = T2F_new(der_low,ts,df,fs); %求出混频后的信号频谱
plot(f,10*log10(abs(fftshift(der_lowf).^2/length(f)))); %画出混频后的信号频谱
xlabel('f');title('混频后的信号功率谱密度');
axis([-fc-6*B,fc+6*B,-50,0]);
%% 上支路混频信号
subplot(422);
wave_high = A * cos(2*pi*f_high*t);
plot(t,wave_high(1:Lt));
axis([min(t),10,min(wave_high)-0.2,max(wave_high)+0.2]);
xlabel('t');title('上支路载波');
subplot(424); %频谱载波
[wave_highf,wave_high,df1,f] = T2F_new(wave_high,ts,df,fs);
plot(f,fftshift(abs(wave_highf)/5)); %画出功率谱密度
xlabel('f');title('上支路载波频谱');
axis([-fc-3*B,fc+3*B,0,6]);
%再画出混频后信号及其频谱
der_high = rf_high(1:Lt).*wave_high(1:Lt); %混频
subplot(426); %画出混频后的信号
plot(t,der_high);
axis([min(t),30,min(der_high)-0.2,max(der_high)+0.2]);
xlabel('t');title('混频后的信号');
subplot(428);
[der_highf,der_high,df1,f] = T2F_new(der_high,ts,df,fs); %求出混频后的信号频谱
plot(f,10*log10(abs(fftshift(der_highf).^2/length(f)))); %画出混频后的信号功率谱密度
xlabel('f');title('混频后信号功率谱密度');
axis([-fc-6*B,fc+6*B,-50,0]);
%% 低通滤波器
%-----------------经过低通滤波器
%画出理想低通滤波器
figure(5);
[LPF_low,f_low] = lp_f(length(der_high),B,df1,fs,1); %求出低通滤波器
subplot(321);
plot(f_low,fftshift(abs(LPF_low))); %画出理想低通滤波器
xlabel('f');title('理想LPF(下支路)');
axis([-f_start-1.5,f_start+1.5,min(LPF_low)-0.1,max(LPF_low)+0.1]);
[LPF_high,f_high] = lp_f(length(der_low),B,df1,fs,1); %求出低通滤波器
subplot(322);
plot(f_high,fftshift(abs(LPF_high))); %画出理想低通滤波器
xlabel('f');title('理想LPF(上支路)');
axis([-f_start-1.5,f_start+1.5,min(LPF_high)-0.1,max(LPF_high)+0.1]);
%% 经过理想低通滤波器后的信号
%混频信号经过理想低通滤波器后的频谱及波形
DM_lowf = LPF_low .* der_lowf; %理想低通滤波器输出的频谱
[dm_low] = F2T_new(DM_lowf,fs); %滤波器的输出波形
DM_highf = LPF_high .* der_highf;
[dm_high] = F2T_new(DM_highf,fs);
subplot(323);
plot(t,dm_low(1:Lt));
axis([min(t),30,min(dm_low)-0.2,max(dm_low)+0.1]);
xlabel('t');title('通过理想LPF后的信号(下支路)');
subplot(324);
plot(t,dm_high(1:Lt));
axis([min(t),30,min(dm_high)-0.2,max(dm_high)+0.1]);
xlabel('t');title('通过理想LPF后的信号(上支路)');
subplot(325);
[dm_lowf,dm_low,df1,f] = T2F_new(dm_low,ts,df,fs); %求出混频后的信号频谱
plot(f,10*log10(abs(fftshift(dm_lowf).^2/length(f)))); %画出混频后的信号功率谱密度
xlabel('f');title('信号功率谱密度(下支路)');
axis([-fc-3*B,fc+3*B,-50,0]);
subplot(326);
[dm_highf,dm_high,df1,f] = T2F_new(dm_high,ts,df,fs); %求出混频后的信号频谱
plot(f,10*log10(abs(fftshift(dm_highf).^2/length(f)))); %画出混频后的信号功率谱密度
xlabel('f');title('信号功率谱密度(上支路)');
axis([-fc-3*B,fc+3*B,-50,0]);
figure(6);
subplot(311);
plot(t,dm_low(1:Lt),'k-.');hold on;
plot(t,dm_high(1:Lt),'g-');
axis([min(t),20,min(dm_high)-0.2,max(dm_high)+0.2]);
xlabel('t');title('上下支路信号比较');
legend('下支路信号','上支路信号');
set(gca,'XTick',0:1:20);
set(gca,'XGrid','on');
panjue = zeros(1,N);%建立存储判决值的矩阵
for i = 1 : Lt
if dm_high(i) > dm_low(i)
panjue(i) = 1;
else
panjue(i) = 0;
end
end
subplot(312);
plot(t,panjue(1:Lt));
axis([min(t),20,min(panjue)-0.2,max(panjue)+0.2]);
xlabel('t');title('恢复信号');
set(gca,'XTick',0:1:20);
set(gca,