|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Created on 2026/03/02 12:16:05 |
| 4 | +@author: Whenxuan Wang |
| 5 | +@email: wwhenxuan@gmail.com |
| 6 | +@url: https://github.com/wwhenxuan/S2Generator |
| 7 | +""" |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +from numpy import fft |
| 11 | + |
| 12 | + |
| 13 | +def sample_random_perturbation( |
| 14 | + K: int, min_alpha: float, max_alpha: float, rng: np.random.RandomState = None |
| 15 | +) -> np.ndarray: |
| 16 | + """ |
| 17 | + Randomly sample K numbers in the interval [-alpha_max, -alpha_min] U [alpha_min, alpha_max] |
| 18 | + The purpose of this sampling is to construct random perturbations in the frequency domain. |
| 19 | +
|
| 20 | + :param K: Number of random numbers to sample |
| 21 | + :param min_alpha: Minimum absolute value of the random numbers |
| 22 | + :param max_alpha: Maximum absolute value of the random numbers |
| 23 | + :param rng: Optional random number generator, if not provided, the global numpy random number generator will be used |
| 24 | +
|
| 25 | + :return: A numpy array containing K random numbers, which are uniformly distributed in the interval [-alpha_max, -alpha_min] U [alpha_min, alpha_max] |
| 26 | + """ |
| 27 | + |
| 28 | + # First generate random numbers in [alpha_min, alpha_max] |
| 29 | + if rng is not None: |
| 30 | + positive_rand = rng.uniform(min_alpha, max_alpha, K) |
| 31 | + |
| 32 | + # Randomly generate sign (-1 or 1) |
| 33 | + signs = rng.choice([-1, 1], size=K) |
| 34 | + |
| 35 | + else: |
| 36 | + # When the random number generator is not passed in, use the global numpy random number generator |
| 37 | + positive_rand = np.random.uniform(min_alpha, max_alpha, K) |
| 38 | + signs = np.random.choice([-1, 1], size=K) |
| 39 | + |
| 40 | + # Combine to get the final result |
| 41 | + final_random_nums = positive_rand * signs |
| 42 | + |
| 43 | + return final_random_nums |
| 44 | + |
| 45 | + |
| 46 | +def frequency_perturbation( |
| 47 | + series: np.ndarray, |
| 48 | + min_alpha: float, |
| 49 | + max_alpha: float, |
| 50 | + r: float = 0.5, |
| 51 | + rng: np.random.RandomState = None, |
| 52 | +) -> np.ndarray: |
| 53 | + """ |
| 54 | + Perform frequency domain perturbation on the input time series. |
| 55 | + This method adds random perturbations to the frequency components of the time series, |
| 56 | + which can help to enhance the diversity of the data and improve the robustness of models trained on it. |
| 57 | +
|
| 58 | + :param series: Input time series, a 1D numpy array |
| 59 | + :param min_alpha: Minimum absolute value of the random perturbation added to the frequency components |
| 60 | + :param max_alpha: Maximum absolute value of the random perturbation added to the frequency components |
| 61 | + :param r: Proportion of frequency components to perturb (default is 0.5, meaning 50% of the frequency components will be perturbed) |
| 62 | + :param rng: Optional random number generator, if not provided, the global numpy random number generator will be used. |
| 63 | +
|
| 64 | + :return: Perturbed time series, a 1D numpy array of the same length as the input series. |
| 65 | + """ |
| 66 | + f = fft.rfft(series) |
| 67 | + f_perturbed = f.copy() |
| 68 | + frequencies = fft.fftfreq(len(series)) |
| 69 | + |
| 70 | + # Calculate the number of frequency domain components that can be perturbed |
| 71 | + K = int(len(frequencies) * r) |
| 72 | + |
| 73 | + # Sample random perturbations for the real and imaginary parts of the frequency components |
| 74 | + alpha_real = sample_random_perturbation( |
| 75 | + K=K, min_alpha=min_alpha, max_alpha=max_alpha, rng=rng |
| 76 | + ) |
| 77 | + alpha_imag = sample_random_perturbation( |
| 78 | + K=K, min_alpha=min_alpha, max_alpha=max_alpha, rng=rng |
| 79 | + ) |
| 80 | + |
| 81 | + # Randomly select K frequency domain components for perturbation |
| 82 | + indices = np.random.choice(len(f_perturbed), size=K, replace=False) |
| 83 | + f_perturbed[indices] += alpha_real + 1j * alpha_imag |
| 84 | + |
| 85 | + # Perform inverse Fourier transform to restore the original time-domain signal |
| 86 | + perturbed_series = fft.irfft(f_perturbed).real |
| 87 | + |
| 88 | + return perturbed_series |
0 commit comments