forked from jmarkow/zftftb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zftftb_song_chop.m
234 lines (187 loc) · 5.25 KB
/
zftftb_song_chop.m
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
function zftftb_song_chop(DIR,varargin)
%zftftb_song_chop takes a directory of wav files, and extracts all segments of the wav files
%that contain sound. Useful in cases where you don't care about using the silent segments.
%
% zftftb_song_chop(DIR,varargin)
%
% DIR
% directory of .wav files to process
%
% the following may be passed as parameter/value pairs:
%
% song_len
% window length for computing power band crossing (in s, default: .005)
%
% song_overlap
% window overlap for computing power band crossing (in s, default: 0)
%
% song_band
% frequencies that contain relevant sound (in Hz, default: [3e3 7e3])
%
% song_ratio
% ratio of power in:out of relevant band (default: 2)
%
% song_duration
% smoothing of song_ratio (in s, default: .8)
%
% song_pow
% threshold on song power (default: -inf)
%
% song_thresh
% threshold on smoothed song_ratio (default: .1)
%
% custom_load
% anonymous functionf or loading MATLAB data (default: '')
%
% file_filt
% filter for file selection (default: '*.wav')
%
% audio_pad
% data to include to the left/right of extraction points (in s, default: 1)
%
% colors
% MATLAB colormap to use for spectrograms (default: 'hot');
%
% disp_band
% frequency band to display for spectrogram export (default: [1 9e3]);
%
% export_wav
% enable wav file export (default: 1)
%
% export_spectrogram
% enable spectrogram export (default: 1)
%
% clipping
% clipping limits for spectrogram (default: [-2 2], linear units)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PARAMETER COLLECTION %%%%%%%%%%%%%%
if nargin<1 | isempty(DIR)
DIR=pwd;
end
nparam=length(varargin);
song_len=.005;
song_overlap=0;
song_band=[3e3 7e3];
song_duration=.8;
song_ratio=2;
song_pow=-inf;
song_thresh=.1;
custom_load=[]; % anonymous function for reading MATLAB files
file_filt='*.wav';
audio_pad=[ 1 ];
colors='hot';
disp_band=[1 9e3];
clipping=[-2 2];
export_wav=1;
export_spectrogram=1;
nparams=length(varargin);
if mod(nparams,2)>0
error('ephysPipeline:argChk','Parameters must be specified as parameter/value pairs!');
end
for i=1:2:nparams
switch lower(varargin{i})
case 'song_len'
song_len=varargin{i+1};
case 'song_overlap'
song_overlap=varargin{i+1};
case 'song_band'
song_band=varargin{i+1};
case 'song_duration'
song_duration=varargin{i+1};
case 'song_ratio'
song_ratio=varargin{i+1};
case 'song_pow'
song_pow=varargin{i+1};
case 'song_thresh'
song_thresh=varargin{i+1};
case 'export_spectrogram'
export_spectrogram=varargin{i+1};
case 'export_wav'
export_wav=varargin{i+1};
case 'custom_load'
custom_load=varargin{i+1};
case 'file_filt'
file_filt=varargin{i+1};
case 'audio_pad'
audio_pad=varargin{i+1};
case 'colors'
colors=varargin{i+1};
case 'clipping'
clipping=varargin{i+1};
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~exist(fullfile(DIR,'chop_data'),'dir')
mkdir(fullfile(DIR,'chop_data'));
end
listing=dir(fullfile(DIR,file_filt));
listing={listing(:).name};
if export_wav
mkdir(fullfile(DIR,'chop_data','wav'));
end
if export_spectrogram
mkdir(fullfile(DIR,'chop_data','gif'));
end
for i=1:length(listing)
input_file=listing{i};
disp([input_file])
[pathname,filename,ext]=fileparts(input_file);
switch lower(ext)
case '.mat'
% use custom loading function
if ~isempty(custom_load)
[audio_data,audio_fs]=custom_load(input_file);
else
error('No custom loading function detected for .mat files.');
end
case '.wav'
if verLessThan('matlab','8')
[audio_data,audio_fs]=wavread(fullfile(DIR,input_file));
else
[audio_data,audio_fs]=audioread(fullfile(DIR,input_file));
end
end
disp('Entering song detection...');
audio_len=length(audio_data);
[song_bin,song_t]=zftftb_song_det(audio_data,audio_fs,'song_band',song_band,...
'len',song_len,'overlap',song_overlap,'song_duration',song_duration,...
'ratio_thresh',song_ratio,'song_thresh',song_thresh,'pow_thresh',song_pow);
raw_t=[1:audio_len]./audio_fs;
% interpolate song detection to original space, collate idxs
detection=interp1(song_t,double(song_bin),raw_t,'nearest');
ext_pts=markolab_collate_idxs(detection,round(audio_pad*audio_fs));
for j=1:size(ext_pts,1)
% grab chunk if possible
curr_ext=ext_pts(j,:);
export_file=[ filename '_chunk_' num2str(j) ];
if curr_ext(1)>0 & curr_ext(2)<audio_len
% now we're extracting
%
extraction=audio_data(curr_ext(1):curr_ext(2));
if export_wav
tmp=extraction;
min_audio=min(tmp);
max_audio=max(tmp);
if min_audio + max_audio < 0
tmp=tmp/(-min_audio);
else
tmp=tmp/(max_audio*(1+1e-3));
end
if verLessThan('matlab','8')
wavwrite(tmp,audio_fs,fullfile(DIR,'chop_data','wav',[ export_file '.wav' ]));
else
audiowrite(fullfile(DIR,'chop_data','wav',[ export_file '.wav']),tmp,audio_fs);
end
end
if export_spectrogram
[im,f,t]=zftftb_pretty_sonogram(extraction,audio_fs,'len',16.7,'overlap',14,...
'zeropad',0,'norm_amp',1,'clipping',clipping);
startidx=max([find(f<=disp_band(1))]);
stopidx=min([find(f>=disp_band(2))]);
im=im(startidx:stopidx,:)*62;
im=flipdim(im,1);
[f,t]=size(im);
imwrite(uint8(im),colormap([ colors '(63)']),fullfile(DIR,'chop_data','gif',[ export_file '.gif']),'gif');
end
end
end
end