-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtktocsvmanyfiles.m
171 lines (130 loc) · 4.22 KB
/
htktocsvmanyfiles.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
function [] = htktocsvmanyfiles(filenamesFiles, outputTrainFile, outputTestFile)
% FilenameFiles is a list of the names of files containing
% the filenames to be parsed
% Based on function written by Mark Hasegawa-Johnson
% July 3, 2002
% Based on function mfcc_read written by Alexis Bernard
%
% Bucket sizes in seconds
bucketSize = 4;
% Sliding Buckets on/off
sliding = 0;
% slidingDist in seconds
slidingDist = 1;
% Coordinate Standardization
standardize = 0;
more off;
% This assumes feature windows of 0.025 seconds. Which means theres 40 features per second.
bucketConst = bucketSize*40;
slidingConst = slidingDist*40;
filenamesFiles
for filenamesFileInd = 1:size(filenamesFiles,1)
['reading files of class', filenamesFileInd-1]
filename = filenamesFiles{filenamesFileInd}
% size(filename, 1)
% size(filename, 2)
filesFid = fopen(filename,'r');
if filesFid < 0,
error(sprintf('Unable to read from file %s',filenamesFiles(filenamesFileInd,:)));
end
files = textscan(filesFid, '%s');
for fileInd = 1:size(files{1,1},1)
file = files{1,1}{fileInd,1};
%['reading file', file]
%size(file,1)
%size(file,2)
file
[DATA, HTKCode] = htkread(file);
%DATA
if (sliding)
'Sliding Buckets'
AVGDATA = repmat(0, (int32(floor(size(DATA, 1)/(slidingConst))) - bucketSize + 1), size(DATA, 2));
for slide = 1:(int32(floor(size(DATA, 1)/(slidingConst))) - bucketSize + 1)
for slideNum = 0:bucketConst-1
for col = 1:size(DATA,2)
AVGDATA(slide, col) = AVGDATA(slide, col) + DATA((slide + slideNum), col);
end
end
end
else
'Non-sliding Buckets'
AVGDATA = repmat(0, (int32(floor(size(DATA, 1)/bucketConst))), (size(DATA, 2)));
for rowNum = 1:(size(DATA, 1)- mod(size(DATA, 1), bucketConst))
avgRowNum = int32(floor((rowNum-1)/bucketConst))+1;
for col=1:size(DATA,2)
AVGDATA(avgRowNum,col) = AVGDATA(avgRowNum,col) + DATA(rowNum,col);
end
end
end
AVGDATA = AVGDATA ./ bucketConst;
classVec = repmat(filenamesFileInd-1,[size(AVGDATA, 1) 1]);
if(standardize)
if fileInd < size(files{1,1},1)*0.7
dlmwrite('outputTrainTemp', [AVGDATA classVec], '-append');
else
dlmwrite('outputTestTemp', [AVGDATA classVec], '-append');
end
else
if filenamesFileInd == 1 && fileInd == 1
dlmwrite(outputTrainFile, 1:(size(AVGDATA,2))+1);
dlmwrite(outputTestFile, 1:(size(AVGDATA,2))+1);
end
if fileInd < size(files{1,1},1)*0.7
dlmwrite(outputTrainFile, [AVGDATA classVec], '-append');
else
dlmwrite(outputTestFile, [AVGDATA classVec], '-append');
end
end
end
fclose(filesFid);
[]
end
if (standardize)
'Starting Standardization'
'Reading tempfiles'
trainTemp = csvread('outputTrainTemp');
numTrainRows = size(trainTemp,1);
testTemp = csvread('outputTestTemp');
numTestRows = size(testTemp,1);
temp = vertcat (trainTemp, testTemp); %Concatinate train on top
[numRows, numCols] = size(temp);
numCols = numCols-1; % To not standardize the label
% Calculate mean
mean = repmat(0, 1, numCols);
for row = 1:numRows
for col = 1:numCols
mean(1,col) = mean(1,col) + temp(row,col);
end
end
mean = mean ./ numRows;
% Calculate stddev
stddev = repmat(0, 1, numCols);
for row = 1:numRows
for col = 1:numCols
stddev(1,col) = stddev(1,col)+(temp(row,col) - mean(1,col))*(temp(row,col) - mean(1,col));
end
end
stddev = stddev ./ numRows;
stddev = sqrt(stddev)
% Calculate coordinate standardization
for row = 1:numRows
for col = 1:numCols
temp(row,col) = (temp(row,col) - mean(1,col))/stddev(1,col);
end
end
%write index on first row
dlmwrite(outputTrainFile, 1:(numCols + 1));
dlmwrite(outputTestFile, 1:(numCols + 1));
Train = zeros(numTrainRows, numCols + 1);
Test = zeros(numRows - numTrainRows, numCols + 1);
for i = 1:numRows
if i < numTrainRows + 1
Train(i,:) = temp(i,:);
else
Test(i-numTrainRows,:) = temp(i,:);
end
end
dlmwrite(outputTrainFile, Train, '-append');
dlmwrite(outputTestFile, Test, '-append');
'Finished Standardization'
end