def inject_noise(data, noise_color, noise_level): noise_data = noise(len(data), color=noise_color) noise_energy = np.sqrt(noise_data.dot(noise_data) / noise_data.size) data_energy = np.sqrt(data.dot(data) / data.size) data += noise_level * noise_data * data_energy / noise_energy return data
""" (Monofractal) Detrended Fluctuation Analysis example """ import numpy as np from acoustics.generator import noise import matplotlib.pyplot as plt n = 8000 whitenoise = noise(n, color='white') pinknoise = noise(n, color='pink') # fractional brownian noise RW1 = np.cumsum(whitenoise - np.mean(whitenoise)) RW2 = np.cumsum(pinknoise - np.mean(pinknoise)) # root-mean-square variation of time series (typical TS approach) ## average variation in time series ## average amplitude is similar for two very different signals RMS_white = np.sqrt(np.mean(np.power(whitenoise, 2))) RMS_pink = np.sqrt(np.mean(np.power(pinknoise, 2))) print("Average variation for white noise {} and pink noise {}".format( RMS_white, RMS_pink)) # Local Detrending: local RMS variation in the time series ts = pinknoise X = np.cumsum(ts - np.mean(ts)) scale = 1000 m = 1 segments = int(np.floor(len(X) / scale)) index = [] fit = []
def get_noise(length: int) -> np.ndarray: color = random.choice(['white', 'pink', 'blue', 'brown', 'violet']) return noise(length, color).astype('float32')
from acoustics.generator import noise from scipy.io import wavfile as wf from os.path import join as jp import numpy as np if __name__ == '__main__': noise_dir = jp('data', 'train', 'audio', '_background_noise_') white_noise_fn = jp(noise_dir, 'custom_white_noise.wav') # 'white' and 'pink' already exist noise_colors = ['blue', 'brown', 'violet'] for noise_color in noise_colors: noise_fn = jp(noise_dir, 'custom_%s_noise.wav' % noise_color) wf.write( noise_fn, 16000, np.int16(((noise(16000 * 60, color=noise_color)) / 3) * 32767)) print("Done!")
def noise_generator(kind, duration, frame_rate=44100, amplitude=0.5): from acoustics.generator import noise samples = noise(frame_rate, kind) samples = [s / 5 * amplitude for s in samples] return cycle_n(samples, int(duration * frame_rate / 1000))
step_size = 1 q = 2 order = 1 XY = [] FBM = [] FWN = [] #colors = ['white', 'white','pink','brown'] colors = ['white', 'white', 'pink'] process_label = [ '$Anti$-$persistent$ $process$', '$Short$-$range$ $correlations$', '$Persistent$ $process$' ] for i, c in enumerate(colors): fwn = noise(n, color=c) # create some colored fBm data if i == 0: fbm = np.mat(fwn) + (np.mat(np.cumsum(fwn) - np.mean(fwn))) / 5 else: fbm = np.mat(np.cumsum(fwn) - np.mean(fwn)) uneven = fbm.shape[1] % 2 if uneven: fbm = fbm[0, :-1] # AFA step XY.append(md.multi_detrending(fbm, step_size, q, order)) FBM.append(fbm) FWN.append(fwn) FWN[0] = difference(FWN[0], interval=1)
""" write time series to csv for cross-platform testing """ import csv import numpy as np from acoustics.generator import noise n = 8000 ts = noise(n, color = 'white') print(ts) with open('ts.csv', 'w') as f: wr = csv.writer(f) wr.writerow(ts)
def test_power_density(self, color, samples, power_density_change): fs = 48000 _, L = octaves(noise(samples, color), fs, density=True) change = np.diff(L).mean() assert (np.abs(change - power_density_change) < self.ERROR)
def test_length(self, color, samples): assert (len(noise(samples, color)) == samples)
def apply_noise(audio, color, snr): noise_data = noise(len(audio.samples), color=color) return _mix(audio, noise_data, snr)