To solve this problem robustly, we can follow these steps:
- Iterate from the end: Start iterating from the end of the Y_Array to find the downward spike.
- Identify the spike: Once the downward spike is identified, start looking for upward bumps.
- Detect local maxima: Track the local maxima within these bumps.
- Find corresponding X_Array values: Once a bump with a local maximum is detected, find the corresponding X_Array value using the index.
Since you mentioned that the input data is noisy, it is essential to employ a method that can handle fluctuations and prevent the detection of false positives. Here's a possible approach:
- Smooth the data: Although you said to avoid
scipy, smoothing can help manage noise without external libraries:
- A simple way is to use a moving average.
- Determine the local maxima: Define local maxima by comparing each point with its neighbors.
- Thresholds for spikes and bumps: Define thresholds to identify significant spikes and bumps, which can be adjusted based on your data's noise level.
Here is a Python code snippet demonstrating this approach:
def find_local_maximum(X_Array, Y_Array, window_size=3, threshold=0.1):
import numpy as np
# Helper function to perform a simple moving average
def moving_average(data, window_size):
return np.convolve(data, np.ones(window_size)/window_size, mode='valid')
# First, smooth the Y_Array to enhance robustness against noise
smoothed_Y = moving_average(Y_Array, window_size)
# Variables to track spikes and bumps
found_spike = False
local_max_value = -np.inf
local_max_index = -1
# Iterate from the end of the arrays
for i in range(len(smoothed_Y) - 1, 0, -1):
if not found_spike and (smoothed_Y[i] < smoothed_Y[i-1] - threshold):
found_spike = True
if found_spike and (smoothed_Y[i] > smoothed_Y[i-1] + threshold):
# The current point is higher than the previous one (beginning of a bump)
if smoothed_Y[i] > local_max_value:
local_max_value = smoothed_Y[i]
local_max_index = i
# If found local max (at a bump) and it's stabilized (we moved beyond the upward bump)
if found_spike and local_max_index != -1 and smoothed_Y[i] < local_max_value - threshold:
break
if local_max_index == -1:
raise ValueError("Local maximum not found in bumps")
# Map the index back to the original data array (considering the effect of moving average)
original_index = local_max_index + (window_size // 2)
return X_Array[original_index], Y_Array[original_index]
# Example usage
X_Array = np.arange(20) # Simple example X_Array with 20 points
Y_Array = np.array([10, 9, 8, 7, 6, 7, 8, 6, 5, 4, 5, 6, 5, 4, 5, 4, 3, 2, 1, 0])
x_val, y_val = find_local_maximum(X_Array, Y_Array, window_size=3, threshold=0.5)
print(f'Local maximum occurs at X: {x_val} with Y: {y_val}')
Explanation
-
Moving Average:
- We apply a moving average to
Y_Array to smooth it. This helps reduce noise.
-
Identify Downward Spike:
- We iterate from the end of
smoothed_Y to find a significant drop below a threshold (defined as threshold).
-
Detect Bumps and Local Maxima:
- After the spike, we look for upward bumps by checking if the current value is higher than the last. If so, we update the
local_max_value and local_max_index.
- Once a local maximum is detected and we move past it, we break the loop.
-
Original Index Mapping:
- Because the moving average affects the index, we adjust
local_max_index by adding half the window size to get the original index.
Robustness and Adjustments
- Window Size and Threshold: Adjusting the
window_size and threshold parameters according to your data's characteristics can help better handle noise and detect meaningful spikes and bumps.
Feel free to modify this code to better fit your data specifics and the noise characteristics inherent to your measurement context.