Main Content

定义复合的 System object

此示例说明如何定义包含其他 System object 的 System object。基于单独的高通和低通滤波器 System object 定义一个带通滤波器 System object™。

将 System object 存储在属性中

要基于其他 System object 来定义 System object,请将这些其他对象以属性方式存储在类定义文件中。在此示例中,高通滤波器和低通滤波器为它们各自的类定义文件中定义的单独 System object。

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

带通滤波器复合 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

带通滤波器的低通 FIR 组件的类定义文件

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

带通滤波器的高通 FIR 组件的类定义文件

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

另请参阅