Skip to content

Commit 6f94308

Browse files
authored
Parallel-safe white noise generation (#87)
1 parent fbb0541 commit 6f94308

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

hydrogym/firedrake/utils/utils.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,22 @@ def get_array(func):
2626

2727
def white_noise(n_samples, fs, cutoff):
2828
"""Generate band-limited white noise"""
29-
from scipy import signal
3029

31-
rng = fd.Generator(fd.PCG64())
32-
noise = rng.standard_normal(n_samples)
30+
import numpy as np
31+
from scipy import signal
3332

34-
# Set up butterworth filter
35-
sos = signal.butter(N=4, Wn=cutoff, btype="lp", fs=fs, output="sos")
36-
filt = signal.sosfilt(sos, noise)
37-
return filt
33+
comm = fd.COMM_WORLD
34+
if comm.rank == 0:
35+
# Generate white noise
36+
rng = fd.Generator(fd.PCG64())
37+
w = rng.standard_normal(n_samples)
38+
39+
# Set up butterworth filter
40+
sos = signal.butter(N=4, Wn=cutoff, btype="lp", fs=fs, output="sos")
41+
x = signal.sosfilt(sos, w)
42+
else:
43+
x = np.empty(n_samples, dtype=np.float64)
44+
45+
# Send the same array to all MPI ranks
46+
comm.Bcast(x, root=0)
47+
return x

0 commit comments

Comments
 (0)