在索引周围替换+/-值-MATLAB [英] Replace +/- values around index - MATLAB

查看:84
本文介绍了在索引周围替换+/-值-MATLAB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循此 问题 以及从中获得的宝贵帮助,我已经涉及到以下问题:

Following this question and the precious help I got from it, I've reached to the following issue:

使用检测到的峰的索引并计算出我的信号在这些峰附近的+/- 3个数据点的中值,我需要用先前计算的中值替换我在峰周围的+/- 5窗口中的信号.

Using indices of detected peaks and having computed the median of my signal +/-3 datapoints around these peaks, I need to replace my signal in a +/-5 window around the peak with the previously computed median.

我只能用中位数代替峰上的数据点,而不能用周围的+/- 5个数据点代替...见图.黑色=原始峰;黄色=峰值处的数据点变为其周围+/- 3个数据点的中值.

I'm only able replace the datapoint at the peak with the median, but not the surrounding +/-5 data points...see figure. Black = original peak; Yellow = data point at peak changed to the median of +/-3 datapoints around it.

原始峰值和已更改峰值

不幸的是,我无法通过遵循上一个问题的建议来使其发挥作用.

Unfortunately I have not been able to make it work by following suggestions on the previous question.

任何帮助将不胜感激!

干杯, M

推荐答案

假设您的意思如下.给定数组

Assuming you mean the following. Given the array

x = [0 1 2 3 4 5 35 5 4 3 2 1 0]

您要用3,4,5,35,5,4,3的中位数4替换35和+/- 5个条目,因此结果数组应为

you want to replace 35 and surrounding +/- 5 entries with the median of 3,4,5,35,5,4,3, which is 4, so the resulting array should be

x = [0 4 4 4 4 4 4 4 4 4 4 4 0]


按照我在这个问题中的回答,一种直观的方法是通过抵消指数来简单地用中间值代替邻居.这可以通过以下方式完成


Following my answer in this question an intuitive approach is to simply replace the neighbors with the median value by offsetting the indicies. This can be accomplished as follows

[~,idx]=findpeaks(x);
med_sz = 3;    % Take the median with respect to +/- this many neighbors
repl_sz = 5;   % Replace neighbors +/- this distance from peak
if ~isempty(idx)
    m = medfilt1(x,med_sz*2+1);
    N = numel(x);
    for offset = -repl_sz:repl_sz
        idx_offset = idx + offset;
        idx_valid = idx_offset >= 1 & idx_offset <= N;
        x(idx_offset(idx_valid)) = m(idx(idx_valid));
    end
end

或者,如果您想避免循环,则等效的无环实现是

Alternatively, if you want to avoid loops, an equivalent loopless implementation is

[~,idx]=findpeaks(x);
med_sz = 3;
repl_sz = 5;
if ~isempty(idx)
    m = medfilt1(x,med_sz*2+1);
    idx_repeat = repmat(idx,repl_sz*2+1,1);
    idx_offset = idx_repeat + repmat((-repl_sz:repl_sz)',1,numel(idx));
    idx_valid = idx_repeat >= 1 & idx_repeat <= numel(x);
    idx_repeat = idx_repeat(idx_valid);
    idx_offset = idx_offset(idx_valid);
    x(idx_offset) = m(idx_repeat);
end

这篇关于在索引周围替换+/-值-MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