-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatEsnData.m
More file actions
194 lines (154 loc) · 7.3 KB
/
formatEsnData.m
File metadata and controls
194 lines (154 loc) · 7.3 KB
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
function formatEsnData(useOnset,useAn,useGabors, rootDir, mloDir,mlaDir,gabDirs)
% This function will read a list of files and the equivalent onsets, gabors
% and AN signals, and then convert all into a data format suitable for
% input into an ESN. Will also create a directory of all file names for
% classification translation purposes, and determine the number of outputs.
% Finally, will save data to chosen filename. Requires a number of
% inputs, useOnset (1 or 0), use An(1 or 0), and useGabors (1 or 0).
% Also requires root dir, meanOnset (mlo), and AN (mla) dirs. Also
% requires cell array of gabor dirs (gabDirs).
% Example call
% formatEsnData(1,1,0,'F:/testallen2','onsetMeans2','AMMeanPlain',{'gabor25042014_1','gabor25042014_2','gabor25042014_3'});
% Note, this works only for Allen Corpus
% AKA - May 2014
%rootDir = 'F:/testallen2'; % Root dir to take all files from
%gabDirs = {'gabor25042014_1','gabor25042014_2','gabor25042014_3'};
%mloDir = 'onsetMeans2'; % Onset dir to add in
%mlaDir = 'AMMeanPlain'; % AN dir to add in
%useOnset = 1;
%useAn = 1;
%useGabors = 1;
if (useOnset+useAn+useGabors ~= 0)
% creates list of all au or wav files in directory
filePattern = fullfile(rootDir, '*.wav');
filePattern2 = fullfile(rootDir, '*.au');
Files=[dir(filePattern);dir(filePattern2)];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Create arrays
%create blank cell array, 3 by however many audio files
allInputData = cell(length(Files),3);
% create a matrix to put the raw cells in
% also create a directory of keys
rawLabels = cell(length(Files),1);
rawDirect = cell(length(Files),2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Create phones and directory
% get file name and add to input as phone label and full name
for k = 1:length(Files)
% Find suffix dot and last _
phoneend = find(Files(k).name==('.'),1, 'last');
phonestart = find(Files(k).name==('_'),1, 'last');
% add to cell array
%allInputData{k,1} = Files(k).name(phonestart+1:phoneend-1);
rawLabels{k,1} = Files(k).name(phonestart+1:phoneend-1);
% add the file name (sans suffix to label (column 2)
allInputData{k,2} = Files(k).name(1:phoneend-1);
end
% ----------------- Directory Creation ------------------------------
% Go through each file in raw label list, compare it to list of
% existing phones in directory. If already exists, assign class. If
% it does not exist, add a row to directory and then assign.
for k = 1:length(Files)
% Take kth value in raLabel
tempL = rawLabels{k};
ismem = 0; % Initially not found
% Compare to directory
for k2 = 1:length(Files)
tempC = rawDirect{k2,1};
ismem = ismember(tempL,tempC);
if( ismem>0)
allInputData{k,1} = k2;
break;
end
end
% if not found, add to table
if (ismem == 0)
firstempty = find(cellfun('isempty', rawDirect(:,1)),1);
rawDirect{firstempty,1} = tempL;
rawDirect{firstempty,2} = firstempty;
allInputData{k,1} = firstempty;
end
clear ismem;
end
% When directories finished, remove blank lines
firstempty = find(cellfun('isempty', rawDirect),1);
if(firstempty ~=0)
directList = rawDirect(1:firstempty-1,:);
else
directList = rawDirect;
end
% ---------- End of Directory Creation ------------------------------
% ----------------- Merging of Data ---------------------------------
% Depending on input parameters chosen, merges combination of AN, mean
% onsets and gabors into one combined array, and then saves.
for k = 1:length(Files)
% variables to create full files
totInputs=0;
totInputDim=0;
% initial empty matrix
mergedall = zeros(totInputs,totInputDim);
% For each gabor dir specified above, read and merge
if(useGabors)
gabData = cell(length(gabDirs),1);
for k2 = 1:length(gabDirs)
% get dir to load
gabfil = fullfile(rootDir,gabDirs{k2},[allInputData{k,2},'_gaboroutputs.mat']);
% load file
% needs to be in the format of inputs x length, so flip
gabRead = load(gabfil);
gabData{k2} = gabRead.gaboroutput{1}';
[inps(k2),dims(k2)] = size(gabData{k2});
totInputs=totInputs+inps(k2);
totInputDim=max(totInputDim,dims(k2));
end
% Merge Gabors
mergedall = zeros(totInputs,totInputDim);
for k3 = 1:k2
mergedall((1+(inps(k3)*(k3-1))):(k3*inps(k3)),1:dims(2)) = gabData{k3};
end
end
% Add onset information for each file , take from onset dir
if(useOnset)
temp1 = mergedall;
onsetfil = fullfile(rootDir,mloDir,[allInputData{k,2},'_onsetmeans.mat']);
onsetread = load(onsetfil);
onsData = onsetread.mlo';
[inps(k),dims(k)] = size(onsData);
% Get Dims for creating combined matrix
totInputs=totInputs+inps(k);
totInputDim=max(totInputDim,dims(k));
% Create combined matrix
mergedall= zeros(totInputs,totInputDim);
% merge in onsets
mergedall(1:inps(k),1:dims(k)) = onsData;
% merge in gabor data to create overall
[tempIns,tempSize] = size(temp1);
mergedall((inps(k)+1):totInputs,1:tempSize) = temp1;
end
% Add AN Signal information for each file, take from AN mean dir
if(useAn)
temp1 = mergedall;
anfil = fullfile(rootDir,mlaDir,[allInputData{k,2},'_plainmeans.mat']);
anread = load(anfil);
anData = anread.mlm';
[inps(k),dims(k)] = size(anData);
% Get Dims for creating combined matrix
totInputs=totInputs+inps(k);
totInputDim=max(totInputDim,dims(k));
% Create combined matrix
mergedall= zeros(totInputs,totInputDim);
% merge in an
mergedall(1:inps(k),1:dims(k)) = anData;
% merge in all to create overall
[tempIns,tempSize] = size(temp1);
mergedall((inps(k)+1):totInputs,1:tempSize) = temp1;
end
% Final format for output
allInputData{k,3} = mergedall;
end
%save output file and variables
totNetInputs = totInputs;
%save((fullfile(rootDir,'inputDataSet.mat')), 'allInputData', 'directList','totNetInputs');
save((fullfile(rootDir,'inputDataSet.mat')), 'allInputData', 'directList','totNetInputs','-v7.3');
else
% If no inputs have been chosen for use, stop writing process
disp('No inputs chosen for processing. Stopping');
end