blib/lib/Math/FFT.pm

filter: Statementstmt 90.2% 344 / 381 Branchbran 50.5% 89 / 176 Conditioncond 48.7% 19 / 39 MC/DCmcdc 18.1% 4 / 22 Subroutinesub 100% 31 / 31 Podpod 100% 23 / 23 totaltotal 75.8% 510 / 672 SCAR 57.4
CC
117
Cov
76%
spctrm
33.2
deconvlv
10.6
invcdft
9.9
CRAP 310.1
↑ Summary arrays.c »
1 package Math::FFT;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 require DynaLoader;
10  
11 4 @ISA = qw(DynaLoader);
12  
13 # Items to export into callers namespace by default. Note: do not export
14 # names by default without a very good reason. Use EXPORT_OK instead.
15 # Do not simply export all your public functions/methods/constants.
16  
17 4 our $VERSION = '1.34';
18  
19 4 bootstrap Math::FFT $VERSION;
20  
21 # Preloaded methods go here.
22  
23 sub new {
24 21 ▶ my ($class, $data) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
25 21 ▶ die 'Must call constructor with an array reference for the data'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 0 21 21
26 unless ref($data) eq 'ARRAY';
27 21 ▶ $data->[0] ||= 0; # keep warnings happy
Condition100%2/2
$data->[0] ||= 0
l!l
174
MC/DC100%1/1
$data->[0] ||= 0
$data->[0]
Truth table100%2/2
A
A= $data->[0]
Aresult
11
00
28 21 my $n = @$data;
29 21 my $nip = int(3 + sqrt($n));
30 21 my $nw = int(2 + 5*$n/4);
31 21 my $ip = pack("i$nip", ());
32 21 my $w = pack("d$nw", ());
33 21 return bless {
34 type => '',
35 mean => '',
36 coeff => '',
37 n => $n,
38 data => $data,
39 ip => \$ip,
40 w => \$w,
41 }, $class;
42 }
43  
44 # clone method to copy the ip and w arrays for data of equal size
45 sub clone {
46 1 ▶ my ($self, $data) = @_;
Subroutine clone: called CC=4 SCAR=14.3
Pod documented
47 1 ▶ die 'Must call clone with an array reference for the data'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 0 1 1
48 unless ref($data) eq 'ARRAY';
49 1 ▶ $data->[0] ||= 0; # keep warnings happy
Condition50%1/2
$data->[0] ||= 0
l!l
10
MC/DC0%0/1
$data->[0] ||= 0
$data->[0]
Truth table50%1/2
A
A= $data->[0]
Aresult
11
00
50 1 my $n = @$data;
51 1 ▶ die "Cannot clone data of unequal sizes" unless $n == $self->{n};
Branch: 1 branch
BranchTrueFalseTotal
unless $n == $self->{"n"} 0 1 1
52 1 my $class = ref($self);
53 return bless {
54 type => '',
55 coeff => '',
56 mean => '',
57 n => $self->{n},
58 data => $data,
59 ip => $self->{ip},
60 w => $self->{w},
61 1 }, $class;
62 }
63  
64 # Complex Discrete Fourier Transform
65 sub cdft {
66 4 ▶ my $self = shift;
Subroutine cdft: called CC=2 SCAR=6.9
Pod documented
67 4 my $n = $self->{n};
68 4 ▶ die "data size ($n) must be an integer power of 2" unless _check_n($n);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_n($n) 0 4 4
69 4 my $data = [ @{$self->{data}} ];
70 4 _cdft($n, 1, $data, $self->{ip}, $self->{w});
71 4 $self->{type} = 'cdft';
72 4 $self->{coeff} = $data;
73 4 return $data;
74 }
75  
76 # Inverse Complex Discrete Fourier Transform
77 sub invcdft {
78 4 ▶ my $self = shift;
Subroutine invcdft: called CC=6 SCAR=23.0
Pod documented
79 4 my $data;
80 4 my $n = $self->{n};
81 4 ▶ if (my $arg = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift()) { } 0 4 4
82 0 ▶ if (ref($arg) ne 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg ne "ARRAY") 0 0 0
83 {
84 0 die 'Must pass an array reference to invcdft';
85 }
86 0 ▶ if ($n != @$arg)
Branch: 1 branch
BranchTrueFalseTotal
if ($n != @$arg) 0 0 0
87 {
88 0 die "Size of data set must be $n";
89 }
90 0 $data = [ @$arg ];
91 }
92 else {
93 die 'Must invert data created with cdft'
94 4 ▶ unless $self->{type} eq 'cdft';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"type"} eq "cdft" 0 4 4
95 4 $data = [ @{$self->{coeff}} ];
96 }
97 4 _cdft($n, -1, $data, $self->{ip}, $self->{w});
98 4 $_ *= 2.0/$n for (@$data);
99 4 return $data;
100 }
101  
102 # Real Discrete Fourier Transform
103 sub rdft {
104 7 ▶ my $self = shift;
Subroutine rdft: called CC=2 SCAR=7.0
Pod documented
105 7 my $n = $self->{n};
106 7 ▶ if (!_check_n($n))
Branch: 1 branch
BranchTrueFalseTotal
unless (_check_n($n)) 0 7 7
107 {
108 0 die "data size ($n) must be an integer power of 2";
109 }
110 7 my $data = [ @{$self->{data}} ];
111 7 _rdft($n, 1, $data, $self->{ip}, $self->{w});
112 7 $self->{type} = 'rdft';
113 7 $self->{coeff} = $data;
114 7 return $data;
115 }
116  
117 # Inverse Real Discrete Fourier Transform
118 sub invrdft {
119 3 ▶ my $self = shift;
Subroutine invrdft: called CC=6 SCAR=21.8
Pod documented
120 3 my $data;
121 3 my $n = $self->{n};
122 3 ▶ if (my $arg = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift()) { } 0 3 3
123 0 ▶ die 'Must pass an array reference to invrdft'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg eq "ARRAY" 0 0 0
124 unless ref($arg) eq 'ARRAY';
125 0 ▶ die "Size of data set must be $n"
Branch: 1 branch
BranchTrueFalseTotal
unless $n == @$arg 0 0 0
126 unless $n == @$arg;
127 0 $data = [ @$arg ];
128 }
129 else {
130 die 'Must invert data created with rdft'
131 3 ▶ unless $self->{type} eq 'rdft';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"type"} eq "rdft" 0 3 3
132 3 $data = [ @{$self->{coeff}} ];
133 }
134 3 _rdft($n, -1, $data, $self->{ip}, $self->{w});
135 3 $_ *= 2.0/$n for (@$data);
136 3 return $data;
137 }
138  
139 # Discrete Cosine Transform
140 sub ddct {
141 2 ▶ my $self = shift;
Subroutine ddct: called CC=2 SCAR=6.9
Pod documented
142 2 my $n = $self->{n};
143 2 ▶ die "data size ($n) must be an integer power of 2" unless _check_n($n);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_n($n) 0 2 2
144 2 my $data = [ @{$self->{data}} ];
145 2 _ddct($n, -1, $data, $self->{ip}, $self->{w});
146 2 $self->{type} = 'ddct';
147 2 $self->{coeff} = $data;
148 2 return $data;
149 }
150  
151 # Inverse Discrete Cosine Transform
152 sub invddct {
153 2 ▶ my $self = shift;
Subroutine invddct: called CC=6 SCAR=21.4
Pod documented
154 2 my $data;
155 2 my $n = $self->{n};
156 2 ▶ if (my $arg = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift()) { } 0 2 2
157 0 ▶ die 'Must pass an array reference to invddct'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg eq "ARRAY" 0 0 0
158 unless ref($arg) eq 'ARRAY';
159 0 ▶ die "Size of data set must be $n"
Branch: 1 branch
BranchTrueFalseTotal
unless $n == @$arg 0 0 0
160 unless $n == @$arg;
161 0 $data = [ @$arg ];
162 }
163 else {
164 die 'Must invert data created with ddct'
165 2 ▶ unless $self->{type} eq 'ddct';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"type"} eq "ddct" 0 2 2
166 2 $data = [ @{$self->{coeff}} ];
167 }
168 2 $data->[0] *= 0.5;
169 2 _ddct($n, 1, $data, $self->{ip}, $self->{w});
170 2 $_ *= 2.0/$n for (@$data);
171 2 return $data;
172 }
173  
174 # Discrete Sine Transform
175 sub ddst {
176 2 ▶ my $self = shift;
Subroutine ddst: called CC=2 SCAR=6.9
Pod documented
177 2 my $n = $self->{n};
178 2 ▶ die "data size ($n) must be an integer power of 2" unless _check_n($n);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_n($n) 0 2 2
179 2 my $data = [ @{$self->{data}} ];
180 2 _ddst($n, -1, $data, $self->{ip}, $self->{w});
181 2 $self->{type} = 'ddst';
182 2 $self->{coeff} = $data;
183 2 return $data;
184 }
185  
186 # Inverse Discrete Sine Transform
187 sub invddst {
188 2 ▶ my $self = shift;
Subroutine invddst: called CC=6 SCAR=21.4
Pod documented
189 2 my $data;
190 2 my $n = $self->{n};
191 2 ▶ if (my $arg = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift()) { } 0 2 2
192 0 ▶ die 'Must pass an array reference to invddst'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg eq "ARRAY" 0 0 0
193 unless ref($arg) eq 'ARRAY';
194 0 ▶ die "Size of data set must be $n"
Branch: 1 branch
BranchTrueFalseTotal
unless $n == @$arg 0 0 0
195 unless $n == @$arg;
196 0 $data = [ @$arg ];
197 }
198 else {
199 die 'Must invert data created with ddst'
200 2 ▶ unless $self->{type} eq 'ddst';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"type"} eq "ddst" 0 2 2
201 2 $data = [ @{$self->{coeff}} ];
202 }
203 2 $data->[0] *= 0.5;
204 2 _ddst($n, 1, $data, $self->{ip}, $self->{w});
205 2 $_ *= 2.0/$n for (@$data);
206 2 return $data;
207 }
208  
209 # Cosine Transform of RDFT (Real Symmetric DFT)
210 sub dfct {
211 2 ▶ my $self = shift;
Subroutine dfct: called CC=2 SCAR=6.9
Pod documented
212 2 my $np1 = $self->{n};
213 2 my $n = $np1 - 1;
214 2 ▶ die "data size ($n) must be an integer power of 2" unless _check_n($n);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_n($n) 0 2 2
215 2 my $nt = int(2 + $n/2);
216 2 my $t = [];
217 2 my $data = [ @{$self->{data}} ];
218 2 pdfct($nt, $n, $data, $t, $self->{ip}, $self->{w});
219 2 $self->{type} = 'dfct';
220 2 $self->{coeff} = $data;
221 2 return $data;
222 }
223  
224 # Inverse Cosine Transform of RDFT (Real Symmetric DFT)
225 sub invdfct {
226 2 ▶ my $self = shift;
Subroutine invdfct: called CC=6 SCAR=19.7
Pod documented
227 2 my $data;
228 2 my $np1 = $self->{n};
229 2 my $n = $np1 - 1;
230 2 ▶ if (my $arg = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift()) { } 0 2 2
231 0 ▶ die 'Must pass an array reference to invdfct'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg eq "ARRAY" 0 0 0
232 unless ref($arg) eq 'ARRAY';
233 0 ▶ die "Size of data set must be $n"
Branch: 1 branch
BranchTrueFalseTotal
unless $np1 == @$data 0 0 0
234 unless $np1 == @$data;
235 0 $data = [ @$arg ];
236 }
237 else {
238 die 'Must invert data created with dfct'
239 2 ▶ unless $self->{type} eq 'dfct';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"type"} eq "dfct" 0 2 2
240 2 $data = [ @{$self->{coeff}} ];
241 }
242 2 my $nt = int(2 + $n/2);
243 2 my $t = [];
244 2 $data->[0] *= 0.5;
245 2 $data->[$n] *= 0.5;
246 2 pdfct($nt, $n, $data, $t, $self->{ip}, $self->{w});
247 2 $data->[0] *= 0.5;
248 2 $data->[$n] *= 0.5;
249 2 $_ *= 2.0/$n for (@$data);
250 2 return $data;
251 }
252  
253 # Sine Transform of RDFT (Real Anti-symmetric DFT)
254 sub dfst {
255 2 ▶ my $self = shift;
Subroutine dfst: called CC=2 SCAR=6.9
Pod documented
256 2 my $n = $self->{n};
257 2 ▶ die "data size ($n) must be an integer power of 2" unless _check_n($n);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_n($n) 0 2 2
258 2 my $data = [ @{$self->{data}} ];
259 2 my $nt = int(2 + $n/2);
260 2 my $t = [];
261 2 pdfst($nt, $n, $data, $t, $self->{ip}, $self->{w});
262 2 $self->{type} = 'dfst';
263 2 $self->{coeff} = $data;
264 2 return $data;
265 }
266  
267 # Inverse Sine Transform of RDFT (Real Anti-symmetric DFT)
268 sub invdfst {
269 2 ▶ my $self = shift;
Subroutine invdfst: called CC=6 SCAR=21.0
Pod documented
270 2 my $n = $self->{n};
271 2 my $data;
272 2 ▶ if (my $arg = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift()) { } 0 2 2
273 0 ▶ die 'Must pass an array reference to invdfst'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg eq "ARRAY" 0 0 0
274 unless ref($arg) eq 'ARRAY';
275 0 ▶ die "Size of data set must be $n"
Branch: 1 branch
BranchTrueFalseTotal
unless $n == @$arg 0 0 0
276 unless $n == @$arg;
277 0 $data = [ @$arg ];
278 }
279 else {
280 die 'Must invert data created with dfst'
281 2 ▶ unless $self->{type} eq 'dfst';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"type"} eq "dfst" 0 2 2
282 2 $data = [ @{$self->{coeff}} ];
283 }
284 2 my $nt = int(2 + $n/2);
285 2 my $t = [];
286 2 pdfst($nt, $n, $data, $t, $self->{ip}, $self->{w});
287 2 $_ *= 2.0/$n for (@$data);
288 2 return $data;
289 }
290  
291 # check if $n is a power of 2
292 sub _check_n {
293 31 ▶ my ($n) = @_;
Subroutine _check_n: called CC=3 SCAR=14.2
294  
295 31 ▶ return scalar($n == int($n) and $n > 0 and (! ($n & ($n-1))));
Condition33%2/6
$n == int $n && $n > 0 && !($n & $n - 1)
!ll&&!rl&&r
0031
$n == int $n and $n > 0
!ll&&!rl&&r
0031
MC/DC0%0/3
$n == int $n && $n > 0 && !($n & $n - 1)
$n == int $n $n > 0 !($n & $n - 1)
Truth table25%1/4
A and B && not(C)
A= $n == int $nB= $n > 0C= !($n & $n - 1)
ABCresult
0XX0
10X0
1100
1111
296 }
297  
298 sub correl {
299 4 ▶ my ($self, $other) = @_;
Subroutine correl: called CC=9 SCAR=22.5
Pod documented
300 4 my $n = $self->{n};
301 my $d1 = $self->{type} ?
302 3 ▶ ($self->{type} eq 'rdft' ? [ @{$self->{coeff}} ] :
Condition50%1/2
$self->rdft and [@{$self->{"coeff"};}]
!ll
01
MC/DC0%0/1
$self->rdft and [@{$self->{"coeff"};}]
$self->rdft
Truth table50%1/2
A
A= $self->rdft
Aresult
00
11
303 die 'correl must involve a real function' ) :
304 4 ▶ $self->rdft && [ @{$self->{coeff}} ];
Branch: 2 branches
BranchTrueFalseTotal
$self->{'type'} ? : 3 1 4
$self->{'type'} eq 'rdft' ? : 3 0 3
305 4 my $d2 = [];
306 4 ▶ if (ref($other) eq 'Math::FFT') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $other eq 'Math::FFT') { } 2 2 4
elsif (ref $other eq 'ARRAY') { } 2 0 2
307 $d2 = $other->{type} ?
308 1 ▶ ($other->{type} eq 'rdft' ? [ @{$other->{coeff}}] :
Condition50%1/2
$other->rdft and [@{$other->{"coeff"};}]
!ll
01
MC/DC0%0/1
$other->rdft and [@{$other->{"coeff"};}]
$other->rdft
Truth table50%1/2
A
A= $other->rdft
Aresult
00
11
309 die 'correl must involve a real function' ) :
310 2 ▶ $other->rdft && [ @{$other->{coeff}}];
Branch: 2 branches
BranchTrueFalseTotal
$other->{'type'} ? : 1 1 2
$other->{'type'} eq 'rdft' ? : 1 0 1
311 }
312 elsif (ref($other) eq 'ARRAY') {
313 2 $d2 = [ @$other ];
314 2 _rdft($n, 1, $d2, $self->{ip}, $self->{w});
315 }
316 else {
317 0 die 'Must call correl with either a Math::FFT object or an array ref';
318 }
319 4 my $corr = [];
320 4 _correl($n, $corr, $d1, $d2, $self->{ip}, $self->{w});
321 4 return $corr;
322 }
323  
324 sub convlv {
325 2 ▶ my ($self, $r) = @_;
Subroutine convlv: called CC=8 SCAR=21.8
Pod documented
326 2 ▶ die 'Must call convlv with an array reference for the response data'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $r eq "ARRAY" 0 2 2
327 unless ref($r) eq 'ARRAY';
328 2 my $respn = [ @$r ];
329 2 my $m = @$respn;
330 2 ▶ die 'size of response data must be an odd integer' unless $m % 2 == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $m % 2 == 1 0 2 2
331 2 my $n = $self->{n};
332 my $d1 = $self->{type} ?
333 2 ▶ ($self->{type} eq 'rdft' ? [ @{$self->{coeff}} ] :
Condition0%0/2
$self->rdft and [@{$self->{"coeff"};}]
!ll
00
MC/DC0%0/1
$self->rdft and [@{$self->{"coeff"};}]
$self->rdft
Truth table0%0/2
A
A= $self->rdft
Aresult
00
11
334 die 'correl must involve a real function' ) :
335 2 ▶ $self->rdft && [ @{$self->{coeff}} ];
Branch: 2 branches
BranchTrueFalseTotal
$self->{'type'} ? : 2 0 2
$self->{'type'} eq 'rdft' ? : 2 0 2
336 2 ▶ for (my $i=1; $i<=($m-1)/2; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= ($m - 1) / 2) 8 2 10
337 8 $respn->[$n-$i] = $respn->[$m-$i];
338 }
339 2 ▶ for (my $i=($m+3)/2; $i<=$n-($m-1)/2; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n - ($m - 1) / 2) 14 2 16
340 14 $respn->[$i-1] = 0.0;
341 }
342 2 my $convlv = [];
343 2 _convlv($n, $convlv, $d1, $respn, $self->{ip}, $self->{w});
344 2 return $convlv;
345 }
346  
347 sub deconvlv {
348 1 ▶ my ($self, $r) = @_;
Subroutine deconvlv: called CC=9 SCAR=23.6
Pod documented
349 1 ▶ die 'Must call deconvlv with an array reference for the response data'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $r eq "ARRAY" 0 1 1
350 unless ref($r) eq 'ARRAY';
351 1 my $respn = [ @$r ];
352 1 my $m = @$respn;
353 1 ▶ die 'size of response data must be an odd integer' unless $m % 2 == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $m % 2 == 1 0 1 1
354 1 my $n = $self->{n};
355 my $d1 = $self->{type} ?
356 0 ▶ ($self->{type} eq 'rdft' ? [ @{$self->{coeff}} ] :
Condition50%1/2
$self->rdft and [@{$self->{"coeff"};}]
!ll
01
MC/DC0%0/1
$self->rdft and [@{$self->{"coeff"};}]
$self->rdft
Truth table50%1/2
A
A= $self->rdft
Aresult
00
11
357 die 'correl must involve a real function' ) :
358 1 ▶ $self->rdft && [ @{$self->{coeff}} ];
Branch: 2 branches
BranchTrueFalseTotal
$self->{'type'} ? : 0 1 1
$self->{'type'} eq 'rdft' ? : 0 0 0
359 1 ▶ for (my $i=1; $i<=($m-1)/2; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= ($m - 1) / 2) 4 1 5
360 4 $respn->[$n-$i] = $respn->[$m-$i];
361 }
362 1 ▶ for (my $i=($m+3)/2; $i<=$n-($m-1)/2; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n - ($m - 1) / 2) 7 1 8
363 7 $respn->[$i-1] = 0.0;
364 }
365 1 my $convlv = [];
366 1 ▶ if (_deconvlv($n, $convlv, $d1, $respn, $self->{ip}, $self->{w}) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (_deconvlv($n, $convlv, $d1, $respn, $self->{"ip"}, $self->{"w"}) != 0) 0 1 1
367 0 die "Singularity encountered for response in deconvlv";
368 }
369 1 return $convlv;
370 }
371  
372 {
373  
374 4 my $PI2 = 2 * 4.0 * atan2(1,1);
375 sub spctrm {
376 12 ▶ my ($self, %args) = @_;
Subroutine spctrm: called CC=31 SCAR=35.0
Pod documented
377 12 my %accept = map {$_ => 1} qw(window segments number overlap);
378 12 for (keys %args) {
379 39 ▶ die "`$_' is not a valid argument to spctrm" if not $accept{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $accept{$_} 0 39 39
380 }
381 12 my $win_fun = $args{window};
382 12 ▶ if ($win_fun and ref($win_fun) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($win_fun and ref $win_fun ne "CODE") 7 5 12
Condition100%3/3
$win_fun and ref $win_fun ne "CODE"
!ll&&!rl&&r
327
MC/DC100%2/2
$win_fun and ref $win_fun ne "CODE"
$win_fun ref $win_fun ne "CODE"
Truth table100%3/3
A and B
A= $win_funB= ref $win_fun ne "CODE"
ABresult
0X0
100
111
383 7 my %accept = map {$_ => 1} qw(hamm hann welch bartlett);
384 die "`$win_fun' is not a known window function in spctrm"
385 7 ▶ if not $accept{$win_fun};
Branch: 1 branch
BranchTrueFalseTotal
unless $accept{$win_fun} 0 7 7
386 }
387 die 'Please specify a value for "segments" in spctrm()'
388 12 ▶ if ($args{number} and ! $args{segments});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"number"} and not $args{"segments"} 0 12 12
Condition66%2/3
$args{"number"} and not $args{"segments"}
!ll&&!rl&&r
2100
MC/DC0%0/2
$args{"number"} and not $args{"segments"}
$args{"number"} not $args{"segments"}
Truth table66%2/3
A and not(B)
A= $args{"number"}B= not $args{"segments"}
ABresult
0X0
100
111
389 12 my $n = $self->{n};
390 12 my $d;
391 12 my $n2 = 0;
392 12 my $spctrm = [];
393 12 my $win_sub = do {
394 my $h = sub {
395 64 ▶ my ($j, $n) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
396 64 return (1 - cos($PI2*$j/$n))/2;
397 12 };
398  
399 +{
400 'hamm' => $h,
401 'hann' => $h,
402 'welch' => sub {
403 64 ▶ my ($j, $n) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
404 64 return 1 - 4*($j-$n/2)*($j-$n/2)/$n/$n;
405 },
406 'bartlett' => sub {
407 80 ▶ my ($j, $n) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
408 80 return 1 - abs(2*($j-$n/2)/$n);
409 },
410 }
411 12 };
412 12 ▶ if (not $args{segments} or ($args{segments} == 1 and not $args{number})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{'segments'} or $args{'segments'} == 1 and not $args{'number'}) { } 2 10 12
Condition50%3/6
not $args{"segments"} or $args{"segments"} == 1 and not $args{"number"}
l!l&&r!l&&!r
2010
$args{"segments"} == 1 and not $args{"number"}
!ll&&!rl&&r
1000
MC/DC33%1/3
not $args{"segments"} or $args{"segments"} == 1 and not $args{"number"}
not $args{"segments"} $args{"segments"} == 1 not $args{"number"}
Truth table50%2/4
not(A) or B and not(C)
A= not $args{"segments"}B= $args{"segments"} == 1C= not $args{"number"}
ABCresult
1XX1
0111
00X0
0100
413 2 ▶ die "data size ($n) must be an integer power of 2" unless _check_n($n);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_n($n) 0 2 2
414 2 ▶ if ($win_fun) {
Branch: 1 branch
BranchTrueFalseTotal
if ($win_fun) { } 1 1 2
415 1 $d = [ @{$self->{data}}];
416 1 ▶ $win_fun = $win_sub->{$win_fun} if ref($win_fun) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $win_fun ne "CODE" 1 0 1
417 1 ▶ for (my $j=0; $j<$n; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 16 1 17
418 16 my $w = $win_fun->($j, $n);
419 16 $d->[$j] *= $w;
420 16 $n2 += $w * $w;
421 }
422 1 $n2 *= $n;
423 1 _spctrm($n, $spctrm, $d, $self->{ip}, $self->{w}, $n2, 1);
424 }
425 else {
426 $d = $self->{type} ?
427 ($self->{type} eq 'rdft' ? $self->{coeff} :
428 die 'correl must involve a real function' ) :
429 1 ▶ $self->rdft && $self->{coeff};
Branch: 2 branches
BranchTrueFalseTotal
$self->{'type'} ? : 0 1 1
$self->{'type'} eq 'rdft' ? : 0 0 0
Condition33%1/3
$self->rdft and $self->{"coeff"}
!ll&&!rl&&r
001
MC/DC0%0/2
$self->rdft and $self->{"coeff"}
$self->rdft $self->{"coeff"}
Truth table33%1/3
A and B
A= $self->rdftB= $self->{"coeff"}
ABresult
0X0
100
111
430 1 $n2 = $n*$n;
431 1 _spctrm($n, $spctrm, $d, $self->{ip}, $self->{w}, $n2, 0);
432 }
433 }
434 else {
435 10 $d = [ @{$self->{data}}];
436 10 my ($data, @w);
437 10 my $k = $args{segments};
438 10 my $m = $args{number};
439 10 ▶ die 'Please specify a value for "number" in spctrm()'
Branch: 1 branch
BranchTrueFalseTotal
if $k and not $m 0 10 10
Condition33%1/3
$k and not $m
!ll&&!rl&&r
0100
MC/DC0%0/2
$k and not $m
$k not $m
Truth table33%1/3
A and not(B)
A= $kB= not $m
ABresult
0X0
100
111
440 if ($k and ! $m);
441 10 ▶ die "number ($m) must be an integer power of 2" unless _check_n($m);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_n($m) 0 10 10
442 10 my $m2 = $m+$m;
443 10 my $overlap = $args{overlap};
444 10 ▶ my $N = $overlap ? ($k+1)*$m : 2*$k*$m;
Branch: 1 branch
BranchTrueFalseTotal
$overlap ? : 5 5 10
445 10 ▶ die "Need $N data points (data only has $n)" if $N > $n;
Branch: 1 branch
BranchTrueFalseTotal
if $N > $n 0 10 10
446 10 ▶ if ($win_fun) {
Branch: 1 branch
BranchTrueFalseTotal
if ($win_fun) { } 8 2 10
447 8 ▶ $win_fun = $win_sub->{$win_fun} if ref($win_fun) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $win_fun ne "CODE" 6 2 8
448 8 ▶ for (my $j=0; $j<$m2; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $m2) 256 8 264
449 256 $w[$j] = $win_fun->($j, $m2);
450 256 $n2 += $w[$j]*$w[$j];
451 }
452 }
453 else {
454 2 $n2 = $m2;
455 }
456 10 ▶ if ($overlap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overlap) { } 5 5 10
457 5 my @old = splice(@$d, 0, $m);
458 5 for (0..$k-1) {
459 80 push @{$data->[$_]}, @old;
460 80 my @new = splice(@$d, 0, $m);
461 80 push @{$data->[$_]}, @new;
462 80 @old = @new;
463 80 ▶ if ($win_fun) {
Branch: 1 branch
BranchTrueFalseTotal
if ($win_fun) 64 16 80
464 64 my $j=0;
465 64 $data->[$_] = [ map {$w[$j++]*$_} @{$data->[$_]}];
466 }
467 }
468 }
469 else {
470 5 for (0..$k-1) {
471 160 push @{$data->[$_]}, splice(@$d, 0, $m2);
472 160 ▶ if ($win_fun) {
Branch: 1 branch
BranchTrueFalseTotal
if ($win_fun) 128 32 160
473 128 my $j=0;
474 128 $data->[$_] = [ map {$w[$j++]*$_} @{$data->[$_]}];
475 }
476 }
477 }
478 10 my $tmp = [];
479 10 my $nip = int(3 + sqrt($m2));
480 10 my $nw = int(2 + 5*$m2/4);
481 10 my $ip = pack("i$nip", ());
482 10 my $w = pack("d$nw", ());
483 10 _spctrm_bin($k, $m2, $spctrm, $data, \$ip, \$w, $n2, $tmp);
484 }
485 12 return $spctrm;
486 }
487 }
488  
489 4 sub mean {
490 1 ▶ my $self = shift;
Subroutine mean: called CC=5 SCAR=18.0
Pod documented
491 1 my $sum = 0;
492 1 my ($n, $data);
493 1 my $flag = 0;
494 1 ▶ if ($data = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data = shift()) { } 0 1 1
495 0 ▶ die 'Must call with an array reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 0 0 0
496 unless ref($data) eq 'ARRAY';
497 0 $n = @$data;
498 0 $flag = 1;
499 }
500 else {
501 1 $data = $self->{data};
502 1 $n = $self->{n};
503 }
504 1 $sum += $_ for @$data;
505 1 my $mean = $sum / $n;
506 1 ▶ $self->{mean} = $mean unless $flag == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $flag == 1 1 0 1
507 1 return $mean;
508 }
509  
510 sub rms {
511 1 ▶ my $self = shift;
Subroutine rms: called CC=4 SCAR=15.5
Pod documented
512 1 my $sum = 0;
513 1 my ($n, $data);
514 1 ▶ if ($data = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data = shift()) { } 0 1 1
515 0 ▶ die 'Must call with an array reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 0 0 0
516 unless ref($data) eq 'ARRAY';
517 0 $n = @$data;
518 }
519 else {
520 1 $data = $self->{data};
521 1 $n = $self->{n};
522 }
523 1 $sum += $_*$_ for @$data;
524 1 return sqrt($sum / $n);
525 }
526  
527 sub stdev {
528 1 ▶ my $self = shift;
Subroutine stdev: called CC=6 SCAR=21.4
Pod documented
529 1 my ($n, $data, $mean);
530 1 ▶ if ($data = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data = shift()) { } 0 1 1
531 0 ▶ die 'Must call with an array reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 0 0 0
532 unless ref($data) eq 'ARRAY';
533 0 $n = @$data;
534 0 $mean = $self->mean($data);
535 }
536 else {
537 1 $data = $self->{data};
538 1 $n = $self->{n};
539 1 ▶ $mean = $self->{mean} || $self->mean;
Condition33%1/3
$self->{'mean'} || $self->mean
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{'mean'} || $self->mean
$self->{'mean'} $self->mean
Truth table33%1/3
A || B
A= $self->{'mean'}B= $self->mean
ABresult
1X1
011
000
540 }
541 1 ▶ die 'Cannot find the standard deviation with n = 1'
Branch: 1 branch
BranchTrueFalseTotal
if $n == 1 0 1 1
542 if $n == 1;
543 1 my $sum = 0;
544 1 $sum += ($_ - $mean)*($_ - $mean) for @$data;
545 1 return sqrt($sum / ($n-1));
546 }
547  
548 sub range {
549 1 ▶ my $self = shift;
Subroutine range: called CC=6 SCAR=19.2
Pod documented
550 1 my ($n, $data);
551 1 ▶ if ($data = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data = shift()) { } 0 1 1
552 0 ▶ die 'Must call with an array reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 0 0 0
553 unless ref($data) eq 'ARRAY';
554 0 $n = @$data;
555 }
556 else {
557 1 $data = $self->{data};
558 1 $n = $self->{n};
559 }
560 1 my $min = $data->[0];
561 1 my $max = $data->[0];
562 1 for (@$data) {
563 5 ▶ $min = $_ if $_ < $min;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < $min 0 5 5
564 5 ▶ $max = $_ if $_ > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $max 4 1 5
565 }
566 1 return ($min, $max);
567 }
568  
569 sub median {
570 2 ▶ my $self = shift;
Subroutine median: called CC=4 SCAR=15.0
Pod documented
571 2 my ($n, $data);
572 2 ▶ if ($data = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data = shift()) { } 0 2 2
573 0 ▶ die 'Must call with an array reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 0 0 0
574 unless ref($data) eq 'ARRAY';
575 0 $n = @$data;
576 }
577 else {
578 2 $data = $self->{data};
579 2 $n = $self->{n};
580 }
581 2 my @sorted = sort {$a <=> $b} @$data;
582 return
583 (
584 2 ▶ ($n & 0x1)
Branch: 1 branch
BranchTrueFalseTotal
$n & 1 ? : 1 1 2
585 ? $sorted[($n-1)/2]
586 : ($sorted[$n/2] + $sorted[$n/2-1])/2
587 );
588 }
589  
590 # Autoload methods go after =cut, and are processed by the autosplit program.
591  
592  
593 4 1;
594  
595 __END__
↑ Summary arrays.c »