blib/lib/DSP/LinPred_XS.pm

filter: Statementstmt 69.6% 78 / 112 Branchbran 62.5% 15 / 24 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 75.0% 9 / 12 Podpod 71.4% 5 / 7 totaltotal 70.5% 120 / 170 CC 19 SCAR 19.8
CC
19
Cov
70%
set_filter
14.4
update
9.1
filter_dc
6.0
CRAP 7.2
↑ Summary Build »
1 package DSP::LinPred_XS;
2 2 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 our $VERSION = "0.02";
5 2 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 XSLoader::load(__PACKAGE__, $VERSION);
7  
8  
9  
10  
11 2 has 'mu' => (
12 is => 'rw',
13 isa => 'Num',
14 default => 0.001
15 );
16 2 has 'mu_mode' => (
17 is => 'rw',
18 isa => 'Int',
19 default => 0
20 );
21 2 has 'h_length' => (
22 is => 'rw',
23 isa => 'Int',
24 default => 100
25 );
26 has 'h' => (
27 is => 'rw',
28 isa => 'ArrayRef[Num]',
29 1 ▶ default => sub{[(0) x 100]}
Subroutine __ANON__: called CC=1 SCAR=0.0
30 2 );
31 has 'x_stack' => (
32 is => 'rw',
33 isa => 'ArrayRef[Num]',
34 1 ▶ default => sub{[(0) x 100]}
Subroutine __ANON__: called CC=1 SCAR=0.0
35 2 );
36 2 has 'x_count' => (
37 is => 'rw',
38 isa => 'Int',
39 default => 0
40 );
41 2 has 'current_error' => (
42 is => 'rw',
43 isa => 'Num',
44 default => 0
45 );
46 2 has 'dc' => (
47 is => 'rw',
48 isa => 'Num',
49 default => 0
50 );
51 2 has 'dc_init' => (
52 is => 'rw',
53 isa => 'Num',
54 default => 0
55 );
56 2 has 'stddev' => (
57 is => 'rw',
58 isa => 'Num',
59 default => 0
60 );
61 2 has 'stddev_init' => (
62 is => 'rw',
63 isa => 'Num',
64 default => 1
65 );
66 2 has 'est_mode' => (
67 is => 'rw',
68 isa => 'Num',
69 default => 1
70 );
71  
72  
73 # filter specification
74 # mu : step size
75 # h_length : filter size
76 sub set_filter{
77 1 ▶ my $self = shift;
Subroutine set_filter: called CC=6 SCAR=26.7
Pod documented
78 1 my $conf = shift;
79 1 ▶ if(defined($conf->{filter_length})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"filter_length"}) 0 1 1
80 0 $self->h_length($conf->{filter_length});
81 0 $self->h([(0) x $conf->{filter_length}]);
82 0 ▶ if(defined($conf->{dc_init})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{'dc_init'}) { } 0 0 0
83 0 $self->x_stack([($conf->{dc_init}) x $conf->{filter_length}]);
84 }else{
85 0 $self->x_stack([(0) x $conf->{filter_length}]);
86 }
87 }
88 1 ▶ if(defined($conf->{dc_init})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"dc_init"}) 0 1 1
89 0 $self->dc($conf->{dc_init});
90 0 $self->dc_init($conf->{dc_init});
91 }
92 1 ▶ if(defined($conf->{est_mode})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"est_mode"}) 0 1 1
93 0 $self->est_mode($conf->{est_mode});
94 }
95 1 ▶ if(defined($conf->{stddev_init})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"stddev_init"}) 0 1 1
96 0 $self->stddev($conf->{stddev_init});
97 0 $self->stddev_init($conf->{stddev_init});
98 }
99 }
100  
101 # reset filter state
102 sub reset_state{
103 0 ▶ my $self = shift;
Subroutine reset_state: not called CC=1 SCAR=6.9
Pod undocumented
104 0 my $h_length = $self->h_length;
105 0 $self->h([(0) x $h_length]);
106 0 $self->x_stack([($self->dc_init) x $h_length]);
107 0 $self->current_error(0);
108 0 $self->dc($self->dc_init);
109 0 $self->x_count(0);
110 0 $self->stddev($self->stddev_init);
111 }
112  
113 # prediction only
114 # predict_num : number of output predicted values
115 # this method returns list reference of predicted values
116 sub predict{
117 1 ▶ my $self = shift;
Subroutine predict: called CC=4 SCAR=13.9
Pod documented
118 1 my $predict_num = shift;
119 1 my $h = $self->h;
120 1 my $x_stack = $self->x_stack;
121 1 my $estimated;
122 1 for(1 .. $predict_num){
123 5 my $x_est = 0;
124 5 ▶ for( my $k = 0; $k <= $#{$h} and $k <= $self->x_count; $k++){
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $#{$h;} and $k <= $self->x_count) 500 5 505
Condition66%2/3
$k <= $#{$h;} and $k <= $self->x_count
!ll&&!rl&&r
50500
MC/DC50%1/2
$k <= $#{$h;} and $k <= $self->x_count
$k <= $#{$h;} $k <= $self->x_count
Truth table66%2/3
A and B
A= $k <= $#{$h;}B= $k <= $self->x_count
ABresult
0X0
100
111
125 500 $x_est += $h->[$k] * ($x_stack->[$k] - $self->dc);
126 }
127 5 $x_est += $self->dc;
128 5 unshift(@$x_stack,$x_est);
129 5 push(@$estimated,$x_est);
130 5 pop(@$x_stack);
131 }
132 1 return($estimated);
133 }
134  
135 # update only
136 # x should be array reference
137  
138 sub update{
139 1 ▶ my $self = shift;
Subroutine update: called CC=9 SCAR=22.1
Pod documented
140 1 my $x = shift;
141 1 my $h_length = $self->h_length;
142 1 my $h = $self->h;
143 1 my $x_stack = $self->x_stack;
144  
145 1 ▶ for ( my $kx=0; $kx <= $#{$x}; $kx++){
Branch: 1 branch
BranchTrueFalseTotal
if ($kx <= $#{$x;}) 4000 1 4001
146 4000 unshift(@$x_stack,$x->[$kx]);
147 4000 pop(@$x_stack);
148 4000 $self->x_count($self->x_count + 1);
149 4000 ▶ if($self->est_mode == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->est_mode == 1) 4000 0 4000
150 4000 $self->dc_stddev_update;
151 }
152 4000 my $x_est = 0;
153 4000 ▶ for( my $k = 0; $k <= $#{$h} and $k <= $self->x_count;$k++){
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $#{$h;} and $k <= $self->x_count) 395149 4000 399149
Condition100%3/3
$k <= $#{$h;} and $k <= $self->x_count
!ll&&!rl&&r
390298395149
MC/DC100%2/2
$k <= $#{$h;} and $k <= $self->x_count
$k <= $#{$h;} $k <= $self->x_count
Truth table100%3/3
A and B
A= $k <= $#{$h;}B= $k <= $self->x_count
ABresult
0X0
100
111
154 395149 $x_est += $h->[$k] * ($x_stack->[$k] - $self->dc);
155 }
156 4000 my $error = $x->[$kx] - ($x_est + $self->dc);
157 4000 $self->current_error($error);
158 4000 my $h_new = $h;
159 4000 my $tmp_coef = 1;
160 4000 ▶ if($self->est_mode == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->est_mode == 1) { } 4000 0 4000
161 4000 $tmp_coef = $self->mu * $error / (1 + $self->stddev);
162 }else{
163 0 $tmp_coef = $self->mu * $error;
164 }
165 4000 ▶ if($self->mu_mode == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mu_mode == 1) 0 4000 4000
166 0 $tmp_coef = 10 * $self->mu / (1 + $self->h_length);
167 }
168  
169 4000 ▶ for(my $k = 0;$k <= $#{$h} and $k <= $self->x_count; $k++){
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $#{$h;} and $k <= $self->x_count) 395149 4000 399149
Condition100%3/3
$k <= $#{$h;} and $k <= $self->x_count
!ll&&!rl&&r
390298395149
MC/DC100%2/2
$k <= $#{$h;} and $k <= $self->x_count
$k <= $#{$h;} $k <= $self->x_count
Truth table100%3/3
A and B
A= $k <= $#{$h;}B= $k <= $self->x_count
ABresult
0X0
100
111
170 395149 $h_new->[$k] =
171 $h->[$k]
172 + $tmp_coef * ($x_stack->[$k] - $self->dc);
173 }
174 4000 $self->h($h_new);
175 }
176 }
177  
178 ## DC component calculation and update
179 # using x_stack
180  
181 sub dc_stddev_update{
182 4000 ▶ my $self = shift;
Subroutine dc_stddev_update: called CC=1 SCAR=0.0
Pod undocumented
183 4000 my $x_stack = $self->x_stack;
184 4000 my ($sum,$mean,$variance,$stddev) = &get_stat($x_stack);
185 4000 $self->dc($mean);
186 4000 $self->stddev($stddev);
187 }
188  
189  
190 ## calculation of mean value of filter
191 sub filter_dc{
192 0 ▶ my $self = shift;
Subroutine filter_dc: not called CC=2 SCAR=17.9
Pod documented
193 0 my $h = $self->h;
194 0 my $mean = 0;
195 0 my $num = $#$h + 1;
196 0 for(0 .. $#$h){
197 0 $mean += $h->[$_];
198 }
199 0 return($mean / $num);
200 }
201  
202 ## calculation of stddev of filter
203 sub filter_stddev{
204 0 ▶ my $self = shift;
Subroutine filter_stddev: not called CC=2 SCAR=17.9
Pod documented
205 0 my $h = $self->h;
206 0 my $variance = 0;
207 0 my $num = $#$h + 1;
208 0 for(0 .. $#$h){
209 0 $variance += ($h->[$_])**2;
210 }
211 0 return(sqrt($variance / $num));
212 }
213  
214  
215  
216 2 1;
217 __END__
↑ Summary Build »