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)) ]
trees = pickle.load(f) for id in test_ids: print("\n -- loading", id) audio_path = os.path.join(args.root, 'test_data', str(id) + '.wav') # print( ' ', audio_path) y, sr = load(audio_path, normalization=True, channels_first=False) y = y.mean(1) y = F.pad(y, (frame_size // 2, frame_size // 2)) y = y[:len(y) // hopsize * hopsize].unfold(0, frame_size, hopsize) if args.snr is None: pass else: noise = torch.Tensor(pink(frame_size)) data_pow = y.pow(2).mean(1) scaler = _noise_scaler(data_pow, args.snr) noise = scaler[:, None] * noise y += noise print(" contaminated with pink noise at SNR = " + str(args.snr)) y = y / (y.norm(dim=1, keepdim=True) + epsilon) label = torch.zeros(y.size(0), 88) label_tree = trees[id] for i in range(y.size(0)): for note in label_tree[i * hopsize]: label[i, note.data[1] - 21] = 1 if y.shape[0] > label.shape[0]:
def Pink_noise(N): x = gen.pink(N) return x
def test_power_density(self): fs = 44100 samples = 44100 * 10 _, L = octaves(pink(samples), fs, density=True) change = np.diff(L).mean().round(0) assert (change == -3.)
def test_power(self): fs = 44100 samples = 44100 * 10 _, L = octaves(pink(samples), fs) change = np.diff(L).mean().round(0) assert (change == 0.)
def test_length(self): N = 1000 assert (len(pink(N)) == N)
import numpy as np """ create signal """ # manually Ns, fs = 44100, 44100. dt = 1 / fs sigf = 1000. sigA = np.sqrt(2) sigx = np.linspace(0, Ns-1, num=Ns)*dt sigy = sigA * np.sin(2*np.pi*sigf*sigx) + 0.2*np.sin(2*np.pi*2*sigf*sigx) # using acoustics import acoustics.generator as acg sigz1 = acg.white(Ns) sigz2 = acg.pink(Ns) """ descriptors """ import acoustics.descriptors as acd print "reference SPL:", acd.REFERENCE_PRESSURE print "equivalent SPL:",acd.equivalent_sound_pressure_level(sigy) print "peak SPL:", acd.peak_sound_pressure(sigy) """ IEC 61672 """ import acoustics.standards as acs print "third octave center frequency vector:", acs.iec_61672_1_2013.NOMINAL_OCTAVE_CENTER_FREQUENCIES print "reference frequency:", acs.iec_61672_1_2013.REFERENCE_FREQUENCY print "slow time constant:", acs.iec_61672_1_2013.SLOW
def test_power_density(self): fs = 44100 samples = 44100 * 10 _, L = octaves(pink(samples), fs, density=True); change = np.diff(L).mean().round(0) assert(change==-3.)
def test_power(self): fs = 44100 samples = 44100 * 10 _, L = octaves(pink(samples), fs); change = np.diff(L).mean().round(0) assert(change==0.)
def test_length(self): N = 1000 assert(len(pink(N))==N)