Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. import numpy as np
  2.  
  3.  
  4. def freq2mel(freq):
  5. return 1127.01048 * np.log(1 + freq / 700.0)
  6.  
  7. def mel2freq(mel):
  8. return (np.exp(mel / 1127.01048) - 1) * 700
  9.  
  10. def mel_binning_matrix(specgram_window_size, sample_frequency, num_mel_bands):
  11. """
  12. function that returns a matrix that converts a regular DFT to a mel-spaced DFT,
  13. by binning coefficients.
  14.  
  15. specgram_window_size: the window length used to compute the spectrograms
  16. sample_frequency: the sample frequency of the input audio
  17. num_mel_bands: the number of desired mel bands.
  18.  
  19. The output is a matrix with dimensions (specgram_window_size/2 + 1, num_bands)
  20. """
  21. min_freq, max_freq = 0, sample_frequency / 2
  22. min_mel = freq2mel(min_freq)
  23. max_mel = freq2mel(max_freq)
  24. num_specgram_components = specgram_window_size / 2 + 1
  25. m = np.zeros((num_specgram_components, num_mel_bands))
  26.  
  27. r = np.arange(num_mel_bands + 2) # there are (num_mel_bands + 2) filter boundaries / centers
  28.  
  29. # evenly spaced filter boundaries in the mel domain:
  30. mel_filter_boundaries = r * (max_mel - min_mel) / (num_mel_bands + 1) + min_mel
  31.  
  32. def coeff(idx, mel): # gets the unnormalised filter coefficient of filter 'idx' for a given mel value.
  33. lo, cen, hi = mel_filter_boundaries[idx:idx+3]
  34. if mel <= lo or mel >= hi:
  35. return 0
  36. # linearly interpolate
  37. if lo <= mel <= cen:
  38. return (mel - lo) / (cen - lo)
  39. elif cen <= mel <= hi:
  40. return 1 - (mel - cen) / (hi - cen)
  41.  
  42.  
  43. for k in xrange(num_specgram_components):
  44. # compute mel representation of the given specgram component idx
  45. freq = k / float(num_specgram_components) * (sample_frequency / 2)
  46. mel = freq2mel(freq)
  47. for i in xrange(num_mel_bands):
  48. m[k, i] = coeff(i, mel)
  49.  
  50. # normalise so that each filter has unit contribution
  51. return m / m.sum(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement