test_Procrustes.praat 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # test_Procrustes.praat
  2. # djmw 20040117, 20070820
  3. appendInfoLine: "test_Procrustes.praat"
  4. eps =2.3e-15
  5. @bg_19_4
  6. @test_orthogonal_procrustes_gvl_12_4_1
  7. @test_procrustes_random_configurations: 12
  8. appendInfoLine: "test_Procrustes.praat OK"
  9. procedure bg_19_4
  10. # example Borg & Groenen section 19.4
  11. appendInfoLine: tab$, "Example in Borg & Groenen section 19.4"
  12. .nrow = 4
  13. .ncol = 2
  14. .x1# = { 1, 2}
  15. .x2# = {-1, 2}
  16. .x3# = {-1, -2}
  17. .x4# = { 1, -2}
  18. .y1# = {0.07, 2.62}
  19. .y2# = {0.93, 3.12}
  20. .y3# = {1.93, 1.38}
  21. .y4# = {1.07, 0.88}
  22. .xconf = Create Configuration: "X", .nrow, .ncol, "0"
  23. .yconf = Create Configuration: "Y", .nrow, .ncol, "0"
  24. for .icol to .ncol
  25. selectObject: .xconf
  26. Set value: 1, .icol, .x1# [.icol]
  27. Set value: 2, .icol, .x2# [.icol]
  28. Set value: 3, .icol, .x3# [.icol]
  29. Set value: 4, .icol, .x4# [.icol]
  30. selectObject: .yconf
  31. Set value: 1, .icol, .y1# [.icol]
  32. Set value: 2, .icol, .y2# [.icol]
  33. Set value: 3, .icol, .y3# [.icol]
  34. Set value: 4, .icol, .y4# [.icol]
  35. endfor
  36. selectObject: .xconf, .yconf
  37. .p = To Procrustes: "no"
  38. .t_given# = {3.72, -2.46}; 3.73, -2.47
  39. .r1_given# = {-0.87, -0.50}
  40. .r2_given# = {-0.50, 0.87}
  41. .s_given = 2.0
  42. .s = Get scale
  43. .srounded = number (fixed$ (.s, 1))
  44. assert .srounded = .s_given; '.srounded' = '.s_given'
  45. for .i to .ncol
  46. .t = Get translation element: .i
  47. .trounded = number (fixed$ (.t, 2))
  48. .tgiven = .t_given#[.i]
  49. assert .trounded = .tgiven; '.trounded' = '.tgiven'
  50. endfor
  51. removeObject: .xconf, .yconf, .p
  52. endproc
  53. procedure test_procrustes_random_configurations: .numconf
  54. appendInfoLine: tab$, .numconf, " randomly generated configurations of dimension 2^k x 2"
  55. .nc = 2
  56. for .k to .numconf
  57. .nr = 2^.k
  58. appendInfoLine: tab$, tab$, .nr, " rows"
  59. .c_x = Create Configuration: "X", .nr, .nc, "randomUniform(-1, 1)"
  60. .c_y = Copy: "Y"
  61. Invert dimension: 1
  62. .alpha = randomUniform (0,90)
  63. Rotate: 1, 2, .alpha
  64. .t1 = randomUniform (0,2)
  65. .t2 = randomUniform (0,2)
  66. .scale = randomUniform(0.5,2)
  67. Formula... .scale*self + (if col=1 then .t1 else .t2 fi)
  68. plusObject: .c_x
  69. .p_xy = To Procrustes: 0
  70. Rename: "X_Y"
  71. plusObject: .c_y
  72. .c_z = To Configuration
  73. Rename: "Z"
  74. Formula: "self -object[.c_x, row, col]"
  75. .eps = 10 * .nr * .nc * eps
  76. for .i to .nr
  77. for .j to 2
  78. assert object[.c_z, .i, .j] < .eps; Configuration_Z['.i','.j'] < '.eps'
  79. endfor
  80. endfor
  81. selectObject: .p_xy
  82. .p_xy_i = Invert
  83. Rename: "X_Yi"
  84. # no need to test the translations, they need not be equal (see BG page 347)
  85. .sp = Get scale
  86. assert abs(.scale - .sp) < .eps; '.scale' '.sp'
  87. removeObject: .c_x, .c_y, .c_z, .p_xy, .p_xy_i
  88. endfor
  89. endproc
  90. procedure test_orthogonal_procrustes_gvl_12_4_1
  91. appendInfoLine: tab$, "Orthognal Procrustes transform (example 12.4.1 Golub & van Loan)"
  92. .a = Create Configuration: "a", 4, 2, "0"
  93. Set value: 1, 1, 1
  94. Set value: 2, 1, 3
  95. Set value: 3, 1, 5
  96. Set value: 4, 1, 7
  97. Set value: 1, 2, 2
  98. Set value: 2, 2, 4
  99. Set value: 3, 2, 6
  100. Set value: 4, 2, 8
  101. .b = Create Configuration: "b", 4, 2, "0"
  102. Set value: 1, 1, 1.2
  103. Set value: 2, 1, 2.9
  104. Set value: 3, 1, 5.2
  105. Set value: 4, 1, 6.8
  106. Set value: 1, 2, 2.1
  107. Set value: 2, 2, 4.3
  108. Set value: 3, 2, 6.1
  109. Set value: 4, 2, 8.1
  110. selectObject: .a, .b
  111. .p = To Procrustes: "yes"
  112. .t2 = Get translation element: 2
  113. assert .t2 = 0
  114. .s = Get scale
  115. assert .s = 1;
  116. .eps = 5e-5
  117. .r11 = Get transformation element: 1, 1
  118. .r22 = Get transformation element: 2, 2
  119. assert .r11-.r22 < .eps; '.r11'-'.r22' < '.eps' ?
  120. assert .r11-0.9999 < .eps; '.r11'-0.9999 < '.eps' ?
  121. .r12 = Get transformation element: 1, 2
  122. .r21 = Get transformation element: 2, 1
  123. assert .r12+0.0126 < .eps; '.r12'+0.0126 < '.eps' ?
  124. assert .r21-0.0126 < .eps; '.r21'-0.0126 < '.eps' ?
  125. removeObject: .a, .b, .p
  126. endproc