| 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.03"; | |||||||||||||||||||||||||||
| 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 | 1 | ▶ | sub set_filter{ | ||||||||||||||||||||||||||
|
Subroutine
set_filter: called CC=6 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 78 | 1 | my $conf = shift; | |||||||||||||||||||||||||||
| 79 | 1 | ▶ | if(defined($conf->{filter_length})){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | $self->h_length($conf->{filter_length}); | |||||||||||||||||||||||||||
| 81 | 1 | $self->h([(0) x $conf->{filter_length}]); | |||||||||||||||||||||||||||
| 82 | 1 | ▶ | if(defined($conf->{dc_init})){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | $self->x_stack([($conf->{dc_init}) x $conf->{filter_length}]); | |||||||||||||||||||||||||||
| 84 | }else{ | ||||||||||||||||||||||||||||
| 85 | 1 | $self->x_stack([(0) x $conf->{filter_length}]); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 1 | ▶ | if(defined($conf->{dc_init})){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 93 | 1 | $self->est_mode($conf->{est_mode}); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 1 | ▶ | if(defined($conf->{stddev_init})){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | $self->stddev($conf->{stddev_init}); | |||||||||||||||||||||||||||
| 97 | 0 | $self->stddev_init($conf->{stddev_init}); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # reset filter state | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub reset_state{ | ||||||||||||||||||||||||||
|
Subroutine
reset_state: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 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 | 1 | ▶ | sub predict{ | ||||||||||||||||||||||||||
|
Subroutine
predict: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 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(0 .. $predict_num){ | |||||||||||||||||||||||||||
| 123 | 6 | my $x_est = 0; | |||||||||||||||||||||||||||
| 124 | 6 | ▶ | for( my $k = 0; $k <= $#{$h} and $k <= $self->x_count; $k++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$k <= $#{$h;} and $k <= $self->x_count
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
| |||||||||||||||||||||||||||||
| 125 | 600 | $x_est += $h->[$k] * ($x_stack->[$k] - $self->dc); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 6 | $x_est += $self->dc; | |||||||||||||||||||||||||||
| 128 | 6 | unshift(@$x_stack,$x_est); | |||||||||||||||||||||||||||
| 129 | 6 | push(@$estimated,$x_est); | |||||||||||||||||||||||||||
| 130 | 6 | pop(@$x_stack); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 1 | shift(@$estimated); | |||||||||||||||||||||||||||
| 133 | 1 | return($estimated); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # update only | ||||||||||||||||||||||||||||
| 137 | # x should be array reference | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1 | ▶ | sub update{ | ||||||||||||||||||||||||||
|
Subroutine
update: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 141 | 1 | my $x = shift; | |||||||||||||||||||||||||||
| 142 | 1 | my $h_length = $self->h_length; | |||||||||||||||||||||||||||
| 143 | 1 | my $h = $self->h; | |||||||||||||||||||||||||||
| 144 | 1 | my $x_stack = $self->x_stack; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 1 | ▶ | for ( my $kx=0; $kx <= $#{$x}; $kx++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 5000 | unshift(@$x_stack,$x->[$kx]); | |||||||||||||||||||||||||||
| 149 | 5000 | pop(@$x_stack); | |||||||||||||||||||||||||||
| 150 | 5000 | $self->x_count($self->x_count + 1); | |||||||||||||||||||||||||||
| 151 | 5000 | ▶ | if($self->est_mode == 1){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 5000 | $self->dc_stddev_update; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 5000 | my $x_est = 0; | |||||||||||||||||||||||||||
| 155 | 5000 | ▶ | for( my $k = 0; $k <= $#{$h} and $k <= $self->x_count;$k++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$k <= $#{$h;} and $k <= $self->x_count
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
| |||||||||||||||||||||||||||||
| 156 | 495149 | $x_est += $h->[$k] * ($x_stack->[$k] - $self->dc); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 5000 | my $error = $x->[$kx] - ($x_est + $self->dc); | |||||||||||||||||||||||||||
| 159 | 5000 | $self->current_error($error); | |||||||||||||||||||||||||||
| 160 | 5000 | my $h_new = $h; | |||||||||||||||||||||||||||
| 161 | 5000 | my $tmp_coef = 1; | |||||||||||||||||||||||||||
| 162 | 5000 | ▶ | if($self->est_mode == 1){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 5000 | $tmp_coef = $self->mu * $error / (1 + $self->stddev); | |||||||||||||||||||||||||||
| 164 | }else{ | ||||||||||||||||||||||||||||
| 165 | 0 | $tmp_coef = $self->mu * $error; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 5000 | ▶ | if($self->mu_mode == 1){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | $tmp_coef = 10 * $self->mu / (1 + $self->h_length); | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 5000 | ▶ | for(my $k = 0;$k <= $#{$h} and $k <= $self->x_count; $k++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$k <= $#{$h;} and $k <= $self->x_count
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
| |||||||||||||||||||||||||||||
| 172 | 495149 | $h_new->[$k] = | |||||||||||||||||||||||||||
| 173 | $h->[$k] | ||||||||||||||||||||||||||||
| 174 | + $tmp_coef * ($x_stack->[$k] - $self->dc); | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 5000 | $self->h($h_new); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | ## DC component calculation and update | ||||||||||||||||||||||||||||
| 181 | # using x_stack | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 5000 | ▶ | sub dc_stddev_update{ | ||||||||||||||||||||||||||
|
Subroutine
dc_stddev_update: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 184 | 5000 | my $self = shift; | |||||||||||||||||||||||||||
| 185 | 5000 | my $x_stack = $self->x_stack; | |||||||||||||||||||||||||||
| 186 | 5000 | my ($sum,$mean,$variance,$stddev) = &get_stat($x_stack); | |||||||||||||||||||||||||||
| 187 | 5000 | $self->dc($mean); | |||||||||||||||||||||||||||
| 188 | 5000 | $self->stddev($stddev); | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | ## calculation of mean value of filter | ||||||||||||||||||||||||||||
| 193 | 0 | ▶ | sub filter_dc{ | ||||||||||||||||||||||||||
|
Subroutine
filter_dc: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 194 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 195 | 0 | my $h = $self->h; | |||||||||||||||||||||||||||
| 196 | 0 | my $mean = 0; | |||||||||||||||||||||||||||
| 197 | 0 | my $num = $#$h + 1; | |||||||||||||||||||||||||||
| 198 | 0 | for(0 .. $#$h){ | |||||||||||||||||||||||||||
| 199 | 0 | $mean += $h->[$_]; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | 0 | return($mean / $num); | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | ## calculation of stddev of filter | ||||||||||||||||||||||||||||
| 205 | 0 | ▶ | sub filter_stddev{ | ||||||||||||||||||||||||||
|
Subroutine
filter_stddev: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 206 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 207 | 0 | my $h = $self->h; | |||||||||||||||||||||||||||
| 208 | 0 | my $variance = 0; | |||||||||||||||||||||||||||
| 209 | 0 | my $num = $#$h + 1; | |||||||||||||||||||||||||||
| 210 | 0 | for(0 .. $#$h){ | |||||||||||||||||||||||||||
| 211 | 0 | $variance += ($h->[$_])**2; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | 0 | return(sqrt($variance / $num)); | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 2 | 1; | |||||||||||||||||||||||||||
| 219 | __END__ | ||||||||||||||||||||||||||||