I'm currently utilizing the find_peaks function to identify peaks within this spectrum. However, despite consulting similar queries on Stack Overflow, my attempts to incorporate features such as prominence and width have been unsuccessful. One peak in particular, labeled as "missing peak" in the image , remains elusive. image

Why I know that is a peak? because i know the spectra, and that that energy correspond to the second level of emission of Niquel that has a peak in 7.48 and other in 8.26. Could anyone suggest a method that might prove effective for identifying such peaks within this type of spectrum?

This is my current code

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
a = -0.0188026396003431
b = 0.039549044037714
shape = np.shape(Data)
if shape == (2048, 1):
    dim = 0
    dim = 1

# Sample function definition for plot_energy_spectrum
def plot_energy_spectrum(Data, a, b,i,ax=None):
    maxim = np.max(Data)
    workingarray = np.squeeze(Data)
    x = a + b * np.linspace(0, 885, 885)
    ax.plot(x, workingarray[:885] / maxim,label=elements[i],color='black', marker='o')

    peaks, _ = find_peaks(workingarray / maxim, height=50,prominence=7,width=0.0756)

    peak_values = workingarray[peaks] / maxim
    peak_indices = peaks
    print( peak_values)
    ax.scatter(a + b * peak_indices, peak_values,color='black', marker='o', s=20)

    for i in range(len(peak_indices)):
        ax.annotate(f"{a + b * peak_indices[i]:.2f} KeV", (a + b * peak_indices[i], peak_values[i]),
                    textcoords="offset points", xytext=(0, 10), ha='center')

    inf.append(x[peak_indices[0] - 5])
    sup.append(x[peak_indices[-1] + 5])
    ax.set_xlabel('Energy (KeV)')
    ax.set_ylabel('Normalized data')
    ax.set_title('Calibrated plot')

   # print(a + b * peak_indices, sep='\n')
   # print(*peak_values, sep='\n')
    energy_spectrum.append(a + b * peak_indices)
    #print(peak_indices, sep='\n')

if dim == 0:
    fig, ax = plt.subplots()
    plot_energy_spectrum(Data[:, 0],a, b,i,ax=ax)
    ax.set_xlim(inf[0], sup[0])

    fig, ax = plt.subplots()
    for i in range(Data.shape[1]):
        plot_energy_spectrum(Data[:, i], a, b,i,ax=ax)  # Pass the ax object to plot_energy_spectrum


And this is the code that I use to prepare the data in order to be analysed, so feel free to use it to open the spectrum data

import os
# Mount Google Drive
from google.colab import drive

# Function to read numeric data from a file
def read_numeric_data(file_path):
    with open(file_path, 'r', encoding='latin1') as f:
        data = []
        live_time = None
        real_time = None
        for line in f:
            if 'LIVE_TIME' in line:
                live_time = line.strip()
            elif 'REAL_TIME' in line:
                real_time = line.strip()
            elif all(char.isdigit() or char in ".-+e" for char in line.strip()):
                row = [float(x) for x in line.split()]
    return np.array(data), live_time, real_time

file_path = '/content/drive/MyDrive/ProjetoXRF_Manuel/April2024/Amostra4.mca'
data, _, _ = read_numeric_data(file_path)

And this is the data

  • 4
    $\begingroup$ I think that's a question you'll have to ask of the authors of the package you are using. But I will point out that the "peak" you label as missing is not in fact a peak! No point there has a value greater than both of its neighbors. $\endgroup$ Commented May 10 at 19:04


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.