Home > classes > @ao > private > aooperate.m

aooperate

PURPOSE ^

AOOPERATE implements specified operator overload for AOs.

SYNOPSIS ^

function [a1,a2,do] = aooperate(varargin)

DESCRIPTION ^

 AOOPERATE implements specified operator overload for AOs.
 
 M Hewitson 16-03-07
 
 $Id: aooperate.html,v 1.14 2008/03/31 10:27:37 hewitson Exp $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [a1,a2,do] = aooperate(varargin)
0002 
0003 % AOOPERATE implements specified operator overload for AOs.
0004 %
0005 % M Hewitson 16-03-07
0006 %
0007 % $Id: aooperate.html,v 1.14 2008/03/31 10:27:37 hewitson Exp $
0008 %
0009 
0010 ALGONAME = mfilename;
0011 VERSION  = '$Id: aooperate.html,v 1.14 2008/03/31 10:27:37 hewitson Exp $';
0012 
0013 % should be possible to operate with
0014 %     cdata and tsdata - only need to be same length if length(cdata)>1
0015 % or  cdata and fsdata - only need to be same length if length(cdata)>1
0016 % or  cdata and cdata
0017 %
0018 % ** how do we deal with units for these different types?
0019 %  - only compare units when types are the same
0020 
0021 op       = varargin{end};
0022 varargin = varargin{1:end-1};
0023 
0024 % % Extract AOs
0025 % aos = [];
0026 % pls = [];
0027 % for j=1:length(varargin)
0028 %   if isa(varargin{j}, 'ao')
0029 %     aos = [aos varargin{j}];
0030 %   elseif isa(varargin{j}, 'plist')
0031 %     pls = [pls varargin{j}];
0032 %   else
0033 %   end
0034 % end
0035 
0036 nargin   = length(varargin);
0037 
0038 as = [];
0039 for j=1:nargin
0040   a = varargin{j};
0041   if isa(a, 'ao')
0042     if any(size(a) > 1)
0043       error('### The operation works only with single analysis objects. Do not use vector or matrices of analysis objects.')
0044     end
0045     as  = [as a];
0046   elseif isnumeric(a)
0047     % create an ao from the values
0048     as = [as ao(a)];
0049   elseif isa(a, 'plist')
0050     val = find(a, 'Value');
0051     if ~isempty(val)
0052       as = [as ao(val)];
0053     end
0054   else
0055     error('### AO/plus inputs must be either numeric, plist(''Value''), or AO.');
0056   end
0057 end
0058 
0059 if length(as) > 2
0060   warning('!!! I only work with the first two input AOs. Ignoring all others.');
0061   warning('!!! Perhaps you have input an AO array?');
0062 end
0063 
0064 %%%%%%%%%%   Now we have two AOs   %%%%%%%%%%
0065 a1 = as(1);
0066 a2 = as(2);
0067 clear as;
0068 d1 = a1.data;
0069 d2 = a2.data;
0070 
0071 
0072 %%%%%%%%%%   Check types   %%%%%%%%%%
0073 if (isa(d1, 'fsdata') && isa(d2, 'tsdata')) || (isa(d1, 'tsdata') && isa(d2, 'fsdata'))
0074   error('### can''t add time-series data to frequency-series data.');
0075 end
0076 if (isa(d1, 'xydata') && isa(d2, 'tsdata')) || (isa(d1, 'tsdata') && isa(d2, 'xydata'))
0077   error('### can''t add XY data to time-series data.');
0078 end
0079 if (isa(d1, 'xydata') && isa(d2, 'fsdata')) || (isa(d1, 'fsdata') && isa(d2, 'xydata'))
0080   error('### can''t add XY data to frequency-series data.');
0081 end
0082 
0083 %%%%%%%%%%   get data   %%%%%%%%%%
0084 [x1, y1] = get_xy_values(a1.data);
0085 [x2, y2] = get_xy_values(a2.data);
0086 ny1 = length(y1);
0087 ny2 = length(y2);
0088 
0089 % Some special cases
0090 % switch op
0091 %   case '*'
0092 %     if size(y1) == size(y2)
0093 %       y1 = y1.';
0094 %     end
0095 % end
0096 
0097 
0098 %%%%%%%%%%   check data lengths   %%%%%%%%%%
0099 if isa(d1, 'cdata') && ~isa(d2, 'cdata')
0100   if ny1 > 1 && ny1~=ny2
0101     error('###1 cdata type must be same length as other AO, or single valued.');
0102   end
0103 elseif isa(d2, 'cdata') && ~isa(d1, 'cdata')
0104   if ny2 > 1 && ny1~=ny2
0105     error('###2 cdata type must be same length as other AO, or single valued.');
0106   end  
0107 end
0108 
0109 if ny1~=ny2
0110   if ny1~=1 && ny2~=1
0111     error('### data vectors must be the same length or one should be single valued.');
0112   end
0113 end
0114 
0115 %%%%%%%%%%   check units   %%%%%%%%%%
0116 if isa(d1, 'fsdata') && isa(d2, 'fsdata')
0117   if ~strcmp(d1.xunits, d2.xunits)
0118     error('### xunits must be the same for same type of AOs.');
0119   end
0120   % check X-axis
0121   if d1.x ~= d2.x
0122     error('### frequency vectors are not the same.');
0123   end
0124 end
0125 if isa(d1, 'tsdata') && isa(d2, 'tsdata')
0126   if ~strcmp(d1.xunits, d2.xunits)
0127     error('### xunits must be the same for same type of AOs.');
0128   end
0129 end
0130 if isa(d1, 'xydata') && isa(d2, 'xydata')
0131   if ~strcmp(d1.xunits, d2.xunits)
0132     error('### xunits must be the same for same type of AOs.');
0133   end
0134 end
0135 
0136 %%%%%%%%%%   Add the data   %%%%%%%%%%
0137 % disp(sprintf('*** applying %s', op))
0138 eval(sprintf('y = y1%sy2;', op));
0139 
0140 %%%%%%%%%%   Create output data object   %%%%%%%%%%
0141 
0142 if isa(d1, 'fsdata') || isa(d2, 'fsdata')
0143   % then we create an output fsdata
0144   % select which object gives properties to
0145   % the output object
0146   if isa(d1, 'fsdata')
0147     d = d1;
0148   else
0149     d = d2;
0150   end
0151   % then we create an output tsdata
0152   do = fsdata(d.x, y, d.fs);
0153   do = set(do, 'name', sprintf('(%s) %s (%s)', d1.name, op, d2.name));
0154   do = set(do, 'xunits', d.xunits);
0155   do = set(do, 'yunits', d.yunits);
0156   do = set(do, 'enbw', d.enbw);
0157 
0158 elseif isa(d1, 'tsdata') || isa(d2, 'tsdata')
0159   % select which object gives properties to
0160   % the output object
0161   if isa(d1, 'tsdata')
0162     d = d1;
0163   else
0164     d = d2;
0165   end
0166   % then we create an output tsdata
0167   do = d;
0168   do = set(do, 'y', y);
0169   do = set(do, 'name', sprintf('(%s) %s (%s)', d1.name, op, d2.name));
0170   
0171 elseif isa(d1, 'xydata') || isa(d2, 'xydata')
0172   % select which object gives properties to
0173   % the output object
0174   if isa(d1, 'xydata')
0175     d = d1;
0176   else
0177     d = d2;
0178   end
0179   % then we create an output xydata
0180   do = xydata(d.x, y);
0181   do = set(do, 'name', sprintf('(%s) %s (%s)', d1.name, op, d2.name));
0182   do = set(do, 'xunits', d.xunits);
0183   do = set(do, 'yunits', d.yunits);
0184   
0185 elseif isa(d1, 'cdata') || isa(d2, 'cdata')
0186   % then we create an output cdata
0187   % select which object gives properties to
0188   % the output object
0189   if isa(d1, 'cdata')
0190     d = d1;
0191   else
0192     d = d2;
0193   end
0194   % then we create an output cdata
0195   do = cdata(y);
0196   do = set(do, 'name', sprintf('(%s) %s (%s)', d1.name, op, d2.name));
0197   do = set(do, 'xunits', d.xunits);
0198   do = set(do, 'yunits', d.yunits);
0199   
0200 else
0201   error('### unknown data type - 2');
0202 end
0203 
0204   
0205 % END

Generated on Mon 31-Mar-2008 12:20:24 by m2html © 2003