def generate(self, n_noise_samples=1): """Generate noise samples. The type of the noise that will be generated, and the size of the noise array are defined by the argument given to the constructor. :param n_noise_samples: The number of noise samples to be generated. :return: an np.array with the specified noise """ n = n_noise_samples * self.noise_size[0] * self.noise_size[1] s = concat([n_noise_samples], list(self.noise_size)) if self.noise_type == 'simplistic': return np.random.uniform(0, 1, size=concat([n_noise_samples], list(self.noise_size))) elif self.noise_type.lower() in {'gaussian', 'white', 'normal'}: return np.reshape(white(n), s) elif self.noise_type.lower() == 'pink': return np.reshape(pink(n), s) elif self.noise_type.lower() == 'blue': return np.reshape(blue(n), s) elif self.noise_type.lower() == 'brown': return np.reshape(brown(n), s) elif self.noise_type.lower() == 'violet': return np.reshape(violet(n), s) else: print("WARNING: noise type " + self.noise_type + " not defined. Returning 0") return np.reshape(np.zeros(n), s)
def __init__(self, size=1000, sample_rate=16000, transform=None): self.size = size self.sample_rate = sample_rate self.transform = None if transform is None else Compose( transform.transforms[1:]) n = 1000000 compute_mag_spectrogram = ComputeMagSpectrogram() self.n_fft = compute_mag_spectrogram.n_fft def compute_spectrogram(sample): clipped_sample = np.clip(sample / 5, -1, 1).astype( np.float32) # amplitude is around (-0.7, 0.7) return compute_mag_spectrogram({ 'text': '', 'samples': clipped_sample, 'sample_rate': sample_rate })['input'] self.noises = [ compute_spectrogram(white(n)), compute_spectrogram(pink(n)), compute_spectrogram(violet(n)) ]
def test_power_density(self): fs = 44100 samples = 44100 * 10 _, L = octaves(violet(samples), fs, density=True) change = np.diff(L).mean().round(0) assert (change == +6.)
def test_power(self): fs = 44100 samples = 44100 * 10 _, L = octaves(violet(samples), fs) change = np.diff(L).mean().round(0) assert (change == +9.)
def test_length(self): N = 1000 assert (len(violet(N)) == N)
def test_power_density(self): fs = 44100 samples = 44100 * 10 _, L = octaves(violet(samples), fs, density=True); change = np.diff(L).mean().round(0) assert(change==+6.)
def test_power(self): fs = 44100 samples = 44100 * 10 _, L = octaves(violet(samples), fs); change = np.diff(L).mean().round(0) assert(change==+9.)
def test_length(self): N = 1000 assert(len(violet(N))==N)