使用滞后对信号进行阈值处理以降低噪声

时间:2018-02-06 07:50:04

标签: matlab optimization signal-processing octave noise-reduction

我正在使用简单的阈值来处理噪声数据,以检测信号中的零点/边缘。由于噪音非常强,我使用滞后来改善结果。虽然这有很大帮助,但它也会减慢很多。有没有办法改善这一点,甚至可能是更好的计算方法?目前我正在使用直接循环方法:

% generate a signal
t = linspace(0, 10, 1000);
y = sin(2 * pi * t); 

% constants
threshold = 0;
hyst = 0.1;

% thresholding
yth = zeros(size(y));
for i = 2:length(y)
    if yth(i - 1) > 0.5 
        yth(i) = y(i) > (threshold - hyst);
    else
        yth(i) = y(i) > (threshold + hyst);
    end 
end

yth = y > threshold相比,这要慢得多。

1 个答案:

答案 0 :(得分:1)

通过预先计算yth(i)的两种可能性,可以获得改进(减少25%的时间;仅适用于大输入;在R2017b上):

function yth = idea1()
  yth = false(size(y)); % change #1 - `false` vs `zeros`
  c1 = y > (th - hyst); % change #2 - precompute c1, c2
  c2 = y > (th + hyst);

  for k = 2:numel(y)
      if yth(k - 1) 
          yth(k) = c1(k);
      else
          yth(k) = c2(k);
      end 
  end
end

通常,矢量化可以获得很大的改进。为了对这个问题进行矢量化,我们必须理解切换逻辑,所以让我们把它写成文字:

  • 模式2 开始。
  • 模式1 :从c1获取值。只要c1包含true值,请接受这些值。遇到false值时,请切换到模式2
  • 模式2 :从c2获取值。只要c2包含false值,请接受这些值。遇到true值时,请切换到模式1

因此,如果我们能够找到转换位置,我们几乎已经完成了。

经过一些试验和错误,当我无法以改善性能的方式摆脱循环时,我确实得出结论:(idea2)。此外,查看RLE编码的正确yth,我想出了一些相当不错的近似值(idea3idea4) - 尽管这些将需要调整其他输入。

也许有人可以用它来找到一个更聪明的实现,减少冗余计算。我的完整代码如下。 RLE编码算法改编自this answer和RLE解码from here

function q48637952
% generate a signal
t = linspace(0, 10, 1000).';
y = sin(2 * pi * t); 

% constants
th = 0;
hyst = 0.1;

%% Comaprison:
% Correctness:
R = {originalIdea(), idea1(), idea2()};
assert(isequal(R{:}));
% Runtime:
T = [timeit(@originalIdea,1), timeit(@idea1,1), timeit(@idea2,1)];
disp(T);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function yth = originalIdea()
  yth = zeros(size(y));
  for i = 2:length(y)
      if yth(i - 1) > 0.5 
          yth(i) = y(i) > (th - hyst);
      else
          yth(i) = y(i) > (th + hyst);
      end 
  end
end

function yth = idea1()
  yth = false(size(y)); % change #1 - `false` vs `zeros`
  c1 = y > (th - hyst); % change #2 - precompute c1, c2
  c2 = y > (th + hyst);

  for k = 2:numel(y)
      if yth(k - 1) 
          yth(k) = c1(k);
      else
          yth(k) = c2(k);
      end 
  end
end

function yth = idea2()
  c = [y > (th - hyst), y > (th + hyst)];
  % Using Run-length encoding:
  [v1,l1] = rlEncode(c(:,1));
  [v2,l2] = rlEncode(c(:,2));
  rle = cat(3,[v1 l1 cumsum(l1)],[v2 l2 cumsum(l2)]);
  % rle(:,:,2) is similar to rle(:,:,1) with small changes:
  %  - col1 is circshifted by 1 and 
  %  - col2 has the 1st and last elements switched
  newenc = reshape([rle(1:2:end,3,2), rle(1:2:end,3,1)].', [], 1);
  yth = rlDecode(rle(:,1,2), [newenc(1); diff(newenc(1:end-1))]);
end

function yth = idea3()
  % Approximation of yth, should be almost indistinguishable with high resolution
  yth = [0 0 repmat(repelem([1,0],50), 1, ceil(numel(y)/(2*50)) )].';
  % The amount of zeros at the beginning as well as the value "50" are problem-specific 
  % and need to be computed using signal-specific considerations
  yth = yth(1:1000);
end

function yth = idea4()
  % Another approximation
  yth = circshift(y > th, 1);
  % The value "1" is problem-specific
end

end % q48637952

%% RLE (de)compression:
function [vals, lens] = rlEncode(vec)
  J = find(diff([vec(1)-1; vec(:)]));
  vals = vec(J);
  lens = diff([J; numel(vec)+1]);
end

function vec = rlDecode(vals, lens)
  l = cumsum([ 1; lens(:) ]);
    z = zeros(1, l(end)-1);
    z(l(1:end-1)) = 1;
    vec = vals(cumsum(z));
end