
// Script function: Top-hat filter
// Requirement: Open a spectrum

// Unless named starting with GlobalSino, EM, My, Self, the function commands are defined
//by Gatan DM, please see at link: http://www.globalsino.com/EM/page2597.html


image MySpectrum := GetFrontImage()

TagGroup GlobalSino, EMItems
GlobalSino = DLGCreateDialog( "Confirm your settings", EMItems )
 
TagGroup MyNumber1, MyNumber2

EMItems.DLGAddElement( DLGCreateRealField( "Signal window                          :", MyNumber1, 3, 8, 3 ) )       
EMItems.DLGAddElement( DLGCreateRealField( "Factor of background window :", MyNumber2, 0.5, 8, 5 ) )       
 
if ( !Alloc( UIframe ).Init( GlobalSino ).Pose() ) exit (0)
 
number MySignalWindow = MyNumber1.DLGGetValue()
number MyFactor = MyNumber2.DLGGetValue()

string MyIMGname=GetName(MySpectrum)
	
image MyFirstB := imageclone(MySpectrum)*0
image MyS := imageclone(MySpectrum)*0
image MySecondB := imageclone(MySpectrum)*0

number i
for(i=0;i<=MySignalWindow;i++)
{	
	MyFirstB=MyFirstB+MySpectrum.offset(i*MyFactor-MySignalWindow,0)
	MyS=MyS+MySpectrum.offset(i,0)
	MySecondB=MySecondB+MySpectrum.offset(i*MyFactor+MySignalWindow,0)	
}

THF = 2*MyS - (MyFirstB + MySecondB)
THF=tert(icol<MySignalWindow,0,THF)
	
THF.setname("Top Hat Filter of " + MyIMGname)
showimage(THF)