-
Notifications
You must be signed in to change notification settings - Fork 3
/
mad.pro
77 lines (72 loc) · 2.17 KB
/
mad.pro
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
function mad, x, window = window, finite = finite, dimension = dimension
;+
; NAME:
; MAD
; PURPOSE:
; To calculate the Median Absolute Deviataion of a set of data in
; order to calucate the RMS of the noise.
;
; CALLING SEQUENCE:
; sigma = MAD(X)
;
; INPUTS:
; X -- A data array
;
; KEYWORD PARAMETERS:
; None
;
; OUTPUTS:
; Sigma -- The standard deviation of the data.
;
; MODIFICATION HISTORY:
;
; Thu Sep 15 18:55:37 2016, <erosolow@siglab>
; Added dimension keyword.
;
; Mon Oct 4 2004, Adam Leroy <aleroy@astro>
; Altered MAD to consider only finite values if
; the finite keyword is on. NB: you may not always want
; this (only matters for infinities).
;
;
; Tue Oct 7 15:59:16 2003, Erik Rosolowsky <eros@cosmic>
; Added Compatibility for distributions with non-zero
; mean (oops).
;
; Mon Oct 6 13:26:11 2003, Erik Rosolowsky <eros@cosmic>
; Written.
;
;
;-
mad2gauss = 0.6744897501960817
if n_elements(dimension) ne 0 then begin
if n_elements(finite) gt 0 or n_elements(window) gt 0 then $
message,'Keywords FINITE is not compatible with DIMENSION',/con
sz = size(x)
remaining = indgen(sz[0])+1
remaining = remaining[where(dimension ne remaining)]
med = median(x, dimension=dimension)
if sz[0] eq 2 then begin
med = rebin(med,sz[remaining[0]],sz[dimension])
sortdim = sort([remaining[0],sz[dimension]])
med = transpose(med,sortdim)
endif
if sz[0] eq 3 then begin
med = rebin(med,sz[remaining[0]],sz[remaining[1]],sz[dimension])
sortdim = sort([remaining[0],remaining[1],dimension])
med = transpose(med,sortdim)
endif
mad = median(abs(x-med), dimension=dimension)/mad2gauss
endif else begin
if (n_elements(window) eq 0) then begin
if (keyword_set(finite)) then begin
ind = where(finite(x) eq 1)
mad = median(abs(x[ind]-median(x[ind])))/mad2gauss
endif else $
mad = median(abs(x-median(x)))/mad2gauss
endif else begin
mad = median(abs(x-median(x, window)),window)/mad2gauss
endelse
endelse
return, mad
end