-1

I must write a function that allows me to find the local max and min from a series of values.

Data for function is x, y of each "peak". Output are 4 vectors that contain x, y max and min "peaks".

To find max peaks, I must "stand" on each data point and check it is mare or less than neighbors on both sides in order to decide if it is a peak (save as max/min peak). Points on both ends only have 1 neighbor, do not consider those for this analysis.

Then write a program to read a data file and invoke the function to calculate the peaks. The program must generate a graph showing the entered data with the calculated peaks.

1st file is an Array of float64 of (2001,) size. All data is in column 0. This file represents the amplitude of a signal in time, frequency of sampling is 200Hz. Asume initial time is 0.

Graph should look like this

Program must also generate an .xls file that shows 2 tables; 1 with min peaks, and another with max peaks. Each table must be titled and consist of 2 column, one with the time at which peaks occur, and the other with the amplitude of each peak.

No Pandas allowed.

first file is a .txt file, and is single column, 2001 rows total
0
0.0188425
0.0376428
0.0563589
0.0749497
0.0933749
0.111596
0.129575
0.147277
0.164669
0.18172
...

Current attempt:

import numpy as np
import matplotlib.pyplot as plt

filename = 'location/file_name.txt'
T = np.loadtxt(filename,comments='#',delimiter='\n')

x = T[::1]  # all the files of column 0 are x vales

a = np.empty(x, dtype=array)
y = np.linspace[::1/200]

X, Y = np.meshgrid(x,y)
  • This question lacks focus, as it has multiple questions (1) mark local max (2) do a bunch of stuff related to excel. Questions without focus will likely be closed. [edit] the post to contain only a single question. Also note that StackOverflow is not a coding service and review [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions). – Trenton McKinney Nov 24 '21 at 00:13
  • You don't need a meshgrid for this. You just iterate through the values, checking to see if the current value is greater than both its neighbors, or less than both its neighbors. – Tim Roberts Nov 24 '21 at 00:13
  • The primary question has many answer on SO: [python find local maxima site:stackoverflow.com](https://www.google.com/search?q=python+find+local+maxima+site:stackoverflow.com&rlz=1C1ONGR_enUS976US976&sxsrf=AOaemvKd-T7SKiyALqc-756pOV9xpSB6Gw:1637712870174&sa=X&sqi=2&ved=2ahUKEwiG__ru26_0AhVviP0HHYb2Cn0QrQIoBHoECA0QBQ&biw=2560&bih=1335&dpr=1) and [How to add outliers as separate colored markers to a line plot](https://stackoverflow.com/q/70027498/7758804) and [Matplotlib scatter plot with different text at each data point](https://stackoverflow.com/q/14432557/7758804) – Trenton McKinney Nov 24 '21 at 00:16
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Nov 28 '21 at 11:17

1 Answers1

0

This does what you ask. I had to generate random data, since you didn't share yours. You can surely build your spreadsheet from the minima and maxima values.

import numpy as np
import matplotlib.pyplot as plt

#filename = 'location/file_name.txt'
#T = np.loadtxt(filename,comments='#',delimiter='\n')
#
#y = T[::1]  # all the files of column 0 are x vales

y = np.random.random(200) * 2.0

minima = []
maxima = []
for i in range(0,y.shape[0]-1):
    if y[i-1] < y[i] and y[i+1] < y[i]:
        maxima.append( (i/200, y[i]) )
    if y[i-1] > y[i] and y[i+1] > y[i]:
        minima.append( (i/200, y[i]) )

minima = np.array(minima)
maxima = np.array(maxima)
print(minima)
print(maxima)

x = np.linspace(0, 1, 200 )

plt.plot( x, y )
plt.scatter( maxima[:,0], maxima[:,1] )
plt.show()
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30