@@ -260,6 +260,7 @@ def sample_from_rms_uvgrid(
260260 nsamples : int = 1 ,
261261 window_fnc : str = "blackmanharris" ,
262262 return_in_uv : bool = False ,
263+ apply_inverse_variance_weighting : bool = False ,
263264):
264265 """Sample noise for a lightcone slice given the corresponding rms noise in uv space.
265266
@@ -281,6 +282,10 @@ def sample_from_rms_uvgrid(
281282 return_in_uv : bool, optional
282283 If True, return the noise sampled in uv space instead of real space,
283284 by default False.
285+ apply_inverse_variance_weighting : bool, optional
286+ If True, apply inverse variance weighting to the noise samples in uv space.
287+ This ensures that uv cells with lower noise contribute more to the final
288+ real-space noise. By default False.
284289
285290 Returns
286291 -------
@@ -307,6 +312,14 @@ def sample_from_rms_uvgrid(
307312 window_fnc = taper2d (rms_noise .shape [0 ], window_fnc )
308313 noise *= window_fnc [None , ..., None ]
309314
315+ # FIXME: this seems to be suppressing noise a LOT. Is this correct?
316+ if apply_inverse_variance_weighting :
317+ with np .errstate (divide = "ignore" , invalid = "ignore" ):
318+ w = 1.0 / (rms_noise ** 2 ).value
319+ w [np .isinf (w )] = 0.0
320+ wsum = w .sum (axis = (0 , 1 ), keepdims = True )
321+ noise *= w .shape [0 ] * w .shape [1 ] * w [None , ...] / wsum [None , ...]
322+
310323 noise = np .fft .ifftshift (noise , axes = (1 , 2 ))
311324
312325 # Make the noise Hermitian so that the real-space noise is real.
0 commit comments