blib/lib/DSP/LinPred.pm

filter: Statementstmt 77.7% 115 / 148 Branchbran 65.0% 26 / 40 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 71.4% 10 / 14 Podpod 50.0% 5 / 10 totaltotal 74.4% 169 / 227 CC 29 SCAR 21.8
CC
29
Cov
76%
set_filter
14.9
update
12.5
filter_dc
6.0
CRAP 8.8
↑ Summary
1 package DSP::LinPred;
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.06";
5  
6 2 has 'mu' => (
7 is => 'rw',
8 isa => 'Num',
9 default => 0.001
10 );
11 2 has 'mu_mode' => (
12 is => 'rw',
13 isa => 'Int',
14 default => 0
15 );
16 2 has 'h_length' => (
17 is => 'rw',
18 isa => 'Int',
19 default => 100
20 );
21 has 'h' => (
22 is => 'rw',
23 isa => 'ArrayRef[Num]',
24 1 ▶ default => sub{[(0) x 100]}
Subroutine __ANON__: called CC=1 SCAR=0.0
25 2 );
26 has 'x_stack' => (
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 2 has 'x_count' => (
32 is => 'rw',
33 isa => 'Int',
34 default => 0
35 );
36 2 has 'current_error' => (
37 is => 'rw',
38 isa => 'Num',
39 default => 0
40 );
41 2 has 'dc' => (
42 is => 'rw',
43 isa => 'Num',
44 default => 0
45 );
46 2 has 'dc_mode' => (
47 is => 'rw',
48 isa => 'Int',
49 default => 1
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_mode' => (
62 is => 'rw',
63 isa => 'Int',
64 default => 1
65 );
66 2 has 'stddev_init' => (
67 is => 'rw',
68 isa => 'Num',
69 default => 1
70 );
71 2 has 'iir_mode' => (
72 is => 'rw',
73 isa => 'Int',
74 default => 0
75 );
76 2 has 'iir_a' => (
77 is => 'rw',
78 isa => 'Num',
79 default => 0.01
80 );
81  
82  
83  
84 # filter specification
85 # mu : step size
86 # h_length : filter size
87 2 ▶sub set_filter{
Subroutine set_filter: called CC=11 SCAR=27.0
Pod documented
88 2 my $self = shift;
89 2 my $conf = shift;
90 2 ▶ if(defined($conf->{mu_mode})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"mu_mode"}) 0 2 2
91 0 $self->mu_mode($conf->{mu_mode});
92 }
93 2 ▶ if(defined($conf->{iir_mode})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"iir_mode"}) 1 1 2
94 1 $self->iir_mode($conf->{iir_mode});
95 }
96 2 ▶ if(defined($conf->{iir_a})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"iir_a"}) 0 2 2
97 0 $self->iir_a($conf->{iir_a});
98 }
99 2 ▶ if(defined($conf->{filter_length})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"filter_length"}) 2 0 2
100 2 $self->h_length($conf->{filter_length});
101 2 $self->h([(0) x $conf->{filter_length}]);
102 2 ▶ if(defined($conf->{dc_init})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{'dc_init'}) { } 2 0 2
103 2 $self->x_stack([($conf->{dc_init}) x $conf->{filter_length}]);
104 }else{
105 0 $self->x_stack([(0) x $conf->{filter_length}]);
106 }
107 2 ▶ if(defined($conf->{iir_a})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{'iir_a'}) { } 0 2 2
108 0 $self->iir_a($conf->{iir_a});
109 }else{
110 $self->iir_a(1 / $conf->{filter_length})
111 2 }
112 }
113 2 ▶ if(defined($conf->{dc_mode})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"dc_mode"}) 2 0 2
114 2 $self->dc_mode($conf->{dc_mode});
115 }
116 2 ▶ if(defined($conf->{dc_init})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"dc_init"}) 2 0 2
117 2 $self->dc($conf->{dc_init});
118 2 $self->dc_init($conf->{dc_init});
119 }
120 2 ▶ if(defined($conf->{stddev_mode})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"stddev_mode"}) 2 0 2
121 2 $self->stddev_mode($conf->{stddev_mode});
122 }
123 2 ▶ if(defined($conf->{stddev_init})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"stddev_init"}) 0 2 2
124 0 $self->stddev($conf->{stddev_init});
125 0 $self->stddev_init($conf->{stddev_init});
126 }
127 }
128  
129 # reset filter state
130 1 ▶sub reset_state{
Subroutine reset_state: called CC=1 SCAR=0.0
Pod undocumented
131 1 my $self = shift;
132 1 my $h_length = $self->h_length;
133 1 $self->h([(0) x $h_length]);
134 1 $self->x_stack([($self->dc_init) x $h_length]);
135 1 $self->current_error(0);
136 1 $self->dc($self->dc_init);
137 1 $self->x_count(0);
138 1 $self->stddev($self->stddev_init);
139 }
140  
141 # prediction only
142 # predict_num : number of output predicted values
143 # this method returns list reference of predicted values
144 2 ▶sub predict{
Subroutine predict: called CC=4 SCAR=13.9
Pod documented
145 2 my $self = shift;
146 2 my $predict_num = shift;
147 2 my $h = $self->h;
148 2 my $x_stack = $self->x_stack;
149 2 my $estimated;
150 2 for(1 .. $predict_num){
151 2 my $x_est = 0;
152 2 ▶ for( my $k = 0; $k <= $#{$h} and $k <= $self->x_count; $k++){
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $#{$h;} and $k <= $self->x_count) 600 2 602
Condition66%2/3
$k <= $#{$h;} and $k <= $self->x_count
!ll&&!rl&&r
20600
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
153 600 $x_est += $h->[$k] * ($x_stack->[$k] - $self->dc);
154 }
155 2 $x_est += $self->dc;
156 2 unshift(@$x_stack,$x_est);
157 2 push(@$estimated,$x_est);
158 2 pop(@$x_stack);
159 }
160 2 return($estimated);
161 }
162  
163 # update only
164 # x should be array reference
165  
166 2 ▶sub update{
Subroutine update: called CC=12 SCAR=25.3
Pod documented
167 2 my $self = shift;
168 2 my $x = shift;
169 2 my $h_length = $self->h_length;
170 2 my $h = $self->h;
171 2 my $x_stack = $self->x_stack;
172  
173 2 ▶ for ( my $kx=0; $kx <= $#{$x}; $kx++){
Branch: 1 branch
BranchTrueFalseTotal
if ($kx <= $#{$x;}) 2000 2 2002
174 2000 unshift(@$x_stack,$x->[$kx]);
175 2000 pop(@$x_stack);
176 2000 $self->x_count($self->x_count + 1);
177 2000 ▶ if($self->dc_mode == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dc_mode == 1) 1000 1000 2000
178 1000 ▶ if($self->iir_mode == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->iir_mode == 1) { } 0 1000 1000
179 0 $self->dc_update_iir($x->[$kx]);
180 }else{
181 1000 $self->dc_update;
182 }
183 }
184 2000 ▶ if($self->stddev_mode == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stddev_mode == 1) 2000 0 2000
185 2000 ▶ if($self->iir_mode == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->iir_mode == 1) { } 0 2000 2000
186 0 $self->stddev_update_iir($x->[$kx]);
187 }else{
188 2000 $self->stddev_update;
189 }
190 }
191 2000 my $x_est = 0;
192 2000 ▶ for( my $k = 0; $k <= $#{$h} and $k <= $self->x_count;$k++){
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $#{$h;} and $k <= $self->x_count) 510898 2000 512898
Condition100%3/3
$k <= $#{$h;} and $k <= $self->x_count
!ll&&!rl&&r
1404596510898
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
193 510898 $x_est += $h->[$k] * ($x_stack->[$k] - $self->dc);
194 }
195 2000 my $error = $x->[$kx] - ($x_est + $self->dc);
196 2000 $self->current_error($error);
197 2000 my $h_new = $h;
198 2000 my $tmp_coef = 1;
199 2000 ▶ if($self->stddev_mode == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stddev_mode == 1) { } 2000 0 2000
200 2000 $tmp_coef = $self->mu * $error / (1 + $self->stddev);
201 }else{
202 0 $tmp_coef = $self->mu * $error;
203 }
204 2000 ▶ if($self->mu_mode == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mu_mode == 1) 0 2000 2000
205 0 $tmp_coef = 10 * $self->mu / (1 + $self->h_length);
206 }
207  
208 2000 ▶ for(my $k = 0;$k <= $#{$h} and $k <= $self->x_count; $k++){
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $#{$h;} and $k <= $self->x_count) 510898 2000 512898
Condition100%3/3
$k <= $#{$h;} and $k <= $self->x_count
!ll&&!rl&&r
1404596510898
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
209 510898 $h_new->[$k] =
210 $h->[$k]
211 + $tmp_coef * ($x_stack->[$k] - $self->dc);
212 }
213 2000 $self->h($h_new);
214 }
215 }
216  
217 ## DC component calculation and update
218 # using x_stack
219  
220 1000 ▶sub dc_update{
Subroutine dc_update: called CC=2 SCAR=6.9
Pod undocumented
221 1000 my $self = shift;
222 1000 my $x_stack = $self->x_stack;
223 1000 my $mean = 0;
224 1000 my $num = $#$x_stack + 1;
225 1000 for(0 .. $#$x_stack){
226 300000 $mean += $x_stack->[$_];
227 }
228 1000 $mean = $mean / $num;
229 1000 $self->dc($mean);
230 }
231  
232 ## update by IIR
233 0 ▶sub dc_update_iir{
Subroutine dc_update_iir: not called CC=1 SCAR=6.9
Pod undocumented
234 0 my $self = shift;
235 0 my $x = shift;
236 0 my $new_dc = ($x - $self->dc * $self->iir_a)/(1 - $self->iir_a);
237 0 $self->dc($new_dc);
238 }
239  
240  
241 ## standard deviation calculation and update
242 2000 ▶sub stddev_update{
Subroutine stddev_update: called CC=2 SCAR=6.9
Pod undocumented
243 2000 my $self = shift;
244 2000 my $x_stack = $self->x_stack;
245 2000 my $variance = 0;
246 2000 my $num = $#$x_stack + 1;
247 2000 for(0 .. $#$x_stack){
248 600000 $variance += ($x_stack->[$_] - $self->dc)**2;
249 }
250 2000 $variance = $variance / $num;
251 2000 $self->stddev(sqrt($variance));
252 }
253  
254 ## update by IIR
255 0 ▶sub stddev_update_iir{
Subroutine stddev_update_iir: not called CC=1 SCAR=6.9
Pod undocumented
256 0 my $self = shift;
257 0 my $x = shift;
258 0 my $tmp = sqrt(($x - $self->dc)**2);
259 0 my $new_stddev = ($tmp - $self->stddev * $self->iir_a)/(1 - $self->iir_a);
260 0 $self->stddev($new_stddev);
261 }
262  
263 ## calculation of mean value of filter
264 0 ▶sub filter_dc{
Subroutine filter_dc: not called CC=2 SCAR=17.9
Pod documented
265 0 my $self = shift;
266 0 my $h = $self->h;
267 0 my $mean = 0;
268 0 my $num = $#$h + 1;
269 0 for(0 .. $#$h){
270 0 $mean += $h->[$_];
271 }
272 0 return($mean / $num);
273 }
274  
275 ## calculation of stddev of filter
276 0 ▶sub filter_stddev{
Subroutine filter_stddev: not called CC=2 SCAR=17.9
Pod documented
277 0 my $self = shift;
278 0 my $h = $self->h;
279 0 my $variance = 0;
280 0 my $num = $#$h + 1;
281 0 for(0 .. $#$h){
282 0 $variance += ($h->[$_])**2;
283 }
284 0 return(sqrt($variance / $num));
285 }
286  
287  
288  
289 2 1;
290 __END__
↑ Summary