我想读取一个 .txt 文件并从中获取数据,然后想出一种方法来找出其中某个 block 中有多少个峰。我有一个盖革计数器,它将运行一段时间,其输出将记录在一个文件中,我将其称为“NuclearCounting.txt”。可以看到前一百行here .您可以看到均值大约为 ~1.7,很少达到 ~3 或下降到 ~1。
然后我想将数据分成一分钟或一秒的 block (在本例中我选择秒)并分析每个时间间隔内发生了多少“事件”。我通过将“事件”称为数据的最大值或最小值来做到这一点。由于背景辐射,有一些噪音,所以我想在那里有一条线说“峰值”必须与平均值相差 3 s.d.如果可能的话。
这是目前的代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
import scipy.stats
x1 = []
y1 = []
f = open("NC.txt", "r")
for line in f:
line = line.strip()
parts = line.split(",") #Separated by commas and spaces
time = float(parts[1]) #This column is the column recording time
value = float(parts[2]) #This one records the value the detector reads.
x1.append(time)
y1.append(value)
f.close()
xv = np.array(x1) #Time
yv = np.array(y1) #Values recorded
#Statistics
m = np.mean(yv)
d = np.std(yv)
timetot = int(round(np.max(xv))) #This gives the total time in seconds to the nearest second. I use this to break it into chunks of one second of data each.
#Counting events
def chunk(l, n):
n = max(1, n)
return [l[k:k+n] for k in range(0, len(l), n)]
t = chunk(xv,timetot) #Breaking the time into chunks of a second
v = chunk(yv,timetot) #Doing the same for the values recorded by the detector
i = []
for x in v:
peak = signal.find_peaks_cwt(x, np.arange(2,10)) #This is the problem area
i.append(peak)
print(len(i))
数组 i 用于存储我得到的“峰”数。我只想知道每个间隔的 i 长度。问题是输出是 python 告诉我我有 25000 个峰。这是没有意义的。每秒,我最多只能得到几个计数。它还只给了我一个数组,其中的条目完全是 15。
有人可以告诉我如何纠正这个问题吗?我不知道为什么 scipy 函数不起作用,当然也不知道为什么在没有数据条目具有该值时它应该吐出 15。
谢谢。
请您参考如下方法:
您代码中的列表 i 不存储每个 block 的峰数,而是存储峰指数。
为了计算峰值,您需要使用 i.append(len(peak))。如果您还想将峰值限制为比中位数大 3 个标准偏差,您可以执行 peak = np.sum(x[signal.find_peaks_cwt(x, np.arange(2,10))] > 3* d)。
最后但同样重要的是 print(len(i)) 将返回峰值列表的长度,即 block 的数量。我认为您想要的是找出每个 block 的峰数。所以,总而言之,我认为这就是您想要的。
for x in v:
peak_index = signal.find_peaks_cwt(x, np.arange(2,10))
peak_sum = np.sum(x[peak_index] > 3*d)
i.append(peak_sum)
for p in i:
print(p)
