function estimated_current = current_estimator(lambda_data,current_data,max_current,desired_lambda)
%
%    Copyright (c) 2014 by Plexim GmbH All rights reserved.
%
%    A free license is granted to anyone to use this software for any legal
%    non safety-critical purpose, including commercial applications,
%    provided that: 1) IT IS NOT USED TO DIRECTLY OR INDIRECTLY COMPETE
%    WITH PLEXIM, and 2) THIS COPYRIGHT NOTICE IS PRESERVED in its
%    entirety.
%
%    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
%    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
%    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
%    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
%    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
%    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
%    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%
% This function takes in real data of flux linkage and current,
% designated as inputs lambda_data and current_data. The function is used
% to estimate the value of the currents at a desired value of flux
% linkage, designated as input desired_lambda.
%
% The model first employs a linear estimator on a a linear range of flux
% linkage values. This is done in order to produce a better and smoother
% fit when using a spline fit in order to produce the estimated current
% values at the desired flux linkage values.
%
% The user also needs to provide the maximum allowable current value that
% will be estimated. This parameter is used to cap the estimated current
% values.
%
% Inputs:
%
%  lambda_data: Monotonically increasing column vector. Must be the same
%  length as the current_data input.
%
%  current_data: Column vector of current values. It is assumed that each
%  entry in this vector corresponds to the flux linkage value in the input
%  lambda_data. Must be the same length as lambda_data.
%
%  max_current: This is a scalar value that is used to cap the estimated
%  current values.
%
%  desired_lambda: Column vector of the desired flux linkage values that
%  the current needs to be estimated. Can be a vector of different length
%  than lambda_data. Must be monotonically increasing.
%
% Outputs:
%
%  estimated_current: Vector of estimated current values corresponding to
%  the input vector desired flux linkage values. Returns a column vector of
%  the same size as desired_lambda.
%
% Plexim, Inc.
% Author: Munadir A. Ahmed
% Date: 07/17/2014