-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmel.py
68 lines (51 loc) · 2.1 KB
/
mel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#coding:utf-8
# A class of mel-frequency equal shifted list
# Check version
# Python 3.6.4 on win32 (Windows 10)
# numpy 1.16.3
# matplotlib 2.1.1
import numpy as np
class Class_mel(object):
def __init__(self, num_band=1024, fmin=40, fmax=8000):
# initalize
self.num_band=num_band # number of equal shift band
self.fmin= fmin # low frequecny limit by unit is [Hz]
self.fmax= fmax # low frequecny limit by unit is [Hz]
self.mel_fmin= self.hz2mel( self.fmin ) # low side center frequecny by unit is [mel frequency]
self.mel_fmax= self.hz2mel( self.fmax ) # high side center frequecny by unit is [mel frequency]
self.mel_flist= np.linspace( self.mel_fmin, self.mel_fmax, self.num_band)
#print ( self.mel_flist)
self.flist = self.mel2hz( self.mel_flist) # equal mel-frequency shifted Hz frequency list
#print (self.flist)
def hz2mel(self,freq):
return 2595. * np.log( freq / 700. + 1.0)
def mel2hz(self,mel_freq):
return 700. * ( np.exp( mel_freq / 2595) - 1.0)
def get_postion(self, in_hzs):
pos=[]
cpos=[]
for in_hz in in_hzs:
pos.append( int((self.num_band-1.0) * (self.hz2mel( in_hz)-self.mel_fmin) / (self.mel_fmax - self.mel_fmin) ))
cpos.append(str(in_hz))
return pos,cpos
if __name__ == '__main__':
from matplotlib import pyplot as plt
from BPF4 import *
# instance
mel=Class_mel(num_band=1024) #
# set Q of BPF
Q0=40.0
sr0=44100 # sampling rate frequency
# show mel-frequecny equal shifted filter bank frequency response
plt.xlabel('Hz')
plt.ylabel('dB')
plt.title('mel-frequency equal shifted Band Pass Filter bank')
plt.xscale('log')
for flist0 in mel.flist:
print ( flist0 )
bpf=Class_BPFtwice(fc=flist0, Q=Q0, sampling_rate=sr0)
amp, freq= bpf.H0(freq_low=20, freq_high=10000, Band_num=1024)
plt.plot(freq, amp)
plt.grid()
plt.tight_layout()
plt.show()