| 1 | =head1 NAME |
| 2 | |
| 3 | PDL::Filter::LinPred - Linear predictive filtering |
| 4 | |
| 5 | =head1 SYNOPSIS |
| 6 | |
| 7 | $x = PDL::Filter::LinPred->new( |
| 8 | {NLags => 10, |
| 9 | LagInterval => 2, |
| 10 | LagsBehind => 2, |
| 11 | Data => $dat}); |
| 12 | |
| 13 | ($pd,$corrslic) = $x->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 strict; |
| 74 | use warnings; |
| 75 | use PDL; |
| 76 | use PDL::Basic; |
| 77 | use PDL::Slices; |
| 78 | use PDL::Primitive; |
| 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->zeroes(PDL::float, $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::PGPLOT::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(PDL::float, 2*$nl); |
| 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->transpose,$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->transpose->slice("-$nl:-1"), |
| 156 | $this->{Weights}->slice("-$nl:-1"), |
| 157 | (my $pred1=PDL->null)); |
| 158 | inner($ldata->transpose->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::Slices; |
| 174 | use PDL::Primitive; |
| 175 | use strict; |
| 176 | |
| 177 | sub _ntotlags { |
| 178 | my($this) = @_; |
| 179 | return $this->{NLags} + $this->{LagsBehind} + 1; |
| 180 | } |
| 181 | |
| 182 | # Create the autocovariance matrix in Toeplitz form |
| 183 | |
| 184 | # FILTER |
| 185 | sub _mk_mat { |
| 186 | my($this) = @_; |
| 187 | |
| 188 | local $PDL::Debug = 1; |
| 189 | |
| 190 | my $n = $this->{LagsBehind}; |
| 191 | my $nl = $this->{NLags}; |
| 192 | my $nl1 = $nl-1; |
| 193 | |
| 194 | my $auc = $this->{AutoCor}; |
| 195 | |
| 196 | print "AUTOCOR: $auc\n"; |
| 197 | |
| 198 | my $sal = $this->{SymAutoCor}->lags(0,1,$this->{NLags})->px; |
| 199 | |
| 200 | my $autocov = |
| 201 | $sal->slice(($this->{LagsBehind}-1).":".(-1-($this->{LagsBehind}+1))) |
| 202 | ->copy()->px; |
| 203 | $this->{AutoCov} = $autocov; |
| 204 | |
| 205 | $| = 1; |
| 206 | print "AUTOCOV: \n\n\n"; |
| 207 | $autocov->dump; |
| 208 | |
| 209 | print "FOOBAR\n"; |
| 210 | |
| 211 | # Invert it |
| 212 | my $autocinv = inv($autocov); |
| 213 | |
| 214 | $this->{AutoSliceUsed} = $auc->slice("$n:-1"); |
| 215 | inner($autocinv->transpose,$this->{AutoSliceUsed},(my $tdw=PDL->null)); |
| 216 | |
| 217 | $this->{AutoCov} = $autocov; |
| 218 | $this->{AutoCovInv} = $autocinv; |
| 219 | $this->{Weights} = $tdw; |
| 220 | } |
| 221 | |
| 222 | sub chkdefault ($$) { |
| 223 | my ($var,$def); |
| 224 | return $def if !ref $var && $var == 0; |
| 225 | return defined $var ? $var : $def; |
| 226 | } |
| 227 | |
| 228 | sub new ($$) { |
| 229 | my($type,$pars) = @_; |
| 230 | my $this = bless {},$type; |
| 231 | $this->{NLags} = chkdefault(delete $pars->{NLags}, 2); |
| 232 | $this->{LagInterval} = chkdefault(delete $pars->{LagInterval}, 1); |
| 233 | $this->{LagsBehind} = chkdefault(delete $pars->{LagsBehind}, 1); |
| 234 | $this->{Smooth} = (delete $pars->{Smooth}); |
| 235 | |
| 236 | $this->{NDeleted} = $this->{LagInterval} * ($this->{NLags} + |
| 237 | $this->{LagsBehind}) - 1; |
| 238 | $this->{NTotLags} = $this->_ntotlags(); |
| 239 | (my $data = delete $pars->{Data}) ; |
| 240 | my ($auc,$auc1); |
| 241 | if(defined $data) { |
| 242 | my $atmp; |
| 243 | my $n = $this->{NTotLags}; |
| 244 | my $da = avg($data); |
| 245 | # Compute autocovariance |
| 246 | my $ldata = $data->lags(0,$this->{LagInterval},$n); |
| 247 | # XXX This takes too much space.. define a special function. |
| 248 | inner($ldata->slice(":,0"),$ldata, ($atmp=PDL->null)); |
| 249 | sumover($atmp->transpose,($auc=PDL->null)); |
| 250 | $auc /= $ldata->getdim(0) * $data->getdim(1); |
| 251 | $auc -= $da ** 2; |
| 252 | # print "AUC: $auc\n"; |
| 253 | } elsif(defined ($auc1 = delete $pars->{AutoCovar})) { |
| 254 | if($this->{LagInterval} != 1) { |
| 255 | $auc = $auc1->slice("0:$this->{LagInterval}:-1"); |
| 256 | } else { |
| 257 | $auc = $auc1; |
| 258 | } |
| 259 | } else { |
| 260 | barf "Nothing to compute autocovariance from!"; |
| 261 | } |
| 262 | $this->{AutoCor} = $auc; |
| 263 | my $n = $this->{NTotLags}; |
| 264 | $this->{SymAutoCor} = |
| 265 | PDL->zeroes(PDL::float, $n * 2 - 1); |
| 266 | my $tmp; |
| 267 | ($tmp = $this->{SymAutoCor}->slice("0:".($n-2))) .= |
| 268 | $auc->slice("-1:1"); |
| 269 | ($tmp = $this->{SymAutoCor}->slice(($n-1).":-1")) .= |
| 270 | $auc->slice("0:-1"); |
| 271 | $this->_mk_mat(); |
| 272 | $this; |
| 273 | } |
| 274 | |
| 275 | sub predict ($$) { |
| 276 | my($this,$data) = @_; |
| 277 | my $ldata = $data->lags(0,$this->{LagInterval},$this->{NTotLags}); |
| 278 | print "PREDICT, weights: $this->{Weights}\n"; |
| 279 | inner($ldata->transpose->slice("$this->{LagsBehind}:-1"), |
| 280 | $this->{Weights}, |
| 281 | (my $pred=PDL->null)); |
| 282 | return wantarray ? ($pred,$ldata->slice(":,(0)")) : |
| 283 | $pred ; |
| 284 | } |
| 285 | |
| 286 | 1; |