/
auditory.py
186 lines (162 loc) · 7.05 KB
/
auditory.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'''
Copyright (c) Baptiste Caramiaux, Etienne Thoret
All rights reserved
'''
import numpy as np
import math
from scipy import signal
import utils
#from lib import utils
import features #import spectrum2scaletime, scaletime2scalerate, scalerate2cortical, waveform2auditoryspectrogram
import matplotlib.pylab as plt
def load_static_params():
strf_params = {}
strf_params['scales'] = [
0.71, 1.0, 1.41, 2.00, 2.83, 4.00, 5.66, 8.00
]
strf_params['rates'] = [
-32, -22.6, -16, -11.3, -8, -5.70, -4, -2, -1, -.5, -.25, .25, .5, 1, 2, 4,
5.70, 8, 11.3, 16, 22.6, 32
]
strf_params['sr_time'] = 250
return strf_params
def load_strf_params(rates = [-32, -22.6, -16, -11.3, -8, -5.70, -4, -2, -1, -.5, -.25, .25, .5, 1, 2, 4,5.70, 8, 11.3, 16, 22.6, 32],
scales = [0.71, 1.0, 1.41, 2.00, 2.83, 4.00, 5.66, 8.00], sr_time=250):
strf_params = {}
strf_params['scales'] = scales
strf_params['rates'] = rates
strf_params['sr_time'] = sr_time
return strf_params
def spectrogram(wavtemp,
audio_fs=44100,
duration=0.25,
duration_cut_decay=0.05,
resampling_fs=16000,
sr_time=250,
offset=0.0):
auditory_params = load_static_params()
# resampling_fs = auditory_params['newFs']
# duration = auditory_params['duration']
# duration_cut_decay = auditory_params['duration_cut_decay']
sr_time = auditory_params['sr_time']
wavtemp = np.r_[wavtemp, np.zeros(resampling_fs)]
print(resampling_fs)
if duration==-1:
print('no duration cut')
elif wavtemp.shape[0] > math.floor(duration * audio_fs):
offset_n = int(offset * audio_fs)
duration_n = int(duration * audio_fs)
duration_decay_n = int(duration_cut_decay * audio_fs)
wavtemp = wavtemp[offset_n:offset_n + duration_n]
if offset_n==0:
wavtemp[wavtemp.shape[0] - duration_decay_n:] = wavtemp[
wavtemp.shape[0] - duration_decay_n:] * utils.raised_cosine(
np.arange(duration_decay_n), 0, duration_decay_n)
else:
wavtemp[wavtemp.shape[0] - duration_decay_n:] = wavtemp[
wavtemp.shape[0] - duration_decay_n:] * utils.raised_cosine(
np.arange(duration_decay_n), 0, duration_decay_n)
wavtemp[:duration_decay_n] = wavtemp[:duration_decay_n] * utils.raised_cosine(
np.arange(duration_decay_n), duration_decay_n, duration_decay_n)
wavtemp = (wavtemp / 1.01) / (np.max(wavtemp) + np.finfo(float).eps)
wavtemp = signal.resample(wavtemp,
int(wavtemp.shape[0] / audio_fs * resampling_fs))
waveform2auditoryspectrogram_args = {
'frame_length':
1000 / sr_time, # sample rate 125 Hz in the NSL toolbox
'time_constant': 8,
'compression_factor': -2,
'octave_shift': math.log2(resampling_fs / resampling_fs),
'filt': 'p',
'VERB': 0
}
auditory_spectrogram_ = features.waveform2auditoryspectrogram(
wavtemp, **waveform2auditoryspectrogram_args)
return auditory_spectrogram_
def spectrum(wavtemp,
audio_fs=44100,
duration=0.25,
duration_cut_decay=0.05,
resampling_fs=16000,
sr_time=250,
offset=0):
auditory_spectrogram_ = spectrogram(wavtemp, audio_fs, duration,
duration_cut_decay, resampling_fs,
sr_time, offset)
auditory_spectrum_ = np.mean(auditory_spectrogram_, axis=0)
return auditory_spectrum_
def mps(wavtemp,
audio_fs=44100,
duration=0.25,
duration_cut_decay=0.05,
resampling_fs=16000,
sr_time=250,
offset=0):
auditory_spectrogram_ = spectrogram(wavtemp, audio_fs, duration,
duration_cut_decay, resampling_fs,
sr_time, offset)
strf_args = {
'num_channels': 128,
'num_ch_oct': 24,
'sr_time': sr_time,
'nfft_rate': 2 * 2**utils.nextpow2(auditory_spectrogram_.shape[0]),
'nfft_scale': 2 * 2**utils.nextpow2(auditory_spectrogram_.shape[1]),
'KIND': 2
}
# Spectro-temporal modulation analysis
# Based on Hemery & Aucouturier (2015) Frontiers Comp Neurosciences
# nfft_fac = 2 # multiplicative factor for nfft_scale and nfft_rate
# nfft_scale = nfft_fac * 2**utils.nextpow2(auditory_spectrogram_.shape[1])
mod_scale, phase_scale, _, _ = features.spectrum2scaletime(
auditory_spectrogram_, **strf_args)
mps_, phase_scale_rate, _, _ = features.scaletime2scalerate(
mod_scale * np.exp(1j * phase_scale), **strf_args)
# repres = repres[:, :int(repres.shape[1] / 2)]
return mps_
def strf(wavtemp,
audio_fs=44100,
duration=0.25,
duration_cut_decay=0.05,
resampling_fs=16000,
sr_time=250,
offset=0,
rates=[-32, -22.6, -16, -11.3, -8, -5.70, -4, -2, -1, -.5, -.25, .25, .5, 1, 2, 4,5.70, 8, 11.3, 16, 22.6, 32],
scales=[0.71, 1.0, 1.41, 2.00, 2.83, 4.00, 5.66, 8.00]):
auditory_spectrogram_ = spectrogram(wavtemp, audio_fs, duration,
duration_cut_decay, resampling_fs,
sr_time, offset)
auditory_params = load_strf_params(rates, scales, sr_time)
scales = auditory_params['scales']
rates = auditory_params['rates']
strf_args = {
'num_channels': 128,
'num_ch_oct': 24,
'sr_time': sr_time,
'nfft_rate': 2 * 2**utils.nextpow2(auditory_spectrogram_.shape[0]),
'nfft_scale': 2 * 2**utils.nextpow2(auditory_spectrogram_.shape[1]),
'KIND': 2
}
# Spectro-temporal modulation analysis
# Based on Hemery & Aucouturier (2015) Frontiers Comp Neurosciences
# nfft_fac = 2 # multiplicative factor for nfft_scale and nfft_rate
# nfft_scale = nfft_fac * 2**utils.nextpow2(stft.shape[1])
mod_scale, phase_scale, _, _ = features.spectrum2scaletime(
auditory_spectrogram_, **strf_args)
# Scales vs. Time => Scales vs. Rates
# nfft_rate = nfft_fac * 2**utils.nextpow2(stft.shape[0])
scale_rate, phase_scale_rate, _, _ = features.scaletime2scalerate(
mod_scale * np.exp(1j * phase_scale), **strf_args)
# print(scale_rate.shape)
# print(phase_scale_rate.shape)
#num_channels, num_ch_oct, sr_time, nfft_rate, nfft_scale)
strf_ = features.scalerate2cortical(auditory_spectrogram_, scale_rate,
phase_scale_rate, scales, rates,
**strf_args)
# print(strf_.shape)
#num_ch_oct, sr_time, nfft_scale, nfft_rate, 2)
return strf_, auditory_spectrogram_, mod_scale, scale_rate
if __name__ == "__main__":
audio, fs = utils.audio_data(
'/Users/baptistecaramiaux/Work/Projects/TimbreProject_Thoret/Code\ and\ data/timbreStudies/ext/sounds/Iverson1993Whole/01.W.Violin.aiff'
)
spectrum(audio, fs)