add a unit test for kalm ... 44/11044/2
Sylvestre Ledru [Wed, 27 Mar 2013 10:29:06 +0000 (11:29 +0100)]
Change-Id: Iba6c93b52cae4a18b7cf1244aada0192e82c13d3

scilab/modules/signal_processing/tests/unit_tests/kalm.dia.ref [new file with mode: 0644]
scilab/modules/signal_processing/tests/unit_tests/kalm.tst [new file with mode: 0644]
scilab/modules/signal_processing/tests/unit_tests/srkf.dia.ref [new file with mode: 0644]
scilab/modules/signal_processing/tests/unit_tests/srkf.tst [new file with mode: 0644]

diff --git a/scilab/modules/signal_processing/tests/unit_tests/kalm.dia.ref b/scilab/modules/signal_processing/tests/unit_tests/kalm.dia.ref
new file mode 100644 (file)
index 0000000..eadf00b
--- /dev/null
@@ -0,0 +1,90 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Charlotte Hecquet
+// Copyright (C) 2013 - Scilab Enterprises - Sylvestre Ledru
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+w=%pi/4; // angular frequency
+T=0.1; // period
+t=0:T:5
+ t  =
+         column  1 to 10
+    0.    0.1    0.2    0.3    0.4    0.5    0.6    0.7    0.8    0.9  
+         column 11 to 20
+    1.    1.1    1.2    1.3    1.4    1.5    1.6    1.7    1.8    1.9  
+         column 21 to 30
+    2.    2.1    2.2    2.3    2.4    2.5    2.6    2.7    2.8    2.9  
+         column 31 to 40
+    3.    3.1    3.2    3.3    3.4    3.5    3.6    3.7    3.8    3.9  
+         column 41 to 50
+    4.    4.1    4.2    4.3    4.4    4.5    4.6    4.7    4.8    4.9  
+         column 51
+    5.  
+signal=cos(w*t);
+// Sinusoid with noise
+v=0:1:50
+ v  =
+         column  1 to 11
+    0.    1.    2.    3.    4.    5.    6.    7.    8.    9.    10.  
+         column 12 to 21
+    11.    12.    13.    14.    15.    16.    17.    18.    19.    20.  
+         column 22 to 31
+    21.    22.    23.    24.    25.    26.    27.    28.    29.    30.  
+         column 32 to 41
+    31.    32.    33.    34.    35.    36.    37.    38.    39.    40.  
+         column 42 to 51
+    41.    42.    43.    44.    45.    46.    47.    48.    49.    50.  
+y=signal+v;
+// System
+n=2; // system order
+f=[cos(w*T) -sin(w*T); sin(w*T) cos(w*T)];
+g=0;
+h=[1 0];
+p0=[1000 0; 0 0];
+R=1;
+Q=0;
+x0=zeros(n,1);
+// Initialize for loop
+x1=x0;
+p1=p0;
+// Kalman filter
+xsum=0;
+x1sum=0;
+psum=0;
+p1sum=0;
+for i=1:length(t)-1
+    [x1(:,i+1),p1,x,p]=kalm(y(i),x1(:,i),p1,f,g,h,Q,R);
+    xsum = xsum + x;
+    p1sum = p1sum + p1;
+    psum = psum + p;
+    x1sum = x1sum + x1(:,i+1);
+end
+assert_checkalmostequal(xsum, [295.374859628719719;202.134913816696837]);
+assert_checkalmostequal(p1sum,  [3.88430845743643571,0.63055345519216977;0.63055345519217121,2.08824467576958606]);
+assert_checkalmostequal(psum, [3.97189248710404863,0.48230715738369057;0.48230715738369184,2.0006606461019758]);
+assert_checkalmostequal(x1sum,  [278.60499495977632;224.686643723725581]);
diff --git a/scilab/modules/signal_processing/tests/unit_tests/kalm.tst b/scilab/modules/signal_processing/tests/unit_tests/kalm.tst
new file mode 100644 (file)
index 0000000..c4e09b7
--- /dev/null
@@ -0,0 +1,46 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Charlotte Hecquet
+// Copyright (C) 2013 - Scilab Enterprises - Sylvestre Ledru
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+
+w=%pi/4; // angular frequency
+T=0.1; // period
+t=0:T:5
+signal=cos(w*t);
+// Sinusoid with noise
+v=0:1:50
+y=signal+v;
+// System
+n=2; // system order
+f=[cos(w*T) -sin(w*T); sin(w*T) cos(w*T)];
+g=0;
+h=[1 0];
+p0=[1000 0; 0 0];
+R=1;
+Q=0;
+x0=zeros(n,1);
+// Initialize for loop
+x1=x0;
+p1=p0;
+// Kalman filter
+xsum=0;
+x1sum=0;
+psum=0;
+p1sum=0;
+for i=1:length(t)-1
+    [x1(:,i+1),p1,x,p]=kalm(y(i),x1(:,i),p1,f,g,h,Q,R);
+    xsum = xsum + x;
+    p1sum = p1sum + p1;
+    psum = psum + p;
+    x1sum = x1sum + x1(:,i+1);
+end
+assert_checkalmostequal(xsum, [295.374859628719719;202.134913816696837]);
+
+assert_checkalmostequal(p1sum,  [3.88430845743643571,0.63055345519216977;0.63055345519217121,2.08824467576958606]);
+
+assert_checkalmostequal(psum, [3.97189248710404863,0.48230715738369057;0.48230715738369184,2.0006606461019758]);
+assert_checkalmostequal(x1sum,  [278.60499495977632;224.686643723725581]);
+
diff --git a/scilab/modules/signal_processing/tests/unit_tests/srkf.dia.ref b/scilab/modules/signal_processing/tests/unit_tests/srkf.dia.ref
new file mode 100644 (file)
index 0000000..0564fa0
--- /dev/null
@@ -0,0 +1,19 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Charlotte Hecquet
+// Copyright (C) 2013 - Scilab Enterprises - Sylvestre Ledru
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+f=[0 0 1; 0 1 0; 2 3 4]; //State matrix
+g=[1;-1;1]; //Input matrix
+h=[1 1 0; 0 1 0; 0 0 0]; //Output matrix
+Q=[3 2 1; 2 2 1; 1 1 1]; //Covariance matrix of dynamic noise
+R=[2 1 1; 1 1 1; 1 1 2]; //Covariance matrix of observation noise
+// Initialisation
+p0=[6 3 2; 3 5 2; 2 2 4];
+x0=[1;1;1];
+y=[2 8 7]'; // Current observation output matrix
+[x1,p1]=srkf(y,x0,p0,f,h,Q,R);
+assert_checkalmostequal(p1, [-3.3826684796422888,0,0;-0.65452136096190749,1.43147468495118102,0;-10.588783837170105,-2.49768257747417177,-5.87595341369592727]);
+assert_checkalmostequal(x1, [1.07553956834532438;4.10971223021582333;6.06654676258992787]);
diff --git a/scilab/modules/signal_processing/tests/unit_tests/srkf.tst b/scilab/modules/signal_processing/tests/unit_tests/srkf.tst
new file mode 100644 (file)
index 0000000..3551ead
--- /dev/null
@@ -0,0 +1,21 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Charlotte Hecquet
+// Copyright (C) 2013 - Scilab Enterprises - Sylvestre Ledru
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+
+f=[0 0 1; 0 1 0; 2 3 4]; //State matrix
+g=[1;-1;1]; //Input matrix
+h=[1 1 0; 0 1 0; 0 0 0]; //Output matrix
+Q=[3 2 1; 2 2 1; 1 1 1]; //Covariance matrix of dynamic noise
+R=[2 1 1; 1 1 1; 1 1 2]; //Covariance matrix of observation noise
+// Initialisation
+p0=[6 3 2; 3 5 2; 2 2 4];
+x0=[1;1;1];
+y=[2 8 7]'; // Current observation output matrix
+[x1,p1]=srkf(y,x0,p0,f,h,Q,R);
+assert_checkalmostequal(p1, [-3.3826684796422888,0,0;-0.65452136096190749,1.43147468495118102,0;-10.588783837170105,-2.49768257747417177,-5.87595341369592727]);
+assert_checkalmostequal(x1, [1.07553956834532438;4.10971223021582333;6.06654676258992787]);
+