【问题标题】:MATLAB: signal clipping method skips array indexes?MATLAB:信号裁剪方法跳过数组索引?
【发布时间】:2016-08-03 16:00:08
【问题描述】:

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

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

代码:

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)

谁能给点建议?

【问题讨论】:

    标签: arrays matlab if-statement for-loop clipping


    【解决方案1】:

    假设您想从大小为 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 索引到最后,生成的逻辑数组。

    【讨论】:

      【解决方案2】:

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

      %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
      

      【讨论】:

        猜你喜欢
        • 2012-11-08
        • 1970-01-01
        • 2018-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-30
        • 2017-02-07
        • 2013-03-08
        相关资源
        最近更新 更多