查找最接近值的numpy数组的所有索引(Find all indexes of a numpy array closest to a value)

编程入门 行业动态 更新时间:2024-10-24 18:17:15
查找最接近值的numpy数组的所有索引(Find all indexes of a numpy array closest to a value)

在numpy数组中,需要最接近给定常量的所有值的索引。 背景是数字信号处理。 该数组保持滤波器的幅度函数( np.abs(np.fft.rfft(h)) )并搜索某些频率(=索引),其中幅度为例如0.5或在另一种情况下为0.大多数时间值有问题的不完全包含在序列中。 关闭值的索引应在此中找到。

到目前为止,我想出了以下方法,其中我看到了序列和常量之间差异的符号变化。 然而,这仅适用于在所讨论的点处单调增加或减少的序列。 它有时也会偏离1。

def findvalue(seq, value): diffseq = seq - value signseq = np.sign(diffseq) signseq[signseq == 0] = 1 return np.where(np.diff(signseq))[0]

我想知道是否有更好的解决方案。 它仅适用于1D实数浮点数组,在我的情况下,计算效率的要求不是很高。

作为一个数值例子,下面的代码应该返回[8, 41] 。 为简单起见,我用半波代替了滤波器幅度响应。

f=np.sin(np.linspace(0, np.pi)) findvalue(f, 0.5)

我发现的类似问题如下,但它们只返回第一个或第二个索引: 找到第二个最接近的索引值 在numpy数组中查找最接近的值

In a numpy array the indexes of all values closest to a given constant are needed. The background is digital signal processing. The array holds the magnitude function of a filter (np.abs(np.fft.rfft(h))) and certain frequencies (=indexes) are searched where the magnitude is e.g. 0.5 or in another case 0. Most the time the value in question is not included exactly in the sequence. The index of the closed value should be found in this.

So far I came up with the following method where I look at the change in sign of the difference between the sequence and the constant. However, this only works for sequences which are monotonically increasing or decresasing at the points in question. It also is off by 1 sometimes.

def findvalue(seq, value): diffseq = seq - value signseq = np.sign(diffseq) signseq[signseq == 0] = 1 return np.where(np.diff(signseq))[0]

I'm wondering if there is a better solution for this. It is only for 1D real float arrays and the requirement of the computation efficiency is not so high in my case.

As a numerical example the following code should return [8, 41]. I replaced the filter magnitude response with a half-wave for simplicity here.

f=np.sin(np.linspace(0, np.pi)) findvalue(f, 0.5)

Similar questions I found are as following but they do only return the first or second index: Find the second closest index to value Find nearest value in numpy array

最满意答案

以下函数将返回一个小数索引,显示大约何时超过该值:

def FindValueIndex(seq, val): r = np.where(np.diff(np.sign(seq - val)) != 0) idx = r + (val - seq[r]) / (seq[r + np.ones_like(r)] - seq[r]) idx = np.append(idx, np.where(seq == val)) idx = np.sort(idx) return idx

逻辑:查找seq-val符号的变化。 取转换下方和上方的一个索引值并插入。添加到此索引,其中值实际上等于该值。

如果你想要一个整数索引,只需使用np.round。 您也可以选择np.floor或np.ceil将索引四舍五入到您的首选项。

def FindValueIndex(seq, val): r = np.where(np.diff(np.sign(seq - val)) != 0) idx = r + (val - seq[r]) / (seq[r + np.ones_like(r)] - seq[r]) idx = np.append(idx, np.where(seq == val)) idx = np.sort(idx) return np.round(idx)

The following function will return a fractional index showing approximately when the value is crossed:

def FindValueIndex(seq, val): r = np.where(np.diff(np.sign(seq - val)) != 0) idx = r + (val - seq[r]) / (seq[r + np.ones_like(r)] - seq[r]) idx = np.append(idx, np.where(seq == val)) idx = np.sort(idx) return idx

The logic: Find where sign of seq - val is changing. Take the value one index below and above the transition and interpolate.Add to this index where the value is actually equals the value.

If you want an integer index just use np.round. You can also choose np.floor or np.ceil to round the index to your preference.

def FindValueIndex(seq, val): r = np.where(np.diff(np.sign(seq - val)) != 0) idx = r + (val - seq[r]) / (seq[r + np.ones_like(r)] - seq[r]) idx = np.append(idx, np.where(seq == val)) idx = np.sort(idx) return np.round(idx)

更多推荐

本文发布于:2023-07-25 10:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1260011.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   最接近   索引   numpy   closest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!