forked from jmarkow/intan_frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intan_frontend_dataextract.m
171 lines (118 loc) · 4.23 KB
/
intan_frontend_dataextract.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
function frontend_extract_data(FILENAME,DATA,DIRS,EXT_PTS,DISP_MINFS,DISP_MAXFS,COLORS,SOURCE,DATASAVE,PREFIX,SUFFIX,SKIP)
%extracts data given a set of points (e.g. TTL or SONG EXTRACTION)
%
%
%
%
%
%
fs=DATA.(SOURCE).fs;
if nargin<12
SKIP=0;
end
if nargin<11
SUFFIX='';
end
if nargin<10
PREFIX='songdet1_';
end
if nargin<9 | isempty(DATASAVE)
DATASAVE=0;
end
if nargin<8 | isempty(SOURCE)
SOURCE='audio';
end
[b,a]=ellip(5,.2,40,[300/(fs/2)],'high');
if ~isfield(DATA.(SOURCE),'norm_data')
DATA.(SOURCE).norm_data=filtfilt(b,a,DATA.(SOURCE).data);
end
[sonogram_im sonogram_f sonogram_t]=zftftb_pretty_sonogram(DATA.(SOURCE).norm_data,fs,'len',16.7,'overlap',3.3,'clipping',[-3 1]);
startidx=max([find(sonogram_f<=DISP_MINFS)]);
if isempty(startidx)
startidx=1;
end
stopidx=min([find(sonogram_f>=DISP_MAXFS)]);
if isempty(stopidx)
stopidx=length(sonogram_f);
end
sonogram_im=sonogram_im(startidx:stopidx,:)*62;
sonogram_im=flipdim(sonogram_im,1);
[f,t]=size(sonogram_im);
im_son_to_vec=(length(DATA.(SOURCE).norm_data)-(3.3/1e3)*fs)/t;
data_types={'ephys','audio'};
found_types=fieldnames(DATA);
to_del=[];
for i=1:length(data_types)
if ~any(strcmp(found_types,data_types{i}))
to_del=[to_del i];
end
end
data_types(to_del)=[];
savefun=@(filename,datastruct) save(filename,'-struct','datastruct','-v7.3');
sonogram_filename=fullfile(DIRS.image,PREFIX+FILENAME+SUFFIX+'.gif');
for i=1:size(EXT_PTS,1)
% cut out the extraction
EXTDATA=DATA;
% trim all data types
for j=1:length(data_types)
% convert to samples (different possible fs for each data type)
if ~isempty(EXTDATA.(data_types{j}).data)
startpoint=floor(EXT_PTS(i,1)*EXTDATA.(data_types{j}).fs);
endpoint=ceil(EXT_PTS(i,2)*EXTDATA.(data_types{j}).fs);
%startpoint=EXT_PTS(i,1);
%endpoint=EXT_PTS(i,2);
if startpoint<1 & SKIP
continue;
elseif startpoint<1 & ~SKIP
startpoint=1;
end
if endpoint>length(EXTDATA.(data_types{j}).data) & SKIP
continue;
elseif endpoint>length(EXTDATA.(data_types{j}).data) & ~SKIP
endpoint=length(EXTDATA.(data_types{j}).data);
end
EXTDATA.(data_types{j}).data=EXTDATA.(data_types{j}).data(:,startpoint:endpoint);
if isfield(EXTDATA.(data_types{j}),'norm_data')
EXTDATA.(data_types{j}).norm_data=EXTDATA.(data_types{j}).norm_data(:,startpoint:endpoint);
end
EXTDATA.(data_types{j}).t=EXTDATA.(data_types{j}).t(startpoint:endpoint);
end
end
if length(EXTDATA.(SOURCE).norm_data)<2
warning('Extraction failed, continuing...');
continue;
end
save_name=PREFIX+FILENAME+'_chunk_'+num2str(i)+SUFFIX;
sonogram_im(1:10,ceil(startpoint/im_son_to_vec):ceil(endpoint/im_son_to_vec))=62;
[chunk_sonogram_im chunk_sonogram_f chunk_sonogram_t]=zftftb_pretty_sonogram(EXTDATA.(SOURCE).norm_data,fs,'len',16.7,'overlap',10,'clipping',[-3 1]);
startidx=max([find(chunk_sonogram_f<=DISP_MINFS)]);
stopidx=min([find(chunk_sonogram_f>=DISP_MAXFS)]);
chunk_sonogram_im=chunk_sonogram_im(startidx:stopidx,:)*62;
chunk_sonogram_im=flipdim(chunk_sonogram_im,1);
[f,t]=size(chunk_sonogram_im);
chunk_im_son_to_vec=(length(EXTDATA.(SOURCE).data)-(10/1e3)*fs)/t;
imwrite(uint8(chunk_sonogram_im),colormap([ COLORS '(63)']),fullfile(DIRS.image,save_name+'.gif'),'gif');
% normalize audio to write out to wav file
min_audio=min(EXTDATA.(SOURCE).norm_data(:));
max_audio=max(EXTDATA.(SOURCE).norm_data(:));
if min_audio + max_audio < 0
EXTDATA.(SOURCE).norm_data=EXTDATA.(SOURCE).norm_data./(-min_audio);
else
EXTDATA.(SOURCE).norm_data=EXTDATA.(SOURCE).norm_data./(max_audio*(1+1e-3));
end
audiowrite(fullfile(DIRS.wav,save_name+'.wav'),EXTDATA.(SOURCE).norm_data,round(fs));
%wavwrite(EXTDATA.(SOURCE).norm_data,fs,fullfile(DIRS.wav,[ save_name '.wav']));
% remove all data used for plotting (i.e. norm_data)
for j=1:length(data_types)
if isfield(EXTDATA.(data_types{j}),'norm_data')
EXTDATA.(data_types{j})=rmfield(EXTDATA.(data_types{j}),'norm_data');
end
end
%EXTDATA.(SOURCE)=rmfield(EXTDATA.(SOURCE),'norm_data');
if DATASAVE
savefun(fullfile(DIRS.data,save_name+'.mat'),EXTDATA);
end
clear EXTDATA;
end
reformatted_im=markolab_im_reformat(sonogram_im,(ceil((length(DATA.(SOURCE).data)/fs)/10)));
imwrite(uint8(reformatted_im),colormap([ COLORS '(63)']),sonogram_filename,'gif');