问题描述
dm-script 提供了拟合指数背景的功能,例如:
image src := GetFrontimage()
number bEs = 340 // eV
number bEe = 390 // eV
image signal := EELSSubtractPowerLawBackground(src,bEs,bEe)
然而,对于弱信号,我们希望通过拟合两个窗口背景来提高信噪比,例如:
有谁知道如何通过脚本来适应这种背景? 提前致谢。
解决方法
感谢 BmyGuest,但我想构建一个全自动程序。 幸运的是,我找到了一个简单的写下脚本的解决方案。 代码如下:
// reference:
// https://towardsdatascience.com/mathematics-for-machine-learning-linear-regression-least-square-regression-de09cf53757c
// key information:
// y = mx + b
// where m = sum ((xdata-xmean)*(ydata-ymean)) / sum ((xdata-xmean)**2)
// b = ymean - m*xmean
class FittingTwoWindowBkg : object {
FittingTwoWindowBkg(object self) {
Result("Script object 'FittingTwoWindowBkg' ["+self.ScriptObjectGetID().hex()+"] constructed.\n");
};
~FittingTwoWindowBkg(object self) {
Result("Script object 'FittingTwoWindowBkg' ["+self.ScriptObjectGetID().hex()+"] destructed.\n");
};
image TwoWindowBkgFitting(object self,image src,number Eng1,number Rng1,number Eng2,number Rng2) {
number Eorignal = src.ImageGetDimensionOrigin(2)
number Estep = src.ImageGetDimensionScale(2)
number sx,sy,sz
src.Get3DSize(sx,sz)
image axis = src.ImageClone()
axis = iplane*Estep + Eorignal
axis.SetName("Energy axis")
// the power law bkg I = A E^-r can be simplified to
// the linear formula by : log(I) = -r*log(E) + log(A)
image data = src.ImageClone()
data = log(data)
data.SetName("log SI")
number ch1 = trunc((Eng1-Eorignal)/Estep)
number r1= trunc(Rng1/Estep)
number ch2 = trunc((Eng2-Eorignal)/Estep)
number r2= trunc(Rng2/Estep)
number nsz = r1+r2
// prepare for regression
image xdata := RealImage("xdata",4,sx,nsz)
xdata.Slice3(0,1,2,r1,1) = axis.Slice3(0,ch1,1)
xdata.Slice3(0,r2,ch2,1)
image ydata := RealImage("ydata",nsz)
ydata.Slice3(0,1) = data.Slice3(0,1)
ydata.Slice3(0,1)
image xmean := RealImage("xmean",nsz)
image ymean := RealImage("ymean",nsz)
for (number i=0; i<nsz; i++) {
// to repeat the mean value as a 3d stack
// the sum value of each pixel can be rapidly done by the Project function
xmean.Slice2(0,i,1) = Project(xdata,2) / nsz
ymean.Slice2(0,1) = Project(ydata,2) / nsz
};
// do regression
image m = Project((xdata-xmean)*(ydata-ymean),2) / Project((xdata-xmean)**2,2)
image b = ymean[icol,irow,0] - m*xmean[icol,0]
// to apply y=mx+b to all the planes
image bkg := src.ImageClone()
for (number i=0; i<sz; i++) {
bkg.Slice2(0,1) = m*axis.Slice2(0,1)+b
};
// reconstruct to the power law bkg
bkg = exp(bkg)
return bkg
};
}
{
Object FTW = Alloc(FittingTwoWindowBkg)
number Eng1 = 1500 // eV,1st Energy of fitting window
number Rng1 = 200 // eV,fitting range of 1st window
number Eng2 = 2600 // eV,2nd Energy of fitting window
number Rng2 = 200 // eV,fitting range of 2nd window
image src := GetFrontImage()
image bkg := FTW.TwoWindowBkgFitting(src,Eng1,Rng1,Eng2,Rng2)
bkg.ShowImage()
};
,
不幸的是,不存在用于此的明确命令。 但是,可以通过模仿手动操作来解决此问题:添加 ROI 以显示内容。
以下脚本执行您想要的操作,但它确实需要显示频谱,并将删除其上的其他 ROI。
context.SaveAsync(item)