0001 function scaleVec = load_scale(d, templateArray, scaleType)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 if scaleType ~= 0
0024 disp('Terribly sorry; only scaling by direct fitting is currently supported.');
0025 end
0026
0027
0028
0029
0030
0031 tScale = 10;
0032 nClip = 5;
0033 clipThresh = 3;
0034
0035
0036
0037 tN = tScale * 100;
0038
0039
0040
0041 nChannels = size(templateArray,2);
0042
0043
0044
0045 [home,installeddir] = where_am_i();
0046 f = open([home '/' installeddir '/reduc/load/filters/load_filter_hp.mat']);
0047
0048
0049
0050 d = noiseRemove(d);
0051
0052
0053
0054
0055 filtData = filter(f.Num, 1, d.antenna0.receiver.data);
0056
0057 filtLen = length(f.Num);
0058 filtData = filtData(filtLen:end,:);
0059 templateArray = templateArray(ceil(filtLen/2):end-floor(filtLen/2),:);
0060
0061 utcVec = d.antenna0.receiver.utc(ceil(filtLen/2):end-floor(filtLen/2));
0062
0063
0064
0065 dataLen = length(filtData);
0066
0067 nSub = floor(dataLen / tN);
0068 subStart = floor((dataLen - nSub * tN) / 2) + 1;
0069 subEnd = dataLen - ceil((dataLen - nSub * tN) / 2);
0070
0071
0072
0073
0074
0075 stdData = squeeze(std( ...
0076 reshape(filtData(subStart:subEnd,:), tN, nSub, nChannels)));
0077 stdUtc = mean(reshape(utcVec(subStart:subEnd), tN, nSub))';
0078
0079
0080
0081 clipVec = zeros(nSub,1);
0082
0083
0084 for l=1:nClip
0085 stdDataStd = std(stdData(clipVec == 0,:));
0086 stdDataMed = median(stdData(clipVec == 0,:));
0087
0088 clipVec = max(abs(stdData - repmat(stdDataMed,nSub,1)) > ...
0089 clipThresh * repmat(stdDataStd,nSub,1),[],2);
0090 end
0091
0092
0093 flagVec = interp1(stdUtc, +(smooth(clipVec,2) > 0), ...
0094 utcVec, 'nearest', 'extrap');
0095
0096
0097
0098 filtData = filtData(flagVec == 0,:);
0099 templateArray = templateArray(flagVec == 0,:);
0100 dfLen = sum(flagVec == 0);
0101
0102
0103 scaleVec = zeros(1,nChannels);
0104 for k=1:nChannels
0105
0106
0107
0108 fitResults = [ones(dfLen,1) templateArray(:,k)] \ filtData(:,k);
0109
0110
0111
0112
0113 scaleVec(k) = fitResults(2);
0114 end
0115
0116 end
0117