Home > matlabmk > baseline_tfrGND.m

baseline_tfrGND

PURPOSE ^

baseline_tfrGND() - Baselines the spectrograms in a tfrGND variable.

SYNOPSIS ^

function tfrGND=baseline_tfrGND(tfrGND,units,bsln_wind,bsln_type,bin)

DESCRIPTION ^

 baseline_tfrGND() - Baselines the spectrograms in a tfrGND variable.
 
 Usage:
  >> tfrGND=baseline_tfrGND(tfrGND,units,bsln_wind,bsln_type);

 Required Inputs:
   tfrGND    - A MATLABmk tfrGND variable. These are produced by the
               MATLABmk function tfrs2tfrGND.m. It contains spectrograms from 
               one or more participants, their grand averages, and
               associated information.
   units     - ['raw' or 'dB' or empty set] 'raw' is raw spectral power 
               (which is in units of (uV^2)/Hz). 'dB' is decibels (units 
               of 10*log10((uV^2)/Hz)). Provide the empty set (i.e., [])
               to keep units the same as they currently are.
   bsln_wind - [start_time end_time] Temporal boundaries of baseline
               window (in units of ms).  Provide the empty set (i.e., [])
               to keep baseline window the same as it currently is.
   bsln_type - ['absolute', 'relative', or empty] Type of baseline
               normalization.  If 'absolute', the mean power in the
               baseline window is subtracted from the rest of the time
               points.  If 'relative', the mean power in the baseline
               window is divided from the rest of the time points. Provide 
               the empty set (i.e., []) to keep baseline type the same as
               it currently is.
 
 Optional Inputs:
   bin       - [integer or vector of integers] The bin or bins you would 
               like to transform. If not specified, all bins will be
               transformed.


 Notes:
 -If you transform a subset of bins in the TFR variable, the baseline
 fields of the TFR variable will be set to 'may vary across bins'.


 Examples:
 Using a relative baseline with a baseline window of -500 to 0 ms and raw
 spectral power. All bins will be affected.
 >> tfrGND=baseline_tfrGND(tfrGND,'raw',[-500 0],'relative');

 Using a relative baseline with a baseline window of -500 to 0 ms and raw
 spectral power.  Only Bin 1 will be affected.
 >> tfrGND=baseline_tfrGND(tfrGND,'raw',[-500 0],'relative',1);

 Just converting to dB (no change of baseline).  All bins will be
 affected.
 >> tfrGND=baseline_tfrGND(tfrGND,'dB',[],[]);


 Author:
 David Groppe
 Kutaslab, 4/2011

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 % baseline_tfrGND() - Baselines the spectrograms in a tfrGND variable.
0002 %
0003 % Usage:
0004 %  >> tfrGND=baseline_tfrGND(tfrGND,units,bsln_wind,bsln_type);
0005 %
0006 % Required Inputs:
0007 %   tfrGND    - A MATLABmk tfrGND variable. These are produced by the
0008 %               MATLABmk function tfrs2tfrGND.m. It contains spectrograms from
0009 %               one or more participants, their grand averages, and
0010 %               associated information.
0011 %   units     - ['raw' or 'dB' or empty set] 'raw' is raw spectral power
0012 %               (which is in units of (uV^2)/Hz). 'dB' is decibels (units
0013 %               of 10*log10((uV^2)/Hz)). Provide the empty set (i.e., [])
0014 %               to keep units the same as they currently are.
0015 %   bsln_wind - [start_time end_time] Temporal boundaries of baseline
0016 %               window (in units of ms).  Provide the empty set (i.e., [])
0017 %               to keep baseline window the same as it currently is.
0018 %   bsln_type - ['absolute', 'relative', or empty] Type of baseline
0019 %               normalization.  If 'absolute', the mean power in the
0020 %               baseline window is subtracted from the rest of the time
0021 %               points.  If 'relative', the mean power in the baseline
0022 %               window is divided from the rest of the time points. Provide
0023 %               the empty set (i.e., []) to keep baseline type the same as
0024 %               it currently is.
0025 %
0026 % Optional Inputs:
0027 %   bin       - [integer or vector of integers] The bin or bins you would
0028 %               like to transform. If not specified, all bins will be
0029 %               transformed.
0030 %
0031 %
0032 % Notes:
0033 % -If you transform a subset of bins in the TFR variable, the baseline
0034 % fields of the TFR variable will be set to 'may vary across bins'.
0035 %
0036 %
0037 % Examples:
0038 % Using a relative baseline with a baseline window of -500 to 0 ms and raw
0039 % spectral power. All bins will be affected.
0040 % >> tfrGND=baseline_tfrGND(tfrGND,'raw',[-500 0],'relative');
0041 %
0042 % Using a relative baseline with a baseline window of -500 to 0 ms and raw
0043 % spectral power.  Only Bin 1 will be affected.
0044 % >> tfrGND=baseline_tfrGND(tfrGND,'raw',[-500 0],'relative',1);
0045 %
0046 % Just converting to dB (no change of baseline).  All bins will be
0047 % affected.
0048 % >> tfrGND=baseline_tfrGND(tfrGND,'dB',[],[]);
0049 %
0050 %
0051 % Author:
0052 % David Groppe
0053 % Kutaslab, 4/2011
0054 
0055 
0056 % Notes:
0057 % How baseling could be done with the Fieldtrip function ft_freqbaseline
0058 % 1. Set up configuration parameters
0059 % cfg.baseline=[-.5 0];
0060 % cfg.baselinetype='absolute';
0061 %
0062 % 2. Extract data for this subject, sub, and bin, b.
0063 % data.powspctrm=squeeze(tfrGND.indiv{b}(sub,:,:,:));
0064 %
0065 % 3. Baseline data
0066 % data=ft_freqbaseline(cfg,data);
0067 %
0068 % 4. Replace old data with baselined data
0069 % tfrGND.indiv{b}(sub,:,:,:)=data.powspctrm;
0070 
0071 function tfrGND=baseline_tfrGND(tfrGND,units,bsln_wind,bsln_type,bin)
0072 
0073 n_sub=size(tfrGND.indiv{1},1);
0074 n_bin=length(tfrGND.indiv);
0075 n_tpt=length(tfrGND.ftrip.time);
0076 changed=0;
0077 
0078 %Error check bin argument
0079 if nargin<5,
0080     %if bin argument not specified, use all bins
0081     use_bins=1:n_bin;
0082 else
0083     if min(bin)<1,
0084         error('All elements of argument bin must be greater than 0.');
0085     elseif max(bin)>n_bin,
0086         error('You can''t baseline Bin %d.  This TFR variable only has % bins.',max(bin),n_bin);
0087     elseif ~isequal(bin,round(bin)),
0088         error('All elements of argument bin need to be integers.');
0089     end
0090     use_bins=bin;
0091 end
0092 
0093 %Make sure user isn't trying to unbaseline data
0094 if strcmpi(bsln_type,'none') && ~strcmpi(tfrGND.bsln_type,'none'),
0095    error('You can''t unbaseline data that have already been baselined.'); 
0096 end
0097 
0098 %If not specified as input arguments, using current values in tfrGND variable
0099 if isempty(units),
0100     units=tfrGND.units;
0101 end
0102 if isempty(bsln_wind),
0103     bsln_wind=tfrGND.bsln_wind;
0104 end
0105 if isempty(bsln_type),
0106     bsln_type=tfrGND.bsln_type;
0107 end
0108 
0109 % First adjust units if necessary
0110 if ~strcmpi(units,tfrGND.units)
0111     changed=1;
0112     if strcmpi(tfrGND.units,'dB'),
0113         fprintf('Temporarily transforming tfrGND data from dB to raw power.\n');
0114         if ~strcmpi(tfrGND.bsln_type,'none'),
0115             watchit(sprintf(['Since the data in this tfrGND variable were originally baselined, \n', ...
0116                 'converting them to raw power from dB will produce somewhat different values than if you had initially created the tfrGND variable in units of raw power']));
0117         end
0118         for b=use_bins,
0119             for sub=1:n_sub,
0120                 tfrGND.indiv{b}(sub,:,:,:)=10.^(tfrGND.indiv{b}(sub,:,:,:)/10);
0121             end
0122         end
0123         tfrGND.units='raw';
0124     else
0125         fprintf('Temporarily transforming tfrGND data from raw to dB power.\n');
0126         if ~strcmpi(tfrGND.bsln_type,'none'),
0127            watchit(sprintf(['Since the data in this tfrGND variable were originally baselined, \n', ...
0128                'converting them to dB will produce somewhat different values than if you had converted the data to dB before baselining.'])); 
0129         end
0130         for b=use_bins,
0131             for sub=1:n_sub,
0132                 tfrGND.indiv{b}(sub,:,:,:)=10*log10(tfrGND.indiv{b}(sub,:,:,:));
0133             end
0134         end
0135         tfrGND.units='dB';
0136     end
0137 end
0138 
0139 
0140 % Next adjust baseline if necessary
0141 if ~isequal(bsln_wind,tfrGND.bsln_wind) || ~strcmpi(bsln_type,tfrGND.bsln_type)
0142     changed=1;
0143     
0144     %error checking
0145     if strcmpi(bsln_type,'none'),
0146        error('You cannot un-baseline spectrograms once they''ve been baselined.'); 
0147     end
0148     if isempty(bsln_wind),
0149        error('You cannot specify a baseline type without a baseline time window.'); 
0150     end
0151     if bsln_wind(2)<bsln_wind(1),
0152         error('The start of your baseline time window, %g, is after the end of your baseline time window, %g\n', ...
0153             bsln_wind(1),bsln_wind(2));
0154     end
0155     
0156     %find time points to use for baselining
0157     start_tpt=find(tfrGND.ftrip.time<(bsln_wind(1)/1000)); %converting from ms to sec
0158     end_tpt=find(tfrGND.ftrip.time<=(bsln_wind(2)/1000)); %converting from ms to sec
0159     if isempty(start_tpt)
0160         if ~isempty(end_tpt),
0161             start_tpt=0;
0162         else
0163             error('The starting point of your baseline time window is earlier than the earliest time point in this tfrGND variable, %g.\n', ...
0164                 tfrGND.ftrip.time(1)*1000);
0165         end
0166     end
0167     start_tpt=start_tpt(end)+1;
0168     end_tpt=end_tpt(end);
0169     
0170     bsln_tpts=start_tpt:end_tpt;
0171     if isempty(bsln_tpts),
0172         error('The basline time window you specified does not include any time points in this tfrGND variable.');
0173     end
0174     fprintf('Baseline time window is from %g to %g ms\n',tfrGND.ftrip.time(start_tpt)*1000, ...
0175         tfrGND.ftrip.time(end_tpt)*1000); %converting from sec to ms
0176     
0177     for b=use_bins,
0178         for sub=1:n_sub,
0179             fprintf('Baselining data for Bin %d, Participant %d\n',b,sub);
0180             mn_in_wind=mean(tfrGND.indiv{b}(sub,:,:,start_tpt:end_tpt),4);
0181             if strcmpi(bsln_type,'absolute')
0182                 tfrGND.indiv{b}(sub,:,:,:)=tfrGND.indiv{b}(sub,:,:,:)-repmat(mn_in_wind,[1 1 1 n_tpt]);
0183             else
0184                 %relative baseline
0185                 tfrGND.indiv{b}(sub,:,:,:)=tfrGND.indiv{b}(sub,:,:,:)./repmat(mn_in_wind,[1 1 1 n_tpt]);
0186             end
0187         end
0188     end
0189     tfrGND.bsln_wind=bsln_wind;
0190     tfrGND.bsln_type=bsln_type;
0191 end
0192 
0193 
0194 %reaverage grands
0195 for b=use_bins,
0196     fprintf('Recomputing grand averages for bin %d.\n',b);
0197     tfrGND.grands{b}=squeeze(mean(tfrGND.indiv{b},1));
0198 end
0199    
0200 %Indicate tfrGND variable hasn't been saved since last change
0201 if changed,
0202     tfrGND.saved='no';
0203 end
0204 
0205 %If only a subset of bins transformed, indicate this
0206 if ~isequal(use_bins,1:n_bin),
0207     tfrGND.bsln_type='may vary across bins';
0208     tfrGND.bsln_wind='may vary across bins';
0209     tfrGND.units='may vary across bins';
0210 end

Generated on Tue 10-May-2016 16:37:45 by m2html © 2005