function [lambda_d_lookup,lambda_q_lookup,Id_lookup,Iq_lookup,lambda_dq_I_dq_data]=Induct2Current(gamma_rad, I_rms, L_d, L_q, Inductance_refine_factor,Current_refine_factor,windowSize)
%
%    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.
%
% The Induct2Current function converts L_d and L_q data obtained as a
% function of the rotor angle and rms current value into Id and Iq look-up
% tables as a function of the flux in the d- and q-axes. This calculated
% information is used to create look-up tables for the d- and q-axis
% currents as a function of the flux in the d- and q-axes.
%
% Inputs:
%
% gamma_rad: nx1 column vector containing angle information at which the
% d- and q- axis inductances are measured. Advance angle should be in the
% range between 0 and pi/2 electrical angle.
%
% I_rms: mx1 column vector containing the rms current information at which
% the d- and q-axis inductances are measured. The I_rms current is assumed
% to be positive.
%
% L_d (L_q): mxn matrix with values of measured d-axis (q-axis) inductances
% at different angle and current values. Each row corresponds to inductances
% measured at different angles for a fixed rms current value. Each column
% represents inductances measured at different rms current values for a fixed
% rotor position.
%
% Inductance_refine_factor: Specified as a real, nonnegative, integer
% scalar. This value specifies the number of times to repeatedly divide the
% intervals of the refined grid in each dimension. This results in 2^k-1
% interpolated points between inductance values, angles, and current data
% provided as inputs. A value of 0 will result in the data entered as is.
% The refinement factor is limited to a maximum value of 3.
% Inductance_refine_factor is used in an INTERP2 function.
%
% Current_refine_factor: Specified as a real, nonnegative, integer scalar.
% This value specifies the number of times to repeatedly divide the
% intervals of the refined grid in each dimension. This results in 2^k-1
% interpolated points between inductance d- and q-axis currents and flux
% linkage values. A value of 0 will result in the data entered as is. The
% refinement factor is limited to a maximum value of 3. The
% Current_refine_factor is used in an INTERP2 function.
%
% windowSize: Corresponds to the averaging length of the averaging vector.
% A windowSize of 3 will create an averaging vector that averages three
% consecutive entries. Must be an odd integer. If an even integer is used
% then it is incremented by 1 to make it odd.
%
% WARNING: Chosing a large windowSize will cause significant deviation from
% the original data. A windowSize of 3,5,7 or 9 is recommended.
%
%   See also CONV, INTERP2.
%
% Outputs:
%
% lambda_d_lookup (lambda_q_lookup): kx1 column vector the stores the
% d-axis (q-axis) flux values that will be used for the d-axis (q-axis)
% current look-up tables.
%
% Id_lookup (Iq_lookup): kxk matrix that stores the d-axis (q-axis)
% estimated current information as a function of flux in the d-axis and
% q-axis.
%
% lambda_dq_I_dq_data: A 4 column matrix that stores the raw calculated
% values obtained from the inductance tables, I_rms current, and angle
% information. The user can check this information to verify the validity
% of the estimated current values.

% Plexim, Inc.
% Author: Munadir A. Ahmed
% Date: 07/17/2014