def _custom_aug(x, normal_noise, musical_noise): db = np.random.uniform(low=-5, high=35) p = np.random.uniform() if p < 0.9 and db > 0: if p < 0.55: pi = random.randint(0, len(musical_noise) - 1) pi2 = random.randint(0, len(musical_noise[pi]) - n_frame - 1) y = musical_noise[pi][pi2:pi2 + n_frame] if np.max(y) - np.min(y) > 0.1: y = normalize(y) else: pi = random.randint(0, len(normal_noise) - 1) pi2 = random.randint(0, len(normal_noise[pi]) - n_frame - 1) y = normal_noise[pi][pi2:pi2 + n_frame] if np.max(y) - np.min(y) > 0.1: y = normalize(y) else: if p < 0.94: y = colorednoise.powerlaw_psd_gaussian(0, x.shape[0]) #whitenoise elif p < 0.98: y = colorednoise.powerlaw_psd_gaussian(1, x.shape[0]) #pinknoise else: y = colorednoise.powerlaw_psd_gaussian( 2, x.shape[0]) # brownnoise y = normalize(y) return mix_db(x, y, db)
def _custom_aug(x, normal_noise, musical_noise): db_1 = np.random.uniform(low=0, high=35) db_2 = np.random.uniform(low=-5, high=45) p_1 = np.random.uniform() p_2 = np.random.uniform() if 0.1 < p_1 < 0.6: pi = random.randint(0, len(normal_noise) - 1) pi2 = random.randint(0, len(normal_noise[pi]) - n_frame - 1) y = normal_noise[pi][pi2:pi2 + n_frame] if np.max(y) - np.min(y) > 0.1: y = normalize(y) x = mix_db(x, y, db_1) elif 0.6 < p_1: pi = random.randint(0, len(musical_noise) - 1) pi2 = random.randint(0, len(musical_noise[pi]) - n_frame - 1) y = musical_noise[pi][pi2:pi2 + n_frame] if np.max(y) - np.min(y) > 0.1: y = normalize(y) x = mix_db(x, y, db_1) if p_2 < 0.4: y = colorednoise.powerlaw_psd_gaussian(0, x.shape[0]) #whitenoise elif p_2 < 0.8: y = colorednoise.powerlaw_psd_gaussian(1, x.shape[0]) #pinknoise else: y = colorednoise.powerlaw_psd_gaussian(2, x.shape[0]) # brownnoise y = normalize(y) return mix_db(x, y, db_2)
def apply(self, waveform, **params): assert waveform.shape[0] == 1, 'waveform should have 1-channel' assert waveform.shape[1] > 0, 'waveform is empty' waveform = waveform.clone() waveform.squeeze_(0) noise_amp = np.random.uniform( self.min_amp, self.max_amp) * waveform.abs().max().numpy( ) # calculate noise amplitude noise_color = np.random.randint(-2, 4) # noise type n_noises = np.random.randint(1, self.max_n_noises + 1) for i in range(n_noises): noise_length = int( np.random.uniform(0.01, 1 / (self.max_n_noises * 2.)) * len(waveform)) start_sample = np.random.randint( i * (len(waveform) // n_noises), (i + 1) * (len(waveform) // n_noises) - noise_length) if noise_color != 3: col_noise = powerlaw_psd_gaussian( noise_color, noise_length) # noise generation else: # grey noise col_noise = powerlaw_psd_gaussian( 2, noise_length) + powerlaw_psd_gaussian(-2, noise_length) col_noise = col_noise * noise_amp / np.abs( col_noise).max() # get noise with desired amplitude waveform[start_sample:start_sample + noise_length] = waveform[start_sample:start_sample + noise_length] + col_noise return waveform.unsqueeze(0)
def testSig(sr, t, tones=[[100, 1, 0, 'sin']], noiseType='no', plotting=False): ''' Creates signals composed of tones and noise for test purposes. This function is not used anywhere in the program. Inputs: - sr: sample rate - t: time in seconds - tones: list of tones that the signal will contain format: tones=[tone1,tone2,...], where tone1 = [frequency, Amplitude, starting phase, type(sin or cos)] - noise: noise added to the resulting signal format: noise=[type, amplitude in percentage of the overall max amplitude of the tonal part of the signal] - plotting: plots the resulting signal ''' tVec = np.linspace(0, t, t * sr) number_of_samples = len(tVec) testSig = 0 for i in range(0, len(tones)): A = tones[i][1] f = tones[i][0] if len(tones[i]) == 2: phi = 0 tone_type = 'sin' elif len(tones[i]) == 3: tone_type = 'sin' else: phi = tones[i][2] tone_type = tones[i][3] if tone_type == 'sin': testSig += A * np.sin(2 * np.pi * f * tVec + phi) elif tone_type == 'cos': testSig += A * np.cos(2 * np.pi * f * tVec + phi) else: print("Unknown tone type skipping...") # Adding noise if noiseType == 'no': noise = 0 else: if noiseType[0] == 'pink': noise = cn.powerlaw_psd_gaussian(1, number_of_samples) elif noiseType[0] == 'white': noise = cn.powerlaw_psd_gaussian(0, number_of_samples) else: noise = np.random.randn(number_of_samples) noise /= np.max(abs(testSig)) noise *= noiseType[1] testSig += noise if plotting: plt.plot(tVec, testSig) plt.show() return testSig, tVec, tones, noise
def create_signal(sample_rate, time_of_signal, pad_samples, signal_type, voltage_out): number_of_samples = int(time_of_signal * sample_rate) if signal_type == "pink_noise": signal_unpadded = cn.powerlaw_psd_gaussian(1, number_of_samples) elif signal_type == "white_noise": signal_unpadded = cn.powerlaw_psd_gaussian(0, number_of_samples) elif signal_type == "debug": signal_unpadded = np.sin( 34 * 2 * np.pi * np.linspace(0, time_of_signal, number_of_samples)) signal_unpadded /= (np.max(abs(signal_unpadded))) / voltage_out signal = np.pad(signal_unpadded, (pad_samples, 0), 'constant', constant_values=[0, 0]) return signal, signal_unpadded
def transform(self, input_dir): tfm = sox.Transformer() if self.contrast_bool: tfm.contrast(self.contrast_val) if self.eq_bool: tfm.equalizer(self.eq_freq1, self.width_q, self.gain_db1) tfm.equalizer(self.eq_freq2, self.width_q, self.gain_db2) if self.reverb_bool: tfm.reverb(self.reverb_val) if self.pinknoise_bool: # if pinknoise is required, output and temp wav to load and add pinknoise output_fname_tmp = Path(input_dir).stem+"_tmp.wav" output_fdir_tmp = os.path.join(self.output_dir, output_fname_tmp ) tfm.build(input_dir, output_fdir_tmp) audio_clean, rate = sf.read(output_fdir_tmp) pinknoise =cn.powerlaw_psd_gaussian(1, len(audio_clean)) audio_noisy = audio_clean + self.pn_vol * pinknoise output_fname = Path(input_dir).stem+".wav" output_fdir = os.path.join(self.output_dir, output_fname) sf.write(output_fdir, audio_noisy, rate) # delete the tmp file try : os.remove(output_fdir_tmp) except OSError as e: print(e) else: print("removed:", output_fdir_tmp) else: output_fname = Path(input_dir).stem+".wav" output_fdir = os.path.join(self.output_dir, output_fname) tfm.build(input_dir, output_fdir)
def _generate_noise(self, length): """Generate noise to mix to target music Args: length (int): desired length of noise signal """ return cn.powerlaw_psd_gaussian(1, length)
def get_game_mat(p, games, scale): p_mat = np.ones((1, games)) * p noise_mat = cn.powerlaw_psd_gaussian(2, games) * scale p_mat_noise = p_mat + noise_mat u = np.random.uniform(0, 1, games) res = u <= p_mat_noise return res
def make_color_noise(beta, audio_path): signal, freq = sf.read(audio_path) signal = np.interp(signal, (signal.min(), signal.max()), (-1, 1)) samples = len(signal) noise = cn.powerlaw_psd_gaussian(beta, samples) noise = np.interp(noise, (noise.min(), noise.max()), (-1, 1)) return noise
def test_pink_noise(self): results = [] for _ in range(self.n_tests): samples = colorednoise.powerlaw_psd_gaussian(1, self.n_points) results.append(features.DFA(samples)) results_mean = np.mean(results) self.assertAlmostEqual(results_mean, 1, delta=0.05)
def _inner(ai: AudioItem) -> AudioItem: # if it's white noise, implement our own for speed if color == 0: noise = torch.randn_like(ai.sig) else: noise = torch.from_numpy( cn.powerlaw_psd_gaussian(exponent=color, size=ai.nsamples)).float() scaled_noise = noise * ai.sig.abs().mean() * noise_level return AudioItem((ai.sig + scaled_noise, ai.sr, ai.path))
def apply(self, y: np.ndarray, **params): snr = np.random.uniform(self.min_snr, self.max_snr) a_signal = np.sqrt(y**2).max() a_noise = a_signal / (10**(snr / 20)) pink_noise = cn.powerlaw_psd_gaussian(1, len(y)) a_pink = np.sqrt(pink_noise**2).max() augmented = (y + pink_noise * 1 / a_pink * a_noise).astype(y.dtype) return augmented
def array_pulse( i_wave, q_wave=None, amp=1, phase=0, detune=0, noise_sigma=0, noise_alpha=0, scale_noise=False, ): """Takes a real or complex waveform and applies an amplitude scaling, phase offset, time-dependent phase, and additive Gaussian noise. Args: i_wave (array-like): Real part of the waveform. q_wave (optional, array-like): Imaginary part of the waveform. If None, the imaginary part is set to 0. Default: None. amp (float): Factor by which to scale the waveform amplitude. Default: 1. phase (optionla, float): Phase offset in radians. Default: 0. detune (optional, float): Software detuning/time-dependent phase to apply to the waveform, in GHz. Default: 0. noise_sigma (optional, float): Standard deviation of additive Gaussian noise applied to the pulse (see scale_noise). Default: 0. noise_alpha (optional, float): Exponent for the noise PSD S(f). S(f) is proportional to (1/f)**noise_alpha. noise_alpha = 0 for white noise, noise_alpha = 1 for 1/f noise, etc. Default: 0 (white noise). scale_noise (optional, bool): Whether to scale the noise by ``amp`` before adding it to the signal. If False, then noise_sigma has units of GHz. Default: False. Returns: ``np.ndarray``: Complex waveform. """ i_wave = np.asarray(i_wave) if q_wave is None: q_wave = np.zeros_like(i_wave) if detune: ts = np.arange(len(i_wave)) c_wave = (i_wave + 1j * q_wave) * np.exp(-2j * np.pi * ts * detune) i_wave, q_wave = c_wave.real, c_wave.imag if noise_sigma: i_noise, q_noise = noise_sigma * powerlaw_psd_gaussian( noise_alpha, [2, i_wave.size]) else: i_noise, q_noise = 0, 0 if scale_noise: i_wave = amp * (i_wave + i_noise) q_wave = amp * (q_wave + q_noise) else: i_wave = amp * i_wave + i_noise q_wave = amp * q_wave + q_noise c_wave = (i_wave + 1j * q_wave) * np.exp(-1j * phase) return c_wave
def mytestdata_gen(x, batch_size, snr_val): noise_sigma = snr_db2sigma(snr_val) while True: #np.random.shuffle(indexs) for i in range(0, len(x), batch_size): p_noise = cn.powerlaw_psd_gaussian( args.beta, [batch_size, x.shape[2], x.shape[1]]) ge_batch_x = x[i:i + batch_size] + noise_sigma * np.swapaxes( p_noise, 1, 2) yield ge_batch_x
def _inner(ai: AudioTensor) -> AudioTensor: # if it's white noise, implement our own for speed if color == 0: noise = torch.randn_like(ai.data) else: noise = torch.from_numpy( cn.powerlaw_psd_gaussian(exponent=color, size=ai.nsamples)).float() scaled_noise = noise * ai.data.abs().mean() * noise_level ai.data += scaled_noise return ai
def test_var_distribution(self): # test deviation from unit variance (with some margin for random errors) size = (100, 2**16) fmin = 0 for exponent in [.5, 1, 2]: y = cn.powerlaw_psd_gaussian(exponent, size, fmin=fmin) ystd = y.std(axis=-1) var_in = (abs(1 - ystd) < 3 * ystd.std()).mean() # var within 3 sigma self.assertTrue(var_in > 0.95) # should even be > 0.99 since distr. normal
def generate_color_noise(samples, color): if color == 'white': beta = 0 elif color == 'pink': beta = 1 elif color == 'brown': beta = 2 else: raise Exception('option not supported') return cn.powerlaw_psd_gaussian(beta, samples)
def __call__(self, y: np.ndarray): if np.random.random() > self.p: return y snr = np.random.uniform(self.min_snr, self.max_snr) a_signal = np.sqrt(y**2).max() a_noise = a_signal / (10**(snr / 20)) pink_noise = cn.powerlaw_psd_gaussian(1, len(y)) a_pink = np.sqrt(pink_noise**2).max() augmented = (y + pink_noise * 1 / a_pink * a_noise).astype(y.dtype) return augmented
def encodes(self, ai: AudioTensor) -> AudioTensor: # if it's white noise, implement our own for speed if self.color == NoiseColor.White: noise = torch.randn_like(ai.data) else: noise = torch.from_numpy( cn.powerlaw_psd_gaussian(exponent=self.color.value, size=ai.nsamples)).float() scaled_noise = noise * ai.data.abs().mean() * self.noise_level ai.data += scaled_noise return ai
def input_noise(samplerate, oversampling_factor): n_sample = 650000 * oversampling_factor * 4 max_freq = 150 min_freq = 0 V_noise_BL = 0.095e-6 noise_BL = band_limited_noise(min_freq, max_freq, n_sample, samplerate) noise_BL = noise_BL * 0.7 * np.sqrt(n_sample * samplerate) * V_noise_BL beta = 1 # the exponent f_flicker = 1 pink_noise = cn.powerlaw_psd_gaussian(beta, n_sample, 0) pink_noise = pink_noise * 3.85 * V_noise_BL * np.sqrt(f_flicker) return (noise_BL, pink_noise)
def make_noise(): beta = numpy.random.uniform(-3, 3) noise = powerlaw_psd_gaussian( beta, size=len(wave), fmin=20 / sampling_rate, ) noise /= 3 # 99.73%が-1~1に入る snr = numpy.random.uniform(5, 30) noise *= 1 / (10**(snr / 20) - 1) return noise
def add_pink_noise(sig, cutoff, amplitude): # synthesize pink noise noise = powerlaw_psd_gaussian(1, len(sig)) # LPF the noise b, a = signal.butter(4, Wn=cutoff, fs=sr) noise = signal.lfilter(b, a, noise) noise = normalize(noise) * amplitude sig += noise sig = normalize(sig) # return sig with noise return sig
def mydata_gen(x, y, batch_size, snr_low, snr_high): indexs = list(range(x.shape[0])) noise_sigma_low = snr_db2sigma(snr_low) # 0dB noise_sigma_high = snr_db2sigma(snr_high) while True: #np.random.shuffle(indexs) for i in range(0, len(indexs), batch_size): sigma = np.random.uniform(noise_sigma_low, noise_sigma_high, 1) p_noise = cn.powerlaw_psd_gaussian( args.beta, [batch_size, x.shape[2], x.shape[1]]) ge_batch_x = x[indexs[i:i + batch_size]] + sigma[0] * np.swapaxes( p_noise, 1, 2) ge_batch_y = y[indexs[i:i + batch_size]] yield ge_batch_x, ge_batch_y
def apply(self, waveform, **params): assert waveform.shape[0] == 1, 'waveform should have 1-channel' assert waveform.shape[1] > 0, 'waveform is empty' waveform = waveform.clone() waveform.squeeze_(0) noise_amp = np.random.uniform( self.min_amp, self.max_amp) * waveform.abs().max().numpy( ) # calculate noise amplitude noise_color = np.random.randint(-2, 4) # noise type if noise_color != 3: col_noise = powerlaw_psd_gaussian( noise_color, len(waveform)) # noise generation else: # grey noise col_noise = powerlaw_psd_gaussian( 2, len(waveform)) + powerlaw_psd_gaussian(-2, len(waveform)) col_noise = col_noise * noise_amp / np.abs( col_noise).max() # get noise with desired amplitude waveform = waveform + torch.tensor(col_noise) return waveform.unsqueeze(0).type(torch.float)
def test_slope_distribution(self): # test deviation from scaling by fitting log-binned spectra size = (100, 2**16) fmin = 0 for exponent in [.5, 1, 2]: y = cn.powerlaw_psd_gaussian(exponent, size, fmin=fmin) yfft = np.fft.fft(y) f = np.fft.fftfreq(y.shape[-1]) m = f > 0 # mask fit, fcov = np.polyfit( np.log10(f[m]), np.log10(abs(yfft[...,m].T**2)), 1, cov=True ) slope_in = (exponent + fit[0] < 3 * np.sqrt(fcov[0,0])).mean() self.assertTrue(slope_in > 0.95)
def create_synthetic_noise_dataset(cfg): """ Creates a NoiseDataset instance of sounds with synthetic noise. """ from colorednoise import powerlaw_psd_gaussian betas = np.linspace(cfg['data.mix_synthetic_noise.min_beta'], cfg['data.mix_synthetic_noise.max_beta'], num=cfg['data.mix_synthetic_noise.num_samples']) sample_rate = cfg['data.sample_rate'] segment_length = 2 * cfg['data.len_min'] wavs = [ powerlaw_psd_gaussian(beta, sample_rate * segment_length) for beta in betas ] wavs = [audio.normalize(wav, low=-1, high=1) for wav in wavs] return NoiseDataset(wavs)
def evaluate_omega(Serie,num_cis): Size= len(Serie) alpha, S,= est_alpha(Serie,w1,w2,b1,b2) S_aux = np.zeros(num_cis) for i in range(num_cis): np.random.seed(i) Serie_aux = cn.powerlaw_psd_gaussian(float(alpha), Size) _, S_aux[i] = est_alpha(Serie_aux,w1,w2,b1,b2) S/=np.log(num_states) S_aux/=np.log(num_states) D = np.zeros(num_cis) for i in range(num_cis): D[i]=abs(S - S_aux[i])/S_aux[i] return (float (alpha),S,np.mean(S_aux),np.mean(D)) #print(evaluate_omega(Serie,10))
def input_noise(f_sample, n_sample, v_therm, f_flick_corner): max_freq = 100 min_freq = 0 thermal_noise = white_noise(f_sample, n_sample, v_therm) beta = 1 # the exponent if f_flick_corner is None: flicker_noise = None total_noise = thermal_noise else: flicker_noise = cn.powerlaw_psd_gaussian(beta, n_sample, 0) f = np.fft.rfftfreq(n_sample, 1 / f_sample) f[0] = f[1] f = f**(-1 / 2.) s = np.sqrt(np.mean(f**2)) flicker_noise = flicker_noise * s * v_therm / (f_flick_corner**( -beta / 2)) * np.sqrt(f_sample / 2) total_noise = thermal_noise + flicker_noise return thermal_noise, flicker_noise, total_noise
def colored_temporal_noise(self): beta = 1 # the exponent. 1 = pink noise, 2 = brown noise, 0 = white noise? variance_limits = np.array([-3, 3]) samples = self.frames.shape[2] # number of time samples to generate frame_time_series_unit_variance = cn.powerlaw_psd_gaussian( beta, samples) # Cut variance to [-3,3] frame_time_series_unit_variance_clipped = np.clip( frame_time_series_unit_variance, variance_limits.min(), variance_limits.max()) # Scale to [0 1] frame_time_series = (frame_time_series_unit_variance_clipped - variance_limits.min()) / variance_limits.ptp() self.options["raw_intensity"] = (0, 1) # Cast time series to frames assert len( frame_time_series ) not in self.frames.shape[: -1], "Oops. Two different dimensions match the time series length." self.frames = np.zeros(self.frames.shape) + frame_time_series
err_squared_phase = [] std_errs_phase = [] err_squared_mags = [] std_errs_mags = [] print('Signal to Noise: ' + str(snr)) for freq in tqdm(freqs, leave=True, position=0): references = [{ 'time': time, 'signal': np.sin(2 * np.pi * freq * time) }] channels = np.arange(0, num_averages, 1) signal = {'time': time} sig_vals = [] for column_num in range(num_averages): column = np.sin(2 * np.pi * freq * time) + np.sqrt( .5 / snr) * cn.powerlaw_psd_gaussian(1, time.size) sig_vals.append(column) sig_vals = np.transpose(sig_vals) signal['signal'] = sig_vals out = LIA.amplify(references, signal) raw_phases = out['reference 1']['phases'] raw_mags = out['reference 1']['magnitudes'] nobs_phase, minmax, mean_phase, variance_phase, skewness, kurtosis = sp.describe( (0 - np.array(raw_phases))**2) nobs_mags, minmax, mean_mags, variance_mags, skewness, kurtosis = sp.describe( (1 - np.array(raw_mags))**2) std_err_phase = np.sqrt(variance_phase / nobs_phase)