MATLAB:信号限幅方法跳过数组索引?

MATLAB: signal clipping method skips array indexes?

问题: 我大致隔离了方波脉冲信号两侧的基线数据点。我现在想将这些数据点数组从 'pulse end' 剪辑到它们各自的均值(因为我仍然有一些来自信号上升和下降的数据点)。

如您所见,我的方法似乎在从数组末尾开始并向后裁剪时有效,但在评估从数组开头开始的数组索引时似乎跳过了数据点。

代码:

baseline1=[1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,3,4;1:18];
baseline2=[4,3,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1;32:49];
%---------------------------------------------------
figure()
hold on
plot(baseline1(2,:),baseline1(1,:),'k')
plot(baseline2(2,:),baseline2(1,:),'k')
%---------------------------------------------------
%Clip baseline1
arrayMean = mean(baseline1(1,:));
x=size(baseline1,2);
meanCrossed = 0;
arrayClipped = baseline1;
for i=x:-1:1 
    if baseline1(1,i)>arrayMean && meanCrossed == 0
        arrayClipped(:,i)=[];
    else
        meanCrossed = 1;
    end
end
baseline1=arrayClipped;
%---------------------------------------------------
%Clip baseline2
arrayMean = mean(baseline2(1,:));
x=size(baseline2,2);
meanCrossed = 0;
arrayClipped = baseline2;
for i=1:x
    if baseline2(1,i)>arrayMean && meanCrossed == 0
        arrayClipped(:,i)=[];
    else
        meanCrossed = 1;
    end
end
baseline2=arrayClipped;
%---------------------------------------------------
plot(baseline1(2,:),baseline1(1,:),'g','LineWidth',2)
plot(baseline2(2,:),baseline2(1,:),'g','LineWidth',2)

有人可以指点一下吗?

假设您想从大小为 4 的向量中删除索引 4,然后删除索引 2。4 消失,所以现在它的大小为 3。2 消失,使其大小为 2:

1 2 3 4
1 2 3
1 3

现在假设您要先删除索引 2,然后再删除索引 4。2 消失了,所以现在大小为 3。现在我们删除索引 4....稍等!

1 2 3 4
1 3 4

索引 4 不存在,因为我们缩短了数组。以前称为第 4 个元素的元素现在位于索引 3。

通过删除元素,您将更改从那时起所有元素的索引。解决方案:计算时保存所有索引,然后将其全部删除:

for ...
  clipped_indices(end+1) = i;
end

arrayClipped(:, clipped_indices) = [];

或者,为了更简洁、更快速的实施,根本不要 arrayClipped。相反,创建一个包含所有正确大小的逻辑数组,并在找到要剪辑的元素时将它们归零,然后在末尾使用结果逻辑数组索引 baseline2

我的工作解决方案受到答案的启发:

%Data points on the rise and fall of the square wave are recursively clipped to the
    %baseline of their respective arrays. With each loop the mean is reassessed
    %and the end of the array clipped to the new mean. 
    %Clip baseline1
    sizeStable = 0; %set flag
    while sizeStable == 0
        arrayMean = mean(baseline1(1,:)); %(re)calculate mean
        x=size(baseline1,2); %(re)calculate size
        meanCrossed = 0; %(re)set 
        for i=x:-1:1 %from x to 1 in steps of -1
            if baseline1(1,i)>arrayMean && meanCrossed == 0 %if the data point is greater than the mean and the mean has not been crossed before
                baseline1(:,i)=0; %make the entire column at that index zero
            else
                meanCrossed = 1;
            end
        end
        baseline1( :, ~any(baseline1,1) ) = [];  %delete columns with zeros
        if size(baseline1,2) == x
            sizeStable = 1;
        end
    end