Home > reduc > calcNoiseIndices.m

calcNoiseIndices

PURPOSE ^

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

SYNOPSIS ^

function [off1StartPos, onEndPos, off2EndPos, onStartPos, off1EndPos, off2StartPos, N] = calcNoiseIndices(d)

DESCRIPTION ^

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 function [off1StartPos, onEndPos, off2EndPos, onStartPos, off1EndPos,
 off2StartPos, N] = calcNoiseIndices(d)

 This function finds all the noise diode off/on times in the data
 structure d.
 Inputs:
       data structure d
 Outputs:
       off1StartPos: list of indices of the start of the first noise diode off events      
       onEndPos: list of indices of the end of the noise diode on events
       off2EndPos: list of indices of the end of the second noise diode off events
       onStartPos: list of indices of the start of the noise diode on events
       off1EndPos: list of indices of the end of first noise diode off events
       off2StartPos:  list of indices of the start of second noise diode off event
       N: number of noise diode events

 Created by OGK on 1 March 2011 from MAS's new code.

  SJCM: modified March 18, 2011 to give more outputs, make sure all the
  vectors are the same length -- this is used by many pipeline plotting
  routines and caused the crashes for the time that I was gone.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [off1StartPos, onEndPos, off2EndPos, onStartPos, off1EndPos, off2StartPos, N] = calcNoiseIndices(d)
