File: linfit.t

package info (click to toggle)
libpdl-fit-perl 2.100-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 164 kB
  • sloc: ansic: 249; perl: 179; makefile: 3
file content (99 lines) | stat: -rw-r--r-- 1,973 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use strict;
use warnings;
use Test::More;
use PDL::Fit::Linfit;
use PDL::LiteF;
use Test::PDL;

{
# Simple Test Case:

# Generate data from a set of functions
my $xvalues = sequence(100);
my $data = 3*$xvalues + 2*cos($xvalues) + 3*sin($xvalues*2);

# Fit functions are the linear, cos, and sin2x functions used in
#   the data generation step above:
my $fitFuncs = cat $xvalues, cos($xvalues), sin($xvalues*2);

# Perform the fit, Coefs should equal 3,2,3
my ($yfit, $coeffs) = PDL::linfit1d($data, $fitFuncs);

my @coefs = $coeffs->list;

is_pdl $coeffs, pdl([3,2,3]);
}

{
# More Complex Example


my $noPoints = 501;

my @expectedCoefs = qw( 0.988375918186647 -0.000278823311771375 0.161669997297754 0.0626069008452451);

my $noCoefs = 4;
my $i;
my  ($deltaT,$Amp,$lin,$HOper,$AmpHO,$Amphalf,$Ampfull);

my @PulsedB;

my  @Pulse;
my  $psum = 0;

my  $pi = 3.1415926;

my  $Pwidth = 2000;
my $pave;

$deltaT = 4;  # 4 nS increments
$Amp = 2.8;   # 2.8V amplitude of pulse
$lin = .2;
$HOper = 200;  # HO period
$AmpHO=.1;
$Amphalf = .5;
$Ampfull = .2;

# generate waveform:
for(my $i = 0; $i < $noPoints; $i++){
	$PulsedB[$i]=
		-$lin*1e-3*$i*$deltaT +
		$Amphalf*sin($pi/$Pwidth*$i*$deltaT)  +
		$Ampfull*sin(2*$pi/$Pwidth*$i*$deltaT) +
		$AmpHO*sin(2*$pi/$HOper*$i*$deltaT);

	$Pulse[$i] = $Amp*exp($PulsedB[$i]/20*2.3025851);
	$psum += $Pulse[$i];  # used to get DC value

	# printf(" %4d  %g  %g\n",$i,$PulsedB[$i],$Pulse[$i]);

}

$pave = $psum/$noPoints;

# printf("DC Value = %g\n",$pave);

# Make PDL from waveform:
my $data = PDL->new(\@Pulse);
my @functions;

# setup matrix contains functions to fit
for ($i=0; $i<$noPoints; $i++) {

$functions[0][$i] = $pave;
$functions[1][$i] = $i;
$functions[2][$i] = sin($pi*$i/($noPoints-1));
$functions[3][$i] = sin(2*$pi*$i/($noPoints-1));

}

my $fitFuncs = PDL->new(\@functions);

my ($yfit, $coeffs) = linfit1d($data, $fitFuncs);

my @coefs = $coeffs->list;

is_pdl $coeffs, pdl( \@expectedCoefs );
}

done_testing;