-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBaselines.ipf
More file actions
executable file
·464 lines (367 loc) · 12.3 KB
/
Baselines.ipf
File metadata and controls
executable file
·464 lines (367 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#pragma rtGlobals=1 // Use modern global access method.
#pragma version=1.10
// 7/23/07 1.10b
// added smoothed spline baseline
// 7/3/07 1.00
Menu "GraphMarquee"
submenu "Baselines"
"Initialise Baseline Fit", /Q, MenuInitBaselineFitting()
"Add Region to Fit", /Q, GetMarquee /K bottom; ResetFitRegion(V_left, V_right, 1)
"Remove Region From Fit", /Q, GetMarquee /K bottom; ResetFitRegion(V_left, V_right, 0)
"Clear All Fit Regions", /Q, ClearFitRegion()
submenu "fit"
"line", /Q, MenuFitBaseline()
"poly 3", /Q, MenuFitBaseline()
"poly 4", /Q, MenuFitBaseline()
"gauss", /Q, MenuFitBaseline()
"lor", /Q, MenuFitBaseline()
"exp", /Q, MenuFitBaseline()
"Tony's gauss", /Q, MenuFitBaseline()
"sin", /Q, MenuFitBaseline()
"sigmoid", /Q, MenuFitBaseline()
"spline", /Q, MenuFitBaseline()
// "exp_XOffset", /Q, MenuFitBaseline()
// "dblexp_XOffset", /Q, MenuFitBaseline()
end
"Subtract Baseline", /Q, MenuSubtractBaseline()
end
End
Menu "TracePopup"
submenu "Baselines"
"Initialise Baseline Fit", /Q, MenuInitBaselineFitting()
submenu "fit"
"line", /Q, MenuFitBaseline()
"poly 3", /Q, MenuFitBaseline()
"poly 4", /Q, MenuFitBaseline()
"gauss", /Q, MenuFitBaseline()
"lor", /Q, MenuFitBaseline()
"exp", /Q, MenuFitBaseline()
"Tony's gauss", /Q, MenuFitBaseline()
"sin", /Q, MenuFitBaseline()
"sigmoid", /Q, MenuFitBaseline()
"spline", /Q, MenuFitBaseline()
// "exp_XOffset", /Q, MenuFitBaseline()
// "dblexp_XOffset", /Q, MenuFitBaseline()
end
"Subtract Baseline", /Q, MenuSubtractBaseline()
end
End
Menu "AllTracesPopup"
submenu "Fit baseline to all traces on plot"
"line", /Q, FitAllTraces()
"poly 3", /Q, FitAllTraces()
"poly 4", /Q, FitAllTraces()
"gauss", /Q, FitAllTraces()
"lor", /Q, FitAllTraces()
"exp", /Q, FitAllTraces()
"Tony's gauss", /Q, FitAllTraces()
"sin", /Q, MenuFitBaseline()
"sigmoid", /Q, MenuFitBaseline()
"spline", /Q, MenuFitBaseline()
// "exp_XOffset", /Q, MenuFitBaseline()
// "dblexp_XOffset", /Q, MenuFitBaseline()
end
end
Menu "Macros"
submenu "Baselines"
"Initialise Baseline Fit", /Q, MenuInitBaselineFitting()
"Add Region to Fit", /Q, GetMarquee /K bottom; ResetFitRegion(V_left, V_right, 1)
"Remove Region From Fit", /Q, GetMarquee /K bottom; ResetFitRegion(V_left, V_right, 0)
"Clear All Fit Regions", /Q, ClearFitRegion()
submenu "fit"
"line", /Q, MenuFitBaseline()
"poly 3", /Q, MenuFitBaseline()
"poly 4", /Q, MenuFitBaseline()
"gauss", /Q, MenuFitBaseline()
"lor", /Q, MenuFitBaseline()
"exp", /Q, MenuFitBaseline()
"Tony's gauss", /Q, MenuFitBaseline()
"sin", /Q, MenuFitBaseline()
"sigmoid", /Q, MenuFitBaseline()
"spline", /Q, MenuFitBaseline()
// "exp_XOffset", /Q, MenuFitBaseline()
// "dblexp_XOffset", /Q, MenuFitBaseline()
end
"Subtract Baseline", /Q, MenuSubtractBaseline()
end
end
// a wrapper function for InitBaselineFit
function MenuInitBaselineFitting()
// need to select data wave
string strDataWave
prompt strDataWave, "Data Wave", popup, WaveList("*", ";", "" )
doPrompt "Initialise Baseline Fit", strDataWave
if (V_flag)
return 0
endif
wave W_data=$strDataWave
InitBaselineFitting(W_data)
CheckDisplayed W_data
if (V_flag==0)
appendtograph W_data
endif
end
// value = 1 to include, 0 to exclude.
function ResetFitRegion(V_left, V_right, value)
variable V_left, V_right, value
if (exists("root:Packages:TonyIR:W_mask")==0)
return 0
endif
SVAR wname=root:Packages:TonyIR:BaselineFitDataWaveName
wave W_data=$wname
wave W_mask=root:Packages:TonyIR:W_mask
wave W_display=root:Packages:TonyIR:W_display
variable p_low=min(x2pnt(W_data,V_left), x2pnt(W_data,V_right))
variable p_high=max(x2pnt(W_data,V_left), x2pnt(W_data,V_right))
W_mask[p_low, p_high]=value
W_display = W_mask[p] ? W_data[p] : NaN
printf "ResetFitRegion(%d, %d, %d)\r", V_left, V_right, value
end
function ClearFitRegion()
wave W_mask=root:Packages:TonyIR:W_mask
wave W_display=root:Packages:TonyIR:W_display
W_mask=0
W_display=nan
end
// set the data wave (raw spectrum from which baseline is to be subtracted)
function InitBaselineFitting(w)
wave w
NewDataFolder /O root:Packages
NewDataFolder /O root:Packages:TonyIR
String /G root:Packages:TonyIR:BaselineFitDataWaveName=GetWavesDataFolder(w, 2)
duplicate /O w root:Packages:TonyIR:W_Display
wave W_display=root:Packages:TonyIR:W_display
// don't reset the mask wave if new data wave has same length as previous one
// in case we want to apply the same fit to many spectra
if (exists("root:Packages:TonyIR:W_mask")==1)
wave W_mask=root:Packages:TonyIR:W_mask
if (numpnts(W_mask)!=numpnts(W_display))
duplicate /O w root:Packages:TonyIR:W_mask
W_mask=0
W_display=nan
endif
else
duplicate /O w root:Packages:TonyIR:W_Mask
wave W_mask=root:Packages:TonyIR:W_mask
W_mask=0
endif
W_display = W_mask[p] ? w[p] : NaN
checkdisplayed W_display
if (V_flag==0)
appendtograph W_display
ModifyGraph mode(W_display)=7,hbFill(W_display)=4
ModifyGraph rgb(W_display)=(24576,24576,65280)
endif
duplicate /O w root:Packages:TonyIR:W_Base
wave W_base=root:Packages:TonyIR:W_base
W_base=nan
if (WhichListItem("W_base",TraceNameList("", ";", 1 ) )==-1)
appendtograph W_base
ModifyGraph rgb(W_base)=(0,15872,65280)
endif
duplicate /O w root:Packages:TonyIR:W_NoBase
wave W_NoBase=root:Packages:TonyIR:W_NoBase
W_NoBase=nan
if (WhichListItem("W_NoBase",TraceNameList("", ";", 1 ) )==-1)
appendtograph W_NoBase
ModifyGraph rgb(W_NoBase)=(0,0,0)
endif
variable /G root:Packages:TonyIR:V_smooth
NVAR V_smooth=root:Packages:TonyIR:V_smooth
V_smooth+=0.5*(V_smooth==0)
printf "InitBaselineFitting%s)\r", nameofwave(w)
end
// wrapper function to subtract the current fit from the data wave
function MenuSubtractBaseline()
SVAR wname=root:Packages:TonyIR:BaselineFitDataWaveName
wave W_data=$wname
SubtractBaseline(W_data)
end
// subtract current baseline from W_data
function SubtractBaseline(W_data)
wave W_data
wave W_base=root:Packages:TonyIR:W_base
if(numpnts(W_base)!=numpnts(W_data))
doalert 0, nameofwave(W_data) +" and baseline have different length"
return 0
endif
// save a copy of the baseline
string strNewName=CleanupName( nameofwave(W_data)+"_BL",0)
if (exists(strNewName))
doalert 1, strNewName+" exists. Overwrite?"
if(V_flag==2)
return 0
endif
endif
duplicate /o W_data $strNewName
wave newbase= $strNewName
newbase=W_base
// subtract baseline
strNewName=CleanupName( nameofwave(W_data)+"_Sub",0)
if (exists(strNewName))
doalert 1, strNewName+" exists. Overwrite?"
if(V_flag==2)
return 0
endif
endif
duplicate /o W_data $strNewName
wave subtracted= $strNewName
subtracted=W_data-W_base
print "SubtractBaseline("+nameofwave(W_data)+")"
checkdisplayed subtracted
if (V_flag)
return 1
else
appendtograph subtracted
endif
KillControl SplineSmoothSetVar
end
// wrapper function to start baseline fit of type defined by menu item
function MenuFitBaseline()
SVAR wname=root:Packages:TonyIR:BaselineFitDataWaveName
wave w=$wname
GetLastUserMenuInfo
printf "FitBaseline(%s, \"%s\")\r", nameofwave(w), S_value
FitBaseline(w, S_value)
end
// fit a baseline defined by type to the data wave W_data using predefined mask wave
// this function allows batch fitting from the command line
function FitBaseline(W_data, type)
wave w_data
string type
wave W_Base=root:Packages:TonyIR:W_Base
wave W_display=root:Packages:TonyIR:W_display
wave W_mask=root:Packages:TonyIR:W_mask
wave W_noBase=root:Packages:TonyIR:W_noBase
if (stringmatch(type, "spline"))
W_display = W_mask[p] ? W_data[p] : NaN // reset W_display in case we're batch fitting
NVAR V_smooth=root:Packages:TonyIR:V_smooth
interpolate2/T=3/I=3/F=(V_smooth)/Y=root:Packages:TonyIR:W_Base root:Packages:TonyIR:W_display
SetVariable SplineSmoothSetVar title="Smoothing", pos={200,100}, size={100,16}
SetVariable SplineSmoothSetVar labelBack=(65535,65535,65535)
SetVariable SplineSmoothSetVar limits={0,1e6,0.1 }, value=root:Packages:TonyIR:V_smooth
SetVariable SplineSmoothSetVar proc=BL_SplineSetVarProc
else
// remove the spline smoothing control in case previous fit was a spline
KillControl SplineSmoothSetVar
string s_hold=""
if (stringmatch(type, "Tony's gauss"))
s_hold="/H=\"1000\""
type="gauss"
execute /Q "K0 = 0"
endif
string cmd=""
variable i
wave w_mask=root:Packages:TonyIR:W_mask
variable mask1=0
for(i=0;i<numpnts(W_mask); i+=1)
if(w_mask[i]!=mask1)
mask1=1-mask1
if(mask1) // started to include
cmd += num2str(round(pnt2x(w_mask, i)))
else // stopped including
cmd+="-"+num2str(round(pnt2x(w_mask, i-1)))+" "
endif
elseif( (i==numpnts(w_mask)-1) && mask1)
cmd+="-"+num2str(round(pnt2x(w_mask, i)))
endif
endfor
print "fitting "+type+" baseline to "+nameofwave(w_data)+" at "+cmd
sprintf cmd, "CurveFit/NTHR=0 /Q %s %s , %s /M=root:Packages:TonyIR:W_mask", s_hold, type, GetWavesDataFolder(w_data, 2)
execute cmd
wave W_coef=W_coef
type=ReplaceString("gauss", type, "Gauss1D")
type=ReplaceString("line", type, "BL_line")
type=ReplaceString("lor", type, "BL_lor")
type=ReplaceString("exp", type, "BL_exp")
type=ReplaceString("3", type, "")
type=ReplaceString("4", type, "")
type=ReplaceString("sin", type, "BL_sin")
type=ReplaceString("sigmoid", type, "BL_sigmoid")
sprintf cmd, "root:Packages:TonyIR:W_base = %s(%s, x)", type, GetWavesDataFolder(W_coef, 2)
execute cmd
endif
W_noBase=W_data-W_base
end
Function BL_SplineSetVarProc(ctrlName,varNum,varStr,varName) : SetVariableControl
String ctrlName
Variable varNum
String varStr
String varName
interpolate2/T=3/I=3/F=(varNum)/Y=root:Packages:TonyIR:W_Base root:Packages:TonyIR:W_display
SVAR S_name=root:Packages:TonyIR:BaselineFitDataWaveName
wave W_Data=$S_name
wave W_noBase=root:Packages:TonyIR:W_noBase
wave W_base=root:Packages:TonyIR:W_base
W_noBase=W_data-W_base
End
// use these functions to fill the baseline wave
function BL_line(w, x)
wave w; variable x
return w[0]+w[1]*x
end
function BL_exp(W_coef, x)
wave W_coef; variable x
return W_coef[0]+W_coef[1]*exp(-W_coef[2]*x)
end
function BL_lor(W_coef, x)
wave W_coef; variable x
return W_coef[0]+W_coef[1]/((x-W_coef[2])^2+W_coef[3])
end
//function BL_exp_XOffset(W_coef, x)
// wave W_coef; variable x
// return W_coef[0]+W_coef[1]*exp(-(x-W_fitConstants[0])/W_coef[2])
//end
//
//
//function BL_dblexp_XOffset(W_coef, x)
// wave W_coef; variable x
// return
//end
function BL_sin(W_coef, x)
wave W_coef; variable x
return W_coef[0]+W_coef[1]*sin(W_coef[2]*x+W_coef[3])
end
function BL_sigmoid(W_coef, x)
wave W_coef; variable x
return W_coef[0] + W_coef[1]/(1+exp(-(x-W_coef[2])/W_coef[3]))
end
function FitAllTraces()
GetLastUserMenuInfo
string ListOfTraces=TraceNameList("", ";", 1 )
string NonDataTraces="W_Display;W_Base;W_NoBase"
ListOfTraces=removefromlist(NonDataTraces,ListOfTraces, ";", 0)
do
wave W_data=tracenametowaveref("", StringFromList(0, ListOfTraces))
ListOfTraces=RemoveListItem(0, ListOfTraces)
FitBaseline(W_data, S_value)
SubtractBaseline(W_data)
while (itemsinlist(ListOfTraces))
end
// can use this function from data browser (Execute Cmd...). N defines baseline type:
// 1 line
// 2 poly 3
// 3 poly 4
// 4 gauss
// 5 lor
// 6 exp
// 7 Tony's gauss
function FitAndGo(W_data, N)
wave W_data; variable N
string FitTypeList="line;poly 3;poly 4;gauss;lor;exp;Tony's gauss"
string type=StringFromList(N-1, FitTypeList)
FitBaseline(W_data, type)
SubtractBaseline(W_data)
end
function OffsetFromZero(W_data, Offset)
wave W_data
variable Offset
variable MinVal=-wavemin(W_data)
W_data=W_data-MinVal+Offset
end
function alignSpectra(ref, wavenumber, w_data)
wave ref, w_data
variable wavenumber
variable offset=w_data(wavenumber)-ref(wavenumber)
W_data-=offset
end