www.pudn.com > LPCToolbox.rar > READDAT.M


% readdat(filelist)  
% 
%  READDAT takes a list of sound filenames and collates the  
%  information from the DAT files of the sounds. The information is 
%  written to LPCINFO.CSV (a comma-separated-value file), which  
%  can be imported into spreadsheet and statistical programs like  
%  MS-Excel, SPSS, and SAS. Each line of the CSV file ("one record") 
%  contains info from one file; the first field is the sound filename. 
%  The first line of the file gives the labels of the fields. 
% 
%    filelist: the list of sound filenames (can be a array of strings,  
%     or a cell. If a single filename is given and it has an extension of 
%     .DAT or .TXT, it is taken to be a file containing the list of  
%     sound filenames (one on each line). Basically, this parameter  
%     any file specification that is accepted by LPCTOOL. 
% 
% [fnamelist, info, key] = readdat(filelist)  
% 
%   If READDAT is invoked as above, the collected information is NOT written  
%   to LPCINFO.CSV, but is instead returned in the three variables.  
%   
%   info: an MxN matrix, where M=# of DAT files that were successfully  
%         read, N=# of fields read from a single DAT file. 
% 
%   key: cell array of length N. key{i} is the label for the i'th field. 
%  
%   fnamelist: cell array of length M. fnamelist{i} is the name of the 
%       i'th soundfile whose DAT file was successfully read in. 
%  
% Gautam Vallabha (vallabha@walt.ccs.fau.edu), NOV-20-2000 
% Center for Complex Systems, Florida Atlantic University. 
 
function [fnamelistx,infox,keyx] = readdat(filelst); 
 
if nargout > 0 
   fnamelistx={}; infox=[]; keyx={}; 
end 
 
Filelist = loadfilelist(filelst); 
if length(Filelist)==0,  
  fprintf(1, 'No sound files!\n'); 
  return;  
end 
 
fnamelist = {}; info=[]; 
for i=1:length(Filelist), 
 [sndfilename, datfilename, ext] = fixfilename(Filelist{i}); 
 ret = readfile(datfilename); 
 if ~isempty(ret),   
   info(end+1,:) = ret;  
   fnamelist{end+1} = sndfilename; 
 end 
end 
 
% the filename field is a hack for writecsv 
key = {'Filename', ... 
       'Starting frame', 'Ending frame', ... 
       'Sampling rate', 'Analysis window', 'Update', 'Preemphasis', ... 
       'Hamming', 'filter order', 'FFT length', '3-point interp',  ... 
       'Mean F1', 'Mean F2', 'Mean F3' }; 
 
if nargout == 0, 
  writecsv(key, fnamelist, info, 'lpcinfo.csv'); 
else 
   fnamelistx = fnamelist; infox = info;  
   keyx = {key{2:end}};  % remove the hacked first field 
end 
 
%--------------------------- 
 
function [ret] = readfile(fname) 
 
ret = [];  
fid = fopen(fname, 'r'); 
if fid < 0,  
  fprintf(1, 'Unable to open %s\n', fname); 
  return; 
end 
 
s = []; 
while isempty(s), s = fgetl(fid); end 
d1 = sscanf(s, 'Frames: %d - %d'); 
nframes = d1(2) - d1(1) + 1; 
 
s = fgetl(fid);  
d2 = sscanf(s, 'p: sf=%d aw=%d cw=%d pr=%f'); 
 
s = fgetl(fid);  
d3 = sscanf(s, 'p: hm=%d nc=%d fft=%d tpi=%d'); 
 
 
fmts = zeros(3,nframes); 
for i=1:nframes, 
 s = fgetl(fid); 
 fmts(:,i) = sscanf(s, 'f:  %d %d %d'); 
end 
dd = round(mean(fmts,2)); 
 
ret = [d1(:) ; d2(:) ; d3(:) ; dd(:)].'; 
 
fclose(fid); 
 
%--------------------------- 
 
function writecsv(key, fnamelist, info, outfname) 
 
fid = fopen(outfname, 'w'); 
if fid < 0, 
  fprintf(1, 'Unable to open %s', outfname); return; 
end 
 
for i=1:length(key)-1, fprintf(fid, '%s,', key{i}); end 
fprintf(fid, '%s\n', key{end}); 
 
for i=1:size(info,1), 
% fprintf(fid, '%s,%d,%d, %d,%d,%d,%f, %d,%d,%d,%d, %d,%d,%d\n', ... 
%         fnamelist{i}, info(i,:)); 
 
  fprintf(fid, '%s,',  fnamelist{i}); 
  fprintf(fid, '%f,',  info(i,1:end-1));  
  fprintf(fid, '%f\n', info(i,end));  
end 
 
fclose(fid); 
 
fprintf(1, 'Output written to %s\n', outfname);