]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/symerr.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / symerr.m
1 ## Copyright (C) 2003 David Bateman
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {[@var{num}, @var{rate}] = } symerr (@var{a},@var{b})
18 ## @deftypefnx {Function File} {[@var{num}, @var{rate}] = } symerr (@var{...},@var{flag})
19 ## @deftypefnx {Function File} {[@var{num}, @var{rate} @var{ind}] = } symerr (@var{...})
20 ##
21 ## Compares two matrices and returns the number of symbol errors and the 
22 ## symbol error rate. The variables @var{a} and @var{b} can be either:
23 ##
24 ## @table @asis
25 ## @item Both matrices
26 ## In this case both matrices must be the same size and then by default the
27 ## the return values @var{num} and @var{rate} are the overall number of symbol
28 ## errors and the overall symbol error rate.
29 ## @item One column vector
30 ## In this case the column vector is used for symbol error comparision
31 ## column-wise with the matrix. The returned values @var{num} and @var{rate}
32 ## are then row vectors containing the num of symbol errors and the symbol
33 ## error rate for each of the column-wise comparisons. The number of rows in 
34 ## the matrix must be the same as the length of the column vector 
35 ## @item One row vector
36 ## In this case the row vector is used for symbol error comparision row-wise
37 ## with the matrix. The returned values @var{num} and @var{rate} are then 
38 ## column vectors containing the num of symbol errors and the symbol error rate
39 ## for each of the row-wise comparisons. The number of columns in the matrix 
40 ## must be the same as the length of the row vector 
41 ## @end table
42 ##
43 ## This behaviour can be overridden with the variable @var{flag}. @var{flag}
44 ## can take the value 'column-wise', 'row-wise' or 'overall'. A column-wise
45 ## comparision is not possible with a row vector and visa-versa.
46 ## @end deftypefn
47
48 ## 2003 FEB 13
49 ##   initial release
50
51 function [num, rate, ind] = symerr (a, b, _flag)
52
53   if ((nargin < 2) || (nargin > 4))
54     usage ("[num rate ind] = symerr (a, b [,flag])");
55   endif
56   if (any(any(isinf(a))) || any(any(isnan(a))) || any(any(isinf(b))) || ...
57       any(any(isnan(b))) || !real(a) || !real(b) || ...
58       any(any((floor(a)) != a)) || any(any((floor(b)) != b)) || ...
59       any(any(a < 0)) || any(any(b < 0)))
60     error ("symerr: a and b must contain only postive integers");
61   endif
62   
63   [ar,ac] = size(a);
64   [br,bc] = size(b);
65
66   if ((ar == br) && (ac == bc))
67     type = "matrix";
68     flag = "overall";
69     c = 1;
70   elseif (any([ar,br] == 1))
71     type = "row";
72     flag = "row";
73     if (ac != bc)
74       error ("symerr: row-wise comparison must have the same number of columns in inputs");
75     endif
76     if (ar == 1)
77       a = ones(br,1) * a;
78     else
79       b = ones(ar,1) * b;
80     endif
81   elseif (any([ac,bc] == 1))
82     type = "column";
83     flag = "column";
84     if (ar != br)
85       error ("symerr: column-wise comparison must have the same number of rows in inputs");
86     endif
87     if (ac == 1)
88       a = a * ones(1,bc);
89     else
90       b = b * ones(1,ac);
91     endif
92   else
93     error ("symerr: matrix sizes must match");
94   endif
95
96   if (nargin > 2)
97     if (ischar(_flag))
98       if (strcmp(_flag,"row-wise"))
99         if (strcmp(type,"column"))
100           error ("symerr: row-wise comparison not possible with column inputs");
101         endif
102         flag = "row";
103       elseif (strcmp(_flag,"column-wise"))
104         if (strcmp(type,"row"))
105           error ("symerr: column-wise comparison not possible with row inputs");
106         endif
107         flag = "column";
108       elseif (strcmp(_flag,"overall"))
109         flag = "overall";
110       else
111         error ("symerr: unrecognized string argument");
112       endif
113     else
114       error ("symerr: unrecognized argument");
115     endif
116   endif
117   
118   ## Call the core error function to count the bit errors 
119   ind = (__errcore__(a,b) != 0);
120
121   switch (flag)
122     case 'row',
123       if (strcmp(type,"matrix") && (ac == 1))
124         num = ind;
125       else
126         num = sum(ind')';
127       endif
128       rate = num / max(ac,bc);
129     case 'column',
130       if (strcmp(type,"matrix") && (ar == 1))
131         num = ind;
132       else
133         num = sum(ind);
134       endif
135       rate = num / max(ar,br);
136     case 'overall',
137       num = sum(sum(ind));
138       rate = num / max(ar,br) / max(ac,bc);
139     otherwise
140       error("impossible");
141   endswitch
142
143 endfunction