在MATLAB中将值分配给邻居相同的值

时间:2017-10-09 08:38:44

标签: matlab statistics signal-processing data-analysis

我有一个小问题,但我无能为力。有人可以指导我正确的方式吗?提前谢谢。

我做了什么。

我的代码找到了本地最大值。

从局部最大值降低到某一点。

分配大于缩小值的邻居,缩小点的值。

小例子

X = [1 0 1 4.3 4.5 5 4.3 4.2 0 0 0 2 6.2 6.3 7 6.2 7.4 8 7.2 1 2 3 4 2];

本地最大值为5,7,8和4

走到某一点。像4,6,7,3一样。

分配值大于4,6,7,3的相邻值。

输出就像

X = [1 0 1 4 4 4 4 4 0 0 0 2 6 6 6 6 6 6 6 1 2 3 3 2];

由于这是一个简单的示例,因此当我在其上应用代码时没有太大区别。

我提供了另一个代码示例。请看一下。

 t = 50;
 X = sin(2*pi*10*(1:t)/t) + 0.5*sin(2*pi*5*(1:50)/t) - linspace(0,3,50);

 % plot(X)
 A= X;
 %   A1 = [1 0 1 4.3 4.5 5 4.3 4.2 0 0 6 6 6 6 6 6 6 6  6 1 2 3 4 2];
 [pks,locs] = findpeaks(A);
 idx = A(locs)-0.3  ; % will bring local maxima to the point where we want 

 for b = 1: numel(idx)  % loop for checking idx 
 for c = 1:numel(A)  % loop for checking neighbourhoods

  if (A(locs(1,b)-c)> idx(1,b) && A(locs(1,b)+c)> idx(1,b))

  else  
      d= c-1;
      z(b)= d;
     break
  end


  end
  end
  for i = 1:numel(idx)

  for n = 1:z(1,i)
       idx_1 = A(locs(i) - n); % will find what is in neighbourhood of local   maxima
        idx_2 = A(locs(i) + n);
 %         idx_1 = A(locs - n); % will find what is in neighbourhood of local maxima
 %         idx_2 = A(locs + n);
    if (idx_1>idx(1,i) && (idx_2>idx(1,i)))  % Check and assign the same value to neighbourhood of local maxima
       A(locs(1,i)) = idx(i);
       A(locs(1,i)-n) = idx(i);
       A(locs(1,i)+n) = idx(i);
%            A(i)) = A(idx(i))
   else if (idx_1 < idx(1,i) && (idx_2>idx(1,i)))
        A(locs(1,i)) = idx(i);
        A(locs(1,i)+n) = idx(i);   
       else if (idx_1 > idx(1,i) && (idx_2<idx(1,i)))
        A(locs(1,i)) = idx(i);
        A(locs(1,i)-n) = idx(i);        
       else
        A(locs(1,i)) = idx(i); 
        A(locs(1,i)-n) = idx_1(i);
        A(locs(1,i)+n) = idx_1(i);
       end
      end

  end
  end
         A(locs(1,i)) = idx(i);
  end

  figure(1)
  hold on 
  plot(X,'r')
  plot(A,'b')
  hold off

预期输出类似于图片中的黑线,而不是由代码形成的蓝线

enter image description here

0 个答案:

没有答案