Define Composite System Objects

This example shows how to define System objects that include other System objects.

This example defines a filter System object™ from an FIR System object and an IIR System object.

Store System Objects in Properties

To define a System object from other System objects, store those objects in your class definition file as properties. In this example, FIR and IIR are separate System objects defined in their own class-definition files. You use those two objects to calculate the pFir and pIir property values.

properties (Nontunable, Access = private)
   pFir % Store the FIR filter
   pIir % Store the IIR filter
end
    
methods
   function obj = Filter(varargin)
      setProperties(obj,nargin,varargin{:});
      obj.pFir = FIR(obj.zero);
      obj.pIir = IIR(obj.pole);
   end
end

Complete Class Definition File of Composite System Object

classdef Filter < matlab.System
% Filter System object with a single pole and a single zero
%
% This System object illustrates composition by 
% composing an instance of itself.
%

    properties (Nontunable)
      zero = 0.01
      pole = 0.5
    end
    
    properties (Nontunable,Access = private)
      pZero % Store the FIR filter
      pPole % Store the IIR filter
    end
    
    methods
        function obj = Filter(varargin)
            setProperties(obj,nargin,varargin{:});
            % Create instances of FIR and IIR as 
            % private properties
            obj.pZero = Zero(obj.zero);
            obj.pPole = Pole(obj.pole);
        end
    end
    
    methods (Access = protected)
        function setupImpl(obj,x)
            setup(obj.pZero,x);
            setup(obj.pPole,x);
        end

        function resetImpl(obj)
            reset(obj.pZero);
            reset(obj.pPole);
        end

        function y = stepImpl(obj,x)
            y = step(obj.pZero,x) + step(obj.pPole,x);
        end
        function releaseImpl(obj)
            release(obj.pZero);
            release(obj.pPole);
        end
    end    
end

Class Definition File for IIR Component of Filter

classdef Pole < matlab.System
   
  properties
    Den = 1
  end
  
  properties (Access = private)
    tap = 0
  end
  
  methods
    function obj = Pole(varargin)
      setProperties(obj,nargin,varargin{:},'Den');
    end
  end
  
  methods (Access = protected)
    function y = stepImpl(obj,x)
      y = x + obj.tap * obj.Den;
      obj.tap = y;
    end
  end
  
end

Class Definition File for FIR Component of Filter

classdef Zero < matlab.System
  
  properties
    Num = 1
  end
  
  properties (Access = private)
    tap = 0
  end
  
  methods
    function obj = Zero(varargin)
      setProperties(obj,nargin,varargin{:},'Num');
    end
  end
  
  methods (Access = protected)
    function y = stepImpl(obj,x)
      y = x + obj.tap * obj.Num;
      obj.tap = x;
    end
  end
  
end

See Also

Was this topic helpful?