Main Content

複合 System object の作成

この例では、System object™ を他の System object で構成して作成する方法を示します。この System object は 2 つの移動平均 System object を使用して、2 つの独立したサンプルの相互相関を求めます。移動平均 System object の作成の例で、System object の作成方法を詳細に説明しています。この例では、System object を他の System object の内部で使用する方法に重点を置いています。

プライベート プロパティとしての System object

ある System object の複数のインスタンスを作成して、それぞれのインスタンスに各自の状態の管理を任せられることが、関数よりも System object を使用する最大のメリットの 1 つです。プライベート プロパティ MovingAverageFilter1 および MovingAverageFilter2 は、2 つの移動平均フィルター オブジェクトの格納に使用されます。

properties (Access=private)
    % This example class contains two moving average filters (more can be added
    % in the same way)
    MovingAverageFilter1
    MovingAverageFilter2
end

移動平均フィルターの設定

setupImpl メソッド内で、2 つの移動平均 System object を作成し、それらのパブリック プロパティを初期化します。

function setupImpl(obj,~)
    % Set up moving average objects with default values
    obj.MovingAverageFilter1 = movingAverageFilter('WindowLength',obj.WindowLength1);
    obj.MovingAverageFilter2 = movingAverageFilter('WindowLength',obj.WindowLength2);
end

依存プロパティの操作

この例で、movingAverageFilter System object のパブリック プロパティ WindowLength"依存" プロパティとして実装されています。

properties(Nontunable,Dependent)
    % WindowLength Moving window length
    WindowLength1;
    WindowLength2;
end

いずれかの依存プロパティに値を代入すると、対応する移動平均フィルターでその値が設定されます。いずれかの依存プロパティを読み取る場合、その値は対応する移動平均フィルターから読み取られます。

function set.WindowLength1(obj,WindowLength1)
    % Set the window length of one moving average filter
    obj.MovingAverageFilter1.WindowLength = WindowLength1;
end
function WindowLength = get.WindowLength1(obj)
    % Read window length from one of the moving average filters
    WindowLength = obj.MovingAverageFilter1.WindowLength;
end
function set.WindowLength2(obj,WindowLength2)
    % Set the window length of one moving average filter
    obj.MovingAverageFilter2.WindowLength = WindowLength2;
end
function WindowLength = get.WindowLength2(obj)
    % Read window length from one of the moving average filters
    WindowLength = obj.MovingAverageFilter2.WindowLength;
end

MATLAB での相互相関オブジェクトの使用

確率変数を作成し、その移動平均の相互相関を計算してから、結果をステム プロットに表示します。

x = rand(20,1);
y = rand(20,1);
crossCorr = crossCorrelationMovingAverages('WindowLength1',1,'WindowLength2',5);

for iter = 1:100
    x = rand(20,1);
    y = rand(20,1);
    [corr,lags] = crossCorr(x,y);
    stem(lags,corr)
end