comparison cortical_model.py @ 8:d5693c7aa0ae

* Excitation pattern extraction is much faster. Calculating partial loudness for three 2 second components is reduced from ~180s to ~25s. * runScript1.py is a script to get loudness data of all Stimuli within a set data tree. Not useful for anyone else. * testELC.py calculates the equal loudness contours. Can also be used to get the equal loudness of two sounds. When I get the time, this will be added to a loudness package. * loudnessRatios performs some descriptive statistics on loudness data obtained from the data within the set file tree. Probably not useful for anybody else but the author.
author Carl Bussey <c.bussey@se10.qmul.ac.uk>
date Mon, 31 Mar 2014 19:22:23 +0100
parents 0ee61aeb789a
children
comparison
equal deleted inserted replaced
7:0ee61aeb789a 8:d5693c7aa0ae
12 > tq.dat 12 > tq.dat
13 13
14 External dependencies: 14 External dependencies:
15 > scipy 15 > scipy
16 > numpy 16 > numpy
17 > copy
18 > matplotlib 17 > matplotlib
19 18
20 Primary functions: 19 Primary functions:
21 > calc_partial_loudness 20 > calc_partial_loudness
22 > calc_loudness 21 > calc_loudness
31 import sys 30 import sys
32 import utils 31 import utils
33 import scipy.signal as sp 32 import scipy.signal as sp
34 import scipy.fftpack as fft 33 import scipy.fftpack as fft
35 import numpy as np 34 import numpy as np
36 from copy import deepcopy
37 import matplotlib.pyplot as plt 35 import matplotlib.pyplot as plt
38 36
39 def calc_loudness_noise(input, fs, SPL, rectify=False, inc_loud_region = False, verbose = False): 37 def calc_loudness_noise(input, fs, SPL, rectify=False, inc_loud_region = False, verbose = False):
40 38
41 numTracks = np.shape(input)[0] 39 numTracks = np.shape(input)[0]
46 exc_i[i] = get_excitation_i(input[i], fs, SPL, rectify = rectify, verbose = verbose) 44 exc_i[i] = get_excitation_i(input[i], fs, SPL, rectify = rectify, verbose = verbose)
47 45
48 sl = get_specific_loudness_noise(exc_i, inc_loud_region = inc_loud_region) 46 sl = get_specific_loudness_noise(exc_i, inc_loud_region = inc_loud_region)
49 loudness = 2*np.sum(sl,1) 47 loudness = 2*np.sum(sl,1)
50 48
51 return loudness 49 return sl, loudness
52 50
53 def calc_loudness_quiet(input, fs, SPL, rectify = False, inc_loud_region = False, verbose = False): 51 def calc_loudness_quiet(input, fs, SPL, rectify = False, inc_loud_region = False, verbose = False):
54 52
55 exc_i = get_excitation_i(input, fs, SPL, rectify = rectify, verbose = verbose) 53 exc_i = get_excitation_i(input, fs, SPL, rectify = rectify, verbose = verbose)
56 sl = get_specific_loudness_quiet(exc_i, inc_loud_region = inc_loud_region) 54 sl = get_specific_loudness_quiet(exc_i, inc_loud_region = inc_loud_region)
57 55
58 loudness = 2*np.sum(sl,0) 56 loudness = 2*np.sum(sl,0)
59 57
60 return loudness 58 return sl, loudness
61 59
62 def get_specific_loudness_noise(exc_i, analyse = "all", inc_loud_region = False): 60 def get_specific_loudness_noise(exc_i, analyse = "all", inc_loud_region = False):
63 """ 61 """
64 62
65 """ 63 """
84 82
85 exc_sig = exc_i[i] 83 exc_sig = exc_i[i]
86 exc_noise = exc_signoise - exc_sig 84 exc_noise = exc_signoise - exc_sig
87 tn = K*exc_noise + tq 85 tn = K*exc_noise + tq
88 86
89 less_than_tq = get_less_than_tq(exc_sig, tn) #boolean array of elements less than or equal to the threshold of quiet 87 less_than_tn = get_less_than_tq(exc_sig, tn) #boolean array of elements less than or equal to the threshold of quiet
90 greater_than_tq = ~less_than_tq #boolean array of elements greater than the threshold of quiet 88 greater_than_tn = ~less_than_tn #boolean array of elements greater than the threshold of quiet
91 89
92 if inc_loud_region: 90 if inc_loud_region:
93
94 c1 = signoise_lt_tl * greater_than_tn #case 1 91 c1 = signoise_lt_tl * greater_than_tn #case 1
95 c2 = signoise_lt_tl * less_than_tn #case 2 92 c2 = signoise_lt_tl * less_than_tn #case 2
96 c3 = signoise_gt_tl * greater_than_tn #case 3 93 c3 = signoise_gt_tl * greater_than_tn #case 3
97 c4 = signoise_gt_tl * less_than_tn 94 c4 = signoise_gt_tl * less_than_tn #case 4
98 specific_loudness[i, c3] = get_specific_loudness_noise_c3(exc_sig[c3], exc_noise[c3], tq[c3], tn[c3], G[c3], A[c3], alpha[c3]) 95 specific_loudness[i, c3] = get_specific_loudness_noise_c3(exc_sig[c3], exc_noise[c3], tq[c3], tn[c3], G[c3], A[c3], alpha[c3])
99 specific_loudness[i, c4] = get_specific_loudness_noise_c4(exc_sig[c4], exc_noise[c4], tq[c4], tn[c4], G[c4], A[c4], alpha[c4]) 96 specific_loudness[i, c4] = get_specific_loudness_noise_c4(exc_sig[c4], exc_noise[c4], tq[c4], tn[c4], G[c4], A[c4], alpha[c4])
100
101 else: 97 else:
102 c1 = greater_than_tq #case 1 98 c1 = greater_than_tn #case 1
103 c2 = less_than_tq #case 2 99 c2 = less_than_tn #case 2
104 100
105 specific_loudness[i][c1] = get_specific_loudness_noise_c1(exc_sig[c1], exc_noise[c1], tq[c1], tn[c1], G[c1], A[c1], alpha[c1]) 101 specific_loudness[i][c1] = get_specific_loudness_noise_c1(exc_sig[c1], exc_noise[c1], tq[c1], tn[c1], G[c1], A[c1], alpha[c1])
106 specific_loudness[i][c2] = get_specific_loudness_noise_c2(exc_sig[c2], exc_noise[c2], tq[c2], tn[c2], G[c2], A[c2], alpha[c2]) 102 specific_loudness[i][c2] = get_specific_loudness_noise_c2(exc_sig[c2], exc_noise[c2], tq[c2], tn[c2], G[c2], A[c2], alpha[c2])
107 103
108 return specific_loudness 104 return specific_loudness
163 specific_loudness = exc_low = exc_mid = exc_high = np.zeros(np.shape(exc_i)) 159 specific_loudness = exc_low = exc_mid = exc_high = np.zeros(np.shape(exc_i))
164 160
165 less_than_tq = get_less_than_tq(exc_i, tq) #boolean array of elements less than or equal to the threshold of quiet 161 less_than_tq = get_less_than_tq(exc_i, tq) #boolean array of elements less than or equal to the threshold of quiet
166 greater_than_tq = ~less_than_tq #boolean array of elements greater than the threshold of quiet 162 greater_than_tq = ~less_than_tq #boolean array of elements greater than the threshold of quiet
167 greater_than_tl = get_greater_than_tl(exc_i) #boolean array of elements greater than the loud threshold 163 greater_than_tl = get_greater_than_tl(exc_i) #boolean array of elements greater than the loud threshold
168 gttq_lttl = greater_than_tq[~greater_than_tl] #boolean array of elements greater than the threshold of quiet but less than the threshold of loud 164 gttq_lttl = greater_than_tq * ~greater_than_tl #boolean array of elements greater than the threshold of quiet but less than the threshold of loud
169 165
170 exc_low = exc_i[less_than_tq] 166 exc_low = exc_i[less_than_tq]
171 specific_loudness[less_than_tq] = get_specific_loudness_low(exc_low, G[less_than_tq], tq[less_than_tq], A[less_than_tq], alpha[less_than_tq]) 167 specific_loudness[less_than_tq] = get_specific_loudness_low(exc_low, G[less_than_tq], tq[less_than_tq], A[less_than_tq], alpha[less_than_tq])
172 168
173 if(inc_loud_region): 169 if(inc_loud_region):
216 fc = (10**(erbs/21.366)-1)/0.004368 212 fc = (10**(erbs/21.366)-1)/0.004368
217 213
218 fkRef = 10**np.append(np.append(np.array(1), np.linspace(np.log10(50), np.log10(500), 11)), np.linspace(3,4.5,16)) 214 fkRef = 10**np.append(np.append(np.array(1), np.linspace(np.log10(50), np.log10(500), 11)), np.linspace(3,4.5,16))
219 kRef = np.append(np.append(13, np.linspace(13,-3,11)), np.ones((1,16))*-3) 215 kRef = np.append(np.append(13, np.linspace(13,-3,11)), np.ones((1,16))*-3)
220 K = np.interp(fc,fkRef,kRef) 216 K = np.interp(fc,fkRef,kRef)
217 K = 10**(K/10)
221 K = np.transpose(np.tile(K, (lenSig,1))) 218 K = np.transpose(np.tile(K, (lenSig,1)))
222 219
223 return K 220 return K
224 221
225 def get_specific_loudness_low(exc_low, G, tq, A, alpha): 222 def get_specific_loudness_low(exc_low, G, tq, A, alpha):
656 if(verbose): sys.stdout.write("Running filterbank decomposition.\n") 653 if(verbose): sys.stdout.write("Running filterbank decomposition.\n")
657 for i in range(nFilters): 654 for i in range(nFilters):
658 if(verbose): 655 if(verbose):
659 sys.stdout.write("\r" + str(int(np.round(100.0*i/nFilters))) + "% complete.") 656 sys.stdout.write("\r" + str(int(np.round(100.0*i/nFilters))) + "% complete.")
660 sys.stdout.flush() 657 sys.stdout.flush()
661 x = deepcopy(input) 658 x = np.array(input)
662 y[i] = sp.lfilter(b[i],a[i],x) 659 y[i] = sp.fftconvolve(x,b[i],'same')
663 660
664 if(verbose): sys.stdout.write("\n") 661 if(verbose): sys.stdout.write("\n")
665 662
666 return y 663 return y
667 664