0002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0003 %
0004 % function [off1StartPos, onEndPos, off2EndPos, onStartPos, off1EndPos,
0005 % off2StartPos, N] = calcNoiseIndices(d)
0006 %
0007 % This function finds all the noise diode off/on times in the data
0008 % structure d.
0009 % Inputs:
0010 %       data structure d
0011 % Outputs:
0012 %       off1StartPos: list of indices of the start of the first noise diode off events
0013 %       onEndPos: list of indices of the end of the noise diode on events
0014 %       off2EndPos: list of indices of the end of the second noise diode off events
0015 %       onStartPos: list of indices of the start of the noise diode on events
0016 %       off1EndPos: list of indices of the end of first noise diode off events
0017 %       off2StartPos:  list of indices of the start of second noise diode off event
0018 %       N: number of noise diode events
0019 %
0020 % Created by OGK on 1 March 2011 from MAS's new code.
0021 %
0022 %  SJCM: modified March 18, 2011 to give more outputs, make sure all the
0023 %  vectors are the same length -- this is used by many pipeline plotting
0024 %  routines and caused the crashes for the time that I was gone.
0025 %
0026 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0027 
0028 % initialize all variables.
0029 off1StartPos = [];
0030 onEndPos = [];
0031 off2EndPos = [];
0032 onStartPos = [];
0033 off1EndPos = [];
0034 off2StartPos = [];
0035 N = 0;
0036 
0037 % Is the features register on the same time scale as the receiver register?
0038 if length(d.array.frame.features) == length(d.antenna0.receiver.utc)
0039     feat = d.array.frame.features;
0040 else
0041     % Interpolate features frame onto time scale of receiver data:
0042     feat = interp1(d.array.frame.utc,...
0043     double(d.array.frame.features),d.antenna0.receiver.utc);
0044 end
0045 
0046 N = 0;
0047 offStartPos = [];
0048 onEndPos = [];
0049 offEndPos = [];
0050 onStartPos = [];
0051 
0052 % Noise diode on:
0053 if(isfield(d, 'index'))
0054   Indon = d.index.noise.fast;
0055 else
0056   Indon = bitand(feat,2^10) > 0;
0057 end
0058 
0059 % Find the start/stop times
0060 diffOn = diff(Indon);
0061 onStartPos = find(diffOn > 0) + 1;
0062 onEndPos = find(diffOn < 0);
0063 
0064 
0065 % No noise events.
0066 if isempty(onStartPos)
0067     return
0068 end
0069 
0070 
0071 % Check to see if we started or stopped in the middle of an event.
0072 if onStartPos(1) > onEndPos(1)
0073     onEndPos = onEndPos(2:end);
0074 end
0075 if onStartPos(end) > onEndPos(end)
0076     onEndPos = cat(1,onEndPos,length(feat));
0077 end
0078 
0079 % Check to see if the noise on events are at least one second long.
0080 onLength = (onEndPos - onStartPos) >= 100;
0081 onStartPos = onStartPos(onLength);
0082 onEndPos = onEndPos(onLength);
0083 
0084 onUTC = d.antenna0.receiver.utc(onStartPos);
0085 
0086 % Prep these arrays:
0087 offStartPos = [];
0088 offEndPos = [];
0089 
0090 % Before we added the 2^11 tag for the Off events.
0091 if onUTC(1) < tstr2mjd('15-May-2010:00:00:00')
0092     
0093     % How many such noise vents do we have?
0094     nRel = sum(onUTC < tstr2mjd('15-May-2010:00:00:00'));
0095     
0096     % Take 3 seconds before the event as the Off state.
0097     offStartPos = cat(1,offStartPos,onStartPos(1:nRel) - 400);
0098     offEndPos = cat(1,offEndPos,onStartPos(1:nRel) - 100);
0099 
0100     % Check to see if the first event is OK.
0101     if offStartPos(1) < 1
0102         
0103         % No remaining noise events.
0104         if length(offStartPos) < 2
0105             return
0106         end
0107         
0108         onStartPos = onStartPos(2:end);
0109         onEndPos = onEndPos(2:end);
0110         offStartPos = offStartPos(2:end);
0111         offEndPos = offEndPos(2:end);
0112         
0113         onUTC = onUTC(2:end);
0114     end
0115 end
0116 
0117 
0118 % Do we have any events which are after the initial dark ages?
0119 if onUTC(end) >= tstr2mjd('15-MAY-2010:00:00:00')
0120     
0121     % Identify the Off states in the same way as we did on the On states.
0122     if(isfield(d, 'index'))
0123         Indoff = d.index.noise_event.fast & ~d.index.noise.fast;
0124     else
0125         Indoff = bitand(feat,2^11) > 0;
0126     end
0127     
0128     % Find the start/stop times
0129     diffOff = diff(Indoff);
0130     offStartPos = cat(1,offStartPos,find(diffOff > 0) + 1);
0131     offEndPos = cat(1,offEndPos,find(diffOff < 0));
0132     
0133 
0134     
0135     % Check to see if we started or stopped in the middle of an event.
0136     if offStartPos(1) > offEndPos(1)
0137         offStartPos = cat(1,1,offStartPos);
0138     end
0139     if offStartPos(end) > offEndPos(end)
0140         offEndPos = cat(1,offEndPos,length(feat));
0141     end
0142     
0143     % Check to see if the noise on events are at least one second long.
0144     offLength = (offEndPos - offStartPos) >= 100;
0145     offStartPos = offStartPos(offLength);
0146     offEndPos = offEndPos(offLength);
0147 
0148 
0149     % Those noise events which are preceded by a noise-off event...
0150     if (onUTC(1) < tstr2mjd('08-SEP-2010:19:00:00')) && ...
0151             (offStartPos(1) > onStartPos(1))
0152         % The first noise on event is not preceded by a noise off event.
0153         onStartPos = onStartPos(2:end);
0154         onEndPos = onEndPos(2:end);
0155     end
0156     if (onUTC(end) < tstr2mjd('08-SEP-2010:19:00:00')) && (offStartPos(end) > onStartPos(end))
0157         % The last noise off event is not followed by a noise on event.
0158         offStartPos = offStartPos(1:end-1);
0159         offEndPos = offEndPos(1:end-1);            
0160     end
0161 
0162         
0163     % Do we have any events which are preceded and
0164     % followed by noise-off events?
0165     if onUTC(end) >= tstr2mjd('08-SEP-2010:19:00:00')
0166 
0167         
0168         nOff = length(offStartPos);
0169         nOn = length(onStartPos);
0170 
0171         % We make arrays which tell us whether to keep or discard a given
0172         % on/off event.
0173         offUsed = zeros(nOff,1);
0174         onUsed = zeros(nOn,1);
0175         
0176         
0177         % Anything prior to the new noise event scheme is given an
0178         % automatic pass.
0179         offUsed(onUTC < tstr2mjd('08-SEP-2010:19:00:00')) = 1;
0180         onUsed(onUTC < tstr2mjd('08-SEP-2010:19:00:00')) = 1;
0181         
0182         
0183         % How many events after the new noise scheme?
0184         nRel = sum(onUTC >= tstr2mjd('08-SEP-2010:19:00:00'));
0185 
0186         
0187         % We now loop over these states and attempt to associate noise off
0188         % events with each noise on event.
0189         for k=(nOn-nRel+1):nOn
0190             
0191             % Check that we have a preceding event:
0192             preOff = find(abs(onStartPos(k) - offEndPos) <= 500,1,'last');
0193             
0194             % Check that we have a following event:
0195             postOff = find(abs(offStartPos - onEndPos(k)) <= 500,1,'first');
0196             
0197 
0198             % Missing preceding or following state.
0199             if isempty(preOff) || isempty(postOff)
0200                 continue;                
0201             % Blended off state.  Split it in half.
0202             elseif offUsed(preOff) == 1
0203                 midPos = floor((offStartPos(preOff) + offEndPos(preOff)) / 2);
0204                 
0205                 offStartPos = cat(1,offStartPos(1:preOff),midPos+1,offStartPos(preOff+1:end));
0206                 offEndPos = cat(1,offEndPos(1:preOff-1),midPos,offEndPos(preOff:end));
0207                 
0208                 offUsed = cat(1,offUsed(1:preOff),1,offUsed(preOff+1:end));
0209                 
0210                 postOff = postOff + 1;
0211             end
0212             
0213             
0214             % We mark the used events as good.
0215             offUsed(preOff) = 1;
0216             offUsed(postOff) = 1;
0217             onUsed(k) = 1;
0218         end        
0219         
0220         % Any events which were not used are discarded.
0221         onStartPos = onStartPos(onUsed == 1);
0222         onEndPos = onEndPos(onUsed == 1);
0223         offStartPos = offStartPos(offUsed == 1);
0224         offEndPos = offEndPos(offUsed == 1);
0225         
0226     end
0227     
0228     % No remaining noise events.
0229     if isempty(onStartPos)
0230         return
0231     end
0232 end
0233 
0234 N = length(onStartPos);
0235 
0236 if(length(onStartPos) ~= length(offStartPos))
0237   % case where we have off_on_off
0238   off1StartPos = offStartPos(1:2:2*N);
0239   off1EndPos   = offEndPos(1:2:2*N);
0240   off2StartPos = offStartPos(2:2:2*N);
0241   off2EndPos   = offEndPos(2:2:2*N);
0242 else
0243   off1StartPos = offStartPos;
0244   off2StartPos = offStartPos;
0245   off1EndPos   = offEndPos;
0246   off2EndPos   = offEndPos;
0247 end
0248 
0249 
0250 return;

Generated on Sun 14-Jun-2015 17:12:45 by m2html © 2005