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

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 105 CC 4 SCAR 13.2
CC
4
Cov
0%
new
12.0
predict
6.0
new
2.0
CRAP 3.7
1 =head1 NAME
2  
3 PDL::Filter::Linear - linear filtering for PDL
4  
5 =head1 SYNOPSIS
6  
7 $a = new PDL::Filter::Linear(
8 {Weights => $v,
9 Point => 10});
10  
11 $b = new PDL::Filter::Gaussian(15,2); # 15 points, 2 std devn.
12  
13 ($pred,$corrslic) = $a->predict($dat);
14  
15 =head1 DESCRIPTION
16  
17 A wrapper for generic linear filters.
18 Just for convenience. This should in the future use DataPresenter.
19  
20 Also, this class should at some point learn to do FFT whenever it is
21 useful.
22  
23 =cut
24  
25 package PDL::Filter::Linear;
26 use PDL;
27 use PDL::Basic;
28 use PDL::Slices;
29 use PDL::Primitive;
30 use strict;
31  
32 sub new($$) {
33 my($type,$pars) = @_;
34  
35 my $this = bless {},$type;
36 barf("Must specify weights\n") unless defined $pars->{Weights};
37 $this->{Weights} = delete $pars->{Weights};
38 $this->{Point} = defined $pars->{Point} ? $pars->{Point} : 0;
39 $this;
40 }
41  
42 sub predict($$) {
43 my($this,$data) = @_;
44 my $ldata = $data->lags(0,1,$this->{Weights}->getdim(0));
45 inner($ldata->xchg(0,1),$this->{Weights},
46 (my $pred = PDL->null));
47 return wantarray ? ($pred,$ldata->slice(":,($this->{Point})")) :
48 $pred ;
49 }
50  
51 package PDL::Filter::Gaussian;
52 use PDL; use PDL::Basic; use PDL::Slices; use PDL::Primitive;
53 use strict;
54  
55 @PDL::Filter::Gaussian::ISA = qw/PDL::Filter::Linear/;
56  
57 sub new($$) {
58 my($type,$npoints,$sigma) = @_;
59 my $cent = int($npoints/2);
60 my $x = ((PDL->zeroes($npoints )->xvals) - $cent)->float;
61 my $y = exp(-($x**2)/(2*$sigma**2));
62 # Normalize to unit total
63 $y /= sum($y);
64 return PDL::Filter::Linear::new($type,{Weights => $y,
65 Point => $cent});
66 }
67  
68 # Savitzky-Golay (see Numerical Recipes)
69 package PDL::Filter::SavGol;
70 use PDL; use PDL::Basic; use PDL::Slices; use PDL::Primitive;
71 use strict;
72  
73 @PDL::Filter::Gaussian::ISA = qw/PDL::Filter::Linear/;
74  
75 # XXX Doesn't work
76 sub new($$) {
77 my($type,$deg,$nleft,$nright) = @_;
78 my $npoints = $nright + $nleft + 1;
79 my $x = ((PDL->zeroes($npoints )->xvals) - $nleft)->float;
80 my $mat1 = ((PDL->zeroes($npoints,$deg+1)->xvals))->float;
81 for(0..$deg-1) {
82 (my $tmp = $mat1->slice(":,($_)")) .= ($x ** $_);
83 }
84 my $y;
85 # Normalize to unit total
86 return PDL::Filter::Linear::new($type,{Weights => $y,
87 Point => $nleft});
88 }
89  
90