TUgridFilter TCL UTILITIES USER MANUAL TUgridFilter
SYNOPSIS
Filter a 2D Grid
PACKAGE
TCLUTILS
NAME
TUgridFilter
USAGE
set rV  [TUgridFilter  fType Grid gInfo fOps {Filter}]
INPUT DEFINITIONS
fType - The filter type.
2DFILTER - This is either one of the built in 2D matrix type filter or an externally defined matrix type filter passed in through Filter
SAVITZKYGOLAY - A Savitzky-Golay filter with specifications provided in fOps
Grid - A 2D data grid generated through a call to TUdataGrid.
gInfo - The grid information array.
fOps - An array of filter options. The option definitions depend on the fiter type selected in fType.
2DFILTER fOps(0) 0: 3x3 Low Pass (Smoothing) Filter
1: 5x5 Low Pass (Smoothing) Filter
2: 3x3 High Pass (Sharpening) Filter
3: 5x5 High Pass (Sharpening) Filter
4: 3x3 Laplacian Filter
5: 5x5 Laplacian Filter
7: Robert's Filter
 
SAVITZKYGOLAY   fOps(0)     Application direction of filter. Must be either ROW or COLUMN.
fOps(1) Unfilled grid cells. Set to NO if you want identified unfilled grid cells left as they are or the value if you want them defaulted to.
fOps(2) Bad grid cells. Set to NO if you want identified bad grid cells left as they are or the value if you want them defaulted to.
fOps(3) Number of past data points to include in the filter.
fOps(4) Number of future data points to include in the filter.
fOps(5) Filter order which is generally 1 or 2.
fOps(6) The S-G filter may incorrectly set the first fOps(3) and last fOps(4) data values in a column or row. You reset these cell to their input values by setting fOps(6) to YES, leave them as they are by setting fOps(6) to NO, or set them so a specified value by setting fOps(6) to that value.
Filter - Optional array input. When fType is set to 2DFILTER you can use the Filter array to enter an external matrix filter to operate on the data grid. The filter must be square (NxN). If present this filter will be used over any of the build in filters regardless of how fOps(0) is set.
RETURN DEFINITION
Will return 1 if no errors or -1 if the procedure detects a non-square matrix passed in through the Filter variable.
DESCRIPTION
TUgridFilter filters a 2D data grid which as been created through the procedure TUdataGrid. Applied filters are either of the square matrix type filters (2DFILTER) or of the Savitzky-Golay (SAVITZKYGOLAY) type of filter. There are a number of built in matrix filters which can be used or the user can enter a custom designed filter through the Filter input variable. The filter must be square (NxN) and entered as a 1D array laid down row by row.
ERRORS
None Generated
C BACKING
No
EXAMPLE(S)
EXAMPLE 1:Show effects of several filters on a noisey data set.
# 2D FILTER Examples

# SET up a data set of four Gaussians centered at (-3,-3) and one at (-3, 3)
#   (3,-3) and (3, 3).  Each has a different intensity.  Add 20% noise to
#   form a noisy version of the data set.

set nP 0
for { set X -6.0 } { $X <= 6.0 } { set X [expr $X + .1] } {
   for { set Y -6.0 } { $Y <= 6.0 } { set Y [expr $Y + .1] } {
       set Xx($nP) $X
       set Yy($nP) $Y
       set R [expr ($X - 3.0) * ($X - 3.0) + ($Y - 3.0) * ($Y - 3.0)]
       set A [expr exp(-$R / 6.0)]
       set R [expr ($X - 3.0) * ($X - 3.0) + ($Y + 3.0) * ($Y + 3.0)]
       set B [expr 3.0 * exp(-$R / 6.0)]
       set R [expr ($X + 3.0) * ($X + 3.0) + ($Y - 3.0) * ($Y - 3.0)]
       set C [expr 4.0 * exp(-$R / 6.0)]
       set R [expr ($X + 3.0) * ($X + 3.0) + ($Y + 3.0) * ($Y + 3.0)]
       set D [expr 2.0 * exp(-$R / 6.0)]
       set Vv($nP) [expr $A + $B + $C + $D] 
       set Vn($nP) [expr $Vv($nP) * ( 1.0 + [TUdataRnd1 DuM 1 NP .2])]
       incr nP
   }
}

# SET up the grid information array.  

set gInfo(0) -6.0
set gInfo(1) 6.0
set gInfo(2) -6.0
set gInfo(3) 6.0
set gInfo(4) 100
set gInfo(5) 100
set gInfo(6) POINT
set gInfo(7) POINT
set gInfo(8) COLUMN
set gInfo(9) IGNORE
set gInfo(10) NEW:ADD:END
set gInfo(11) -1
set gInfo(12) -5
set gInfo(13) -1.0e30
set gInfo(14)  1.0e30
set gInfo(15)  NO
set gInfo(16)  NO

# GRID the clean data set and 5 versions of the noisy data set for
#    filtering

TUdataGrid $nP 1 Xx Xx 1 Yy Yy Vv gCl Norm gInfo

TUdataGrid $nP 1 Xx Xx 1 Yy Yy Vn gNsA Norm gInfo
TUdataGrid $nP 1 Xx Xx 1 Yy Yy Vn gNsB Norm gInfo
TUdataGrid $nP 1 Xx Xx 1 Yy Yy Vn gNsC Norm gInfo
TUdataGrid $nP 1 Xx Xx 1 Yy Yy Vn gNsD Norm gInfo
TUdataGrid $nP 1 Xx Xx 1 Yy Yy Vn gNsE Norm gInfo

