forked from ShreyaKhare/speechpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
executable file
·62 lines (47 loc) · 1.84 KB
/
functions.py
File metadata and controls
executable file
·62 lines (47 loc) · 1.84 KB
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
"""function module.
This module contains necessary functions for calculating the features
in the `features` module.
Attributes:
frequency_to_mel: Converting the frequency to Mel scale.
This is necessary for filterbank energy calculation.
mel_to_frequency: Converting the Mel to frequency scale.
This is necessary for filterbank energy calculation.
triangle: Creating a triangle for filterbanks.
This is necessary for filterbank energy calculation.
zero_handling: Handling zero values due to the possible
issues regarding the log functions.
"""
from __future__ import division
import numpy as np
from . import processing
from scipy.fftpack import dct
import math
def frequency_to_mel(f):
"""converting from frequency to Mel scale.
:param f: The frequency values(or a single frequency) in Hz.
:returns: The mel scale values(or a single mel).
"""
return 1127 * np.log(1 + f / 700.)
def mel_to_frequency(mel):
"""converting from Mel scale to frequency.
:param mel: The mel scale values(or a single mel).
:returns: The frequency values(or a single frequency) in Hz.
"""
return 700 * (np.exp(mel / 1127.0) - 1)
def triangle(x, left, middle, right):
out = np.zeros(x.shape)
out[x <= left] = 0
out[x >= right] = 0
first_half = np.logical_and(left < x, x <= middle)
out[first_half] = (x[first_half] - left) / (middle - left)
second_half = np.logical_and(middle <= x, x < right)
out[second_half] = (right - x[second_half]) / (right - middle)
return out
def zero_handling(x):
"""
This function handle the issue with zero values if the are exposed
to become an argument for any log function.
:param x: The vector.
:return: The vector with zeros substituted with epsilon values.
"""
return np.where(x == 0, np.finfo(float).eps, x)