Main Content

Define Composite System Objects

This example shows how to define System objects that include other System objects. Define a bandpass filter System object™ from separate highpass and lowpass filter System objects.

Store System Objects in Properties

To define a System object from other System objects, store those other objects in your class definition file as properties. In this example, the highpass and lowpass filters are the separate System objects defined in their own class-definition files.

properties (Access = private)
   % Properties that hold filter System objects
   pLowpass
   pHighpass
end

Complete Class Definition File of Bandpass Filter Composite System Object

classdef BandpassFIRFilter < matlab.System
% Implements a bandpass filter using a cascade of eighth-order lowpass
% and eighth-order highpass FIR filters.
    
    properties (Access = private)
        % Properties that hold filter System objects
        pLowpass
        pHighpass
    end
    
    methods (Access = protected)
        function setupImpl(obj)
            % Setup composite object from constituent objects
            obj.pLowpass = LowpassFIRFilter;
            obj.pHighpass = HighpassFIRFilter;
        end
        
        function yHigh = stepImpl(obj,u)
            yLow = obj.pLowpass(u);
            yHigh = obj.pHighpass(yLow);
        end
        
        function resetImpl(obj)
            reset(obj.pLowpass);
            reset(obj.pHighpass);
        end
    end
end

Class Definition File for Lowpass FIR Component of Bandpass Filter

classdef LowpassFIRFilter < matlab.System
% Implements eighth-order lowpass FIR filter with 0.6pi cutoff

  properties (Nontunable)
  % Filter coefficients
    Numerator = [0.006,-0.0133,-0.05,0.26,0.6,0.26,-0.05,-0.0133,0.006];
  end
    
  properties (DiscreteState)
    State
  end
    
  methods (Access = protected)
    function setupImpl(obj)
      obj.State = zeros(length(obj.Numerator)-1,1);
    end

    function y = stepImpl(obj,u)
      [y,obj.State] = filter(obj.Numerator,1,u,obj.State);
    end

    function resetImpl(obj)
      obj.State = zeros(length(obj.Numerator)-1,1);
    end
  end
end

Class Definition File for Highpass FIR Component of Bandpass Filter

classdef HighpassFIRFilter < matlab.System
% Implements eighth-order highpass FIR filter with 0.4pi cutoff

  properties (Nontunable)
  % Filter coefficients
    Numerator = [0.006,0.0133,-0.05,-0.26,0.6,-0.26,-0.05,0.0133,0.006];
  end
    
  properties (DiscreteState)
    State
  end
    
  methods (Access = protected)
    function setupImpl(obj)
      obj.State = zeros(length(obj.Numerator)-1,1);
    end

    function y = stepImpl(obj,u)
      [y,obj.State] = filter(obj.Numerator,1,u,obj.State);
    end

    function resetImpl(obj)
      obj.State = zeros(length(obj.Numerator)-1,1);
    end
  end
end

See Also