% Various routines that simulate or compute aspects of digital communication systems
%
% Simulate matched filter receiver 接收端
figure(1);
N = 100;
%每个bit采样N=100次
noise_amp = 3;
signal_set = 'bpsk2'; % either 'bpsk1', 'bpsk2', 'ask', or 'fsk'
bits = ['1', '0', '1', '0', '0', '1'];
%
if strcmp(signal_set, 'bpsk1')
signal1 = ones(1,N);
signal0 = -signal1;
elseif strcmp(signal_set, 'bpsk2')
signal1 = sqrt(2)*sin(2*pi*2*[0:N-1]/N);
signal0 = -signal1;
%载波幅值为sqrt(2),载波中心频率为2, signal0和signal1为采样点的值(每个bit采样N=100次)
elseif strcmp(signal_set,'fsk')
signal1 = sqrt(2)*sin(2*pi*2*[0:N-1]/N);
signal0 = sqrt(2)*sin(2*pi*3*[0:N-1]/N);
elseif strcmp(signal_set, 'ask');
signal1 = ones(1,N);
signal0 = zeros(1,N);
else
perror(sprintf('Unknown signal set %s\n',signal_set));
end
color0='r';color1='b';
x = []; xcolor = [];
%disp(length(bits));
for n=1:length(bits)
x=[x eval(strcat('signal',bits(n)))]; % 0/1 二进制
xcolor = [xcolor eval(strcat('color',bits(n)))];
% disp(x);
% disp(xcolor)
end
% Send signal through white noise channel
r=x
%r = x + noise_amp*randn(1,length(x)); %信道噪声
% Run matched filters,利用匹配滤波器进行解调
y1=filter(signal1(N:-1:1),1,r);
%求通过滤波器的信号
y0=filter(signal0(N:-1:1),1,r);
% Graphics
subplot(211)
t=[0:length(r)-1];
plot(t,r,'k');hold on
a = axis;
xp=x*(0.75*max(abs([a(3) a(4)])/max(x)));
for n=1:length(bits)
plot(t((n-1)*N+1:n*N),xp((n-1)*N+1:n*N),[xcolor(n) '--']);
h = text((n-1)*N+N/2,max(xp),bits(n));
set(h,'fontsize',16);set(h,'color',xcolor(n));
end
%for n=N*[1:length(bits)],h=line([n n],a(3:4));set(h,'linestyle','--');end
h=title('Received signal');set(h,'fontsize',18);
hold off
subplot(212)
plot(t,y0,color0,t,y1,color1)
a = axis;
for n=1:length(bits)
if y1(n*N)>= y0(n*N)
h = text(n*N-10,.75*a(4),'1');
set(h,'fontsize',16);set(h,'color',color1);
if bits(n) == '0'
set(h,'fontweight','bold');
end
else
h = text(n*N-10,.75*a(4),'0');
set(h,'fontsize',16);set(h,'color',color0);
if bits(n) == '1'
set(h,'fontweight','bold');
end
end
end
for n=N*[1:length(bits)],h=line([n n],a(3:4));set(h,'linestyle','--');end
h=title('Matched Filter Output');set(h,'fontsize',18);
%figure2是接收端的误码率
% Compute Pr[e] curves 计算误码率
%
figure(2)
snrdb = [-10:.5:12];
snr = 10.^(snrdb/10);
p_bpsk = Qfunct(sqrt(2*snr));
%需要编写Q函数 Qfunct()
p_fsk = Qfunct(sqrt(snr));
h=semilogy(snrdb,p_bpsk,snrdb,p_fsk,'r--');grid;axis([-10 12 10^(-8) 1])
set(gca,'fontsize',18);
h=xlabel('Signal-to-Noise Ratio (dB)');set(h,'fontsize',18);
h=ylabel('Bit Error Probability');set(h,'fontsize',18);
legend('BPSK','FSK');
评论0