# SET up the plot basics.  There are a total of 6 plots defined here.

set GphInfo(xScrL) 500
set GphInfo(yScrL) 760
GraphicsOn TK ETones SWAP

GenWindow 1 0.0 0.0 0.0 1.0 1.0 0.0  0. 0. 0. 1. 1. 0. XY
set WinInfo(2,corFmt) 1
RelWindow 2 1 75.0 515.0 0. 275. 715. 0. -6. -6. 0. 6. 6. 0.
DefinePlot 2 2 $gInfo(4) $gInfo(5)
set WinInfo(3,corFmt) 1
RelWindow 3 1 280.0 515.0 0. 480. 715. 0. -6. -6. 0. 6. 6. 0.
DefinePlot 3 3 $gInfo(4) $gInfo(5)
set WinInfo(4,corFmt) 1
RelWindow 4 1 75.0 290.0 0. 275. 490. 0. -6. -6. 0. 6. 6. 0.
DefinePlot 4 4 $gInfo(4) $gInfo(5)
set WinInfo(5,corFmt) 1
RelWindow 5 1 280.0 290.0 0. 480. 490. 0. -6. -6. 0. 6. 6. 0.
DefinePlot 5 5 $gInfo(4) $gInfo(5)
set WinInfo(6,corFmt) 1
RelWindow 6 1 75.0 65.0 0. 275. 265. 0. -6. -6. 0. 6. 6. 0.
DefinePlot 6 6 $gInfo(4) $gInfo(5)
set WinInfo(7,corFmt) 1
RelWindow 7 1 280.0 65.0 0. 480. 265. 0. -6. -6. 0. 6. 6. 0.
DefinePlot 7 7 $gInfo(4) $gInfo(5)

# SET up the color map to run from 0 to 4.5

PLTcolorMap 0.0 4.5 LINEAR 0 cA

# PLOT the noiseless data set in upper left plot

Plot3D 2 Xb Yb Dummy Dummy gCl AUTO Dummy cA

# PLOT the noisey data set in upper right plot

Plot3D 3 Xb Yb Dummy Dummy gNsA AUTO Dummy cA

# SET up a Savitzky-Golay Filter.  Filter first along the columns and
#   then along the rows.   This is a two step process.  Put the results
#   in the middle left-hand plot

set fOps(0) COLUMN
set fOps(1) NO
set fOps(2) NO
set fOps(3) 3
set fOps(4) 3
set fOps(5) 1
set fOps(6) YES
TUgridFilter SAVITZKYGOLAY gNsB gInfo fOps DUMMY
set fOps(0) ROW
TUgridFilter SAVITZKYGOLAY gNsB gInfo fOps DUMMY
set fOps(0) 0

Plot3D 4 Xb Yb Dummy Dummy gNsB AUTO Dummy cA

# DO a 3x3 Low Pass filter on the grid.  Plot the results in the middle 
#    right-hand plot

set fOps(0) 0
TUgridFilter 2DFILTER gNsC gInfo fOps
Plot3D 5 Xb Yb Dummy Dummy gNsC AUTO Dummy cA

# DO a 3x3 High Pass filter on the grid.  Plot the results in the lower 
#    right-hand plot

set fOps(0) 2
TUgridFilter 2DFILTER gNsD gInfo fOps
Plot3D 6 Xb Yb Dummy Dummy gNsD AUTO Dummy cA

# DO a 3x3 Laplacian Pass filter on the grid.  Plot the results in the lower 
#    right-hand plot

set fOps(0) 4
TUgridFilter 2DFILTER gNsE gInfo fOps
Plot3D 7 Xb Yb Dummy Dummy gNsE AUTO Dummy cA

# THROW up the axis.  Only annotate lower left-hand  plot

PlotOutlines 2 [list "" 1.0 "" 1.0] [list NONE NONE BOTH BOTH]
PlotOutlines 3 [list "" 1.0 "" 1.0] [list NONE NONE BOTH BOTH]
PlotOutlines 4 [list "" 1.0 "" 1.0] [list NONE NONE BOTH BOTH]
PlotOutlines 5 [list "" 1.0 "" 1.0] [list NONE NONE BOTH BOTH]
PlotOutlines 6 [list "X" 1.0 "Y" 1.0] [list BOT BOT BOTH BOTH]
PlotOutlines 7 [list "" 1.0 "" 1.0] [list NONE NONE BOTH BOTH]
update

# LABEL the plots 

TextProp 12 HOLD HOLD
WinClip 2 0
TexT 2 0.0 6.50 0.0 center "ORIGINAL" $GphInfo(White)
WinClip 3 0
TexT 3 0.0 6.50 0.0 center "NOISY" $GphInfo(White)
WinClip 4 0
TexT 4 0.0 6.50 0.0 center "SAVITZKY-GOLAY" $GphInfo(White)
WinClip 5 0
TexT 5 0.0 6.50 0.0 center "3x3 LOW PASS" $GphInfo(White)
WinClip 6 0
TexT 6 0.0 6.50 0.0 center "3x3 HIGH PASS" $GphInfo(White)
WinClip 7 0
TexT 7 0.0 6.50 0.0 center "3x3 LAPLACIAN" $GphInfo(White)
      
Sept 22, 2006