Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

White noise #97

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ml4gw/simulation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .sine_gaussian import SineGaussian
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
48 changes: 48 additions & 0 deletions ml4gw/simulation/noise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from typing import Tuple

import torch


def colored_gaussian_noise(shape: Tuple[int, int, int], psd: torch.Tensor):
"""
Generate time-domain Gaussian noise colored by a specified PSD.

Args:
shape:
3D shape of noise tensor to generate.
First dimension corresponds to `batch_size`,
Second dimension corresponds to the number of channels,
Last dimension corresponds to the time dimension.
psd:
Spectral density used to color noise
sample_rate:
Sampling rate of data

Returns:
torch.Tensor:
Colored Gaussian noise
"""

if len(shape) != 3:
raise ValueError("Shape must have 3 dimensions")

X = torch.randn(shape)
# possibly interpolate our PSD to match the number
# of frequency bins we expect to get from X
N = X.size(-1)
num_freqs = N // 2 + 1

# normalize the number of expected dimensions in the PSD
while psd.ndim < 3:
psd = psd[None]

if psd.size(-1) != num_freqs:
psd = torch.nn.functional.interpolate(
psd, size=(num_freqs), mode="linear"
)

X_fft = torch.fft.rfft(X, norm="forward", dim=-1)
X_fft *= psd**0.5
X_fft = torch.fft.irfft(X_fft, norm="forward", dim=-1)
X_fft /= torch.std(X_fft, dim=-1, keepdim=True)
return X_fft
File renamed without changes.
4 changes: 2 additions & 2 deletions ml4gw/waveforms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .phenom_d import IMRPhenomD
from .cbc.phenom_d import IMRPhenomD
from .cbc.taylorf2 import TaylorF2
from .sine_gaussian import SineGaussian
from .taylorf2 import TaylorF2
Loading
Loading