mean.praat 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. writeInfoLine: "Mean..."
  2. durations# = zero# (100)
  3. for n from 1 to 100
  4. result$ = Praat test: "TimeMean", string$ (10^8 / n), string$ (n), "", ""
  5. durations# [n] = extractNumber (result$, newline$)
  6. appendInfoLine (n, " ", durations# [n])
  7. endfor
  8. appendInfoLine: "mean ", mean (durations#), " nanoseconds"
  9. n = 1e5+1
  10. n7 = 7 * n
  11. d = 0
  12. d = 0.234567
  13. ;d = 0.000547462463
  14. big0 = 1 + d
  15. sequenceA# = { 1, 2, 3, 4, 5, 6, 7 }
  16. meanA = mean (sequenceA#)
  17. stdevA = stdev (sequenceA#) * sqrt (6 / 7) / sqrt (1 - 1 / n7)
  18. sequenceB# = linear# (1, n, n, 0)
  19. meanB = mean (sequenceB#)
  20. stdevB = stdev (sequenceB#)
  21. sequenceC# = { 0 }
  22. meanC = 0.0
  23. stdevC = 0.0
  24. Debug: "no", 48 ; naive mean
  25. big = big0
  26. for power from 1 to 25
  27. big *= 10
  28. a# = repeat# (big + sequenceA#, n)
  29. mean1 = mean (a#)
  30. dmean1 = mean1 - big - meanA
  31. mean2 = a# [1] + mean (- a# [1] + a#)
  32. dmean2 = mean2 - big - meanA
  33. assert big <> round (big) or dmean2 = 0 or power > 18 - log10 (n)
  34. mean3 = mean (a#) + mean (- mean1 + a#)
  35. dmean3 = mean3 - big - meanA
  36. assert big <> round (big) or dmean3 = 0 or power > 18 - log10 (n)
  37. diffSquare1# = (a# - mean1) * (a# - mean1)
  38. meanSquare1 = mean (diffSquare1#)
  39. diffSquare2# = (a# - mean2) * (a# - mean2)
  40. meanSquare2 = mean (diffSquare2#)
  41. diffSquare3# = (a# - mean3) * (a# - mean3)
  42. meanSquare3 = mean (diffSquare3#)
  43. ;mean2q = mean2 + mean (- mean2 + diff2#)
  44. stdev1 = sqrt (meanSquare1 * n7 / (n7 - 1))
  45. stdev2 = sqrt (meanSquare2 * n7 / (n7 - 1))
  46. stdev3 = sqrt (meanSquare3 * n7 / (n7 - 1))
  47. appendInfoLine: power, " mean: ", dmean1, " ", dmean2, " ", dmean3, " ; stdev: ", stdev1 - stdevA, " ", stdev2 - stdevA, " ", stdev3 - stdevA
  48. endfor
  49. Debug: "no", 0
  50. debug# = { 48, 49, 50, 51, 0 }
  51. debug$ [1] = "Naive 64-bits"
  52. debug$ [2] = "Naive 80-bits"
  53. debug$ [3] = "Kahan"
  54. debug$ [4] = "Two-loop (as in R)"
  55. debug$ [5] = "Pairwise base case 64"
  56. appendInfoLine: newline$, "OFFSET"
  57. for idebug from 1 to size (debug#)
  58. appendInfoLine: newline$, debug$ [idebug]
  59. Debug: "no", debug# [idebug]
  60. big = big0
  61. for power from 1 to 25
  62. big *= 10
  63. a# = repeat# (big + sequenceA#, n)
  64. b# = big + sequenceB#
  65. c# = repeat# (big + sequenceC#, n7)
  66. appendInfoLine: "Power ", power, ". Sawtooth: mean ", mean (a#) - big - meanA, ", stdev ", stdev (a#) - stdevA,
  67. ... ". Line: mean ", mean (b#) - big - meanB, ", stdev ", stdev (b#) - stdevB,
  68. ... ". Constant: mean ", mean (c#) - big - meanC, ", stdev ", stdev (c#) - stdevC
  69. endfor
  70. Debug: "no", 0
  71. endfor
  72. assert mean ({ -1e18, 3, 1e18 }) = 1
  73. assert mean ({ -1e19, 3, 1e19 }) = 1
  74. ;assert mean ({ -1e20, 3, 1e20 }) = 1
  75. for power from 1 to 16
  76. assert (mean (repeat# (10^power + sequenceA#, n)) - 10^power = mean (sequenceA#))
  77. endfor
  78. appendInfoLine: newline$, "TIMING"
  79. numberOfTrials = 100
  80. stopwatch
  81. for i to numberOfTrials
  82. size: a#
  83. size: a#
  84. size: a#
  85. size: a#
  86. size: a#
  87. size: a#
  88. size: a#
  89. size: a#
  90. size: a#
  91. size: a#
  92. endfor
  93. appendInfoLine: "Baseline: ", stopwatch / numberOfTrials / n7 * 1e9 / 10, " ns"
  94. for idebug from 1 to size (debug#)
  95. Debug: "no", debug# [idebug]
  96. stopwatch
  97. for i to numberOfTrials
  98. mean: a#
  99. mean: a#
  100. mean: a#
  101. mean: a#
  102. mean: a#
  103. mean: a#
  104. mean: a#
  105. mean: a#
  106. mean: a#
  107. mean: a#
  108. endfor
  109. appendInfoLine: debug$ [idebug], " mean: ", stopwatch / numberOfTrials / n7 * 1e9 / 10, " ns"
  110. endfor
  111. for idebug from 1 to size (debug#)
  112. Debug: "no", debug# [idebug]
  113. stopwatch
  114. for i to numberOfTrials
  115. stdev: a#
  116. stdev: a#
  117. stdev: a#
  118. stdev: a#
  119. stdev: a#
  120. stdev: a#
  121. stdev: a#
  122. stdev: a#
  123. stdev: a#
  124. stdev: a#
  125. endfor
  126. appendInfoLine: debug$ [idebug], " stdev: ", stopwatch / numberOfTrials / n7 * 1e9 / 10, " ns"
  127. endfor
  128. appendInfoLine: newline$, "ONE PEAK"
  129. procedure do_single_peak: peakLocation, zeroLocation
  130. a# = d + repeat# ({ 1e13+1e5 }, 1e6 + 2)
  131. a# [peakLocation] = d + (-1e19-1e11)
  132. a# [zeroLocation] = d
  133. appendInfoLine: debug$ [idebug], ": ", mean (a#) - d, " ", mean (a#) + mean (a# - mean (a#)) - d
  134. endproc
  135. for idebug from 1 to size (debug#)
  136. Debug: "no", debug# [idebug]
  137. @do_single_peak: 1, 2
  138. endfor
  139. appendInfoLine: newline$, "OK"