Skip to content

Piecewise wavy (pw_wavy)#

Return a 1D piecewise wavy signal and the associated changepoints.

Parameters:

Name Type Description Default
n_samples int

signal length

200
n_bkps int

number of changepoints

3
noise_std float

noise std. If None, no noise is added

None
seed int

random seed

None

Returns:

Name Type Description
tuple

signal of shape (n_samples, 1), list of breakpoints

Source code in ruptures/datasets/pw_wavy.py
 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
def pw_wavy(n_samples=200, n_bkps=3, noise_std=None, seed=None):
    """Return a 1D piecewise wavy signal and the associated changepoints.

    Args:
        n_samples (int, optional): signal length
        n_bkps (int, optional): number of changepoints
        noise_std (float, optional): noise std. If None, no noise is added
        seed (int): random seed

    Returns:
        tuple: signal of shape (n_samples, 1), list of breakpoints
    """
    # breakpoints
    bkps = draw_bkps(n_samples, n_bkps, seed=seed)
    # we create the signal
    f1 = np.array([0.075, 0.1])
    f2 = np.array([0.1, 0.125])
    freqs = np.zeros((n_samples, 2))
    for sub, val in zip(np.split(freqs, bkps[:-1]), cycle([f1, f2])):
        sub += val
    tt = np.arange(n_samples)

    # DeprecationWarning: Calling np.sum(generator) is deprecated
    # Use np.sum(np.from_iter(generator)) or the python sum builtin instead.
    signal = np.sum([np.sin(2 * np.pi * tt * f) for f in freqs.T], axis=0)

    if noise_std is not None:
        rng = np.random.default_rng(seed=seed)
        noise = rng.normal(scale=noise_std, size=signal.shape)
        signal += noise

    return signal, bkps