Advertisement
x89codered89x

XDomainFunction.m

Dec 14th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 6.21 KB | None | 0 0
  1. classdef XDomainFunction < handle
  2.     %The purpose of this class is to store some basic  
  3.     %functions pertaining to linear interpolation
  4.     %
  5.     %function values are allowed through scaling and setting
  6.     %member functions.
  7.    
  8.     properties (SetAccess = 'private',GetAccess = 'public')
  9.         X = [];
  10.         dX = [];
  11.         fX = [];
  12.         supportPointsAdded = [];
  13.        
  14.         nMaxValue = [];
  15.         nMinValue = [];      
  16.     end
  17.     methods
  18.         function XDF = XDomainFunction(a,b)
  19.             %X            
  20.             XDF.X = [min(a,b), max(a,b)];
  21.  
  22.             %dX
  23.             XDF.dX = XDF.X(end)-XDF.X(1);
  24.            
  25.             %fX
  26.             XDF.fX = ones(size(XDF.X))/2;
  27.            
  28.             %support added
  29.             XDF.supportPointsAdded = 2;          
  30.            
  31.             %nMaxValue
  32.             XDF.nMaxValue = 2;
  33.  
  34.             %nMinValue
  35.             XDF.nMinValue = 1;                        
  36.         end            
  37.         function addPoint(XDF, x, weight)
  38.             if nargin == 2
  39.                 weight = 1;
  40.             end
  41.  
  42.             %pad support
  43.             XDF.padSupportByPoint(x);
  44.  
  45.             %add linear interpolated weight to function
  46.             XDF.addLinearInterpolatedWeight(x,weight);  
  47.                        
  48.             %update statistics
  49.             XDF.updateStatistics(x);%not optimized
  50.         end        
  51.        
  52.         %Read fX
  53.         function [value,nn] = linearInterpolatedValue(XDF, x)
  54.             [leftValue,nLeft] = XDF.leftInterpolatedValue(x);
  55.             [rightValue,nRight] = XDF.rightInterpolatedValue(x);
  56.            
  57.             nn = [nLeft,nRight];    
  58.             value = rightValue + leftValue;            
  59.         end      
  60.         function [value,nRight] = rightInterpolatedValue(XDF,x)
  61.             if (XDF.X(1) - XDF.dX) <= x && x < XDF.X(end)                
  62.                 n_float = XDF.getNFloat(x);
  63.                 nRight = ceil(n_float);%numerically stable                
  64.                 value = XDF.fX(nRight)*mod(n_float,1)/XDF.dX;                    
  65.             else
  66.                nRight = [];                
  67.                value = 0;
  68.             end
  69.         end        
  70.         function [value,nLeft] = leftInterpolatedValue(XDF,x)
  71.             if XDF.X(1) <= x && x < ( XDF.X(end) + XDF.dX )        
  72.                 n_float = XDF.getNFloat(x);
  73.                 nLeft = floor(n_float);%numerically stable                  
  74.                 value = XDF.fX(nLeft)*(1-mod(n_float,1))/XDF.dX;                    
  75.             else
  76.                nLeft = [];                
  77.                value = 0;
  78.             end    
  79.         end
  80.        
  81.         function n_float = getNFloat(XDF,x)
  82.            n_float = ( x + XDF.dX - XDF.X(1) )/XDF.dX;
  83.         end
  84.         %modify statistics
  85.         function updateStatistics(XDF,x)
  86.             'UPDATING'
  87.             n_float = XDF.getNFloat(x);
  88.             nn = [floor(n_float),ceil(n_float)];
  89.            
  90.             display(['maxValue: ', num2str(XDF.maxValue)]);
  91.             display(['minValue: ', num2str(XDF.minValue)]);            
  92.             XDF.minValue
  93.             if XDF.fX(nn(1)) > XDF.maxValue %max vs @nn(1)
  94.                 'FOUND1'
  95.                 XDF.nMaxValue = nn(1);
  96.             elseif XDF.fX(nn(2)) > XDF.maxValue%max vs @nn(2)
  97.                 XDF.nMaxValue = nn(2);'FOUND2'
  98.             end
  99.             if XDF.minValue > XDF.fX(nn(1))%min vs @nn(1)
  100.                 XDF.nMinValue = nn(1);'FOUND3'
  101.             elseif XDF.minValue > XDF.fX(nn(2))%min vs @nn(1)
  102.                 XDF.nMinValue = nn(2);'FOUND4'            
  103.             end
  104.         end
  105.         function mValue = maxValue(XDF)
  106.            mValue = XDF.fX(XDF.nMaxValue);            
  107.         end
  108.         function mValue = minValue(XDF)
  109.            mValue = XDF.fX(XDF.nMinValue);            
  110.         end    
  111.         %Modify fX
  112.         function addLinearInterpolatedWeight(XDF,x,weight)            
  113.             n_float = XDF.getNFloat(x);
  114.             nn = [floor(n_float),ceil(n_float)];    
  115.             weights = [mod(n_float,1),1-mod(n_float,1)]*weight;
  116.             XDF.setfX(XDF.fX(nn) + weights,nn);            
  117.         end        
  118.         function setfX(XDF, subfX, nn)
  119.             if nargin == 2
  120.                 nn = 1:numel( XDF.X );
  121.             end
  122.            
  123.             XDF.fX(nn) = subfX;
  124.         end        
  125.         function scalefX(XDF, a)
  126.             if a ~= 0
  127.                 XDF.fX = XDF.fX*a;
  128.             else
  129.                display('Error: Attenting to divide XDomainFunction.fX instance member by 0');
  130.             end
  131.         end
  132.        
  133.         %Mod Support
  134.         function padSupportByPoint(XDF,x)
  135.             if x > XDF.X(end)
  136.                 %Calculate the number of added points
  137.                 N = ceil((x-XDF.X(end))/XDF.dX);
  138.                
  139.                 %pad
  140.                 XDF.padPositiveSupport(N);
  141.             elseif x < XDF.X(1)
  142.                 %Calculate the number of added points
  143.                 N = ceil((XDF.X(1)-x)/XDF.dX);
  144.                
  145.                 %pad
  146.                 XDF.padNegativeSupport(N);                
  147.             else                
  148.                 %register no points added
  149.                 XDF.supportPointsAdded = 0;
  150.             end
  151.         end        
  152.         function padPositiveSupport(XDF,N)
  153.             %Added xLow, xHigh
  154.             xLow = XDF.X(end)+XDF.dX;
  155.             xHigh = XDF.X(end) + N*XDF.dX;
  156.            
  157.             %pad
  158.             XDF.X = [ XDF.X, xLow:XDF.dX:xHigh ];
  159.             XDF.fX = [ XDF.fX, zeros(1,N) ];
  160.            
  161.             %register points added
  162.             XDF.supportPointsAdded = N;
  163.         end
  164.         function padNegativeSupport(XDF,N)
  165.             %Added xLow, xHigh
  166.             xLow = XDF.X(1)-N*XDF.dX;
  167.             xHigh = XDF.X(1)-XDF.dX;
  168.            
  169.             %pad
  170.             XDF.X = [ xLow:XDF.dX:xHigh, XDF.X ];
  171.             XDF.fX = [ zeros(1,N), XDF.fX ];
  172.            
  173.             %nMaxValue, nMinValue
  174.             XDF.nMaxValue= XDF.nMaxValue + N;
  175.             XDF.nMinValue= XDF.nMinValue + N;            
  176.            
  177.             %register points added
  178.             XDF.supportPointsAdded = -N;
  179.         end        
  180.     end
  181. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement