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 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 white_noise(N): x = gen.white(N) return x
from librosa import display fname = "../MAPS_MUS-mond_1_SptkBGAm.wav" data, fs = sf.read (fname) laenge = 300000 links = data[0:laenge,0] rechts = data[0:laenge,1] maxdata = np.max(links) mindata = np.min(links) y, sr = librosa.load(fname) c = np.abs(librosa.cqt(y[0:laenge], sr=sr)) wh = white(laenge) plt.figure(figsize = (1,3)) plt.subplot(131) librosa.display.specshow(librosa.amplitude_to_db(c, ref=np.max), sr=sr, y_axis = 'linear') plt.colorbar() plt.xlabel("time") plt.ylabel("Frequency [Hz]") plt.title("CQT of Clean Signal") plt.subplot(132) d = np.abs(librosa.cqt(y[0:laenge] + 0.02*wh[0:laenge], sr=sr)) librosa.display.specshow(librosa.amplitude_to_db(d, ref=np.max), sr=sr, y_axis = 'linear') plt.colorbar() plt.xlabel("time")
def test_power_density(self): fs = 44100 samples = 44100 * 10 _, L = octaves(white(samples), fs, density=True) change = np.diff(L).mean().round(0) assert (change == 0.)
def test_power(self): fs = 44100 samples = 44100 * 10 _, L = octaves(white(samples), fs) change = np.diff(L).mean().round(0) assert (change == +3.)
def test_length(self): N = 1000 assert (len(white(N)) == N) N = 1001 assert (len(white(N)) == N)
import acoustics 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(white(samples), fs, density=True); change = np.diff(L).mean().round(0) assert(change==0.)
def test_power(self): fs = 44100 samples = 44100 * 10 _, L = octaves(white(samples), fs); change = np.diff(L).mean().round(0) assert(change==+3.)
def test_length(self): N = 1000 assert(len(white(N))==N) N = 1001 assert(len(white(N))==N)