-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bpsk.m
156 lines (136 loc) · 3.33 KB
/
Bpsk.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
close all;
clear all;
clc;
% the reference carrier signal
ref=zeros(1,25);
for i=1:25
ref(i)=sin(2*pi*(i-1)/25);
end
%generator matrix for systematic (15,11) cyclic Hamming code
G=[1 0 0 0 0 0 0 0 0 0 0 0 1 1 1;
0 1 0 0 0 0 0 0 0 0 0 1 0 1 1;
0 0 1 0 0 0 0 0 0 0 0 1 1 0 1;
0 0 0 1 0 0 0 0 0 0 0 1 1 1 0;
0 0 0 0 1 0 0 0 0 0 0 1 1 1 1;
0 0 0 0 0 1 0 0 0 0 0 1 1 0 0;
0 0 0 0 0 0 1 0 0 0 0 1 0 1 0;
0 0 0 0 0 0 0 1 0 0 0 1 0 0 1;
0 0 0 0 0 0 0 0 1 0 0 0 1 1 0;
0 0 0 0 0 0 0 0 0 1 0 0 1 0 1;
0 0 0 0 0 0 0 0 0 0 1 0 0 1 1];
%parity check matrix
H=[1 1 1 0 1 0 0 1 0 1 1 0 0 0 1;
1 1 0 1 1 0 1 0 1 0 1 0 0 1 0;
1 0 1 1 1 1 0 0 1 1 0 0 1 0 0;
0 1 1 1 1 1 1 1 0 0 0 1 0 0 0];
S=[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15;
0 14 13 11 7 15 3 5 9 6 10 12 1 2 4 8];
msg=zeros(1,11);
ucBER=zeros(1,7);
cBER=zeros(1,7);
for c=0:(2^11-1)
k=c;
for j=1:11
msg(1,j)=mod(k,2);
k=floor(k/2);
end
% Uncoded Transmission
N=11;
signal=zeros(1,N*25);
for i=1:N
if msg(1,i)==0
signal(1,(25*(i-1)+1):(25*i))=ref(1,:);
else
signal(1,(25*(i-1)+1):(25*i))=-ref(1,:);
end
end
for snr=0:2:12
%adding noise
noisy=awgn(signal,snr);
%demodulating BPSK signal
dMod=zeros(1,N);
for j=1:N
k=(j-1)*25;
if (noisy(k+7)+ref(7))>1
dMod(j)=0;
else
dMod(j)=1;
end
end
k=0;
for i=1:N
if dMod(1,i)~=msg(1,i)
k=k+1;
end
end
ucBER(snr/2+1)=ucBER(snr/2+1)+k;
end
%----------------------------------------------------------------------
%Coded Transmission
code=zeros(1,15);
for i=1:15
mul=0;
for j=1:11
mul=mul+and(msg(j),G(j,i));
end
code(i)=mod(mul,2);
end
N=15;
signal=zeros(1,N*25);
for i=1:N
if code(1,i)==0
signal(1,(25*(i-1)+1):(25*i))=ref(1,:);
else
signal(1,(25*(i-1)+1):(25*i))=-ref(1,:);
end
end
for snr=0:2:12
%adding noise
noisy=awgn(signal,snr);
%demodulating BPSK signal
dMod=zeros(1,N);
for j=1:N
k=(j-1)*25;
if (noisy(k+7)+ref(7))>1
dMod(j)=0;
else
dMod(j)=1;
end
end
syndrome=0;
for i=1:4
mul=0;
for j=1:15
mul=mul+and(dMod(j),H(i,j));
end
syndrome=syndrome+mod(mul,2)*(2^(4-i));
end
for i =1:16
if syndrome==S(2,i);
syndrome=S(1,i);
break;
end
end
error=zeros(1,15);
if syndrome~=0
error=[zeros(1,(syndrome-1)) 1 zeros(1,(15-syndrome))];
end
dCode=xor(dMod,error);
dCode=dCode(1,1:11);
k=0;
for i=1:11
if dCode(1,i)~=msg(1,i)
k=k+1;
end
end
cBER(snr/2+1)=cBER(snr/2+1)+k;
end
end
snr=0:2:12;
ucBER=ucBER/((2^11)*11);
cBER=cBER/((2^11)*11);
figure;
grid on;
axis([0 12 0 .2]);
subplot(2,1,1),stem(snr,ucBER);
subplot(2,1,2),stem(snr,cBER);