1

I plot the frequency on x-axis and intensity on y-axis via matplotlib.

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import argrelmax, argrelmin

data = np.loadtxt('pr736024.dat')
z=np.array(data[:,1])   
y = (z-z.min())/(z.max()-z.min()) #for normalization

x = np.loadtxt('frq.txt')

plt.plot(x,y, linewidth = 0.2, color='black')
plt.plot(x,indexes, "o")
plt.show()

enter image description here

I want to get the intensity value at the peaks (there are 6 peaks which can be seen in plot). How can I achieve this?

DavidG
  • 24,279
  • 14
  • 89
  • 82
  • query your plot for anything above `0.6`. – Adelin Jul 11 '18 at 10:22
  • Very much related with: https://stackoverflow.com/questions/31070563/find-all-local-maxima-and-minima-when-x-and-y-values-are-given-as-numpy-arrays/31073798 – BcK Jul 11 '18 at 10:24

1 Answers1

1

This answer is an adaptation of my answer in here.

Here you have a numpythonic solution (which is much better than doing a loop explicitly).

You have to define a threshold where where above this value the maxima are detected, that in your case can be 0.2.

I use the roll function to shift the numbers +1 or -1 in the position. Also a "peak" is defined as a local maximum, where the previous and posterior number are smaller then the central value.

The full code is below:

import numpy as np
import matplotlib.pyplot as plt

# input signal
x = np.arange(1,100,1)
y = 0.3 * np.sin(t) + 0.7 * np.cos(2 * t) - 0.5 * np.sin(1.2 * t)
threshold = 0.2

# max
maxi = np.where(np.where([(y - np.roll(y,1) > 0) & (y - np.roll(y,-1) > 0)],y, 0)> threshold, y,np.nan)
DavidG
  • 24,279
  • 14
  • 89
  • 82
nunodsousa
  • 2,635
  • 4
  • 27
  • 49
  • maxi = np.where(np.where([(y - np.roll(y,1) > 0) & (y - np.roll(y,-1) > 0)],y, 0)> threshold, y,np.nan) with these code I got 5 values which are corresponding to peak values, while I have 6 peaks. [plot] (https://i.imgur.com/2u1EMNd.png) – Hamza Hanif Jul 11 '18 at 15:20