| 1 |
|
| 2 |
(use-modules (ice-9 format)) |
| 3 |
|
| 4 |
(define dimensions 72) |
| 5 |
(define base-array (make-array 0.0 dimensions dimensions)) |
| 6 |
|
| 7 |
(define read-rama-data |
| 8 |
(lambda (filename) |
| 9 |
|
| 10 |
(let ((arr (make-array 0.0 dimensions dimensions)) |
| 11 |
(fac (/ 360.0 dimensions))) |
| 12 |
|
| 13 |
(call-with-input-file filename |
| 14 |
(lambda (port) |
| 15 |
|
| 16 |
(let f ((s (read-line port))) |
| 17 |
(if (not (eof-object? s)) |
| 18 |
|
| 19 |
|
| 20 |
(let ((parts (string-split s #\space))) |
| 21 |
|
| 22 |
(if (= 3 (length parts)) |
| 23 |
|
| 24 |
(let ((phi (string->number (list-ref parts 0))) |
| 25 |
(psi (string->number (list-ref parts 1))) |
| 26 |
(val (string->number (list-ref parts 2)))) |
| 27 |
|
| 28 |
(let ((phi-index (inexact->exact (/ phi fac))) |
| 29 |
(psi-index (inexact->exact (/ psi fac)))) |
| 30 |
|
| 31 |
(array-set! arr val phi-index psi-index)))) |
| 32 |
|
| 33 |
(f (read-line port))))))) |
| 34 |
|
| 35 |
arr))) |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
(define indices-for-peak |
| 40 |
(lambda (arr) |
| 41 |
|
| 42 |
(let f ((peak-pair (cons -1 -1)) |
| 43 |
(max-value -1) |
| 44 |
(phi-index 0) |
| 45 |
(psi-index 0)) |
| 46 |
(cond |
| 47 |
((= phi-index dimensions) peak-pair) |
| 48 |
((= psi-index dimensions) (f peak-pair max-value (+ phi-index 1) 0)) |
| 49 |
((> (abs (array-ref arr phi-index psi-index)) max-value) |
| 50 |
(f (cons phi-index psi-index) |
| 51 |
(abs (array-ref arr phi-index psi-index)) |
| 52 |
phi-index (+ psi-index 1))) |
| 53 |
(else (f peak-pair max-value phi-index (+ psi-index 1))))))) |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
(define gaussian-base-value |
| 61 |
(lambda (arr peak-pair variance) |
| 62 |
|
| 63 |
(let ((three-sigma (* (sqrt variance) 3.0))) |
| 64 |
|
| 65 |
4.0)) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
(define gaussian-value |
| 70 |
(lambda (centre height variance point-x point-y) |
| 71 |
|
| 72 |
|
| 73 |
(let* ((degrees/step (/ 360 dimensions)) |
| 74 |
(diff-y (* (- (cdr centre) point-y) degrees/step)) |
| 75 |
(diff-x (* (- (car centre) point-x) degrees/step)) |
| 76 |
(diff-y2 (* diff-y diff-y)) |
| 77 |
(diff-x2 (* diff-x diff-x)) |
| 78 |
(z2 (/ (+ diff-y2 diff-x2) variance))) |
| 79 |
|
| 80 |
(* height (exp (* -0.5 z2)))))) |
| 81 |
|
| 82 |
|
| 83 |
(define remove-gaussian |
| 84 |
(lambda (arr peak-pair height variance) |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
(let* ((sigma-guess 10) |
| 91 |
(grid-size (inexact->exact (* 3 (/ sigma-guess (/ 360 dimensions)))))) |
| 92 |
|
| 93 |
(format #t "choosing grid size ~s from sigma ~s ~%" grid-size (sqrt variance)) |
| 94 |
|
| 95 |
(let loop ((phi-index (- (car peak-pair) grid-size)) |
| 96 |
(psi-index (- (cdr peak-pair) grid-size))) |
| 97 |
|
| 98 |
(cond |
| 99 |
((> phi-index (+ (car peak-pair) grid-size)) 'done) |
| 100 |
((> psi-index (+ (cdr peak-pair) grid-size)) (loop (+ phi-index 1) |
| 101 |
(- (cdr peak-pair) grid-size))) |
| 102 |
(else |
| 103 |
(let ((g (gaussian-value peak-pair height variance phi-index psi-index))) |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
(let* ((current-pair (cons phi-index psi-index)) |
| 114 |
(current-value (arr-value arr current-pair))) |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
(arr-remove base-array current-pair (- g)) |
| 120 |
(arr-remove arr current-pair g) |
| 121 |
(loop phi-index (+ psi-index 1)))))))))) |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
(define gaussian-fit |
| 127 |
(lambda (arr peak-pair gaussian-height-scale gaussian-variance-scale) |
| 128 |
|
| 129 |
(define variance-est |
| 130 |
(lambda (peak-value neighbours x-dist) |
| 131 |
|
| 132 |
(let ((y (average (map (lambda (x) |
| 133 |
(arr-value arr x)) |
| 134 |
neighbours)))) |
| 135 |
|
| 136 |
(if (< (* y peak-value) 0.0) |
| 137 |
(begin |
| 138 |
(format #t "bad neighb avearge: peak: ~s average ~s neigb values: ~s~%" |
| 139 |
peak-value y (map (lambda (x) |
| 140 |
(arr-value arr x)) |
| 141 |
neighbours)) |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
(let ((degrees/step (/ 360 dimensions))) |
| 146 |
(* 0.8 degrees/step degrees/step))) |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
(- (/ (* x-dist x-dist ) (* 2 (log (/ y peak-value))))))))) |
| 151 |
|
| 152 |
(define neighbours-avearge |
| 153 |
(lambda (neighbours) |
| 154 |
|
| 155 |
(average (map (lambda (x) |
| 156 |
(arr-value arr x)) |
| 157 |
neighbours)))) |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
(let* ((phi-index (car peak-pair)) |
| 185 |
(psi-index (cdr peak-pair)) |
| 186 |
(neighbours-1 (list (cons (+ phi-index 1) psi-index) |
| 187 |
(cons (- phi-index 1) psi-index) |
| 188 |
(cons phi-index (+ psi-index 1)) |
| 189 |
(cons phi-index (- psi-index 1)))) |
| 190 |
(neighbours-2 (list (cons (+ phi-index 1) (+ psi-index 1)) |
| 191 |
(cons (+ phi-index 1) (- psi-index 1)) |
| 192 |
(cons (- phi-index 1) (+ psi-index 1)) |
| 193 |
(cons (- phi-index 1) (- psi-index 1)))) |
| 194 |
(neighbours-3 (list (cons (+ phi-index 2) psi-index) |
| 195 |
(cons (- phi-index 2) psi-index) |
| 196 |
(cons phi-index (+ psi-index 2)) |
| 197 |
(cons phi-index (- psi-index 2)))) |
| 198 |
(neighbours-4 (list (cons (+ phi-index 1) (+ psi-index 2)) |
| 199 |
(cons (+ phi-index 1) (- psi-index 2)) |
| 200 |
(cons (- phi-index 1) (+ psi-index 2)) |
| 201 |
(cons (- phi-index 1) (- psi-index 2)) |
| 202 |
(cons (+ phi-index 2) (+ psi-index 1)) |
| 203 |
(cons (+ phi-index 2) (- psi-index 1)) |
| 204 |
(cons (- phi-index 2) (+ psi-index 1)) |
| 205 |
(cons (- phi-index 2) (- psi-index 1)))) |
| 206 |
(neighbours-5 (list (cons (+ phi-index 2) (+ psi-index 2)) |
| 207 |
(cons (+ phi-index 2) (- psi-index 2)) |
| 208 |
(cons (- phi-index 2) (+ psi-index 2)) |
| 209 |
(cons (- phi-index 2) (- psi-index 2)))) |
| 210 |
(n-1-dist (* 1.0 (/ 360 dimensions))) |
| 211 |
(n-2-dist (* (sqrt 2.0) (/ 360 dimensions))) |
| 212 |
(n-3-dist (* 2.0 (/ 360 dimensions))) |
| 213 |
(n-4-dist (* (sqrt 5.0) (/ 360 dimensions))) |
| 214 |
(n-5-dist (* (sqrt 8.0) (/ 360 dimensions)))) |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
(format #t "peak: ~s at ~s with neibs average: ~s ~%" |
| 220 |
(arr-value arr peak-pair) |
| 221 |
peak-pair |
| 222 |
(neighbours-avearge neighbours-1)) |
| 223 |
|
| 224 |
(let* ((peak-value (arr-value arr peak-pair)) |
| 225 |
(variance-estimate-1 (variance-est peak-value neighbours-1 n-1-dist)) |
| 226 |
(variance-estimate-2 (variance-est peak-value neighbours-2 n-2-dist)) |
| 227 |
(variance-estimate-3 (variance-est peak-value neighbours-3 n-3-dist)) |
| 228 |
(variance-estimate-4 (variance-est peak-value neighbours-4 n-4-dist)) |
| 229 |
(variance-estimate-5 (variance-est peak-value neighbours-5 n-5-dist))) |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
(let ((average-var (/ (+ (* variance-estimate-1 (length neighbours-1)) |
| 236 |
(* variance-estimate-2 (length neighbours-2)) |
| 237 |
(* variance-estimate-3 (length neighbours-3)) |
| 238 |
(* variance-estimate-4 (length neighbours-4)) |
| 239 |
(* variance-estimate-5 (length neighbours-5))) |
| 240 |
(+ (length neighbours-1) |
| 241 |
(length neighbours-2) |
| 242 |
(length neighbours-3) |
| 243 |
(length neighbours-4) |
| 244 |
(length neighbours-5))))) |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
(remove-gaussian arr peak-pair |
| 253 |
(* gaussian-height-scale (arr-value arr peak-pair)) |
| 254 |
(* gaussian-variance-scale average-var))))))) |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
(define wrapped-phi-psi-pair |
| 259 |
(lambda (phi-psi-pair) |
| 260 |
|
| 261 |
(let ((phi (car phi-psi-pair)) |
| 262 |
(psi (cdr phi-psi-pair))) |
| 263 |
(cond |
| 264 |
((and (>= phi 0) (< phi dimensions) |
| 265 |
(>= psi 0) (< psi dimensions)) |
| 266 |
phi-psi-pair) |
| 267 |
((and (>= phi 0) (< phi dimensions)) |
| 268 |
|
| 269 |
(if (< psi 0) |
| 270 |
(cons phi (+ psi dimensions)) |
| 271 |
(if (>= psi dimensions) |
| 272 |
(cons phi (- psi dimensions)) |
| 273 |
'this-cannot-happen-psi))) |
| 274 |
((and (>= psi 0) (< psi dimensions)) |
| 275 |
|
| 276 |
(if (< phi 0) |
| 277 |
(cons (+ phi dimensions) psi) |
| 278 |
(if (>= phi dimensions) |
| 279 |
(cons (- phi dimensions) psi) |
| 280 |
'this-cannot-happen-phi))) |
| 281 |
|
| 282 |
|
| 283 |
((and (< phi 0) (< psi 0)) |
| 284 |
(cons (+ phi dimensions) (+ psi dimensions))) |
| 285 |
((and (< phi 0) (>= dimensions psi)) |
| 286 |
(cons (+ phi dimensions) (- psi dimensions))) |
| 287 |
((and (>= dimensions phi) (< psi 0)) |
| 288 |
(cons (- phi dimensions) (+ psi dimensions))) |
| 289 |
((and (>= dimensions phi) (>= dimensions psi)) |
| 290 |
(cons (- phi dimensions) (- psi dimensions))) |
| 291 |
(else |
| 292 |
'this-cannont-happen-fall-through))))) |
| 293 |
|
| 294 |
|
| 295 |
(define average |
| 296 |
(lambda (lsi) |
| 297 |
|
| 298 |
(if (null? lsi) |
| 299 |
0.0 |
| 300 |
(let loop ((sum 0.0) |
| 301 |
(ls lsi)) |
| 302 |
(cond |
| 303 |
((null? ls) (/ sum (length lsi))) |
| 304 |
(else |
| 305 |
(loop (+ sum (car ls)) (cdr ls)))))))) |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
(define arr-value |
| 312 |
(lambda (arr phi-psi-pair) |
| 313 |
|
| 314 |
(let ((w (wrapped-phi-psi-pair phi-psi-pair))) |
| 315 |
(array-ref arr (car w) (cdr w))))) |
| 316 |
|
| 317 |
|
| 318 |
(define arr-remove |
| 319 |
(lambda (arr phi-psi-pair val) |
| 320 |
|
| 321 |
(let* ((w (wrapped-phi-psi-pair phi-psi-pair)) |
| 322 |
(current-value (arr-value arr w))) |
| 323 |
|
| 324 |
(array-set! arr (- current-value val) (car w) (cdr w))))) |
| 325 |
|
| 326 |
|
| 327 |
(define unfitted-sum |
| 328 |
(lambda (arr) |
| 329 |
|
| 330 |
(let f ((sum 0.0) |
| 331 |
(phi-index 0) |
| 332 |
(psi-index 0)) |
| 333 |
(cond |
| 334 |
((= phi-index dimensions) (sqrt sum)) |
| 335 |
((= psi-index dimensions) (f sum (+ phi-index 1) 0)) |
| 336 |
(else |
| 337 |
(let ((v (array-ref arr phi-index psi-index))) |
| 338 |
(f (+ sum (* v v)) phi-index (+ psi-index 1)))))))) |
| 339 |
|
| 340 |
(define print-arr |
| 341 |
(lambda (arr) |
| 342 |
|
| 343 |
(let f ((phi-index 0) |
| 344 |
(psi-index 0)) |
| 345 |
(cond |
| 346 |
((= phi-index dimensions) 'done) |
| 347 |
((= psi-index dimensions) |
| 348 |
(format #t "~%") |
| 349 |
(f (+ phi-index 1) 0)) |
| 350 |
(else |
| 351 |
(format #t "~5f " (array-ref arr phi-index psi-index)) |
| 352 |
(f phi-index (+ psi-index 1))))))) |
| 353 |
|
| 354 |
(define print-arr-for-gnuplot |
| 355 |
(lambda (arr filename) |
| 356 |
|
| 357 |
(call-with-output-file filename |
| 358 |
(lambda (port) |
| 359 |
(let f ((phi-index 0) |
| 360 |
(psi-index 0) |
| 361 |
(degrees/step (/ 360 dimensions))) |
| 362 |
(cond |
| 363 |
((= phi-index dimensions) 'done) |
| 364 |
((= psi-index dimensions) |
| 365 |
(format port "~%") |
| 366 |
(f (+ phi-index 1) 0 degrees/step)) |
| 367 |
(else |
| 368 |
(let ((v (array-ref arr phi-index psi-index))) |
| 369 |
(format port "~s ~s ~s~%" |
| 370 |
(* phi-index degrees/step) |
| 371 |
(* psi-index degrees/step) |
| 372 |
(if (> v 0) |
| 373 |
v v)) |
| 374 |
(f phi-index (+ psi-index 1) degrees/step))))))))) |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
(define fit-rama |
| 380 |
(lambda (rounds height-scale var-scale) |
| 381 |
(let* ((arr (read-rama-data "rama-data.tab")) |
| 382 |
(initial-sum (unfitted-sum arr))) |
| 383 |
|
| 384 |
(let loop ((n 0)) |
| 385 |
|
| 386 |
(cond |
| 387 |
((= n rounds ) (* 100.0 (/ (unfitted-sum arr) initial-sum))) |
| 388 |
(else |
| 389 |
(let ((peak-pair (indices-for-peak arr))) |
| 390 |
|
| 391 |
(if (< (arr-value arr peak-pair) 0.0) |
| 392 |
(format #t "Fitted a negative peak (~s) at: ~s~%" |
| 393 |
(arr-value arr peak-pair) peak-pair)) |
| 394 |
|
| 395 |
(let ((us (unfitted-sum arr))) |
| 396 |
(format #t "----- unfitted sum: ~s (~s%)~%" us (* 100.0 (/ us initial-sum))) |
| 397 |
(gaussian-fit arr peak-pair height-scale var-scale) |
| 398 |
(loop (+ n 1))))))) |
| 399 |
|
| 400 |
(* 100.0 (/ (unfitted-sum arr) initial-sum))))) |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
(let loop ((height-scale 0.4)) |
| 417 |
|
| 418 |
(cond |
| 419 |
((> height-scale 2.0) 'done) |
| 420 |
(else |
| 421 |
(let ((r (fit-rama 30 height-scale 1.0))) |
| 422 |
|
| 423 |
(format #t "height: ~s final remainder: ~s~%" height-scale r) |
| 424 |
(loop (+ height-scale 0.1)))))) |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|