This repository has been archived by the owner on Feb 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
tercja.py
74 lines (61 loc) · 1.78 KB
/
tercja.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
69
70
71
72
73
74
#!/usr/bin/env python
# coding=utf-8
__author__ = "Xevaquor"
from math import pow, log
# import numpy as np
# import matplotlib.pyplot as plt
base = pow(2, (1. / 3))
def compute(xarg):
"""
Computes value of tercja function ignoring set bounds
:param xarg: x argument for func
:return: computed y value
"""
return base ** xarg
def compute_inverse(yarg):
"""
Computes inverse of tercja
:param yarg: y argument for func
:return: corresponding x value
"""
# non positive numbers are out of domain of log func
#we are silently ignoring it
if yarg <= 0:
return 0
return log(yarg, base)
def get_value_from_x(xx, minimum_x, maximum_x):
"""
Computes value from percentage in interval. For more details please see:
https://github.com/Xevaquor/GXAudioVisualisation/wiki/Tercja
Eg: .42 means 42%
:param xx: Percent in interval. Must be in range [0,1]
:param minimum_x: Lower x bound
:param maximum_x: Upper x bound
:return: corresponding value of Tercja func
"""
assert (0 <= xx <= 1)
return compute(xx * (maximum_x - minimum_x) + minimum_x)
if __name__ == "__main__":
print("For usage info please visit: https://github.com/Xevaquor/GXAudioVisualisation/wiki/Tercja")
# uncomment following for plot
# steps = 500
# a = 7
# b = 15
#
# assert get_value_from_x(.0 == compute(a), a, b)
# assert get_value_from_x(1. == compute(b), a, b)
# assert get_value_from_x(0.5 == compute((a + b) / 2.), a, b)
#
# x = np.linspace(a, b, steps, True)
# y = compute(x)
# z = [compute_inverse(i) for i in y]
#
# for i, v in enumerate(range(0, 34)):
# print(str(i) + ":" + str(compute(i)))
#
# plt.plot(x, y, lw=3.7, c='orange')
# plt.plot(y, z, lw=3.7, c='purple')
# plt.plot(np.linspace(0, max(y)), np.linspace(0, max(y)), '--k')
# plt.legend(["Tercja", "Inverse of Tercja"])
#
# plt.show()