classdef (StrictDefaults)slexSysIdentMATLABSystemLMSAdaptSysObj < matlab.System
%slexSysIdentMATLABSystemLMSAdaptSysObj Least mean squares (LMS) adaptive filtering. 
%

%#codegen 
% Copyright 2013 The MathWorks, Inc.

%% Properties
  properties
      % Mu Step Size
      Mu = 0.005;
  end

  properties (Nontunable)
      % TrueCoefficients Actual Filter Coefficients
      TrueCoefficients = 0;
      
      % NumCoefficients Number of Filter Coefficients
      NumCoefficients = 32;
  end

  
  properties (DiscreteState) 
      X;
      H;
  end
  
  methods
    function this = slexSysIdentMATLABSystemLMSAdaptSysObj(varargin)
        setProperties(this, nargin, varargin{:});
    end
  end
  
  methods(Static, Access=protected)
      
      function header = getHeaderImpl
          header = matlab.system.display.Header(...
              'slexSysIdentMATLABSystemLMSAdaptSysObj', ...
              'Title', 'LMS Adaptive Filter');
      end
      
      function groups = getPropertyGroupsImpl
          firstGroup = matlab.system.display.SectionGroup(...
              'Title', 'General', ...
              'PropertyList', {'Mu'});
            
          secondGroup = matlab.system.display.SectionGroup(...
              'Title', 'Coefficients', ...
              'PropertyList', {'TrueCoefficients', 'NumCoefficients'});
            
          groups = [firstGroup, secondGroup];
      end
  
  end
  
  methods(Access=protected)

      function setupImpl(obj, ~, ~)
          obj.X = zeros(obj.NumCoefficients,1);
          obj.H = zeros(obj.NumCoefficients,1);
      end
      
      function resetImpl(obj)
          obj.X = zeros(obj.NumCoefficients,1);
          obj.H = zeros(obj.NumCoefficients,1);
      end
      
      function [y, e_norm] = stepImpl(this, d, u)
          tmp = this.X(1:this.NumCoefficients-1);
          this.X(2:this.NumCoefficients,1) = tmp;
          this.X(1,1) = u;
          y = this.X'*this.H;
          e = d-y;
          this.H = this.H+this.Mu*e*this.X;
          e_norm = norm(this.TrueCoefficients'-this.H);
      end
    
      function num = getNumInputsImpl(~)
          num = 2;
      end
    
      function num = getNumOutputsImpl(~)
          num = 2;
      end

      function icon = getIconImpl(~)
          icon = sprintf('LMS Adaptive\nFilter');
      end
        
        function [name1, name2] = getInputNamesImpl(~)
            name1 = 'Desired';
            name2 = 'Actual';
        end
        
        function [name1, name2] = getOutputNamesImpl(~)
            name1 = 'Output';
            name2 = 'EstError';
        end
        
  end
end
