Mercurial > hg > cm
comparison equalLoudness.py @ 10:c5e7162fb8ea
* A bit of commenting
author | Carl Bussey <c.bussey@se10.qmul.ac.uk> |
---|---|
date | Tue, 22 Apr 2014 01:20:56 +0100 |
parents | ef62cca13f36 |
children | ca98f5f26bcb |
comparison
equal
deleted
inserted
replaced
9:ef62cca13f36 | 10:c5e7162fb8ea |
---|---|
10 dArray = np.array([8, 16, 32, 64, 128, 256, 512, 1024])/1000.0 | 10 dArray = np.array([8, 16, 32, 64, 128, 256, 512, 1024])/1000.0 |
11 | 11 |
12 def get_elc_range(xtol=0.0000005, verbose=False): | 12 def get_elc_range(xtol=0.0000005, verbose=False): |
13 """ | 13 """ |
14 Plot the prediction of the equal loudness contours of frequencies for a range of SPL levels from 10 to 120dB, intervals of 10dB. | 14 Plot the prediction of the equal loudness contours of frequencies for a range of SPL levels from 10 to 120dB, intervals of 10dB. |
15 | |
16 Parameters: | |
17 * xtol (type: float) - x tolerance of fsolve | |
18 * verbose (type: boolean) - I tell you what I know | |
19 | |
20 Returns: | |
21 * elc (type: numpy array of floats) - SPL at which each frequency in fArray is equal to i*10 phons, where i is the first dimension index | |
22 | |
15 """ | 23 """ |
16 levels = range(20, 100, 20) | 24 levels = range(20, 100, 20) |
17 elc = np.zeros((len(levels), len(fArray))) | 25 elc = np.zeros((len(levels), len(fArray))) |
18 | 26 |
19 for i in range(len(levels)): | 27 for i in range(len(levels)): |
27 return elc | 35 return elc |
28 | 36 |
29 def get_elc(SPL = 90, xtol=0.0000005, verbose=False): | 37 def get_elc(SPL = 90, xtol=0.0000005, verbose=False): |
30 """ | 38 """ |
31 Plot the prediction of the equal loudness contours for specified level. | 39 Plot the prediction of the equal loudness contours for specified level. |
40 | |
41 Parameters: | |
42 * SPL (type: float) - SPL of the reference, or the loudness level | |
43 * xtol (type: float) - x tolerance of fsolve | |
44 * verbose (type: boolean) - I tell you what I know if you give me True | |
45 | |
46 Returns: | |
47 * elc (type: numpy array of floats) - SPL at which each frequency in fArray is equal to SPL phons | |
32 """ | 48 """ |
33 fs = 44100 | 49 fs = 44100 |
34 t = np.array(range(44100)) | 50 t = np.array(range(44100)) |
35 elc = np.zeros((len(fArray))) | 51 elc = np.zeros((len(fArray))) |
36 env = get_cos2_env(44100, fs) | 52 env = get_cos2_env(44100, fs) |
51 | 67 |
52 return elc | 68 return elc |
53 | 69 |
54 def get_temporal_descrimination(verbose=False): | 70 def get_temporal_descrimination(verbose=False): |
55 """ | 71 """ |
56 Return the prediction of STL and peak of STL for tones with short durations. | 72 Return the prediction of STL and peak of STL for tones with short duration. |
73 | |
74 Parameters | |
75 * verbose (type: boolean) - I tell you what I know if you give me True | |
76 | |
77 Returns: | |
78 * STLMax (type: array-like of floats) - peak short term loudness for each duration | |
79 * STL (type: array-like of floats) - short term loudness function for each duration | |
57 """ | 80 """ |
58 | 81 |
59 | 82 |
60 fs = 44100 | 83 fs = 44100 |
61 STLMax = np.zeros((len(dArray))) | 84 STLMax = np.zeros((len(dArray))) |
105 Parameters: | 128 Parameters: |
106 * input (type: array-like of floats) - input signal | 129 * input (type: array-like of floats) - input signal |
107 * fs (type: int) - sampling frequency | 130 * fs (type: int) - sampling frequency |
108 * SPL (type: float) - the level to start searching at | 131 * SPL (type: float) - the level to start searching at |
109 * xtol (type: float) - x tolerance of fsolve | 132 * xtol (type: float) - x tolerance of fsolve |
110 * verbose (type: boolean) - tell me everything I need to know | 133 * verbose (type: boolean) - I tell you what I know if you give me True |
134 | |
135 Returns: | |
136 x (type: float) - sound pressure level at equal loudness | |
111 """ | 137 """ |
112 | 138 |
113 x = opt.fsolve(getLoudnessDifference, SPL, (input, fs, refL, verbose), factor=0.1, xtol=xtol) | 139 x = opt.fsolve(getLoudnessDifference, SPL, (input, fs, refL, verbose), factor=0.1, xtol=xtol) |
114 | 140 |
115 return x | 141 return x |
120 | 146 |
121 Parameters: | 147 Parameters: |
122 * SPL (type: float) - the level to set input to | 148 * SPL (type: float) - the level to set input to |
123 * input (type: array-like of floats) - input signal | 149 * input (type: array-like of floats) - input signal |
124 * fs (type: int) - sampling frequency | 150 * fs (type: int) - sampling frequency |
151 | |
152 Returns: | |
153 difference (type: float) - difference in loudness | |
125 """ | 154 """ |
126 if(verbose): | 155 if(verbose): |
127 print "SPL: ", SPL | 156 print "SPL: ", SPL |
128 input = input/rms(input) | 157 input = input/rms(input) |
129 compLoudness = pp.calc_loudness_quiet(input, fs, SPL, rectify=False, inc_loud_region=True)[2].max() | 158 compLoudness = pp.calc_loudness_quiet(input, fs, SPL, rectify=False, inc_loud_region=True)[2].max() |
134 return difference | 163 return difference |
135 | 164 |
136 def rms(input): | 165 def rms(input): |
137 """ | 166 """ |
138 RMS of the input | 167 RMS of the input |
168 | |
169 Parameters: | |
170 * input (type: array-like of floats): input array | |
171 | |
172 Returns: | |
173 * output (type: float): the rms | |
139 """ | 174 """ |
140 output = np.sqrt(np.mean(input**2)) | 175 output = np.sqrt(np.mean(input**2, -1)) |
141 | 176 |
142 return output | 177 return output |
143 | 178 |
144 def plot_elc(ppOut, show = True): | 179 def plot_elc(elc, show = True): |
145 """ | 180 """ |
146 Plot an equal loudness contour | 181 Plot an equal loudness contour |
182 | |
183 Parameters: | |
184 * elc (type: array-like of floats) - equal loudness contour x, obtained from get_elc | |
185 * show (type: boolean) - show the figure or turn hold on | |
147 """ | 186 """ |
148 plt.gca().set_xscale('log') | 187 plt.gca().set_xscale('log') |
149 plt.ylim((0,170)) | 188 plt.ylim((0,170)) |
150 plt.plot(fArray, ppOut, 'x-') | 189 plt.plot(fArray, elc, 'x-') |
151 if(show): | 190 if(show): |
152 plt.show() | 191 plt.show() |
153 else: | 192 else: |
154 plt.hold(True) | 193 plt.hold(True) |
155 | 194 |