def generate_melbank(f1=1000, f2=8000): ''' @INPUT: low and high frequency range @RETURN: melmat, (num_mel_bands, num_fft_bands) melmat: Transformation matrix for the mel spectrum. Use this with fft spectra of num_fft_bands_bands length and multiply the spectrum with the melmat this will tranform your fft-spectrum to a mel-spectrum. num_mel_bands: Center frequencies of the mel bands num_fft_bands: center frequencies of fft spectrum ''' # f1, f2 = 1000, 8000 #low and high frequency range #use the same sampling rate and numbers of frequency bank melmat, (melfreq, fftfreq) = melbank.compute_melmat(12, f1, f2, num_fft_bands=4097, sample_rate=22050) # fig, ax = plt.subplots(figsize=(8, 3)) # ax.plot(fftfreq, melmat.T) # ax.grid(True) # ax.set_ylabel('Weight') # ax.set_xlabel('Frequency / Hz') # ax.set_xlim((f1, f2)) # ax2 = ax.twiny() # ax2.xaxis.set_ticks_position('top') # ax2.set_xlim((f1, f2)) # ax2.xaxis.set_ticks(melbank.mel_to_hertz(melfreq)) # ax2.xaxis.set_ticklabels(['{:.0f}'.format(mf) for mf in melfreq]) # ax2.set_xlabel('Frequency / mel') # plt.tight_layout() # fig, ax = plt.subplots() # ax.matshow(melmat) # plt.axis('equal') # plt.axis('tight') # plt.title('Mel Matrix') # plt.tight_layout() # plt.show() return melmat, (melfreq, fftfreq)
def create_mel_bank(): global samples, mel_y, mel_x samples = int(config.MIC_RATE * config.N_ROLLING_HISTORY / (2.0 * config.FPS)) mel_y, (_, mel_x) = melbank.compute_melmat(num_mel_bands=config.N_FFT_BINS, freq_min=config.MIN_FREQUENCY, freq_max=config.MAX_FREQUENCY, num_fft_bands=samples, sample_rate=config.MIC_RATE)
def create_mel_bank(): global samples, mel_y, mel_x samples = int(config.MIC_RATE * config.N_ROLLING_HISTORY / (2.0 * config.FPS)) mel_y, (_, mel_x) = melbank.compute_melmat(num_mel_bands=config.N_FFT_BINS, freq_min=config.MIN_FREQUENCY, freq_max=config.MAX_FREQUENCY, num_fft_bands=samples, sample_rate=config.MIC_RATE)
def create_mel_bank(running_config): global samples, mel_y, mel_x samples = int(running_config['MIC_RATE'] * running_config['N_ROLLING_HISTORY'] / (2.0 * running_config['FPS'])) mel_y, (_, mel_x) = melbank.compute_melmat( num_mel_bands=running_config['N_FFT_BINS'], freq_min=running_config['MIN_FREQUENCY'], freq_max=running_config['MAX_FREQUENCY'], num_fft_bands=samples, sample_rate=running_config['MIC_RATE'])
def create_mel_bank(self): mic_rate = self.configs['mic_rate'] n_fft_bins = self.configs['n_fft_bins'] min_frequency = self.configs['min_frequency'] max_frequency = self.configs['max_frequency'] samples = int(self.configs['mic_rate'] * self.configs['n_rolling_history'] / (2.0 * self.configs['fps'])) self.mel_y, (_, self.mel_x) = melbank.compute_melmat( num_mel_bands=n_fft_bins, freq_min=min_frequency, freq_max=max_frequency, num_fft_bands=samples, sample_rate=mic_rate)
def ComputeMelFilter(nb_bands, f_low, f_high, shift, Fs, frequencies, times, spectrogram, step): """ Apply a Mel-filter to a spectrogram :param nb_bands: number of Mel bands :param f_low: lowest frequency cutoff :param f_high: highest frequency cutoff :param shift: time shift (in terms of number of steps) :param Fs: audio frame rate :param frequencies: list of frequencies of the spectrogram :param times: list of timings of the spectrogram :param spectrogram: spectrogram :param step: step (in seconds) :return: bands frequencies, timings, and filtered spectrogram """ melmat, (melfreq, fftfreq) = melbank.compute_melmat(nb_bands, f_low, f_high, num_fft_bands=len(frequencies), sample_rate=Fs) # apply filter m = np.zeros((len(times), nb_bands)) i = 0 for fft_spectra in spectrogram.transpose(): mel_spectra = melmat.dot(fft_spectra) m[i] = mel_spectra i += 1 # take the log m[m == 0] = np.nan m = np.log(m.transpose()) # centre around mean and normalization of variance m = (m - np.nanmean(m, 1, keepdims=True)) / np.nanstd(m, 1, keepdims=True) np.nan_to_num(m, copy=False) ashift = (max(0, -shift), max(0, shift)) m = np.pad(m, ((0, 0), ashift), 'constant') return (melfreq, times + step * shift, m)
xs = np.fft.rfftfreq(len(data), 1.0 / config.MIC_RATE) return xs, ys def fft(data, window=None): window = 1.0 if window is None else window(len(data)) ys = np.fft.fft(data * window) xs = np.fft.fftfreq(len(data), 1.0 / config.MIC_RATE) return xs, ys def log_partition(xs, ys, subbands): f = interp1d(xs, ys) xs_log = np.logspace(np.log10(xs[0]), np.log10(xs[-1]), num=subbands * 24) xs_log[0] = xs[0] xs_log[-1] = xs[-1] ys_log = f(xs_log) X, Y = [], [] for i in range(0, subbands * 24, 24): X.append(np.mean(xs_log[i:i + 24])) Y.append(np.mean(ys_log[i:i + 24])) return np.array(X), np.array(Y) samples = int( round(config.MIC_RATE * config.N_ROLLING_HISTORY / (2.0 * config.FPS))) mel_y, (_, mel_x) = melbank.compute_melmat(num_mel_bands=config.N_SUBBANDS, freq_min=config.MIN_FREQUENCY, freq_max=config.MAX_FREQUENCY, num_fft_bands=samples, sample_rate=config.MIC_RATE)