-
Notifications
You must be signed in to change notification settings - Fork 3
/
addnoise.pro
69 lines (62 loc) · 2.24 KB
/
addnoise.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
pro addnoise, data, sigma, corrnoise = corrnoise, beaminpix = beaminpix
;+
;
; NAME:
; ADDNOISE
; PURPOSE:
; Adds noise to a datacube
; CALLING SEQUENCE:
; ADDNOISE, data, sigma [/corrnoise, beaminpix = beaminpix]
; INPUTS:
; DATA -- a datacube
; SIGMA -- The variance of the noise _added_ to the datacube.
; KEYWORD PARAMETERS:
; /CORRNOISE -- Smooths the noise to be correlated over the size of
; the beam.
; BEAMINPIX -- The length of the beam, measured in pixels.
; OUTPUTS:
; DATA -- replaces data in situ.
; SOME PROCEDURES THAT AREN'T IDL/GODDARD THAT WE USE:
;
; MODIFICATION HISTORY:
; Adam's code.
;-
; %&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&
; I. ADD NOISE TO THE DATA CUBE
; %&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&
; IF THE ADDNOISE KEYWORD IS SET JUST ADD AN APPROPRIATE AMOUNT OF
; NOISE IN THE DUMBEST WAY POSSIBLE
if (n_elements(corrnoise) eq 0) then begin
data = data + randomn(seed, n_elements(data))*sigma
return
endif else begin
; IF THE ADDCORRNOISE KEYWORD IS SET, ADD THE APPROPRIATE LEVEL OF
; *CORRELATED* NOISE (MAKE THE NOISE, CONVOLVE WITH A BEAM AND THEN
; ADD).
if (n_elements(beaminpix) eq 0) then begin
return
endif
; AN UNCORRELATED CUBE OF NOISE
corrnoise = finite(data)*0. + randomn(seed, n_elements(data))*sigma
; MAKE A 2D BEAM. DOESN'T HAVE TO BE PERFECT, JUST CLOSE ENOUGH TO
; GET THE NOISE CORRELATED ABOUT RIGHT. MAKE IT TWO FWHM ACROSS...
corrxaxis = findgen(beaminpix*2+1)
corryaxis = findgen(beaminpix*2+1)
corrxaxis = corrxaxis - mean(corrxaxis)
corryaxis = corryaxis - mean(corryaxis)
corrxra = (fltarr(n_elements(corrxaxis)) + 1.0) ## corrxaxis
corryra = corryaxis ## (fltarr(n_elements(corryaxis)) + 1.0)
corrbeam = exp(-(corrxra^2/2./(beaminpix^2-1)) - $
(corryra^2/2./(beaminpix^2-1)))
corrbeam = corrbeam/total(corrbeam, /nan)
; CONVOLVE THE CUBE, PLANE BY PLANE WITH THE PSF
for l = 0, (size(corrnoise))[3]-1 do begin
corrnoise[*, *, l] = convolve(corrnoise[*, *, l], corrbeam)
endfor
; NORMALIZE IT, JUST IN CASE
corrnoise = corrnoise*sigma/mad(corrnoise)
data = data + corrnoise
return
endelse
return
end