Home > matlabmk > baseline_TFR.m

baseline_TFR

PURPOSE ^

baseline_TFR() - Baselines the spectrograms in a TFR variable.

SYNOPSIS ^

function TFR=baseline_TFR(TFR,units,bsln_wind,bsln_type,bin)

DESCRIPTION ^

 baseline_TFR() - Baselines the spectrograms in a TFR variable.
 
 Usage:
  >> TFR=baseline_TFR(TFR,units,bsln_wind,bsln_type,bin);

 Required Inputs:
   TFR       - A MATLABmk TFR variable. These are produced by the
               MATLABmk function set2tfr.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.
 >> TFR=baseline_TFR(TFR,'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.
 >> TFR=baseline_TFR(TFR,'raw',[-500 0],'relative',1);

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


 Author:
 David Groppe
 Kutaslab, 4/2011

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 % baseline_TFR() - Baselines the spectrograms in a TFR variable.
0002 %
0003 % Usage:
0004 %  >> TFR=baseline_TFR(TFR,units,bsln_wind,bsln_type,bin);
0005 %
0006 % Required Inputs:
0007 %   TFR       - A MATLABmk TFR variable. These are produced by the
0008 %               MATLABmk function set2tfr.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 % Notes:
0032 % -If you transform a subset of bins in the TFR variable, the baseline
0033 % fields of the TFR variable will be set to 'may vary across bins'.
0034 %
0035 %
0036 % Examples:
0037 % Using a relative baseline with a baseline window of -500 to 0 ms and raw
0038 % spectral power.  All bins will be affected.
0039 % >> TFR=baseline_TFR(TFR,'raw',[-500 0],'relative');
0040 %
0041 % Using a relative baseline with a baseline window of -500 to 0 ms and raw
0042 % spectral power.  Only Bin 1 will be affected.
0043 % >> TFR=baseline_TFR(TFR,'raw',[-500 0],'relative',1);
0044 %
0045 % Just converting to dB (no change of baseline). All bins will be affected.
0046 % >> TFR=baseline_TFR(TFR,'dB',[],[]);
0047 %
0048 %
0049 % Author:
0050 % David Groppe
0051 % Kutaslab, 4/2011
0052 
0053 
0054 % Notes:
0055 % How baseling could be done with the FieldTrip function ft_freqbaseline
0056 % 1. Set up configuration parameters
0057 % cfg.baseline=[-.5 0];
0058 % cfg.baselinetype='absolute';
0059 %
0060 % 2. Baseline data in Bin 1
0061 % TFR.bins{1}=ft_freqbaseline(cfg,TFR.bins{1});
0062 %
0063 
0064 function TFR=baseline_TFR(TFR,units,bsln_wind,bsln_type,bin)
0065 
0066 n_bin=length(TFR.bins);
0067 n_tpt=length(TFR.bins{1}.time);
0068 changed=0;
0069 
0070 %Error check bin argument
0071 if nargin<5,
0072     %if bin argument not specified, use all bins
0073     use_bins=1:n_bin;
0074 else
0075     if min(bin)<1,
0076         error('All elements of argument bin must be greater than 0.');
0077     elseif max(bin)>n_bin,
0078         error('You can''t baseline Bin %d.  This TFR variable only has % bins.',max(bin),n_bin);
0079     elseif ~isequal(bin,round(bin)),
0080         error('All elements of argument bin need to be integers.');
0081     end
0082     use_bins=bin;
0083 end
0084 
0085 %Make sure user isn't trying to unbaseline data
0086 if strcmpi(bsln_type,'none') && ~strcmpi(TFR.bsln_type,'none'),
0087    error('You can''t unbaseline data that have already been baselined.'); 
0088 end
0089 
0090 %If not specified as input arguments, using current values in TFR variable
0091 if isempty(units),
0092     units=TFR.units;
0093 end
0094 if isempty(bsln_wind),
0095     bsln_wind=TFR.bsln_wind;
0096 end
0097 if isempty(bsln_type),
0098     bsln_type=TFR.bsln_type;
0099 end
0100 
0101 % First adjust units if necessary
0102 if ~strcmpi(units,TFR.units)
0103     changed=1;
0104     if strcmpi(TFR.units,'dB'),
0105         fprintf('Temporarily transforming TFR data from dB to raw power.\n');
0106         if ~strcmpi(TFR.bsln_type,'none'),
0107             watchit(sprintf(['Since the data in this TFR variable were originally baselined, \n', ...
0108                 'converting them to raw power from dB will produce somewhat different values than if you had initially created the TFR variable in units of raw power']));
0109         end
0110         for b=use_bins,
0111             TFR.bins{b}.powspctrm=10.^(TFR.bins{b}.powspctrm/10);
0112         end
0113         TFR.units='raw';
0114     else
0115         fprintf('Temporarily transforming TFR data from raw to dB power.\n');
0116         if ~strcmpi(TFR.bsln_type,'none'),
0117             watchit(sprintf(['Since the data in this TFR variable were originally baselined, \n', ...
0118                 'converting them to dB will produce somewhat different values than if you had converted the data to dB before baselining.']));
0119         end
0120         for b=use_bins,
0121             TFR.bins{b}.powspctrm=10*log10(TFR.bins{b}.powspctrm);
0122         end
0123         TFR.units='dB';
0124     end
0125 end
0126 
0127 
0128 % Next adjust baseline if necessary
0129 if ~isequal(bsln_wind,TFR.bsln_wind) || ~strcmpi(bsln_type,TFR.bsln_type)
0130     changed=1;
0131     
0132     %error checking
0133     if strcmpi(bsln_type,'none'),
0134        error('You cannot un-baseline spectrograms once they''ve been baselined.'); 
0135     end
0136     if isempty(bsln_wind),
0137        error('You cannot specify a baseline type without a baseline time window.'); 
0138     end
0139     if bsln_wind(2)<bsln_wind(1),
0140         error('The start of your baseline time window, %g, is after the end of your baseline time window, %g\n', ...
0141             bsln_wind(1),bsln_wind(2));
0142     end
0143     
0144     %find time points to use for baselining
0145     start_tpt=find(TFR.bins{1}.time<(bsln_wind(1)/1000)); %converting from ms to sec
0146     end_tpt=find(TFR.bins{1}.time<=(bsln_wind(2)/1000)); %converting from ms to sec
0147     if isempty(start_tpt)
0148         if ~isempty(end_tpt),
0149             start_tpt=0;
0150         else
0151             error('The starting point of your baseline time window is earlier than the earliest time point in this TFR variable, %g.\n', ...
0152                 TFR.bins{1}.time(1)*1000);
0153         end
0154     end
0155     start_tpt=start_tpt(end)+1;
0156     end_tpt=end_tpt(end);
0157     
0158     bsln_tpts=start_tpt:end_tpt;
0159     if isempty(bsln_tpts),
0160         error('The basline time window you specified does not include any time points in this TFR variable.');
0161     end
0162     fprintf('Baseline time window is from %g to %g ms\n',TFR.bins{1}.time(start_tpt)*1000, ...
0163         TFR.bins{1}.time(end_tpt)*1000); %converting from sec to ms
0164     
0165     for b=use_bins,
0166         fprintf('Baselining data for Bin %d\n',b);
0167         mn_in_wind=mean(TFR.bins{b}.powspctrm(:,:,start_tpt:end_tpt),3);
0168         if strcmpi(bsln_type,'absolute')
0169             TFR.bins{b}.powspctrm=TFR.bins{b}.powspctrm-repmat(mn_in_wind,[1 1 n_tpt]);
0170         else
0171             %relative baseline
0172             TFR.bins{b}.powspctrm=TFR.bins{b}.powspctrm./repmat(mn_in_wind,[1 1 n_tpt]);
0173         end
0174     end
0175     TFR.bsln_wind=bsln_wind;
0176     TFR.bsln_type=bsln_type;
0177 end
0178 
0179 
0180 %Indicate TFR variable hasn't been saved since last change
0181 if changed,
0182     TFR.saved='no';
0183 end
0184 
0185 %If only a subset of bins transformed, indicate this
0186 if ~isequal(use_bins,1:n_bin),
0187     TFR.bsln_type='may vary across bins';
0188     TFR.bsln_wind='may vary across bins';
0189     TFR.units='may vary across bins';
0190 end

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