-
Notifications
You must be signed in to change notification settings - Fork 18
/
htkwrite.m
79 lines (71 loc) · 2.49 KB
/
htkwrite.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
function htkwrite( DATA, Filename, HTKCode, sampPeriod )
% htkwrite( DATA, Filename, HTKCode, sampPeriod )
%
% Write DATA to HTK format file.
%
% Filename (string) - Name of the file to write to
% DATA (NFRAMES x NUMCOFS) - data to write
% HTKCode (scalar) - code describing write format
% Default value: 512+64+3 means LPCC_C_E
% sampPeriod (scalar) - sample period, in 100ns units
% Default value: 100000 means 10ms
%
% DATA must be already formatted for HTK, i.e. coefficients first, then
% energy, then deltas if desired, then delta-energies if desired, etc.
%
% This function DOES NOT CHECK compatibility of HTKCode with DATA matrix.
%
% HTKCode is only checked to see whether or not we should perform compression.
% Compression is performed using the algorithm in 5.10 of the HTKBook.
% CRC is not supported by this function.
%
% Mark Hasegawa-Johnson
% July 3, 2002
% Based on function mfcc_write written by Alexis Bernard
%
% Find out whether or not compression is requested
if nargin<3,
HTKCode = 3; % Default code is LPCC
HTKCode = bitset(HTKCode, 7); % Set the energy bit
HTKCode = bitset(HTKCode, 11); % Set the compression bit
end
% Open the file
fid=fopen(Filename,'w','ieee-be');
if fid<0,
error(sprintf('Unable to write to file %s',Filename));
end
% Find nSamp and NCOFS
[ nSamp, NCOFS ] = size(DATA);
% Write sampPeriod
if nargin<4,
sampPeriod = 100000;
end
% If data are compressed, write compression parameters and compressed data
if bitget(HTKCode, 11),
sampSize = 2*NCOFS;
%disp(sprintf('htkwrite: Writing %d frames, dim %d, compressed, to %s',nSamp,NCOFS,Filename));
fwrite(fid,nSamp+4,'int32');
fwrite(fid,sampPeriod,'int32');
fwrite(fid,sampSize,'int16');
fwrite(fid,HTKCode,'int16');
% Create and write the compression parameters
xmax = max(DATA);
xmin = min(DATA);
A = 2*32767./(xmax-xmin);
B = (xmax+xmin)*32767 ./ (xmax-xmin);
fwrite(fid, A, 'float');
fwrite(fid, B, 'float');
% Write compressed data
Xshort = round( repmat(A,[nSamp 1]) .* DATA - repmat(B,[nSamp 1]) );
fwrite(fid, Xshort', 'int16');
else
sampSize = 4*NCOFS;
%disp(sprintf('htkwrite: Writing %d frames, dim %d, uncompressed, to %s',nSamp,NCOFS,Filename));
fwrite(fid,nSamp,'int32');
fwrite(fid,sampPeriod,'int32');
fwrite(fid,sampSize,'int16');
fwrite(fid,HTKCode,'int16');
% Write uncompressed data
fwrite(fid, DATA', 'float');
end
fclose(fid);