Final commit
[GMM_FEL.git] / gmm_test.m
blob0315f27af102543cafc329b4b44c0edd8411b926
1 function gmm_test()
2 % Just a few automated tests.
4 function test(num, asserted, obtained)
5     if size(asserted) == size(obtained)
6             r = max(abs(asserted - obtained)) > max(asserted)/1E5; % direct comparing is not good because of rounding errors
7     else
8             r = 1;
9     end
10     if r == 0
11             fprintf(1, 'Test %i successfull.\n', num)
12     else
13             fprintf(1, 'FAIL - Test %i\n\tAsserted: ', num)
14             disp(asserted');
15             fprintf(1, '\n\tObtained:')
16             disp(obtained');
17     end
18 end
21 fprintf(1, '\nTesting the function hmm_viterbi...\n\n');
22 s = [0.5; 0.5];
23 S = [0.5 0.5; 0.5 0.5];
24 X = [0.9 0.1; 0.9 0.1; 0.9 0.1; 0.1 0.9; 0.1 0.9; 0.9 0.1];
25 test(1, [1 1 1 2 2 1]', hmm_viterbi(s, S, X));
26        
27 s = [0.33; 0.33; 0.34];
28 S = [0.33 0.33 0.34; 0.33 0.33 0.34; 0.33 0.33 0.34;];
29 X = [0.8 0.1 0.1; 0.1 0.1 0.8; 0.1 0.1 0.8; 0.2 0.8 0.0];
30 test(2, [1 3 3 2]', hmm_viterbi(s, S, X));
32 s = [0.03; 0.03; 0.94];
33 S = [0.33 0.33 0.34; 0.33 0.33 0.34; 0.33 0.33 0.34;];
34 X = [0.8 0.1 0.1; 0.1 0.1 0.8; 0.1 0.1 0.8; 0.2 0.8 0.0];
35 test(3, [3 3 3 2]', hmm_viterbi(s, S, X));
37 s = [0.5; 0.5]; % what if more than one path is the most probable one?
38 S = [0.5 0.5; 0.5 0.5];
39 X = [0.5 0.5; 0.5 0.5;];
40 test(4, [1 1]', hmm_viterbi(s, S, X));
42 fprintf(1, '\nTesting the function hmm_forward_backward...\n\n');
43 s = [0.3; 0.3; 0.4];
44 S = [0.33 0.33 0.34; 0.33 0.33 0.34; 0.33 0.33 0.34;];
45 X = [0.8 0.1 0.1; 0.1 0.1 0.8; 0.1 0.1 0.8; 0.2 0.8 0.0];
46 test(5, [1 3 3 2]', hmm_forward_backward(s, S, X));
48 fprintf(1, '\nTesting the function smooth_x...\n\n');
49 X = [0.02; 0.58; 0.4];
50 X_ht = java.util.Hashtable;
51 X_ht.put(num2str([1]), 1);
52 X_ht.put(num2str([2]), 2);
53 X_ht.put(num2str([3]), 3);
54 test(6, [0.02*exp(0)+0.58*exp(-1/2)+0.4*exp(-4/2), 0.02*exp(-1/2)+0.58*exp(0)+0.4*exp(-1/2),0.02*exp(-4/2)+0.58*exp(-1/2)+0.4*exp(0)]', smooth_x(X, X_ht, 2, 'gaussian', 1));
56 end