-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound_analysis.py
164 lines (138 loc) · 5.02 KB
/
sound_analysis.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 26 12:12:19 2019
@author: sylvia
"""
from scipy.io import wavfile as wvf
import wavfile
import numpy as np
import glob
import os
import matplotlib.pyplot as plt
#import soundsig.sound as ssnd
#from matplotlib.backends.backend_pdf import PdfPages
import scipy.signal.signaltools as spsg
from mkl_fft._numpy_fft import rfft, irfft, fft, ifft
import wave
#import plotBiosound
import h5py
# Monkey patch ffts to use mkl to speed up standardize
spsg.fftpack.fft = fft
spsg.fftpack.ifft = ifft
def get_files(path = '.',ext='.wav',recursive = True):
my_path = path + '/**/*'+ext
file_list = glob.glob(my_path, recursive = recursive)
return file_list
def process_sound_list(file_list,samp_new=192000,percentile = 99.9999, bitdepth = 32,stereo = False,
standardize = True,make_summary_plots = True, save_h5 = True,
make_specgrams = True):
# get statistics for all files
durations = []
samp_freqs = []
bitdepths = []
amplitudes = []
success = []
for file in file_list:
# read file stats
try:
file_wav = wave.open(file,mode = 'rb')
samp_f0 = file_wav.getframerate()
nframes = file_wav.getnframes()
dur = nframes/samp_f0
bdepth = file_wav.getsampwidth()
file_wav.close()
wavarray = wavfile.read(file)
samp_f0 = wavarray[0]
sig = wavarray[1]
amp = np.amax(np.abs(sig))
durations.append(dur)
samp_freqs.append(samp_f0)
bitdepths.append(bdepth)
amplitudes.append(amp)
if standardize:
sig = standardize_wav(sig,samp_f0,samp_new,percentile,stereo)
success.append(True)
except:
print('Could not read file: '+file)
durations.append(0)
samp_freqs.append(0)
bitdepths.append(0)
amplitudes.append(0)
success.append(False)
if make_summary_plots:
f,axes = plot_summary_statistics(duration=list(np.compress(durations,success)),sampling_frequency=list(np.compress(samp_freqs,success)),
bit_depth=list(np.compress(bitdepths,success)),amplitude=list(np.compress(amplitudes,success)))
return f, axes, durations, samp_freqs, bitdepths, amplitudes, success
def standardize_wav(sig,samp_in,samp_new = 192000,percentile = 99.9999, bitdepth = 32, stereo = False):
# convert to mono
dims = len(sig.shape)
if stereo == False:
if dims>1:
numChannels = float(sig.shape[1])
sig = np.sum(sig, axis=1,keepdims=0)/numChannels
else:
numChannels = 1.0
# standardize sampling frequency
numppoints0 = len(sig)
numpointsT = int(np.ceil(samp_new*numppoints0/samp_in))
if samp_in<samp_new:
sig = spsg.resample(sig,numpointsT)
elif samp_in == samp_new:
print('Sampling frequency already at target value!')
else:
print('f0 higher than target')
# normalize and clip
sig = normalize_and_clip(sig,percentile = percentile)
# normalize bit depth
sig = normalize_bit_depth(sig,bitdepth)
return sig
#def plot_hist(a,)
#def sound_histograms(file_list):
# n,bins,patches = plt.hist(x = dur,bins = 'auto',alpha = 0.7,rwidth=0.85)
# plt.grid(axis = 'y',alpha = 0.75);
# plt.xlabel('Duration')
# plt.ylabel('#')
def plot_summary_statistics(duration = None,sampling_frequency = None,bit_depth = None,amplitude = None):
numfigs = 0
titles = []
arrays = []
if duration != None:
titles.append('Duration (s)')
arrays.append(duration)
numfigs+=1
if sampling_frequency != None:
titles.append('Sampling Frequency (Hz)')
arrays.append(sampling_frequency)
numfigs += 1
if bit_depth !=None:
titles.append('Bit Depth')
arrays.append(bit_depth)
numfigs+=1
if amplitude!=None:
titles.append('Amplitude')
arrays.append(amplitude)
numfigs+=1
fig, axes = plt.subplots(numfigs,1)
plt.subplots_adjust(hspace = 1.5)
for i in range(numfigs):
n,bins,patches = axes[i].hist(x = arrays[i],bins = 500,alpha = 0.7,rwidth = 0.85)
axes[i].grid(True,alpha = 0.75)
axes[i].set_xlabel(titles[i])
print(i)
return fig, axes
def normalize_and_clip(sig,percentile = 99.9999):
sig_clipped = np.copy(sig)
norm_factor = np.percentile(abs(sig),99.9999)
sig_clipped[sig>norm_factor] = norm_factor
sig_clipped[sig<-norm_factor] = -norm_factor
sig_norm = sig_clipped/norm_factor
return sig_norm
def normalize_bit_depth(sig, bitdepth):
sig_max = np.abs(sig).max()
sig_norm = sig/sig_max
# quantize (bit-depth)
q = np.round(2**(bitdepth-1)*sig_norm);
sig_out = sig_max*q/(2**(bitdepth-1));
return sig_out
#def save_h5(sound_object)