1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """ util.py - classes and functions that are not specific to the calculation
25 methods.
26
27 """
28
29 import numpy
30 import scipy
31
32 import wiat
33
35 """ Properties of air as a function of temperature
36
37 The formula, valid for ±10 Celcius around 26.85 C, comes from Table 1
38 of reference 1.
39
40 References:
41
42 1. Keefe, D. H. (1984).
43 Acoustical wave propagation in cylindrical ducts: Transmission
44 line parameter approximations for isothermal and nonisothermal
45 boundary conditions. J. Aoucst. Soc. Am., 75(1), 58-62.
46
47 TODO: look for a more recent source to double check the validity
48
49 """
50 deltaT = T - 26.85
51 properties = {
52
53 'rho': 1.1769 * (1 - 0.00335*deltaT),
54
55 'mu': 1.8460E-5 * (1 + 0.00250*deltaT),
56
57 'gamma': 1.4017 * (1 - 0.00002*deltaT),
58
59 'nu': 0.8410 * (1 - 0.00020*deltaT),
60
61 'c': 3.4723E+2 * (1 + 0.00166*deltaT)}
62 return properties
63
65 " Calculate the interval in cents between two frequencies. "
66 return 1200. * numpy.log2(f2/f1)
67
69 " Calculate the ratio in dB between two values. "
70 return 20. * numpy.log10(A2/A1)
71
75
79
81 """ Return the equivalent length of a complex reflection coefficient
82 R: reflection coefficient
83 k: wavenumber
84 """
85
86
87 return numpy.angle(-R)/(-2*k)
88
89
91 """ Function that will load a backup of the results if available,
92 otherwise it calculates the function and save the results.
93 """
94 try:
95 f,Z = wiat.Data.load_from_mat(('f', 'Z'), filename)
96 except IOError,arg:
97 print arg
98 print 'No backup available (%s), calculating...'%filename
99 Z = func(**args)
100 scipy.io.savemat(filename, {'f': args['freqs'], 'Z': Z})
101 return Z
102
104 scale = {}
105 note_name = \
106 ['Cx','Dx flat','Dx','Ex flat','Ex',\
107 'Fx','Gx flat','Gx','Ax flat','Ax','Bx flat','Bx']
108
109
110 for i in range(8):
111 for j,note in enumerate(note_name):
112 name = note.replace("x","%d"%i)
113 scale[name] = 440*2**(-4+i+(j-9)/12.)
114 return scale
115
116 EQUAL_TEMPERED_SCALE = __create_equal_tempered_scale()
117