c@0
|
1 """
|
c@0
|
2 A module used for auditory analysis.
|
c@0
|
3
|
c@0
|
4 Models currently implemented:
|
c@0
|
5 * Frequency-modulation analysis model based on the human auditory system.
|
c@0
|
6
|
c@0
|
7 Model implementations in progress:
|
c@0
|
8 * Glasberg and Moore model
|
c@0
|
9
|
c@0
|
10 Packaged dependencies:
|
c@0
|
11 * utils.py and/or utils.pyc
|
c@0
|
12 * erb.dat
|
c@0
|
13 * outMidFir.dat
|
c@1
|
14 * tq.dat
|
c@0
|
15
|
c@0
|
16 External dependencies:
|
c@0
|
17 * scipy
|
c@0
|
18 * numpy
|
c@0
|
19 * copy
|
c@0
|
20 * matplotlib
|
c@0
|
21 """
|
c@0
|
22
|
c@0
|
23 import utils
|
c@0
|
24 import scipy.signal as sp
|
c@1
|
25 import scipy.fftpack as fft
|
c@0
|
26 import numpy as np
|
c@0
|
27 from copy import deepcopy
|
c@0
|
28 import matplotlib.pyplot as plt
|
c@0
|
29
|
c@1
|
30 def get_specific_loudness(exc_i, inc_loud_region = False):
|
c@0
|
31 """
|
c@3
|
32 A function to calculate the specific loudness of the excitation patterns at each ERB. Specific loudness is calculated for three regions of
|
c@3
|
33 signal intensity, low level, mid level and high level. The high level processing is optional, and by default, ignored. When ignored the high level
|
c@3
|
34 region is processed equivalent to that of the mid region.
|
c@3
|
35
|
c@3
|
36 Parameters:
|
c@3
|
37 * exc_i (type: array-like of floats) - an array of shape (39, lenSig) of the excitation intensity of the signal. The outer dimension determines the
|
c@3
|
38 ERB channel. (Required)
|
c@3
|
39 * inc_loud_region (type: boolean) - Specifies whether to process the high levels differently to the mid levels. (Optional; Default = False)
|
c@0
|
40
|
c@0
|
41 TO DO
|
c@0
|
42 """
|
c@3
|
43 exc_i = np.array(exc_i)
|
c@3
|
44
|
c@1
|
45 lenSig = np.shape(exc_i)[-1] #length signal
|
c@3
|
46 tq, G, A, alpha = get_specific_loudness_parameters(lenSig)
|
c@1
|
47 specific_loudness = exc_low = exc_mid = exc_high = np.zeros(np.shape(exc_i))
|
c@0
|
48
|
c@1
|
49 less_than_tq = get_less_than_tq(exc_i, tq) #boolean array of elements less than or equal to the threshold of quiet
|
c@1
|
50 greater_than_tq = ~less_than_tq #boolean array of elements greater than the threshold of quiet
|
c@1
|
51 greater_than_tl = get_greater_than_tl(exc_i) #boolean array of elements greater than the loud threshold
|
c@1
|
52 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
|
c@0
|
53
|
c@1
|
54 exc_low = exc_i[less_than_tq]
|
c@1
|
55 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])
|
c@1
|
56
|
c@1
|
57 if(inc_loud_region):
|
c@1
|
58 exc_mid = exc_i[gttq_lttl]
|
c@1
|
59 exc_high = exc_i[greater_than_tl]
|
c@1
|
60 specific_loudness[gttq_lttl] = get_specific_loudness_mid(exc_mid, G[gttq_lttl], A[gttq_lttl], alpha[gttq_lttl])
|
c@1
|
61 specific_loudness[greater_than_tl] = get_specific_loudness_high(exc_high)
|
c@1
|
62 else:
|
c@1
|
63 exc_mid = exc_i[greater_than_tq]
|
c@1
|
64 specific_loudness[greater_than_tq] = get_specific_loudness_mid(exc_mid, G[greater_than_tq], A[greater_than_tq], alpha[greater_than_tq])
|
c@1
|
65
|
c@1
|
66 return specific_loudness
|
c@0
|
67
|
c@3
|
68 def get_specific_loudness_parameters(lenSig = 1):
|
c@3
|
69 """
|
c@3
|
70 Loads and returns the specific loudness values. If lenSig is specified, the parameters are shaped equal to the excitation signal to allow for
|
c@3
|
71 matrix elementwise operations between the parameters and signal. (Assumes excitation signal shape is [39, lenSig]).
|
c@3
|
72
|
c@3
|
73 Parameters:
|
c@3
|
74 * lenSig (type: numerical int) - the length of the excitation signal to be analysed. (Optional; Default = 1)
|
c@3
|
75
|
c@3
|
76 Returns:
|
c@3
|
77 * tq (type: numpy array of floats) - the threshold of quiet for each centre frequency of the ERBs
|
c@3
|
78 * G (type: numpy array of floats) - the frequency dependent gain values for each centre frequency of the ERBs
|
c@3
|
79 * A (type: numpy array of floats) - the frequency dependent A values for each centre frequency of the ERBs
|
c@3
|
80 * alpha (type: numpy array of floats) - the frequency dependent alpha values for each centre frequency of the ERBs
|
c@3
|
81 """
|
c@3
|
82
|
c@3
|
83 tq_dB, A, alpha = utils.load_sl_parameters() #load tq, A and alpha parameters
|
c@3
|
84 tq_dB = np.transpose(np.tile(tq_dB, (lenSig,1)))
|
c@3
|
85 tq = 10**(tq_dB / 10)
|
c@3
|
86 A = np.transpose(np.tile(A, (lenSig,1)))
|
c@3
|
87 alpha = np.transpose(np.tile(alpha, (lenSig,1)))
|
c@3
|
88 tq500_dB = 3.73 #threshold of quiet at 500Hz reference
|
c@3
|
89 G = 10**((tq500_dB - tq_dB)/10) #gain parameters
|
c@3
|
90
|
c@3
|
91 return tq, G, A, alpha
|
c@3
|
92
|
c@1
|
93 def get_specific_loudness_low(exc_low, G, tq, A, alpha):
|
c@3
|
94 """
|
c@3
|
95 Returns the specific loudness of the low level parts of the signal. Use get_specific_loudness_parameters() and specify lenSig
|
c@3
|
96 to obtain the correct values for G, tq, A and alpha. Use get_less_than_tq() to find the indexes of the samples with lower level
|
c@3
|
97 excitations.
|
c@3
|
98
|
c@3
|
99 e.g.,
|
c@3
|
100
|
c@3
|
101 # exc_i is the excitation intensity
|
c@3
|
102 lenSig = np.shape(exc_i)[-1] # find the length of the signal
|
c@3
|
103 tq, G, A, alpha = get_specific_loudness_parameters(lenSig) # get the shaped loudness parameters
|
c@3
|
104 less_than_tq = get_less_than_tq(exc_i, tq) # find which samples are lower level
|
c@3
|
105 specific_loudness[less_than_tq] = get_specific_loudness_low(exc_low[less_than_tq], G[less_than_tq], tq[less_than_tq], A[less_than_tq], alpha[less_than_tq])
|
c@3
|
106 # only process the low level part of the signal
|
c@3
|
107
|
c@3
|
108 Parameters:
|
c@3
|
109 * exc_low (type: array-like matrix of floats) - the lower level (less than or equal to tq) excitation pattern for each ERB
|
c@3
|
110 (Required)
|
c@3
|
111 * G (type: array-like matrix of floats) - the frequency dependent loudness gain parameters for each ERB, must be same shape
|
c@3
|
112 as exc_low. (Required)
|
c@3
|
113 * tq (type: array-like matrix of floats) - the frequency dependent threshold of quiet parameters for each ERB. Must be same
|
c@3
|
114 shape as exc_low. (Required)
|
c@3
|
115 * A (type: array-like matrix of floats) - the frequency dependent A parameters for each ERB. Must be same shape as exc_low.
|
c@3
|
116 * alpha (type: array-like matrix of floats) - the frequency dependent alpha parameters for each ERB. Must be same shape as
|
c@3
|
117 exc_low. (Required)
|
c@3
|
118
|
c@3
|
119 Returns:
|
c@3
|
120 * specific_loudness_low (type: array-like matrix of floats) - An array with dimensions equal to the exc_low containing the
|
c@3
|
121 specific loudness of the signal at levels below the threshold of quiet.
|
c@3
|
122 """
|
c@1
|
123
|
c@1
|
124 C = 0.047 #constant parameter
|
c@1
|
125 specific_loudness_low = C * ((2*exc_low)/(exc_low+tq))**1.5 * ((G * exc_low + A)**alpha - A**alpha)
|
c@1
|
126
|
c@1
|
127 return specific_loudness_low
|
c@1
|
128
|
c@1
|
129 def get_specific_loudness_mid(exc_mid, G, A, alpha):
|
c@3
|
130 """
|
c@3
|
131 Returns the specific loudness of the mid level parts of the signal.
|
c@3
|
132
|
c@3
|
133 e.g.,
|
c@3
|
134
|
c@3
|
135 # exc_i is the excitation intensity
|
c@3
|
136 lenSig = np.shape(exc_i)[-1] # find the length of the signal
|
c@3
|
137 tq, G, A, alpha = get_specific_loudness_parameters(lenSig) # get the shaped loudness parameters
|
c@3
|
138 less_than_tq = get_less_than_tq(exc_i, tq) # find which samples are lower level
|
c@3
|
139 greater_than_tq = ~less_than_tq # find which samples are greater than the threshold of quiet
|
c@3
|
140 greater_than_tl = get_greater_than_tl(exc_i) # find which samples are greater than the loud threshold
|
c@3
|
141 gttq_lttl = greater_than_tq[~greater_than_tl] # find which samples are mid level
|
c@3
|
142 specific_loudness[gttq_lttl] = get_specific_loudness_low(exc_low[gttq_lttl], G[gttq_lttl], tq[gttq_lttl], A[gttq_lttl], alpha[gttq_lttl])
|
c@3
|
143 # only process the mid level part of the signal
|
c@3
|
144
|
c@3
|
145 NOTE: The above is an example of use assuming the higher level processing IS NOT IGNORED. Use variable greater_than_tq if processing
|
c@3
|
146 higher levels equivalent to the mid.
|
c@3
|
147
|
c@3
|
148
|
c@3
|
149 Parameters:
|
c@3
|
150 * exc_mid (type: array-like matrix of floats) - the mid level (larger than tq (and, optionally less than high level threshold))
|
c@3
|
151 excitation pattern for each ERB (Required)
|
c@3
|
152 * G (type: array-like matrix of floats) - the frequency dependent loudness gain parameters for each ERB, must be same shape
|
c@3
|
153 as exc_low. (Required)
|
c@3
|
154 * A (type: array-like matrix of floats) - the frequency dependent A parameters for each ERB. Must be same shape as exc_low.
|
c@3
|
155 * alpha (type: array-like matrix of floats) - the frequency dependent alpha parameters for each ERB. Must be same shape as
|
c@3
|
156 exc_low. (Required)
|
c@3
|
157
|
c@3
|
158 Returns:
|
c@3
|
159 * specific_loudness_mid (type: array-like matrix of floats) - An array with dimensions equal to the exc_mid containing the
|
c@3
|
160 specific loudness of the signal at mid levels.
|
c@3
|
161 """
|
c@1
|
162
|
c@1
|
163 C = 0.047 #constant parameter
|
c@1
|
164 specific_loudness_mid = C * ((G * exc_mid + A)**alpha - A**alpha)
|
c@1
|
165
|
c@1
|
166 return specific_loudness_mid
|
c@1
|
167
|
c@1
|
168 def get_specific_loudness_high(exc_high):
|
c@1
|
169 """
|
c@3
|
170 Returns the specific loudness of the high level parts of the signal.
|
c@3
|
171
|
c@3
|
172 e.g.,
|
c@3
|
173
|
c@3
|
174 # exc_i is the excitation intensity
|
c@3
|
175 lenSig = np.shape(exc_i)[-1] # find the length of the signal
|
c@3
|
176 tq, G, A, alpha = get_specific_loudness_parameters(lenSig) # get the shaped loudness parameters
|
c@3
|
177 greater_than_tl = get_greater_than_tl(exc_i) # find which samples are greater than the loud threshold
|
c@3
|
178 specific_loudness[greater_than_tl] = get_specific_loudness_low(exc_low[greater_than_tl], G[greater_than_tl], tq[greater_than_tl], A[greater_than_tl], alpha[greater_than_tl])
|
c@3
|
179 # only process the mid level part of the signal
|
c@3
|
180
|
c@3
|
181 Parameters:
|
c@3
|
182 * exc_high (type: array-like matrix of floats) - the high level (larger than the threshold of high level) excitation pattern
|
c@3
|
183 for each ERB (Required)
|
c@3
|
184
|
c@3
|
185 Returns:
|
c@3
|
186 * specific_loudness_high (type: array-like matrix of floats) - An array with dimensions equal to the exc_high containing the
|
c@3
|
187 specific loudness of the signal at high levels.
|
c@1
|
188 """
|
c@1
|
189
|
c@1
|
190 C = 0.047 #constant parameter
|
c@1
|
191 specific_loudness_high = C * (exc_high / (1.04 * 10**6))**0.5
|
c@1
|
192
|
c@1
|
193 return specific_loudness_high
|
c@1
|
194
|
c@1
|
195 def get_greater_than_tl(exc_i):
|
c@1
|
196 """
|
c@1
|
197 A function to return if each element of the excitation intensity is greater than the threshold of loud.
|
c@1
|
198
|
c@1
|
199 Parameters:
|
c@1
|
200 * exc_i (type: array-like matrix of floats) - the input excitation intensity
|
c@1
|
201
|
c@1
|
202 Returns:
|
c@3
|
203 * le_tq (type: array-like matrix of booleans) - a boolean array with dimensions equal to the input
|
c@3
|
204 specifying if the excitation intensity is greater than the threshold of loud
|
c@1
|
205 """
|
c@1
|
206
|
c@1
|
207 lenSig = np.shape(exc_i)[-1]
|
c@1
|
208 g_tl = exc_i[:,:]>np.transpose(np.tile(10**10,(lenSig,39)))
|
c@1
|
209
|
c@1
|
210 return g_tl
|
c@1
|
211
|
c@1
|
212 def get_less_than_tq(exc_i, tq):
|
c@1
|
213 """
|
c@1
|
214 A function to return if each element of the excitation intensity is less than the threshold of quiet.
|
c@1
|
215
|
c@1
|
216 Parameters:
|
c@1
|
217 * exc_i (type: array-like matrix of floats) - the input excitation intensity
|
c@1
|
218 * tq (type: array-like matrix of floats) - the threshold of quiet for each ERB.
|
c@1
|
219
|
c@1
|
220 Returns:
|
c@1
|
221 * le_tq (type: array-like matrix of booleans) - a boolean array with dimensions equal to the input
|
c@1
|
222 specifying if the excitation intensity is less than the threshold of quiet
|
c@1
|
223 """
|
c@1
|
224
|
c@1
|
225 if (np.shape(exc_i)!=np.shape(tq)):
|
c@1
|
226 np.transpose(np.tile(exc_i,(np.shape(exc_i)[-1],1)))
|
c@1
|
227
|
c@1
|
228 le_tq = exc_i<=tq
|
c@1
|
229
|
c@1
|
230 return le_tq
|
c@1
|
231
|
c@1
|
232 def get_excitation_i(input, fs, SPL, rectify=False, verbose = False):
|
c@0
|
233 """
|
c@0
|
234 A function to calculate the excitation intensity of the input signal.
|
c@0
|
235
|
c@0
|
236 Parameters:
|
c@0
|
237 * input (type: array-like matrix of floats) - signal normalised to an amplitude range of -1 to 1. (Required)
|
c@0
|
238 * fs (type: numerical) - sample frequency of the signal, input. (Required)
|
c@0
|
239 * SPL (type: double) - the sound pressure level (SPL) at 0 dBFS, e.g., the SPL of a sine
|
c@0
|
240 wave with peaks at amplitude 1 and troughs at amplitude -1. (Required)
|
c@0
|
241 * rectify (type: boolean) - Specifies whether to include half wave rectification, modelling the direction
|
c@0
|
242 of that the cochlear nerves vibrate.
|
c@0
|
243 True to include, False to ignore. (Optional; Default = False)
|
c@0
|
244
|
c@0
|
245 Returns:
|
c@0
|
246 * gtfs (type: numpy array of floats) - array with size ((39,) + np.shape(input)) containing the excitation
|
c@0
|
247 pattern (in sound intensity) for each ERB of input signal. The excitation pattern for the nth ERB can
|
c@0
|
248 be accessed with gtfs[n].
|
c@0
|
249 """
|
c@0
|
250
|
c@0
|
251 input = np.array(input)
|
c@0
|
252 inputOMFir = outMidFir(input)
|
c@0
|
253 inputPa = v_Pascal(inputOMFir, SPL)
|
c@1
|
254 gtfs = gamma_tone_filter(inputPa, fs, verbose = verbose)
|
c@0
|
255 if (rectify):
|
c@0
|
256 gtfs = half_rectification(gtfs)
|
c@0
|
257 gtfs = pa_i(gtfs)
|
c@0
|
258 gtfs = at_normalise(gtfs)
|
c@3
|
259 b,a = exp_smoothing(fs)
|
c@3
|
260 gtfs = sp.lfilter(b,a,gtfs)
|
c@0
|
261
|
c@0
|
262 return gtfs
|
c@0
|
263
|
c@1
|
264 def plot_excitation_response(input = 0, fs = 44100, outMidFilt = True, xscale = 'log', yscale = 'log'):
|
c@0
|
265 """
|
c@0
|
266 A function that plots the transfer function of the outer middle ear and each gammatone filter.
|
c@0
|
267
|
c@0
|
268 Parameters:
|
c@0
|
269 * fs (type: numerical) - the sampling frequency of the signal. (Optional; Default = 44100)
|
c@0
|
270 * outMidFilt (type: boolean) - filter the signal by the outer and middle ear FIR filter. (Optional; Default = True)
|
c@0
|
271 * xscale (type: string) - the scale of the frequency axis. Values are 'log' or 'linear'. (Optional; Default = 'log')
|
c@0
|
272 * yscale (type: string) - the scale of the amplitude axis. Values are 'log' or 'linear'. (Optional; Default = 'log')
|
c@0
|
273
|
c@0
|
274 Returns:
|
c@0
|
275 * y (type: numpy array of floats) - array with size ((39,np.ceil(fs))) containing the impulse response of the
|
c@0
|
276 signal at each gammatone filter (and optionally, including the outer middle ear response). The response at the
|
c@0
|
277 nth gammatone filter can be accessed by y[n].
|
c@0
|
278 """
|
c@1
|
279
|
c@1
|
280 if ((np.shape(input)==())):
|
c@1
|
281 input = np.zeros(np.ceil(fs))
|
c@1
|
282 input[0] = 1
|
c@0
|
283 if(outMidFilt): input = outMidFir(input)
|
c@0
|
284 y = gamma_tone_filter(input, fs)
|
c@0
|
285
|
c@0
|
286 for i in range(np.shape(y)[0]):
|
c@0
|
287 utils.plot_fft(y[i],xscale, yscale, False)
|
c@0
|
288
|
c@0
|
289 plt.show()
|
c@0
|
290
|
c@1
|
291 return fft.fft(y)
|
c@0
|
292
|
c@0
|
293 def get_modulation_i(input, fs):
|
c@0
|
294 """
|
c@0
|
295 A function to calculate the modulation intensity of the input intensity signal. The function implements a
|
c@0
|
296 filter bank of bandpass filters with cut off frequencies ranging from 0.25 to 16 Hz.
|
c@0
|
297
|
c@0
|
298 Parameters:
|
c@0
|
299 * input (type: array-like matrix of floats) - the input intensity signal.
|
c@0
|
300 E.g., use get_excitation_i() to obtain excitation intensity and use as input.
|
c@0
|
301 * fs (type: numerical) - sampling frequency of input signal
|
c@0
|
302
|
c@0
|
303 Returns:
|
c@0
|
304 * y (type: numpy array of floats) - array with size ((10,) + np.shape(input)) containing the
|
c@0
|
305 modulation intensity of the signal at each modulation filter. The modulation intensity for the nth filter can
|
c@0
|
306 be accessed with y[n].
|
c@0
|
307 """
|
c@0
|
308
|
c@0
|
309 input = np.array(input)
|
c@0
|
310 b = fir_antialias(fs)
|
c@0
|
311 input_lp = sp.lfilter(b,(1),input_fr)
|
c@0
|
312 input_ds = downsample(input_lp, fs)
|
c@0
|
313 fc = np.array(utils.exp_sequence(-2,4,10))
|
c@0
|
314 bw = fc/2
|
c@0
|
315 y = decomposition(input_ds, fs, fc, bw)
|
c@0
|
316
|
c@0
|
317 return y
|
c@0
|
318
|
c@0
|
319 def outMidFir(input):
|
c@0
|
320 """
|
c@0
|
321 A function to filter the input signal with the response of the outer and middle ear.
|
c@0
|
322
|
c@0
|
323 Parameters:
|
c@0
|
324 * input (type: array-like matrix of floats) - signal normalised to an amplitude range of -1 to 1. (Required)
|
c@0
|
325
|
c@0
|
326 Returns:
|
c@0
|
327 * y (type: numpy array of floats) - array with dimensions equal to the input signal filtered by the response of
|
c@0
|
328 the outer and middle ear.
|
c@0
|
329 """
|
c@0
|
330
|
c@0
|
331 input = np.array(input)
|
c@0
|
332 b = utils.load_outMidFir_coeff()
|
c@0
|
333 y = sp.lfilter(b, (1), input)
|
c@0
|
334
|
c@0
|
335 return y
|
c@0
|
336
|
c@1
|
337 def gamma_tone_filter(input, fs, verbose = False):
|
c@0
|
338 """
|
c@0
|
339 A function to filter to decompose the input signal into 39 different gammatones filtered signals modelling the ERBs.
|
c@0
|
340
|
c@0
|
341 Parameters:
|
c@0
|
342 * input (type: array-like matrix of floats) - signal normalised to an amplitude range of -1 to 1. (Required)
|
c@0
|
343 * fs (type: numerical) - sample frequency of the signal, input. (Required)
|
c@0
|
344
|
c@0
|
345 Returns:
|
c@0
|
346 * y (type: numpy array of floats) - array with size ((39),np.shape(input)) containing the impulse response of the
|
c@0
|
347 signal at each gammatone filter. The response at the nth gammatone filter can be accessed by y[n].
|
c@0
|
348 """
|
c@0
|
349
|
c@0
|
350 input = np.array(input)
|
c@0
|
351 fc, bw = utils.load_erb_data()
|
c@1
|
352 y = decomposition(input,fs,fc,bw, verbose = verbose)
|
c@0
|
353
|
c@0
|
354 return y
|
c@0
|
355
|
c@0
|
356 def v_Pascal(input, SPL):
|
c@0
|
357 """
|
c@0
|
358 A function to convert a signal, normalised to an amplitude range of -1 to 1, to a signal represented in pressure (units: Pascal).
|
c@0
|
359
|
c@0
|
360 Parameters:
|
c@0
|
361 * input (type: array-like matrix of floats) - signal normalised to an amplitude range of -1 to 1. (Required)
|
c@0
|
362 * SPL (type: double) - the sound pressure level (SPL) at 0 dBFS, e.g., the SPL of a sine
|
c@0
|
363 wave with peaks at amplitude 1 and troughs at amplitude -1. (Required)
|
c@0
|
364
|
c@0
|
365 Returns:
|
c@0
|
366 * y (type: numpy array of floats) - array with dimensions equal to the input signal containing the input represented
|
c@0
|
367 as a pressure signal.
|
c@0
|
368 """
|
c@0
|
369
|
c@0
|
370 input = np.array(input)
|
c@1
|
371 y = np.sign(input)*(0.00002*10**(np.log10(np.abs(input))+SPL/20))
|
c@0
|
372
|
c@0
|
373 return y
|
c@0
|
374
|
c@0
|
375 def pa_i(input, C=406):
|
c@0
|
376 """
|
c@0
|
377 A function to convert a pressure signal (unit: Pascal) to an intensity signal.
|
c@0
|
378
|
c@0
|
379 Parameters:
|
c@0
|
380 * input (type: array-like matrix of floats) - pressure signal (unit: Pascal) (Required)
|
c@0
|
381 * C (type: double) - the acoustic impedance of the air (Optional; Default = 406)
|
c@0
|
382
|
c@0
|
383 Returns:
|
c@0
|
384 * y (type: numpy array of floats) - array with dimensions equal to the input signal containing the input represented
|
c@0
|
385 as a pressure signal.
|
c@0
|
386 """
|
c@0
|
387
|
c@0
|
388 input = np.array(input)
|
c@0
|
389 y = (input**2) / C
|
c@0
|
390
|
c@0
|
391 return y
|
c@0
|
392
|
c@0
|
393 def at_normalise(input):
|
c@0
|
394 """
|
c@0
|
395 A function to normalise an intensity signal with the audibility threshold.
|
c@0
|
396
|
c@0
|
397 Parameters:
|
c@0
|
398 * input (type: array-like matrix of floats) - intensity signal (unit: Pascal) (Required)
|
c@0
|
399
|
c@0
|
400 Returns:
|
c@0
|
401 * y (type: numpy array of floats) - array with dimensions equal to the input signal containing the input normalised
|
c@0
|
402 with the audibility threshold.
|
c@0
|
403 """
|
c@0
|
404
|
c@0
|
405 input = np.array(input)
|
c@0
|
406 y = input / 1*(10**12)
|
c@0
|
407
|
c@1
|
408 return y
|
c@0
|
409
|
c@0
|
410 def downsample(input, factor=100):
|
c@0
|
411 """
|
c@0
|
412 A function to downsample a signal, input, with sampling frequency, fs, by a downsample factor of factor.
|
c@0
|
413
|
c@0
|
414 NOTE: It is advised to use the fir_antialias() function before downsampling to remove any high frequencies
|
c@0
|
415 which would otherwise represented as low frequencies due to aliasing.
|
c@0
|
416
|
c@0
|
417 Parameters:
|
c@0
|
418 * input (type: array-like matrix of floats) - input signal. (Required)
|
c@0
|
419 * factor - downsample factor (Optional; Default = 100)
|
c@0
|
420
|
c@0
|
421 Returns:
|
c@0
|
422 * output (type: numpy array of floats) - array with outer dimensions equivalent to the to the input, and
|
c@0
|
423 inner dimension equal to np.floor(lenIn / factor) where lenIn is the length of the inner dimension.
|
c@0
|
424 """
|
c@0
|
425
|
c@0
|
426 input = np.array(input)
|
c@0
|
427 shapeIn = np.shape(input)
|
c@0
|
428 nDim = np.shape(shapeIn)
|
c@0
|
429 lenIn = shapeIn[nDim[0]-1]
|
c@0
|
430 lenOut = np.floor(lenIn / factor)
|
c@0
|
431 n = np.linspace(0,lenIn,lenOut, endpoint=False).astype(np.int)
|
c@0
|
432 output = input[...,n]
|
c@0
|
433
|
c@0
|
434 return output
|
c@0
|
435
|
c@0
|
436 def half_rectification(input):
|
c@0
|
437 """
|
c@0
|
438 A function which performs a half-wave rectification on the input signal.
|
c@0
|
439
|
c@0
|
440 Parameters:
|
c@0
|
441 * input (type: array-like matrix of floats) - input signal. (Required)
|
c@0
|
442
|
c@0
|
443 Returns:
|
c@0
|
444 * y (type: numpy array of floats) - an array with dimensions of input containing the half-wave rectification of
|
c@0
|
445 input.
|
c@0
|
446 """
|
c@0
|
447
|
c@0
|
448
|
c@0
|
449 y = np.array(input)
|
c@0
|
450 y[y<0] = 0
|
c@0
|
451
|
c@0
|
452 return y
|
c@0
|
453
|
c@0
|
454 def decomposition(input, fs, fc, bw, order=1024, verbose = False):
|
c@0
|
455 """
|
c@0
|
456 A function to run the input filter through a bandpass filter bank of length equal to the length of fc. Each
|
c@0
|
457 bandpass filter is designed by defining the centre frequencies, fc, and bandwidths, bw.
|
c@0
|
458
|
c@0
|
459 Parameters:
|
c@0
|
460 * input (type: array-like matrix of floats) - input signal. (Required)
|
c@0
|
461 * fs (type: numerical) - the sampling frequency of the input signal. (Required)
|
c@0
|
462 * fc (type: array-like vector of floats) - the centre off frequencies (unnormalised) of each bandpass filter.
|
c@0
|
463 The length of this vector determines the number of filters in the bank.
|
c@0
|
464 * bw (type: array-like vector of floats) - the bandwidths (unnormalised) of each bandpass filter. Must be equal
|
c@0
|
465 to or more than the length of fc. If the length is more, all elements exceeding the length of fc - 1 will be
|
c@0
|
466 ignored.
|
c@0
|
467 * order (type: numerical int) - the order of the filter (number of taps minus 1) (Optional; Default = 1024)
|
c@0
|
468 * verbose (type: boolean) - determines whether to display the current subroutine/progress of the procedure.
|
c@0
|
469 (Optional; Default = False)
|
c@0
|
470
|
c@0
|
471 Returns:
|
c@0
|
472 * y (type: numpy array of floats) - an array with inner dimensions equal to that of the input and outer dimension equal to
|
c@0
|
473 the length of fc (i.e. the number of bandpass filters in the bank) containing the outputs to each filter. The output
|
c@0
|
474 signal of the nth filter can be accessed using y[n].
|
c@0
|
475 """
|
c@0
|
476
|
c@0
|
477 input = np.array(input)
|
c@0
|
478 nFreqs = len(fc)
|
c@0
|
479 shape = (nFreqs,) + np.shape(input)
|
c@0
|
480 shape = shape[0:]
|
c@0
|
481 y = np.zeros(shape)
|
c@0
|
482
|
c@0
|
483 if(verbose): print "Running frequency decomposition."
|
c@0
|
484 for i in range(nFreqs):
|
c@0
|
485 if(verbose): print str(100.0*i/nFreqs) + "% complete."
|
c@0
|
486 low = fc[i]-bw[i]/2;
|
c@0
|
487 high = fc[i]+bw[i]/2;
|
c@0
|
488 if(verbose): print "Low: " + str(low) + "; High: " + str(high)
|
c@0
|
489 b = fir_bandpass(low, high, fs, order, verbose)
|
c@0
|
490 x = deepcopy(input)
|
c@0
|
491 y[i] = sp.lfilter(b,(1),x)
|
c@0
|
492
|
c@0
|
493 return y
|
c@0
|
494
|
c@3
|
495 def exp_smoothing(fs, tc = 0.01):
|
c@3
|
496 """
|
c@3
|
497 Designs an exponential filter, y[n] = alpha*x[n] - -(1-alpha)*y[n-1], where alpha = T/(tc+T), where T
|
c@3
|
498 is the inverse of the sampling frequency and time constant, tc, is specified.
|
c@3
|
499
|
c@3
|
500 Parameters:
|
c@3
|
501 * fs (type: numerical) - sampling frequency of the signal to be filtered (Required)
|
c@3
|
502 * tc (type: numerical) - the time constant of the filter. (Optional; Default = 0.01)
|
c@3
|
503
|
c@3
|
504 Returns:
|
c@3
|
505 * b (type: numerical) - the coefficient of x[n]. Equal to alpha.
|
c@3
|
506 * a (type: numpy array of floats) - an array of size 2 of the coefficients of y[n] (a[0] = 1) and y[n-1]
|
c@3
|
507 """
|
c@3
|
508
|
c@3
|
509 T = 1.0/fs
|
c@3
|
510 alpha = T/(tc + T)
|
c@3
|
511 b = [alpha]
|
c@3
|
512 a = [1, -(1-alpha)]
|
c@3
|
513
|
c@3
|
514 return b, a
|
c@3
|
515
|
c@3
|
516 def antialias_fir(fs, fc=100, order=64):
|
c@3
|
517 """
|
c@3
|
518 A function which returns the b coefficients for a lowpass fir filter with specified requirements.
|
c@3
|
519 Made specifically to remove aliasing when downsampling, but can be used for any application that
|
c@3
|
520 requires a lowpass filter.
|
c@3
|
521
|
c@3
|
522 Parameters:
|
c@3
|
523 * fs (type: numerical) - sampling frequency of the signal to be filtered (Required)
|
c@3
|
524 * fc (type: numerical) - unnormalised cut off frequency of the filter (Optional; Default = 100)
|
c@3
|
525 * order (type: numerical int) - the order of the filter (number of taps minus 1) (Optional; Default = 64)
|
c@3
|
526
|
c@3
|
527 Returns:
|
c@3
|
528 * b (type: numpy array of floats) - an array of size order + 1 (i.e. a coefficient for each tap)
|
c@3
|
529 """
|
c@3
|
530
|
c@3
|
531 nyquist = 0.5*fs
|
c@3
|
532 fcNorm = fc/nyquist
|
c@3
|
533 b = sp.firwin(order+1, fcNorm)
|
c@3
|
534
|
c@3
|
535 return b
|
c@3
|
536
|
c@0
|
537 def fir_bandpass(low, high, fs, order = 1024, verbose = False):
|
c@0
|
538 """
|
c@0
|
539 A function which returns the b coefficients for a bandpass fir filter with specified requirements.
|
c@0
|
540
|
c@0
|
541 Parameters:
|
c@0
|
542 * low - the lower cutoff frequency of the filter. (Required)
|
c@0
|
543 * high - the upper cutoff frequency of the filter. (Required)
|
c@0
|
544 * fs (type: numerical) - sampling frequency of the signal to be filtered. (Required)
|
c@0
|
545 * order (type: numerical int) - the order of the filter (number of taps minus 1) (Optional; Default = 1024)
|
c@0
|
546 * verbose (type: boolean) - determines whether to display the current progress (or info on the current subroutine)
|
c@0
|
547 of the procedure. (Optional; Default = False)
|
c@0
|
548
|
c@0
|
549 Returns:
|
c@0
|
550 * b (type: numpy array of floats) - an array of size order + 1 (i.e. a coefficient for each tap).
|
c@0
|
551 """
|
c@0
|
552
|
c@0
|
553 nyquist = 0.5*fs
|
c@0
|
554 lowNorm = low/nyquist
|
c@0
|
555 highNorm = high/nyquist
|
c@0
|
556 if(verbose): print "Low: " + str(lowNorm) + "; High: " + str(highNorm)
|
c@0
|
557 b = sp.firwin(order+1, [lowNorm, highNorm], pass_zero=False)
|
c@0
|
558
|
c@0
|
559 return b |