Home > NoiseTools > nt_idxx_old.m

nt_idxx_old

PURPOSE ^

nt_idxx(fname,iname,blksize,chunksize,nfft) - create an index file to summarize large data file

SYNOPSIS ^

function [bstats,wstats,cstats,sstats]=nt_idxx(fname,iname,blksize,channels_to_keep,nfft,chunksize)

DESCRIPTION ^

nt_idxx(fname,iname,blksize,chunksize,nfft) - create an index file to summarize large data file

  fname: name of data file to index
  iname: name of index file to create [default fname with EXT = .idxx in directory i]
  bsize: size of blocks over which to calculate stats [default: 100]
  channels_to_keep: ignore other channels
  nfft: fft size for psd [default: 1024]
  chunksize: size of chunks to read from disk [default: 500000]

 If blksize is a struct, the following fields are expected:
   blksize.wav:  blocksize to calculate basic statistics [default: 100]
   blksize.cov:  blocksize to calculate covariance [default: none]
   blksize.psd: blocksize to calculate psd [default: none]
 If blksize is numeric, it refers to basic statistics.

 Usage:
   nt_idx(fname,...): calculate index structs, store in index file

   [bstats,wstats,cstats,sstats]=nt_idx(fname,...): return index structs:
     bstats: basic stats (size, etc.)
     wstats: waveform (min, max, mean, std)
     cstats: covariance
     sstats: psd

 NoiseTools

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [bstats,wstats,cstats,sstats]=nt_idxx(fname,iname,blksize,channels_to_keep,nfft,chunksize)
0002 %nt_idxx(fname,iname,blksize,chunksize,nfft) - create an index file to summarize large data file
0003 %
0004 %  fname: name of data file to index
0005 %  iname: name of index file to create [default fname with EXT = .idxx in directory i]
0006 %  bsize: size of blocks over which to calculate stats [default: 100]
0007 %  channels_to_keep: ignore other channels
0008 %  nfft: fft size for psd [default: 1024]
0009 %  chunksize: size of chunks to read from disk [default: 500000]
0010 %
0011 % If blksize is a struct, the following fields are expected:
0012 %   blksize.wav:  blocksize to calculate basic statistics [default: 100]
0013 %   blksize.cov:  blocksize to calculate covariance [default: none]
0014 %   blksize.psd: blocksize to calculate psd [default: none]
0015 % If blksize is numeric, it refers to basic statistics.
0016 %
0017 % Usage:
0018 %   nt_idx(fname,...): calculate index structs, store in index file
0019 %
0020 %   [bstats,wstats,cstats,sstats]=nt_idx(fname,...): return index structs:
0021 %     bstats: basic stats (size, etc.)
0022 %     wstats: waveform (min, max, mean, std)
0023 %     cstats: covariance
0024 %     sstats: psd
0025 %
0026 % NoiseTools
0027 nt_greetings;
0028 
0029 
0030 
0031 assert(nargin>0, '!');
0032 if nargin<2 ; iname=[]; end
0033 if nargin<3 || isempty(blksize)
0034     blksize.wav=100;
0035 end
0036 if nargin<4; channels_to_keep=[]; end
0037 if nargin<5 || isempty(nfft); nfft=1024; end
0038 if nargin<6 || isempty(chunksize); chunksize=500000; end
0039 
0040 if isnumeric(blksize); tmp=blksize; blksize=[]; blksize.wav=tmp; end
0041 if ~isempty(iname) && ~ischar(iname); error('!'); end % common error
0042 
0043 % check for FieldTrip
0044 try, ft_version; catch, disp('You must download FieldTrip from http://www.fieldtriptoolbox.org'); return; end
0045 
0046 % use separate structs to make it easy to read just one kind of stats from file
0047 bstats=[]; % index structure for basic stats
0048 wstats=[]; % index structure for waveform
0049 cstats=[]; % index structure for covariance
0050 sstats=[]; % index structure for spectrogram
0051 bstats.fname=fname; 
0052 
0053 % read header
0054 h=ft_read_header(fname);
0055 bstats.header=h;
0056 bstats.sr=h.Fs;
0057 bstats.nsamples=h.nSamples;
0058 bstats.label=h.label;
0059 bstats.nchans=h.nChans;
0060 
0061 if isempty(channels_to_keep); channels_to_keep=1:bstats.nchans; end
0062 if any(channels_to_keep>bstats.nchans); error('!'); end
0063 bstats.channels_to_keep=channels_to_keep;
0064 bstats.nchans=numel(channels_to_keep);
0065 
0066 % allocate basic stats arrays:
0067 nbasic=ceil(bstats.nsamples/blksize.wav); % total number of blocs for basic stats
0068 wstats.min=zeros(nbasic,bstats.nchans);
0069 wstats.max=zeros(nbasic,bstats.nchans); 
0070 wstats.mean=zeros(nbasic,bstats.nchans);
0071 wstats.rms=zeros(nbasic,bstats.nchans);
0072 wstats.card=zeros(nbasic,1,'uint32');
0073 
0074 chunksize=floor(chunksize/blksize.wav)*blksize.wav; 
0075 
0076 % allocate covariance array
0077 if isfield(blksize,'cov')
0078     tmp=log2(blksize.cov/blksize.wav);
0079     assert(tmp==round(tmp), ...
0080         'blksize.cov should be power of 2 times blksize.wav');
0081     ncov=ceil(bstats.nsamples/blksize.cov);
0082     cstats.cov=zeros(ncov,bstats.nchans,bstats.nchans);
0083     cstats.card=zeros(ncov,1,'uint32');
0084     chunksize=floor(chunksize/blksize.cov)*blksize.cov;
0085 end
0086 
0087 % allocate psd array
0088 if isfield(blksize,'psd') 
0089     if blksize.psd < nfft; error('!'); end;
0090     tmp=log2(blksize.psd/blksize.wav);
0091     assert(tmp==round(tmp), ...
0092         'blksize.psd should be power of 2 times blksize.wav');
0093     npsd=ceil(bstats.nsamples/blksize.psd);
0094     sstats.psd=zeros(npsd,bstats.nchans,nfft/2+1);
0095     sstats.card=zeros(npsd,1,'uint32');
0096     sstats.nfft=nfft;
0097     chunksize=floor(chunksize/blksize.psd)*blksize.psd;
0098 end
0099 
0100 
0101 foffset=0;
0102 boffset=0;
0103 coffset=0;
0104 soffset=0;
0105 
0106 while true
0107     
0108     %if file_offset>=i.nsamples; break; end
0109     
0110     % read chunk from disk
0111     begsample=foffset+1;
0112     endsample=min(foffset+chunksize,bstats.nsamples);
0113     x=ft_read_data(fname, 'begsample',begsample,'endsample',endsample);
0114     x=x'; % --> time X channels
0115     x=x(:,channels_to_keep);
0116     
0117     % fold chunk into blocks
0118     n=floor(size(x,1)/blksize.wav); % number of blocks in this chunk
0119     xb=x(1:n*blksize.wav,:);
0120     xb=reshape(xb,[blksize.wav,n,bstats.nchans]);
0121     wstats.min(boffset+(1:n),:)=min(xb);
0122     wstats.max(boffset+(1:n),:)=max(xb);
0123     wstats.mean(boffset+(1:n),:)=mean(xb);
0124     wstats.rms(boffset+(1:n),:)=sqrt(mean(xb.^2));
0125     wstats.card(boffset+(1:n),:)=blksize.wav;
0126     boffset=boffset+n; 
0127 
0128     % extra bit at end of file?
0129     if size(x,1)>n*blksize.wav
0130         tmp=x(n*blksize.wav+1:end,:);
0131         wstats.min(boffset+1,:)=min(tmp);
0132         wstats.max(boffset+1,:)=max(tmp);
0133         wstats.mean(boffset+1,:)=mean(tmp);
0134         wstats.rms(boffset+1,:)=sqrt(mean(tmp.^2));
0135         wstats.card(boffset+1,:)=size(tmp,1);
0136     end
0137     
0138     
0139     foffset=foffset+n*blksize.wav;
0140 
0141     if ~isempty(cstats) && isfield(cstats, 'cov')
0142         n=floor(size(x,1)/blksize.cov); % number of blocks
0143         xb=x(1:n*blksize.cov,:);        
0144         xb=reshape(xb,[blksize.cov, n, bstats.nchans]);
0145         for iBlock=1:n
0146             tmp=squeeze(xb(:,iBlock,:));
0147             tmp=nt_demean(tmp);
0148             cstats.cov(coffset+iBlock,:,:) = tmp'*tmp;
0149             cstats.cardcov(coffset+iBlock,:)=blksize.cov;
0150         end
0151         coffset=coffset+size(xb,2);
0152         if size(x,1)>n*blksize.cov
0153             tmp=x(n*blksize.cov+1:end,:);
0154             tmp=nt_demean(tmp);
0155             cstats.cov(coffset+1,:,:)=tmp'*tmp;
0156             cstats.cardcov(coffset+1,:)=size(tmp,1);
0157         end              
0158     end
0159        
0160     if ~isempty(sstats) && isfield(sstats, 'psd')
0161         n=floor(size(x,1)/blksize.psd); % number of blocks
0162         xb=x(1:n*blksize.psd,:);        
0163         xb=reshape(xb,[blksize.psd, n, bstats.nchans]);
0164         for iBlock=1:n
0165             tmp=squeeze(xb(:,iBlock,:));
0166             tmp=nt_demean(tmp);
0167             sstats.psd(soffset+iBlock,:,:) = pwelch(tmp, nfft, 'power')';
0168             sstats.cardpsd(soffset+iBlock,:,:)=blksize.psd;
0169         end
0170         soffset=soffset+size(xb,2);
0171         if size(x,1)>n*blksize.psd
0172             tmp=x(n*blksize.psd+1:end,:);
0173             if size(tmp,1)<nfft; break; end
0174             tmp=nt_demean(tmp);
0175             sstats.psd(soffset+1,:,:) = pwelch(tmp, nfft, 'power')';
0176             sstats.cardpsd(soffset+1,:)=size(tmp,1);
0177         end              
0178     end
0179     
0180     nt_whoss
0181     disp([num2str(foffset), '/', num2str(h.nSamples), ' (', num2str(foffset/h.nSamples*100), '%)']);
0182     disp([boffset, coffset, soffset]);
0183     
0184     if endsample>=bstats.nsamples; break; end;
0185 end
0186    
0187 if ~nargout
0188     if isempty(iname)
0189         [FILEPATH,NAME,EXT]=fileparts(fname);
0190         if isempty(FILEPATH); FILEPATH=pwd; end
0191         if ~exist([FILEPATH,filesep,'idxx'], 'dir')
0192             mkdir([FILEPATH,filesep,'idxx']);
0193         end        
0194         iname=[FILEPATH,filesep,'idxx',filesep,NAME,EXT,'.idxx'];
0195     end
0196     wstats.min=nt_double2int(wstats.min); 
0197     wstats.max=nt_double2int(wstats.max);
0198     wstats.mean=nt_double2int(wstats.mean);
0199     wstats.rms=nt_double2int(wstats.rms);
0200     save(iname, 'bstats', 'wstats','cstats', 'sstats','-v7.3');
0201     clear bstats wstats cstats sstats;
0202 end
0203 
0204 
0205     
0206

Generated on Wed 27-Nov-2019 09:49:00 by m2html © 2005