def logMelSpectrum(input, samplingrate): """ Calculates the log output of a Mel filterbank when the input is the power spectrum Args: input: array of power spectrum coefficients [N x nfft] where N is the number of frames and nfft the length of each spectrum samplingrate: sampling rate of the original signal (used to calculate the filterbank shapes) Output: array of Mel filterbank log outputs [N x nmelfilters] where nmelfilters is the number of filters in the filterbank Note: use the trfbank function provided in lab1_tools.py to calculate the filterbank shapes and nmelfilters """ filter_bank = trfbank(samplingrate, input.shape[1]) #uncomment to display filter bank plots # _ = plt.rcParams['figure.figsize'] = [15, 3] # _ = plt.subplot(211) # _ = plt.plot(filter_bank) # _ = plt.subplot(212) # _ = plt.plot(filter_bank.T) # plt.show() #too much variation from the example #without doing this there were only handful of variations from example #filter_bank = np.where(filter_bank == 0, np.finfo(float).eps, filter_bank) # Numerical Stability return np.log(input.dot(filter_bank.T))
def logMelSpectrum(input, samplingrate): """ Calculates the log output of a Mel filterbank when the input is the power spectrum Args: input: array of power spectrum coefficients [N x nfft] where N is the number of frames and nfft the length of each spectrum samplingrate: sampling rate of the original signal (used to calculate the filterbank shapes) Output: array of Mel filterbank log outputs [N x nmelfilters] where nmelfilters is the number of filters in the filterbank Note: use the trfbank function provided in lab1_proto.py to calculate the filterbank shapes and nmelfilters """ N, nfft = input.shape # 92frames Mel = trfbank(samplingrate, nfft) # 40filters*512 M = Mel.shape[0] logMelSpec = np.zeros((N, M)) for i in range(N): for j in range(M): logMelSpec[i, j] = np.log(np.sum(input[i] * Mel[j])) return logMelSpec
def logMelSpectrum(input, samplingrate): """ Calculates the log output of a Mel filterbank when the input is the power spectrum Args: input: array of power spectrum coefficients [N x nfft] where N is the number of frames and nfft the length of each spectrum samplingrate: sampling rate of the original signal (used to calculate the filterbank shapes) Output: array of Mel filterbank log outputs [N x nmelfilters] where nmelfilters is the number of filters in the filterbank Note: use the trfbank function provided in lab1_tools.py to calculate the filterbank shapes and nmelfilters """ fil_bank = trfbank(samplingrate, 512, lowfreq=133.33, linsc=200 / 3., logsc=1.0711703, nlinfilt=13, nlogfilt=27, equalareas=False) mspec = np.log(np.dot(input, np.transpose(fil_bank))) #plt.pcolormesh(mspecc) return mspec
def logMelSpectrum(input, samplingrate): """ Calculates the log output of a Mel filterbank when the input is the power spectrum Args: input: array of power spectrum coefficients [N x nfft] where N is the number of frames and nfft the length of each spectrum samplingrate: sampling rate of the original signal (used to calculate the filterbank shapes) Output: array of Mel filterbank log outputs [N x nmelfilters] where nmelfilters is the number of filters in the filterbank Note: use the trfbank function provided in lab1_tools.py to calculate the filterbank shapes and nmelfilters """ return np.log(input.dot(trfbank(samplingrate, input.shape[1]).T))
def logMelSpectrum(input, samplingrate, nfft): """ Calculates the log output of a Mel filterbank when the input is the power spectrum Args: input: array of power spectrum coefficients [N x nfft] where N is the number of frames and nfft the length of each spectrum samplingrate: sampling rate of the original signal (used to calculate the filterbank shapes) Output: array of Mel filterbank log outputs [N x nmelfilters] where nmelfilters is the number of filters in the filterbank Note: use the trfbank function provided in lab1_tools.py to calculate the filterbank shapes and nmelfilters """ import matplotlib.pyplot as plt t = trfbank(samplingrate, nfft) if (False): # add flag plt.plot(t) plt.title('Trfbank') plt.show() return np.log(np.dot(input, t.T))
def logMelSpectrum(input, samplingrate): """ Calculates the log output of a Mel filterbank when the input is the power spectrum Args: input: array of power spectrum coefficients [N x nfft] where N is the number of frames and nfft the length of each spectrum samplingrate: sampling rate of the original signal (used to calculate the filterbank shapes) Output: array of Mel filterbank log outputs [N x nmelfilters] where nmelfilters is the number of filters in the filterbank Note: use the trfbank function provided in lab1_tools.py to calculate the filterbank shapes and nmelfilters """ N = len(input) # 92frames Mel = lab1_tools.trfbank(samplingrate, len(input[0])) # 40filters*512 M = Mel.shape[0] # plot the filters in linear frequency scale x=k*512/fs(20000) #plt.figure() x = np.zeros((N, M)) for i in range(N): for j in range(M): x[i, j] = np.log(np.sum(input[i, :] * Mel[j, :])) return x
freq = np.linspace(-0.5, 0.5, len(A)) response = np.abs(fftshift(A / abs(A).max())) plt.plot(freq, response) plt.title("Frequency response of the Hamming window") plt.ylabel("Frequency space amplitude") plt.xlabel("Frequency space sample") plt.savefig("./plots/hamming_fft.png", bbox_inches='tight') # =================================================================== fig, ax = plt.subplots(figsize=fig_size) ax.set_title("Power spectrum") ax.pcolormesh(example['spec'].T) fig.savefig("./plots/power_spectrum.png", bbox_inches='tight') # =========================================================== # 4.5 filter_ = trfbank(example['samplingrate'], 512) plt.figure(figsize=(12, 2)) plt.xlim(0, 180) for cap in filter_: plt.plot(cap) plt.title("Mel filterbank") plt.savefig("./plots/mel_filterbank.png", bbox_inches='tight') # fig, ax = plt.subplots(figsize=fig_size) ax.set_title("log-scale Mel spectrum") ax.pcolormesh(example['mspec'].T) fig.savefig("./plots/log_mel_spectrum.png", bbox_inches='tight') # ==================================================================== # 4.6 fig, ax = plt.subplots(figsize=fig_size)