Home > database >  How to get the highest Two peak values from a discrete sampled data point using python?
How to get the highest Two peak values from a discrete sampled data point using python?

Time:04-08

I have Radar Data for a vehicle going further from it, The Radar outputs a .csv file. Once The Radar detects something, the amplitude column switches from a 0 to a one and starts outputting values, plotting it across. For example, here: enter image description here

When the column for Distance/Amplitude goes from 0 to a number, it can be inferred that target has been seen by the Radar. So plotting the row of the first instance gives out this blue wave

enter image description here

If we plot the rows below it, We get this,

enter image description here

the Radar was placed in the back, so the target was moving away from it. The x-axis represents distance multiplied by .077 m. So, for the first blue wave, the distance that the Radar registers for 37*.077m. I was wondering if there is a way where I can get a range of values from the .csv file to take into account of the two peaks, for example: I was wondering how I could get the top two peaks from the blue wave, get the x-axis coordinates for them and then get a median point for them and track it for the orange, which is the second row below the first one.

enter image description here

I have attached below the .csv file. https://drive.google.com/file/d/1IJOebiXuScjLPytemulcXph7ZB1X65wU/view?usp=sharing

I have an algorithm that gets the index of the first hit and the last hit, for example when switching from a 0 to a value and a value to a zero, these allow me to catch the when the radar detects a target. This was helpful while I was using the values directly given by the Radar, like the distance and amplitude values, but now that I need a whole row, I don't know how to proceed with this. I don't know if Pandas or Numpy has ways I can utilize to deal with this

CodePudding user response:

There are a few ways to get peaks, and thus get the two peak positions. Get the derivative of the data set. The points where the derivative data intersects the x-axis will be your peaks and valleys of the original data. While doing that, you can also grab the indices of those peaks and valleys. From there, you can iterate through those points in the original data to get the two maximum values, and their indices.

It would look something like this:



import matplotlib.pyplot as plt
import numpy as np


# My data set (example is a crazy cosine wave)
x = np.linspace(1, 100, 1000)
y = np.cos(x*0.5)*((x - 50)**3)

# Find first derivative:
m = np.diff(y)/np.diff(x)

# Get indicies of peaks and valleys
c = len(m)
indices = []
for i in range(1, c):
    prev_val = m[i-1]
    val = m[i]
    if prev_val < 0 and val >= 0:
        indices.append(i)
    elif prev_val > 0 and val <= 0:
        indices.append(i)

# Get the values, positions, and indicies of the two peaks
max_list = [0, 0]
index_list = [0, 0]
pos_list = [0, 0]
for index in indices:
    val = y[index]
    if val > max_list[0]:
        max_list[0] = val
        index_list[0] = index
        pos_list[0] = x[index]
    elif val > max_list[1]:
        max_list[1] = val
        index_list[1] = index
        pos_list[1] = x[index]


print('Two peak indices:', index_list)
print('Two peak values:', max_list)
print('Two peak x-positions:', pos_list)

average_pos = (pos_list[0]   pos_list[1])/2

print('Average x-position:', average_pos)



plt.plot(x, y)
plt.show()
  • Related