-
Notifications
You must be signed in to change notification settings - Fork 0
/
viterbi_algorithm.m
165 lines (137 loc) · 5.4 KB
/
viterbi_algorithm.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
157
158
159
160
161
162
163
164
function [out,sequence_bit1, sequence_bit2, past_cost] = viterbi_algorithm(y, past_cost, sequence_bit1, sequence_bit2, i)
out = -1000;
if i == 1
for level = 1:2:length(y)-1
rx = [y(level) y(level+1)];
% fprintf('entering sequence: ');
% fprintf('%d ', rx);
% fprintf('\n');
%
cost = get_cost_enter(rx, level, 0);
cost = [cost; get_cost_enter(rx, level, 1)];
if level == 1
% fprintf('cost matrix:\t');
% fprintf('%d ', cost(1, :));
% fprintf('\n\t\t');
% fprintf('%d ', cost(2, :));
% fprintf('\n');
current_cost = cost + past_cost;
% fprintf('current cost matrix:\t');
% fprintf('%d ', current_cost(1, :));
% fprintf('\n\t\t');
% fprintf('%d ', current_cost(2, :));
% fprintf('\n');
%
sequence_bit1 = [];
sequence_bit2 = [];
past_cost = [min(cost(1, :)) min(cost(2, :))];
else
cost = get_cost_enter(rx, level, 0);
cost = [cost; get_cost_enter(rx, level, 1)];
% fprintf('cost matrix:\t');
% fprintf('%d ', cost(1, :));
% fprintf('\n\t\t');
% fprintf('%d ', cost(2, :));
% fprintf('\n');
%
current_cost = cost + past_cost;
% fprintf('current cost matrix:\t');
% fprintf('%d ', current_cost(1, :));
% fprintf('\n\t\t\t');
% fprintf('%d ', current_cost(2, :));
% fprintf('\n');
%
for i=1:2
[m, ind] = min(current_cost(i, :));
past_cost(i) = m;
if i == 1
if ind == 1
sequence_bit1 = [sequence_bit1 'u'] ;
else
sequence_bit1 = [sequence_bit1 'd'] ;
end
else
if ind == 1
sequence_bit2 = [sequence_bit2 'u'] ;
else
sequence_bit2 = [sequence_bit2 'd'] ;
end
end
end
% fprintf('evolving sequence_bit1:\t');
% fprintf('%c ', sequence_bit1);
%
% fprintf('\nevolving sequence_bit2:\t');
% fprintf('%c ', sequence_bit2);
% fprintf('\n');
%
% fprintf('\ncost until now:\t');
% fprintf('%d ', past_cost);
% fprintf('\n');
end
if level == length(y)-1
[min_path, index_min] = min(past_cost);
if index_min == 1
out = backtrack(sequence_bit1,sequence_bit2, 1);
else
out = backtrack(sequence_bit1,sequence_bit2, 2);
end
end
end
else
% CONSIDERO SOLO L'ULTIMO RX
rx = [y(length(y) -1) y(length(y))];
% fprintf('entering sequence: ');
% fprintf('%d ', rx);
% fprintf('\n');
%
cost = get_cost_enter(rx, length(y) -1, 0);
cost = [cost; get_cost_enter(rx, length(y) -1, 1)];
% fprintf('cost matrix:\t');
% fprintf('%d ', cost(1, :));
% fprintf('\n\t\t');
% fprintf('%d ', cost(2, :));
% fprintf('\n');
%
current_cost = cost + past_cost;
% fprintf('current cost matrix:\t');
% fprintf('%d ', current_cost(1, :));
% fprintf('\n\t\t\t');
% fprintf('%d ', current_cost(2, :));
% fprintf('\n');
%
for i=1:2
[m, ind] = min(current_cost(i, :));
past_cost(i) = m;
if i == 1
if ind == 1
sequence_bit1 = [sequence_bit1 'u'] ;
else
sequence_bit1 = [sequence_bit1 'd'] ;
end
else
if ind == 1
sequence_bit2 = [sequence_bit2 'u'] ;
else
sequence_bit2 = [sequence_bit2 'd'] ;
end
end
% fprintf('evolving sequence_bit1:\t');
% fprintf('%c ', sequence_bit1);
%
% fprintf('\nevolving sequence_bit2:\t');
% fprintf('%c ', sequence_bit2);
% fprintf('\n');
%
% fprintf('\ncost until now:\t');
% fprintf('%d ', past_cost);
% fprintf('\n');
end
[min_path, index_min] = min(past_cost);
if index_min == 1
out = backtrack(sequence_bit1,sequence_bit2, 1);
else
out = backtrack(sequence_bit1,sequence_bit2, 2);
end
end
end