我是靠谱客的博主 调皮小虾米,最近开发中收集的这篇文章主要介绍python中定义数组的最大范围_Python,找到数组的所有局部最大值,调整测量中的缺陷...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

i'm trying to get the maximum points in a one dimensional array, where it makes several curves. To do this i use scipy.signal.argrelextrema, along with np.greater. Give here where the array is y:

argrelextrema(y, np.greater)

The issue is, that this one dimensional data has inaccuracies due to the way the data for y was gathered. And so, there's a lot of "false positives" at the bottom of the curve, where there technically is a maxima at the bottom because of one value being bigger than the surrounding ones.

For reference, here's y, over x which is just the index of each y value, to demonstrate the array i'm working with. The inaccuracies at the bottom is not visible. Ignore axises, used what i had in the code.

Also, here's the result of me using the found maxima to calculate a value, as seen this is not wanted, as the expected result should have been a smooth falling curve. The graph was made with one point for each maxima, in an increasing order. And it's clearly wrong, as one can observe from the actual graph.

So, what's the best solution to avoid shis? I failed to find something that could approximate the graph in a good enough manner for me the be able to use it. I looked into smoothening, but the methods i found, like savgol_filter from scipy.signal, was something i could not understand.

The current solution was to ignore values of y that were below 5, which was roughly a bit over the bottom of the curve, but not an ideal solution at all.

Update:

Found out find_peaks_cwt from scipy.signal works for this too. It's a tad more complex as i have absolutely no clue how most of it works, even after reading up on it a bit. However, i managed to make a slightly better graph, i think, using: find_peaks_cwt(y, [3], noise_perc=2) However, the result seen below, is only a result of me dropping noise from 10 to 2, without really knowing how that affects the result.

Edit:

Here's is the 1D-array i'm working on: https://pastebin.com/GZrBBRce

Sorry for bad representation, but each line is the next value in the list. It's a bit large.

Edit: Minimum working examplem, y is from the pastebin, a bit large to include in minimum working example:

energy = []

for i in find_peaks_cwt(y, [3],noise_perc=2):

energy.append(y[i])

plt.plot([i for i in range(len(energy))], energy)

plt.show()

This was made with some guessing, and the result is seen in the last image in this question.

Update 2: Further progress, i smoothed out the y function using numpy.polyfit with a 15 degree approximation. It's surprisingly accurate. And since that is smooth, i can revert to the first function argrelextrema(y, np.greater), and it's gives me a pretty decent answer as well as not including false positives, as seen in the above graphs. (I got 30-40 maximas, when my graph only has a little above 20 of them.)

I'll let it stand a bit, before marking solved, in case anyone want to have a go at a better solution and approximating the graph with numpy.polyfit. However this solved the issue for my usecase.

解决方案

I would use: scipy.signal.find_peaks_cwt().

From its documentation:

Attempt to find the peaks in a 1-D array.

The general approach is to smooth vector by convolving it with wavelet(width) for each width in widths. Relative maxima which appear at enough length scales, and with sufficiently high SNR, are accepted.

UPDATE (with actual data)

import numpy as np

import scipy as sp

import matplotlib.pyplot as plt

import scipy.signal

y_arr = np.loadtxt('/home/raid1/metere/Downloads/1d_array.txt')

print('array size: ', y_arr.shape)

arr_size = len(y_arr)

expected_num = 30

expected_width = arr_size // expected_num // 2

print('expected width of peaks: ', expected_width)

peaks = sp.signal.find_peaks_cwt(y_arr, np.linspace(2, expected_width, 10))

print('num peaks: ', len(peaks))

print('peaks: ', peaks)

plt.plot(y_arr)

for peak in peaks:

plt.axvline(peak)

plt.show()

This can probably tweaked further, for example to increase the accuracy.

最后

以上就是调皮小虾米为你收集整理的python中定义数组的最大范围_Python,找到数组的所有局部最大值,调整测量中的缺陷...的全部内容,希望文章能够帮你解决python中定义数组的最大范围_Python,找到数组的所有局部最大值,调整测量中的缺陷...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部