blib/lib/Math/DWT/UDWT.pm

filter: Statementstmt 6.7% 10 / 149 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 37.5% 3 / 8 Podpod 100% 5 / 5 totaltotal 7.7% 18 / 231 CC 38 SCAR 57.1
CC
38
Cov
5%
iudwt
420.0
udwt
306.0
new
6.0
CRAP 302.9
↑ Summary
1 package Math::DWT::UDWT;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 =head1 NAME
9  
10 Math::DWT::UDWT - Pure Perl 1-D Undecimated Discrete Wavelet Transform.
11  
12 =head1 VERSION
13  
14 Version 0.01
15  
16 =cut
17  
18 1 our $VERSION = '0.01';
19  
20  
21 =head1 SYNOPSIS
22  
23 This module implements a pure Perl version of the 1-D Undecimated Discrete Wavelet Transform (UDWT), also known as the "stationary" Discrete Wavelet Transform, because it is shift-invariant.
24  
25 Do not look here for efficiency - only implementation. It is designed to be a reference for others who would like to see wavelets in action. It is based off of MATLAB code from Ivan Selesnick's website at L<http://eeweb.poly.edu/iselesni/software/index.html>.
26  
27 use Math::DWT::UDWT;
28  
29 # new object with Math::DWT::Wavelet::Symmlet module loaded
30 # as wavelet; specifically, Symmlet5
31 my $udwt = Math::DWT::UDWT->new('Symmlet',5);
32  
33 @signal=@signal[0..509];
34 my $x = \@signal; # can be any length - not just 2^n
35  
36 # performs one iteration of the UDWT on $x
37 my $coeffs = $udwt->udwt($x);
38  
39 # notice how the UDWT returns more data than it took in
40 # this is due to the linear convolution used right now.
41 print scalar(@{$coeffs->[0]}); # 265
42 print scalar(@{$coeffs->[1]}); # 265
43  
44 # test Perfect Reconstruction
45 my $y=$udwt->iudwt($coeffs);
46  
47 my $maxerr=0;
48 foreach my $i (0 .. scalar(@x)-1) {
49 my $err = abs($x[$i] - $y->[$i]);
50 $maxerr = $err if ($err > $maxerr);
51 }
52 print "$maxerr\n"; # prints 7.92255150372512e-12 (close to 0)
53  
54  
55 =cut
56  
57
58  
59  
60 =head1 SUBROUTINES/METHODS
61  
62 =head2 new(WAVELET,VAR)
63  
64 =head2 new(WAVELET)
65  
66 =head2 new()
67  
68 Create a new UDWT object with the wavelet WAVELET and variable VAR.
69  
70 =cut
71  
72 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
73 0 my $class=shift;
74 0 my $self={};
75 0 my $wavelet=shift;
76 0 my $var=shift;
77  
78 0 my $wname; my $w;
79 0 my ($lo_d,$hi_d,$lo_r,$hi_r);
80 0 ▶ if (defined($wavelet)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wavelet) 0 0 0
81 0 $wname="Math::DWT::Wavelet::$wavelet";
82 0 require "Math/DWT/Wavelet/$wavelet.pm";
83 0 $w=$wname->new($var);
84  
85 0 ($lo_d,$hi_d,$lo_r,$hi_r)=@{$w->filters()};
86 }
87  
88 0 $self={ lo_d=>$lo_d,
89 hi_d=>$hi_d,
90 lo_r=>$lo_r,
91 hi_r=>$hi_r };
92  
93 0 bless $self,$class;
94 0 return $self;
95 };
96  
97
98  
99 =head2 udwt(SIGNAL,LEVEL,LO-PASS,HI-PASS)
100  
101 =head2 udwt(SIGNAL,LEVEL)
102  
103 =head2 udwt(SIGNAL)
104  
105 This performs the forward Undecimated Discrete Wavelet Transform on SIGNAL using LO-PASS and HI-PASS as filters. The process is repeated LEVEL times. If the filters are omitted, it uses the filters set in the wavelet when the udwt object was created. LEVEL defaults to 1.
106  
107 The structure of $coeffs is the same as in Math::DWT, i.e., the first LEVEL arrayrefs are iterative arrays of detail coefficients, and the last arrayref is the remaining array of scaling coefficients.
108  
109  
110 =cut
111  
112 0 ▶sub udwt {
Subroutine udwt: not called CC=17 SCAR=57.2
Pod documented
113 0 my $self=shift;
114 0 my ($x,$level,$lo,$hi)=@_;
115  
116 0 ▶ if (!defined($level)|| $level == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $level or $level == 0) 0 0 0
Condition0%0/3
not defined $level or $level == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $level or $level == 0
not defined $level $level == 0
Truth table0%0/3
not(A) or B
A= not defined $levelB= $level == 0
ABresult
1X1
011
000
117 0 $level = 1;
118 };
119  
120 0 ▶ if (!defined($lo)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lo) 0 0 0
121 0 $lo = $self->{lo_d};
122 };
123 0 ▶ if (!defined($hi)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hi) 0 0 0
124 0 $hi = $self->{hi_d};
125 };
126  
127 0 my @out;
128  
129 # normalize
130  
131 0 my @LO=@{$lo};
132 0 my @HI=@{$hi};
133 0 my @X=@{$x};
134  
135 0 foreach (@LO) { $_=$_/sqrt(2); }
136 0 foreach (@HI) { $_=$_/sqrt(2); }
137  
138 0 my $Nlo=scalar(@LO);
139 0 my $Nhi=scalar(@HI);
140  
141 0 my $N=scalar(@X);
142  
143 0 ▶ for (my $j=1;$j<=$level;$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $level) 0 0 0
144 0 my $L=scalar(@X);
145 0 my $M=2**($j-1);
146 0 my @lo_tmp;
147 my @hi_tmp;
148  
149 0 ▶ for (my $k=0;$k<=$Nhi-1;$k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $Nhi - 1) 0 0 0
150 0 ▶ for (my $i=1;$i<=$L;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $L) 0 0 0
151 0 my $a=0;
152 0 ▶ if (!defined($hi_tmp[$M*$k+$i-1])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hi_tmp[$M * $k + $i - 1]) 0 0 0
153 0 $hi_tmp[$M*$k+$i-1]=0;
154 }
155 0 ▶ if (defined($X[$i-1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $X[$i - 1]) 0 0 0
156 0 $a=$X[$i-1];
157 };
158 0 $hi_tmp[$M*$k+$i-1]=$hi_tmp[$M*$k+$i-1] + $HI[$k]*$a;
159 };
160 };
161 0 ▶ for (my $k=0;$k<=$Nlo-1;$k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $Nlo - 1) 0 0 0
162 0 ▶ for (my $i=1;$i<=$L;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $L) 0 0 0
163 0 my $a=0;
164 0 ▶ if (!defined($lo_tmp[$M*$k+$i-1])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lo_tmp[$M * $k + $i - 1]) 0 0 0
165 0 $lo_tmp[$M*$k+$i-1]=0;
166 }
167 0 ▶ if (defined($X[$i-1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $X[$i - 1]) 0 0 0
168 0 $a=$X[$i-1];
169 };
170 0 $lo_tmp[$M*$k+$i-1]=$lo_tmp[$M*$k+$i-1] + $LO[$k]*$a;
171 };
172 };
173
174 0 $out[$j-1]=\@hi_tmp;
175  
176 0 @X=@lo_tmp;
177  
178 }
179 0 $out[$level]=\@X;
180  
181 0 ▶ if (wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
182 0 return (@out);
183 };
184 0 return \@out;
185 }
186
187  
188 =head2 iudwt(COEFFICIENTS,LEVEL,LO-PASS,HI-PASS)
189  
190 =head2 iudwt(COEFFICIENTS,LEVEL)
191  
192 =head2 iudwt(COEFFICIENTS)
193  
194 Inverse Undecimated Discrete Wavelet Transform. Same options as for Math::DWT::idwt.
195  
196 =cut
197  
198 0 ▶sub iudwt {
Subroutine iudwt: not called CC=20 SCAR=60.4
Pod documented
199 0 my $self=shift;
200 0 my ($w,$J,$lo,$hi)=@_;
201  
202 0 my @counts;
203  
204 0 ▶ if (!defined($J)) { $J=1; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $J) 0 0 0
205  
206 0 ▶ for (my $i=0;$i<scalar(@$w);$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$w) 0 0 0
207 0 push @counts, scalar(@{$w->[$i]});
208 };
209  
210 0 ▶ if (!defined($lo)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lo) 0 0 0
211 0 $lo = $self->{lo_r};
212 };
213 0 ▶ if (!defined($hi)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hi) 0 0 0
214 0 $hi = $self->{hi_r};
215 };
216  
217 0 my @LO=@{$lo};
218 0 my @HI=@{$hi};
219  
220 0 foreach (@LO) { $_=$_/sqrt(2);}
221 0 foreach (@HI) { $_=$_/sqrt(2);}
222  
223 0 my $Nlo=scalar(@LO);
224 0 my $Nhi=scalar(@HI);
225  
226 0 my $N=$Nlo+$Nhi;
227  
228 0 my $y;
229 my @tmpy;
230  
231 0 ▶ for (my $i=0;$i<$counts[$J];$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $counts[$J]) 0 0 0
232 0 push @{$y}, $w->[$J]->[$i];
233 };
234  
235 0 ▶ for (my $j=$J;$j>=1;$j--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= 1) 0 0 0
236 0 my $M=2**($j-1);
237 0 my @lo=@{$y};
238  
239 0 my @hi;
240 0 ▶ for (my $i=0; $i<$counts[$j-1];$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $counts[$j - 1]) 0 0 0
241 0 push @hi, $w->[$j-1]->[$i];
242 };
243  
244 0 my $Llo=scalar(@lo);
245 0 my $Lhi=scalar(@hi);
246  
247 0 my @ylo;
248 my @yhi;
249  
250 #for(my $i=0;$i<$Llo+$M*($Nlo-1);$i++) {
251 #push @ylo, 0;
252 #};
253 #for(my $i=0;$i<$Lhi+$M*($Nhi-1);$i++) {
254 #push @yhi, 0;
255 #};
256  
257 0 ▶ for(my $k=0;$k<=$Nlo-1;$k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $Nlo - 1) 0 0 0
258 0 ▶ for (my $l=1;$l<=$Llo;$l++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l <= $Llo) 0 0 0
259 0 my $a=0;
260 0 ▶ if (!defined($ylo[$M*$k+$l-1])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ylo[$M * $k + $l - 1]) 0 0 0
261 0 $ylo[$M*$k+$l-1]=0;
262 };
263 0 ▶ if (defined($lo[$l-1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lo[$l - 1]) 0 0 0
264 0 $a=$lo[$l-1];
265 };
266 0 $ylo[$M*$k+$l-1]=$ylo[$M*$k+$l-1] + $LO[$k] * $a;
267 }
268 }
269 0 ▶ for(my $k=0;$k<=$Nhi-1;$k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $Nhi - 1) 0 0 0
270 0 ▶ for (my $l=1;$l<=$Lhi;$l++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l <= $Lhi) 0 0 0
271 0 my $a=0;
272 0 ▶ if (!defined($yhi[$M*$k+$l-1])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $yhi[$M * $k + $l - 1]) 0 0 0
273 0 $yhi[$M*$k+$l-1]=0;
274 };
275 0 ▶ if (defined($hi[$l-1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hi[$l - 1]) 0 0 0
276 0 $a=$hi[$l-1];
277 };
278 0 $yhi[$M*$k+$l-1]=$yhi[$M*$k+$l-1] + $HI[$k] * $a;
279 }
280 }
281  
282 0 my @cur_y;
283 0 ▶ for (my $i=0;$i<scalar(@yhi);$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @yhi) 0 0 0
284 0 push @cur_y, $ylo[$i]+$yhi[$i];
285 }
286  
287 0 $y=\@cur_y;
288  
289 0 my $L=$M*($N/2-1);
290  
291 0 my @tmpyy;
292  
293 #for (my $i=$L-1;$i<scalar(@yhi)-$L-1;$i++) {
294 0 ▶ for (my $i=$L;$i<scalar(@yhi)-$L;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar(@yhi) - $L) 0 0 0
295 0 push @tmpyy, $y->[$i];
296 };
297 0 @tmpy=@tmpyy;
298 0 $y=\@tmpy;
299 };
300 0 return \@tmpy;
301 };
302  
303 =head2 set_filters(LO_D,HI_D,LO_R,HI_R)
304  
305 Set the filters manually. Each of LO_D, HI_D, LO_R, and HI_R is an arrayref to the set of coefficients for the respective filter set. Returns undef.
306  
307 =cut
308  
309 0 ▶sub set_filters {
Subroutine set_filters: not called CC=1 SCAR=6.9
Pod documented
310 0 my $self=shift;
311 0 my ($lo_d,$hi_d,$lo_r,$hi_r)=@_;
312  
313 0 $self->{lo_d}=$lo_d;
314 0 $self->{hi_d}=$hi_d;
315 0 $self->{lo_r}=$lo_r;
316 0 $self->{hi_r}=$hi_r;
317  
318 0 return undef;
319 };
320  
321 =head2 get_filters()
322  
323 Get the set of filters. Returns an array or an arrayref containing LO_D, HI_D, LO_R, HI_R in that order.
324  
325 =cut
326  
327 0 ▶sub get_filters {
Subroutine get_filters: not called CC=2 SCAR=17.9
Pod documented
328 0 my $self=shift;
329 0 my ($lo_d,$hi_d,$lo_r,$hi_r)=($self->{lo_d},$self->{hi_d},$self->{lo_r},$self->{hi_r});
330  
331 0 ▶ if (wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
332 0 return ($lo_d,$hi_d,$lo_r,$hi_r);
333 };
334 0 return [$lo_d, $hi_d, $lo_r, $hi_r ];
335 };
336  
337  
338  
339 =head1 AUTHOR
340  
341 Mike Kroh, C<< <kroh at cpan.org> >>
342  
343 =head1 BUGS
344  
345 Please report any bugs or feature requests to C<bug-math-dwt-udwt at rt.cpan.org>, or through
346 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math-DWT-UDWT>. I will be notified, and then you'll
347 automatically be notified of progress on your bug as I make changes.
348  
349 =head1 ACKNOWLEDGEMENTS
350  
351  
352  
353  
354 =head1 LICENSE AND COPYRIGHT
355  
356 Copyright 2016 Mike Kroh.
357  
358 This program is free software; you can redistribute it and/or modify it
359 under the terms of the the Artistic License (2.0). You may obtain a
360 copy of the full license at:
361  
362 L<http://www.perlfoundation.org/artistic_license_2_0>
363  
364 Any use, modification, and distribution of the Standard or Modified
365 Versions is governed by this Artistic License. By using, modifying or
366 distributing the Package, you accept this license. Do not use, modify,
367 or distribute the Package, if you do not accept this license.
368  
369 If your Modified Version has been derived from a Modified Version made
370 by someone other than you, you are nevertheless required to ensure that
371 your Modified Version complies with the requirements of this license.
372  
373 This license does not grant you the right to use any trademark, service
374 mark, tradename, or logo of the Copyright Holder.
375  
376 This license includes the non-exclusive, worldwide, free-of-charge
377 patent license to make, have made, use, offer to sell, sell, import and
378 otherwise transfer the Package with respect to any patent claims
379 licensable by the Copyright Holder that are necessarily infringed by the
380 Package. If you institute patent litigation (including a cross-claim or
381 counterclaim) against any party alleging that the Package constitutes
382 direct or contributory patent infringement, then this Artistic License
383 to you shall terminate on the date that such litigation is filed.
384  
385 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
386 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
387 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
388 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
389 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
390 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
391 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
392 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
393  
394  
395 =cut
396  
397 1 1; # End of Math::DWT::UDWT
↑ Summary