def build(self, file_origin, file_destination): """ Add noise to the file_origin and save the result in file_destination. Parameters ---------- file_origin : str Path to the source file. file_destination : str Path to the destination file. """ audio, sr = sf.read(file_origin) # Calculate signal mean power s_power = np.mean(audio**2) s_db = power_to_db(s_power) # Get noise power n_db = s_db - self.snr n_power = db_to_power(n_db) # Define noise signal noise = np.random.normal(loc=0.0, scale=np.sqrt(n_power), size=audio.shape) # Sum noise aug_audio = audio + noise # Check if the new singal clipped if np.any(aug_audio > 1.0): # TODO: check this solution aug_audio = aug_audio / np.amax(aug_audio) # Save result to file sf.write(file_destination, aug_audio, sr)
def plotter( spectrogram, freqs=None, # Frequencies array that goes with this spect title=None, db=False, #db transform the spect fig_size=(15, 15), #Without this, just plots without a figsize boxes=None, ): if fig_size: fig, ax = plt.subplots(1, figsize=fig_size) else: fig, ax = plt.subplots(1) #, figsize=(10, 10)) if db: ax.imshow(power_to_db(spectrogram), cmap=plt.get_cmap("gray_r")) else: ax.imshow(spectrogram, cmap=plt.get_cmap("gray_r")) ax.set_ylim(ax.get_ylim()[::-1]) if title: ax.set_title(title) if freqs is not None: res = 1000 import math # Find the factor by which to multiply the current y axis high_y = ax.get_ylim()[1] high_freq = math.ceil(freqs.max()) low_freq = math.ceil(freqs.min()) multiply_by = high_freq / high_y # Set the current ticks at strange numbers # such that when they are replaced with multiplication # by multiply_by, they will be even numbers desired_high_tick_freq = high_freq - (high_freq % res) desired_low_tick_freq = low_freq - (low_freq % res) eventual_ticks = np.arange(desired_low_tick_freq, desired_high_tick_freq + res, res) new_ticks = eventual_ticks / multiply_by ax.set_yticks(new_ticks) ax.set_yticklabels(new_ticks * multiply_by) if boxes: for box in boxes: rect = patch.Rectangle(xy=(box[2], box[0]), width=box[3] - box[2], height=box[1] - box[0], fill=None) ax.add_patch(rect) ax.set_aspect(spectrogram.shape[1] / (3 * spectrogram.shape[0])) #return fig, ax plt.show()
def plotter(spectrogram, frequencies, times, ax, title=None): ax.set_title(title) ax.pcolormesh(times, frequencies, power_to_db(spectrogram), cmap=plt.get_cmap("gray_r"), shading='auto') ax.set_xlabel("time (sec)") ax.set_ylabel("frequency (Hz)")
def plotlogspectrogram(soundnames, rawsounds): """ Plot the log spectrogram of sound data. :param soundnames: labels for plotting. :param rawsounds: raw audio data for plotting. """ plt.figure(figsize=(25, 60)) i = 1 for n, f in zip(soundnames, rawsounds): plt.subplot(7, 1, i) d = core.power_to_db(np.abs(librosa.stft(f))**2) librosa.display.specshow(d, x_axis='time', y_axis='log') plt.colorbar(format='%+2.0f dB') plt.title(n.title()) i += 1 plt.suptitle("Log power spectrogram", x=0.5, y=0.915, fontsize=18) plt.show()
def extracting_acoustic_features(y): power = 2 energy = get_energy(y, win_length=NFFT, hop_length=HOP_LENGTH) s = stft(y=np.ascontiguousarray(y), n_fft=NFFT, hop_length=HOP_LENGTH, win_length=WIN_LENGTH, center=False) assert energy.shape[0] == s.shape[1] # magnitude spectrogram spec = np.abs(s) # phase spectrogram phase = np.angle(s) # spectrum (all the arrays are: [freq, time]) power_spec = spec**power # power-spectrogram # power mel-filter banks spectrogram power_mel_spec = power_to_db( melspectrogram(sr=sr, S=power_spec, n_fft=NFFT, hop_length=HOP_LENGTH, n_mels=NMELS)) # MFCCs coefficiences mfcc_spec = mfcc(sr=sr, S=power_mel_spec, n_mfcc=NMFCC) return {'energy': energy, 'spec': spec.T, 'pspec': power_spec.T, 'mspec': power_mel_spec.T, 'mfcc': mfcc_spec.T, 'phase': phase.T}
def plotter( spectrogram, ax, title=None, upside_down=False, db=True, #db transform the spect return_ax=False): # Plot, flip the y-axis if db: ax.imshow(power_to_db(spectrogram), cmap=plt.get_cmap("gray_r")) else: ax.imshow(spectrogram, cmap=plt.get_cmap("gray_r")) if upside_down: ax.set_ylim(ax.get_ylim()[::-1]) if title: ax.set_title(title, fontsize=12) ax.set_aspect(spectrogram.shape[1] / (3 * spectrogram.shape[0])) if return_ax: return ax plt.show()
def plotter( spectrogram, title=None, upside_down=False, db=False, #db transform the spect fig_size=(15, 15), #Without this, just plots without a figsize ): return # Plot, flip the y-axis if fig_size: fig, ax = plt.subplots(1, figsize=fig_size) else: fig, ax = plt.subplots(1) #, figsize=(10, 10)) if db: ax.imshow(power_to_db(spectrogram), cmap=plt.get_cmap("gray_r")) else: ax.imshow(spectrogram, cmap=plt.get_cmap("gray_r")) if upside_down: ax.set_ylim(ax.get_ylim()[::-1]) if title: ax.set_title(title) ax.set_aspect(spectrogram.shape[1] / (3 * spectrogram.shape[0]))
def spectrogram_image(mels): fig = Figure() ax = fig.add_subplot(1, 1, 1) specshow(data=power_to_db(mels), ax=ax, sr=22050, cmap='Blues') return encode_mpl_fig(fig, ax)