-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBPSK_74Hamming_sim.m
64 lines (54 loc) · 1.93 KB
/
BPSK_74Hamming_sim.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
EbNodB = 6;
R = 4 / 7; % (7, 4) Hamming (4/7 bits/symbol)
EbNo = 10 ^ (EbNodB / 10);
sigma = sqrt(1 / (2 * R * EbNo));
k = 4; % number of message bits
n = 7; % number of codeword bits
cwords = [ ...
0 0 0 0 0 0 0;
0 0 0 1 0 1 1;
0 0 1 0 1 1 0;
0 0 1 1 1 0 1;
0 1 0 0 1 1 1;
0 1 0 1 1 0 0;
0 1 1 0 0 0 1;
0 1 1 1 0 1 0;
1 0 0 0 1 0 1;
1 0 0 1 1 1 0;
1 0 1 0 0 1 1;
1 0 1 1 0 0 0;
1 1 0 0 0 1 0;
1 1 0 1 0 0 1;
1 1 1 0 1 0 0;
1 1 1 1 1 1 1];
Nblocks = 1000;
Nbiterrs = 0;
Nblkerrs = 0;
for i = 1 : Nblocks
msg = randi([0 1], 1, k); % generate random k-bit message
% Encoding
cword = [msg ...
mod(msg(1) + msg(2) + msg(3), 2) ...
mod(msg(2) + msg(3) + msg(4), 2) ...
mod(msg(1) + msg(2) + msg(4), 2)]; % (7, 4) Hamming
s = 1 - 2 * cword; % BPSK bit to symbol conversion
r = s + sigma * randn(1, n); % AWGN channel
% Hard-Decision Decoding
b = (r < 0); % threshold at zero
dist = mod(repmat(b, 16, 1) + cwords, 2) * ones(7, 1);
[mind1, pos] = min(dist);
msg_cap1 = cwords(pos, 1:4);
% Soft-Decision Decoding
corr = (1 - 2 * cwords) * r';
[mind2, pos] = max(corr);
msg_cap2 = cwords(pos, 1:4);
Nerrs = sum(msg ~= msg_cap1);
if Nerrs > 0
Nbiterrs = Nbiterrs + Nerrs;
Nblkerrs = Nblkerrs + 1;
end
Nbiterrs = Nbiterrs + sum(msg ~= msg_cap2);
end
BER_sim = Nbiterrs / k / Nblocks;
FER_sim = Nblkerrs / Nblocks;
disp([EbNodB FER_sim BER_sim Nblkerrs Nbiterrs Nblocks])