Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- classdef XDomainFunction < handle
- %The purpose of this class is to store some basic
- %functions pertaining to linear interpolation
- %
- %function values are allowed through scaling and setting
- %member functions.
- properties (SetAccess = 'private',GetAccess = 'public')
- X = [];
- dX = [];
- fX = [];
- supportPointsAdded = [];
- nMaxValue = [];
- nMinValue = [];
- end
- methods
- function XDF = XDomainFunction(a,b)
- %X
- XDF.X = [min(a,b), max(a,b)];
- %dX
- XDF.dX = XDF.X(end)-XDF.X(1);
- %fX
- XDF.fX = ones(size(XDF.X))/2;
- %support added
- XDF.supportPointsAdded = 2;
- %nMaxValue
- XDF.nMaxValue = 2;
- %nMinValue
- XDF.nMinValue = 1;
- end
- function addPoint(XDF, x, weight)
- if nargin == 2
- weight = 1;
- end
- %pad support
- XDF.padSupportByPoint(x);
- %add linear interpolated weight to function
- XDF.addLinearInterpolatedWeight(x,weight);
- %update statistics
- XDF.updateStatistics(x);%not optimized
- end
- %Read fX
- function [value,nn] = linearInterpolatedValue(XDF, x)
- [leftValue,nLeft] = XDF.leftInterpolatedValue(x);
- [rightValue,nRight] = XDF.rightInterpolatedValue(x);
- nn = [nLeft,nRight];
- value = rightValue + leftValue;
- end
- function [value,nRight] = rightInterpolatedValue(XDF,x)
- if (XDF.X(1) - XDF.dX) <= x && x < XDF.X(end)
- n_float = XDF.getNFloat(x);
- nRight = ceil(n_float);%numerically stable
- value = XDF.fX(nRight)*mod(n_float,1)/XDF.dX;
- else
- nRight = [];
- value = 0;
- end
- end
- function [value,nLeft] = leftInterpolatedValue(XDF,x)
- if XDF.X(1) <= x && x < ( XDF.X(end) + XDF.dX )
- n_float = XDF.getNFloat(x);
- nLeft = floor(n_float);%numerically stable
- value = XDF.fX(nLeft)*(1-mod(n_float,1))/XDF.dX;
- else
- nLeft = [];
- value = 0;
- end
- end
- function n_float = getNFloat(XDF,x)
- n_float = ( x + XDF.dX - XDF.X(1) )/XDF.dX;
- end
- %modify statistics
- function updateStatistics(XDF,x)
- 'UPDATING'
- n_float = XDF.getNFloat(x);
- nn = [floor(n_float),ceil(n_float)];
- display(['maxValue: ', num2str(XDF.maxValue)]);
- display(['minValue: ', num2str(XDF.minValue)]);
- XDF.minValue
- if XDF.fX(nn(1)) > XDF.maxValue %max vs @nn(1)
- 'FOUND1'
- XDF.nMaxValue = nn(1);
- elseif XDF.fX(nn(2)) > XDF.maxValue%max vs @nn(2)
- XDF.nMaxValue = nn(2);'FOUND2'
- end
- if XDF.minValue > XDF.fX(nn(1))%min vs @nn(1)
- XDF.nMinValue = nn(1);'FOUND3'
- elseif XDF.minValue > XDF.fX(nn(2))%min vs @nn(1)
- XDF.nMinValue = nn(2);'FOUND4'
- end
- end
- function mValue = maxValue(XDF)
- mValue = XDF.fX(XDF.nMaxValue);
- end
- function mValue = minValue(XDF)
- mValue = XDF.fX(XDF.nMinValue);
- end
- %Modify fX
- function addLinearInterpolatedWeight(XDF,x,weight)
- n_float = XDF.getNFloat(x);
- nn = [floor(n_float),ceil(n_float)];
- weights = [mod(n_float,1),1-mod(n_float,1)]*weight;
- XDF.setfX(XDF.fX(nn) + weights,nn);
- end
- function setfX(XDF, subfX, nn)
- if nargin == 2
- nn = 1:numel( XDF.X );
- end
- XDF.fX(nn) = subfX;
- end
- function scalefX(XDF, a)
- if a ~= 0
- XDF.fX = XDF.fX*a;
- else
- display('Error: Attenting to divide XDomainFunction.fX instance member by 0');
- end
- end
- %Mod Support
- function padSupportByPoint(XDF,x)
- if x > XDF.X(end)
- %Calculate the number of added points
- N = ceil((x-XDF.X(end))/XDF.dX);
- %pad
- XDF.padPositiveSupport(N);
- elseif x < XDF.X(1)
- %Calculate the number of added points
- N = ceil((XDF.X(1)-x)/XDF.dX);
- %pad
- XDF.padNegativeSupport(N);
- else
- %register no points added
- XDF.supportPointsAdded = 0;
- end
- end
- function padPositiveSupport(XDF,N)
- %Added xLow, xHigh
- xLow = XDF.X(end)+XDF.dX;
- xHigh = XDF.X(end) + N*XDF.dX;
- %pad
- XDF.X = [ XDF.X, xLow:XDF.dX:xHigh ];
- XDF.fX = [ XDF.fX, zeros(1,N) ];
- %register points added
- XDF.supportPointsAdded = N;
- end
- function padNegativeSupport(XDF,N)
- %Added xLow, xHigh
- xLow = XDF.X(1)-N*XDF.dX;
- xHigh = XDF.X(1)-XDF.dX;
- %pad
- XDF.X = [ xLow:XDF.dX:xHigh, XDF.X ];
- XDF.fX = [ zeros(1,N), XDF.fX ];
- %nMaxValue, nMinValue
- XDF.nMaxValue= XDF.nMaxValue + N;
- XDF.nMinValue= XDF.nMinValue + N;
- %register points added
- XDF.supportPointsAdded = -N;
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement