📄 bpskfig.m
字号:
clear all;
close all;
%% Setup
% Define parameters.
M = 2; % Size of signal constellation
k = log2(M); % Number of bits per symbol
n = 10; % Number of bits to process
nk = n/k; % Symbol Number
nsamp = 1; % Oversampling rate
samp = 16; % for bandpass cos, sin signal
srs = sqrt(samp); % square root of bandpass sample
%% Signal Source
% Generate a binary data stream
AA = randint(n,1);
%Plot first 10 bits in a stem plot.
figure;
stem(AA(1:10));
title('Random Bits');
xlabel('Bit Index');
ylabel('Binary Value');
axis([0 10 -1 2]);
%% Modulation
BB = pskmod(AA,M);
figure;
stem([1:1:10],BB(1:10));
title('BPSK Modulator Output Symbols');
xlabel('Symbol Index');
ylabel('Integer Value');
%% Bandpass signal
pc = [0:2*pi/samp:2*pi*(1-1/samp)];
tt = BB * (cos(pc) + j*sin(pc))/(srs);
lBB = length(BB);
BB = reshape(tt,1,lBB * samp);
%% Transmitted Signal
% Nyqusit Filter
Delay = 3; DataL = lBB*samp; R = .5; Fs = 8; Fd = 1; PropD = 0;
[BBs, ts] = rcosine(Fd, Fs, 'fir/sqrt', R, Delay);
[BBc, tc] = rcosflt(BB, Fd, Fs, 'filter', BBs);
BBtx = BBc;
tcc = tc + Delay .* Fd;
PropD = Delay * Fd;
tx = [PropD: PropD + samp*lBB - 1] ./ Fd;
txx = tx + Delay .* Fd;
figure;
stem(txx, BB, 'kx'); hold on;% Plot filtered data.
plot(tc, BBc, 'm-'); hold off;% Set axes and labels.
title('Square Root Nyquist filtered output');
axis([0 samp*n -2 2]); xlabel('Time'); ylabel('Amplitude');
%% Channel
% Add AWGN Noise.
EbNo = 3; % In dB
BBn = awgn(BBtx,EbNo,'measured');
%% Received Signal
[BBr, tr] = rcosflt(BBn, Fd, Fs, 'filter/Fs', BBs);% with noise
[BBr1, tr1] = rcosflt(BBtx, Fd, Fs, 'filter/Fs', BBs); % without noise
trr = BBr(2*Delay*Fs+1:Fs:(2*Delay+lBB*samp)*Fs);
BBrt = reshape(trr, samp, lBB);
BBrx = (cos(pc) - j*sin(pc)) * BBrt/(srs);
%% Figure plot
%plot nyquist filter
figure;
hh = rcosflt(3, 1, 8);
freqz(hh,1)
axis([0 1 -100 15]);
%without noise
figure;
stem(txx, BB, 'kx'); hold on;% Plot filtered data.
plot(tr1, BBr1, 'b-'); hold off;% Set axes and labels.
title('Received Input without noise');
axis([0 samp*n -2 2]); xlabel('Time'); ylabel('Amplitude');
%with noise
figure,
stem(txx, BB, 'kx'); hold on;% Plot filtered data.
plot(tr, BBr, 'b-'); hold off;% Set axes and labels.
title('Received Input with noise');
axis([0 samp*n -2 2]); xlabel('Time'); ylabel('Amplitude');
figure;
stem(txx, BB, 'kx'); hold on;
stem(trr, 'bo'); hold on;% Set axes and labels.
title('Demodulated Signal Waveform before Sampling');
axis([0 samp*n -2 2]); xlabel('Time'); ylabel('Amplitude');
%% Demodulation
CCrx = BBrx;
CC = pskdemod(CCrx,M);
figure;
stem(CC);
title('BPSK DeModulated Symbols');
xlabel('Symbol Index'); ylabel('Integer Value');
%% Eye pattern for Rxer
% Truncate the output of rcosflt to remove response tails.
N = Fs/Fd;
propdelay = Delay .* N + 1; % Propagation delay of filter
TRC = rcosflt(BBn, Fd, Fs, 'filter/Fs', BBs);
TRC1 = TRC(propdelay:end-(propdelay-1),:); % Truncated version
% Plot the eye diagram of the resulting sampled signal
offset1 = 0;
h1 = eyediagram(TRC1,N,1/Fd,offset1);
set(h1,'Name','Eye Diagram Displayed with No Offset');
for j=1:10;
if AA(j)==1;
forNRZ(j)=1;
elseif AA(j)==0;
forNRZ(j)=-1;
end
end
as=0:0.01:9.99;
for mm=1:length(as);
NRZ(mm)=forNRZ(fix((mm-1)/100)+1);
end
for j=1:10;
if AA(j)==1;
forNRZ(j)=1;
elseif AA(j)==0;
forNRZ(j)=-1;
end
end
as=0:0.01:9.99;
for mm=1:length(as);
NRZ(mm)=forNRZ(fix((mm-1)/100)+1);
end
for mm=1:length(as);
forbpsk(mm)=AA(fix((mm-1)/100)+1);
end
bpsk=pskmod(forbpsk, 2, 0, 'gray');
for mm=1:1000;
bpsk(mm)=bpsk(mm).*cos(2*pi*as(mm));
end
figure;
plot(as, real(bpsk));
title('BPSK modulated signals');
axis([0 10 -4 4]);
xlabel('time');
ylabel('magnitude');
grid on;
figure;
plot(as, NRZ);
title('NRZ form of random symbols');
axis([0 10 -4 4]);
xlabel('time');
ylabel('magnitude');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -