blib/lib/PDL/Filter/LinPred.pm untested

filter: Statementstmt 0.0% 0 / 142 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 187 CC 8 SCAR 18.5
CC
8
Cov
0%
new
20.0
chkdefault
12.0
predict
6.0
CRAP 6.4
1 =head1 NAME
2  
3 PDL::Filter::LinPred - Linear predictive filtering
4  
5 =head1 SYNOPSIS
6  
7 $a = new PDL::Filter::LinPred(
8 {NLags => 10,
9 LagInterval => 2,
10 LagsBehind => 2,
11 Data => $dat});
12  
13 ($pd,$corrslic) = $a->predict($dat);
14  
15 =head1 DESCRIPTION
16  
17 A filter by doing linear prediction: tries to predict the next value
18 in a data stream as accurately as possible. The filtered data is the
19 predicted value. The parameters are
20  
21 =over 8
22  
23 =item NLags
24  
25 Number of time lags used for prediction
26  
27 =item LagInterval
28  
29 How many points each lag should be
30  
31 =item LagsBehind
32  
33 If, for some strange reason, you wish to predict not the next but
34 the one after that (i.e. usually f(t) is predicted from f(t-1) and f(t-2)
35 etc., but with LagsBehind => 2, f(t) is predicted from f(t-2) and f(t-3)).
36  
37 =item Data
38  
39 The input data, which may contain other dimensions past the first (time).
40 The extraneous dimensions are assumed to represent epochs so the data
41 is just concatenated.
42  
43 =item AutoCovar
44  
45 As an alternative to B<Data>, you can just give the temporal autocorrelation
46 function.
47  
48 =item Smooth
49  
50 Don't do prediction or filtering but smoothing.
51  
52 =back
53  
54 The method B<predict> gives a prediction for some data plus a corresponding
55 slice of the data, if evaluated in list context. This slice is given
56 so that you may, if you wish, easily plot them atop each other.
57  
58 The rest of the documentation is under lazy evaluation.
59  
60 =head1 AUTHOR
61  
62 Copyright (C) Tuomas J. Lukka 1997.
63 All rights reserved. There is no warranty. You are allowed
64 to redistribute this software / documentation under certain
65 conditions. For details, see the file COPYING in the PDL
66 distribution. If this file is separated from the PDL distribution,
67 the copyright notice should be included in the file.
68  
69  
70 =cut
71  
72 package PDL::Filter::LinSmooth;
73 use PDL;
74 use PDL::Basic;
75 use PDL::Slatec;
76 use PDL::Slices;
77 use PDL::Primitive;
78 use strict;
79  
80 @PDL::Filter::LinSmooth::ISA = qw/PDL::Filter::LinPred/;
81  
82 sub _ntotlags {
83 my($this) = @_;
84 return 2 * ( $this->{NLags} + $this->{LagsBehind} );
85 }
86  
87 # nlags == 3, lagsbehind == 1 -> totlags = 7
88 #
89 # Symautocor: 6543210123456
90 # -> lags(nlags) ->
91 #
92 # 43210123456
93 # 54321012345
94 # 65432101234
95  
96 # SMOOTH
97 sub _mk_mat {
98 my($this) = @_;
99  
100 local $PDL::Debug = 1;
101  
102 my $n = $this->{LagsBehind};
103 my $nl = $this->{NLags};
104 my $nl1 = $nl-1;
105  
106 my $auc = $this->{AutoCor};
107  
108 my $autocov = PDL::float PDL->zeroes($nl*2,$nl*2);
109 $this->{AutoCov} = $autocov;
110  
111 my $sal = $this->{SymAutoCor}->px->lags(0,1,$this->{NLags})->px;
112 print "L,LB: $nl,$n\n";
113  
114 my ($tmp,$tmp2);
115 PDL::Graphics::PG::imag ($sal->copy);
116  
117 # First, the 2 diagonal slices
118 ($tmp = $autocov->slice("$nl:-1,$nl:-1")->px) .=
119 ($tmp2 = $autocov->slice("0:".($nl-1).",0:".($nl-1))->px) .=
120 $sal->slice(($this->{NLags}+2*$this->{LagsBehind}-1).":".
121 (-1-($this->{NLags}+2*$this->{LagsBehind}+1)))->px;
122  
123 # Then, the off-diagonal slices
124 ($tmp = $autocov->slice("-1:$nl,$nl1:0")) .=
125 ($tmp2 = $autocov->slice("0:$nl1,$nl:-1")) .=
126 $sal->slice("0:$nl1");
127  
128 # Invert it
129  
130 my $autocinv = inv($autocov);
131 # print "$autocinv,$auc,$n\n"; $auc->slice("$n:-1");
132 $this->{AutoSliceUsed} = PDL->zeroes(2*$nl)->float;
133  
134 ($tmp = $this->{AutoSliceUsed}->slice("0:$nl1"))
135 .= $auc->slice(($n+$nl-1).":$n");
136  
137 ($tmp = $this->{AutoSliceUsed}->slice("-1:$nl"))
138 .= $auc->slice(($n+$nl-1).":$n");
139  
140 inner($autocinv->xchg(0,1),$this->{AutoSliceUsed},(my $tdw=PDL->null));
141  
142 $this->{AutoCov} = $autocov;
143 $this->{AutoCovInv} = $autocinv;
144 $this->{Weights} = $tdw;
145 }
146  
147 sub predict ($$) {
148 my($this,$data) = @_;
149 my $nl = $this->{NLags};
150 my $nl1 = $nl - 1;
151  
152 my $ldata = $data->lags(0,$this->{LagInterval},$this->{NTotLags}+1);
153 print "PREDICT, weights: $this->{Weights}\n";
154  
155 inner($ldata->xchg(0,1)->slice("-$nl:-1"),
156 $this->{Weights}->slice("-$nl:-1"),
157 (my $pred1=PDL->null));
158 inner($ldata->xchg(0,1)->slice("0:$nl1"),
159 $this->{Weights}->slice("0:$nl1"),
160 (my $pred2=PDL->null));
161  
162 my $pred = $pred1 + $pred2;
163  
164 return wantarray ?
165 ($pred,$ldata->slice(":,(".($nl+$this->{LagsBehind}).")"),
166 $pred1, $pred2) :
167 $pred ;
168 }
169  
170 package PDL::Filter::LinPred;
171 use PDL;
172 use PDL::Basic;
173 use PDL::Slatec;
174 use PDL::Slices;
175 use PDL::Primitive;
176 use strict;
177  
178 sub _ntotlags {
179 my($this) = @_;
180 return $this->{NLags} + $this->{LagsBehind} + 1;
181 }
182  
183 # Create the autocovariance matrix in Toeplitz form
184  
185 # FILTER
186 sub _mk_mat {
187 my($this) = @_;
188  
189 local $PDL::Debug = 1;
190  
191 my $n = $this->{LagsBehind};
192 my $nl = $this->{NLags};
193 my $nl1 = $nl-1;
194  
195 my $auc = $this->{AutoCor};
196  
197 print "AUTOCOR: $auc\n";
198  
199 my $sal = $this->{SymAutoCor}->lags(0,1,$this->{NLags})->px;
200  
201 my $autocov =
202 $sal->slice(($this->{LagsBehind}-1).":".(-1-($this->{LagsBehind}+1)))
203 ->copy()->px;
204 $this->{AutoCov} = $autocov;
205  
206 $| = 1;
207 print "AUTOCOV: \n\n\n";
208 $autocov->dump;
209  
210 print "FOOBAR\n";
211  
212 # Invert it
213 my $autocinv = inv($autocov);
214  
215 $this->{AutoSliceUsed} = $auc->slice("$n:-1");
216 inner($autocinv->xchg(0,1),$this->{AutoSliceUsed},(my $tdw=PDL->null));
217  
218 $this->{AutoCov} = $autocov;
219 $this->{AutoCovInv} = $autocinv;
220 $this->{Weights} = $tdw;
221 }
222  
223 sub chkdefault ($$) {
224 my ($var,$def);
225 return $def if !ref $var && $var == 0;
226 return defined $var ? $var : $def;
227 }
228  
229 sub new ($$) {
230 my($type,$pars) = @_;
231 my $this = bless {},$type;
232 $this->{NLags} = chkdefault(delete $pars->{NLags}, 2);
233 $this->{LagInterval} = chkdefault(delete $pars->{LagInterval}, 1);
234 $this->{LagsBehind} = chkdefault(delete $pars->{LagsBehind}, 1);
235 $this->{Smooth} = (delete $pars->{Smooth});
236  
237 $this->{NDeleted} = $this->{LagInterval} * ($this->{NLags} +
238 $this->{LagsBehind}) - 1;
239 $this->{NTotLags} = $this->_ntotlags();
240 (my $data = delete $pars->{Data}) ;
241 my ($auc,$auc1);
242 if(defined $data) {
243 my $atmp;
244 my $n = $this->{NTotLags};
245 my $da = avg($data);
246 # Compute autocovariance
247 my $ldata = $data->lags(0,$this->{LagInterval},$n);
248 # XXX This takes too much space.. define a special function.
249 inner($ldata->slice(":,0"),$ldata, ($atmp=PDL->null));
250 sumover($atmp->xchg(0,1),($auc=PDL->null));
251 $auc /= $ldata->getdim(0) * $data->getdim(1);
252 $auc -= $da ** 2;
253 # print "AUC: $auc\n";
254 } elsif(defined ($auc1 = delete $pars->{AutoCovar})) {
255 if($this->{LagInterval} != 1) {
256 $auc = $auc1->slice("0:$this->{LagInterval}:-1");
257 } else {
258 $auc = $auc1;
259 }
260 } else {
261 barf "Nothing to compute autocovariance from!";
262 }
263 $this->{AutoCor} = $auc;
264 my $n = $this->{NTotLags};
265 $this->{SymAutoCor} =
266 (PDL->zeroes($n * 2 - 1)->float);
267 my $tmp;
268 ($tmp = $this->{SymAutoCor}->slice("0:".($n-2))) .=
269 $auc->slice("-1:1");
270 ($tmp = $this->{SymAutoCor}->slice(($n-1).":-1")) .=
271 $auc->slice("0:-1");
272 $this->_mk_mat();
273 $this;
274 }
275  
276 sub predict ($$) {
277 my($this,$data) = @_;
278 my $ldata = $data->lags(0,$this->{LagInterval},$this->{NTotLags});
279 print "PREDICT, weights: $this->{Weights}\n";
280 inner($ldata->xchg(0,1)->slice("$this->{LagsBehind}:-1"),
281 $this->{Weights},
282 (my $pred=PDL->null));
283 return wantarray ? ($pred,$ldata->slice(":,(0)")) :
284 $pred ;
285 }
286