MINUS overloads - operator for analysis objects. M Hewitson 05-02-07
0001 function b = minus(varargin) 0002 0003 % MINUS overloads - operator for analysis objects. 0004 % 0005 % M Hewitson 05-02-07 0006 % 0007 0008 0009 % capture input variable names 0010 invars = {}; 0011 for j=1:nargin 0012 iname = inputname(j); 0013 if isempty(iname) & isnumeric(varargin{j}) 0014 iname = num2str(varargin{j}); 0015 elseif isempty(iname) & ischar(varargin{j}) 0016 iname = varargin{j}; 0017 end 0018 invars = [invars cellstr(iname)]; 0019 end 0020 0021 ALGONAME = mfilename; 0022 VERSION = '$Id: minus.html,v 1.1 2007/06/08 14:15:02 hewitson Exp $'; 0023 0024 op = '-'; 0025 0026 [a1,a2,do] = aooperate(varargin, op); 0027 0028 %--------- create output AO 0029 0030 % make a new history object 0031 h = history(ALGONAME, VERSION, [], [a1.hist a2.hist]); 0032 h = set(h, 'invars', invars); 0033 0034 % get names for output 0035 if isempty(char(invars{1})) 0036 n1 = a1.name; 0037 else 0038 n1 = char(invars{1}); 0039 end 0040 if isempty(char(invars{2})) 0041 n2 = a2.name; 0042 else 0043 n2 = char(invars{2}); 0044 end 0045 0046 0047 % make output analysis object 0048 b = ao(do, h); 0049 b = set(b, 'name', sprintf('%s%s%s', n1, op, n2)); 0050 0051 end 0052 % 0053 % % capture input variable names 0054 % invars = {}; 0055 % for j=1:nargin 0056 % invars = [invars cellstr(inputname(j))]; 0057 % end 0058 % 0059 % ALGONAME = mfilename; 0060 % VERSION = '$Id: minus.html,v 1.1 2007/06/08 14:15:02 hewitson Exp $'; 0061 % 0062 % % We can force these to be two analysis objects here so 0063 % % that a=b-1 works. 0064 % % 0065 % % If b or c is a double of length 1, then we grow a new vector the same 0066 % % length as the data in the other. The other possibility is that one of the 0067 % % inputs is a vector the same length as the data vector in the other 0068 % % analysis object. If both are analysis objects, we deal with them below. 0069 % 0070 % if ~isa(b, 'ao') 0071 % cd = c.data; 0072 % if isa(cd, 'tsdata') 0073 % x = tsdata(ones(size(cd.x))*b, cd.fs); 0074 % x = set(x, 'xunits', cd.xunits, 'yunits', cd.yunits, 't0', cd.t0, 'name', num2str(b)); 0075 % bval = b; 0076 % b = ao(x, history('const', '', plist(param('const', b)))); 0077 % b = set(b, 'name', num2str(bval)); 0078 % elseif isa(cd, 'fsdata') 0079 % x = fsdata(ones(size(cd.x))*b, cd.fs); 0080 % x = set(x, 'xunits', cd.xunits, 'yunits', cd.yunits, 't0', cd.t0, 'name', num2str(b)); 0081 % bval = b; 0082 % b = ao(x, history('const', '', plist(param('const', b)))); 0083 % b = set(b, 'name', num2str(bval)); 0084 % else 0085 % error('### analysis object has an unknown data type.'); 0086 % end 0087 % clear cd; 0088 % end 0089 % if ~isa(c, 'ao') 0090 % bd = b.data; 0091 % if isa(bd, 'tsdata') 0092 % x = tsdata(ones(size(bd.x))*c, bd.fs); 0093 % x = set(x, 'xunits', bd.xunits, 'yunits', bd.yunits, 't0', bd.t0, 'name', num2str(c)); 0094 % cval = c; 0095 % c = ao(x, history('const', '', plist(param('const', c)))); 0096 % c = set(c, 'name', num2str(cval)); 0097 % elseif isa(bd, 'fsdata') 0098 % x = fsdata(ones(size(bd.x))*c, bd.fs) 0099 % x = set(x, 'xunits', bd.xunits, 'yunits', bd.yunits, 't0', bd.t0, 'name', num2str(c)); 0100 % cval = c; 0101 % c = ao(x, history('const', '', plist(param('const', c)))); 0102 % c = set(c, 'name', num2str(cval)); 0103 % else 0104 % error('### analysis object has an unknown data type.'); 0105 % end 0106 % clear bd; 0107 % end 0108 % 0109 % % Check both analysis objects have the same data type 0110 % d1 = get(b, 'data'); 0111 % d2 = get(c, 'data'); 0112 % d1info = whos('d1'); 0113 % d2info = whos('d2'); 0114 % 0115 % if d1info.class ~= d2info.class 0116 % error('### two analysis objects have different data types. They can not be multiplied.'); 0117 % end 0118 % 0119 % % Check both data types have the same Y units 0120 % if ~strcmp(d1.xunits, d2.xunits) 0121 % warning('!!! multiplying two time/freq-series objects with different X units'); 0122 % xunits = [d1.xunits '/' d2.xunits]; 0123 % else 0124 % xunits = d1.xunits; 0125 % end 0126 % 0127 % % Check both data types have the same Y units 0128 % if ~strcmp(d1.yunits, d2.yunits) 0129 % warning('!!! subtracting two time/freq-series objects with different Y units'); 0130 % yunits = [d1.yunits '*' d2.yunits]; 0131 % else 0132 % yunits = d1.yunits; 0133 % end 0134 % 0135 % % Which data type do we have 0136 % dtype = d1info.class; 0137 % switch dtype 0138 % case 'tsdata' 0139 % disp('* subtracting two tsdata objects'); 0140 % 0141 % % for now we ignore the time vectors 0142 % if length(d1.x) ~= length(d2.x) 0143 % error('### two data vectors are different lengths. They can not be subtracted.'); 0144 % end 0145 % if length(d1.fs) ~= length(d2.fs) 0146 % error('### two data vectors have different sample rates. They can not be subtracted.'); 0147 % end 0148 % 0149 % % Check both data types have the same time origin 0150 % if ~strcmp(d1.t0, d2.t0) 0151 % warning('!!! subtracting two time-series objects with different time origins'); 0152 % t0 = [d1.t0 '/' d2.t0]; 0153 % else 0154 % t0 = d1.t0; 0155 % end 0156 % 0157 % % make a new tsdata object 0158 % ts = tsdata(d1.x-d2.x, d1.fs); 0159 % ts = set(ts, 'name', sprintf('(%s)-(%s)', d1.name, d2.name)); 0160 % ts = set(ts, 'xunits', xunits); 0161 % ts = set(ts, 'yunits', yunits); 0162 % ts = set(ts, 't0', t0); 0163 % 0164 % % make a new history object 0165 % h = history(ALGONAME, VERSION, [], [b.hist c.hist]); 0166 % h = set(h, 'invars', invars); 0167 % 0168 % % make output analysis object 0169 % a = ao(ts, h); 0170 % % a = set(a, 'name', sprintf('(%s)-(%s)', b.name, c.name)); 0171 % a = set(a, 'name', sprintf('%s-%s', char(invars{1}), char(invars{2}))); 0172 % 0173 % case 'fsdata' 0174 % disp('* multiplying two fsdata objects'); 0175 % 0176 % % for now we ignore the freq vectors 0177 % if length(d1.xx) ~= length(d2.xx) 0178 % error('### two frequency vectors are different lengths. They can not be subtracted.'); 0179 % end 0180 % if length(d1.fs) ~= length(d2.fs) 0181 % error('### two original data vectors have different sample rates. They can not be subtracted.'); 0182 % end 0183 % if d1.enbw ~= d2.enbw 0184 % warning('!!! Equivalent noise bandwidth of the two frequency series does not match.'); 0185 % end 0186 % % make a new tsdata object 0187 % fs = fsdata(d1.f,d1.xx-d2.xx, d1.fs); 0188 % fs = set(fs, 'name', sprintf('(%s)-(%s)', d1.name, d2.name)); 0189 % fs = set(fs, 'xunits', xunits); 0190 % fs = set(fs, 'yunits', yunits); 0191 % fs = set(fs, 'enbw', d1.enbw); 0192 % 0193 % % make a new history object 0194 % h = history(ALGONAME, VERSION, [], [b.hist c.hist]); 0195 % h = set(h, 'invars', invars); 0196 % 0197 % % make output analysis object 0198 % a = ao(fs, h); 0199 % % a = set(a, 'name', sprintf('(%s)-(%s)', b.name, c.name)); 0200 % a = set(a, 'name', sprintf('%s-%s', char(invars{1}), char(invars{2}))); 0201 % 0202 % otherwise 0203 % error('### unknown data type. They can not be addded.') 0204 % end 0205 0206 % END 0207 0208 0209 0210 0211