Search code examples
pythonmathspectra

Integrate data with specific x range


I have multiple text files that have data in two columns: energy & intensity. I would like to be able to integrate the data between two specific values in energy (e.g., integrate between 8900 and 9000).

I am able to integrate over the entire range of energy but not between two specific values.

area_value =[]
for i in formatname:
    format_name= i
    energy,intensity=np.loadtxt(format_name,usecols=(0,1),unpack=True)
    area_value.append(np.trapz(intensity,energy))
print (area_value)

Solution

  • You could just add a condition for the energy range that interests you like:

    my_condition = (energy > 8900) & (energy < 9000) 
    

    And then in your loop:

    my_energy = energy[condition]
    my_intensity = intensity [condition]
    

    Then calculate the area using those values.