×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Savitzky-Golay filter

## Savitzky-Golay filter

(OP)
Recently I came across the versatile Savitzky-Golay filter on a different forum. The creator for the code Conrad Hoffman has kindly shared the code, as below.
This filter is very useful for smoothing data which is voluminous and has too many spikes. Try it.
Sub SG_five()
'5 Point Savitzky-Golay Smoothing Filter
'Multiple InputBoxes are used so the macro is self-contained.
'Note that this is a fixed output calculation and does not update if input data is changed.
On Error GoTo NotValidInput
Dim i As Integer 'counter
Dim Lrow As Integer 'low cell of data column
Dim Urow As Integer 'high cell of data column
Dim Ic As Integer 'column NUMBER for input data
Dim Cnum As Integer 'column NUMBER for output
Ic = InputBox("Enter column NUMBER of input data (A=1, B=2 etc.)")
Lrow = InputBox("Enter row number of first data cell.")
Urow = InputBox("Enter row number of last data cell.")
Cnum = InputBox("Enter column NUMBER for output (A=1, B=2 etc.).")
'5 point S-G coefficients are -3, 12, 17, 12, -3 and the divisor is 35
For i = (Lrow + 2) To (Urow - 2)
Cells(i, Cnum).Value = (-3 * Cells(i - 2, Ic).Value + 12 * Cells(i - 1, Ic).Value _
+ 17 * Cells(i, Ic).Value + 12 * Cells(i + 1, Ic).Value - 3 * Cells(i + 2, Ic).Value) / 35
Next i
Exit Sub
NotValidInput:
MsgBox ("Non valid entry- terminating.")

End Sub

Sub SG_eleven()
'11 Point Savitzky-Golay Smoothing Filter
'Multiple InputBoxes are used so the macro is self-contained.
'Note that this is a fixed output calculation and does not update if input data is changed.
On Error GoTo NotValidInput
Dim i As Integer 'counter
Dim Lrow As Integer 'lower cell of data column
Dim Urow As Integer 'uppper cell of data column
Dim Ic As Integer 'column NUMBER for input data
Dim Cnum As Integer 'column number for output
Ic = InputBox("Enter column NUMBER of input data (A=1, B=2 etc.)")
Lrow = InputBox("Enter row number of first data cell.")
Urow = InputBox("Enter row number of last data cell.")
Cnum = InputBox("Enter column NUMBER for output (A=1, B=2 etc.).")
For i = (Lrow + 5) To (Urow - 5)
Cells(i, Cnum).Value = (-36 * Cells(i - 5, Ic).Value + 9 * Cells(i - 4, Ic).Value + 44 * Cells(i - 3, Ic).Value + 69 * Cells(i - 2, Ic).Value _
+ 84 * Cells(i - 1, Ic).Value + 89 * Cells(i, Ic).Value + 84 * Cells(i + 1, Ic).Value + 69 * Cells(i + 2, Ic).Value _
+ 44 * Cells(i + 3, Ic).Value + 9 * Cells(i + 4, Ic).Value - 36 * Cells(i + 5, Ic).Value) / 429
Next i
Exit Sub
NotValidInput:
MsgBox ("Non valid entry- terminating.")

End Sub

Sub SG_twentynine()
'29 Point Savitzky-Golay Smoothing Filter
'Multiple InputBoxes are used so the macro is self-contained.
'Note that this is a fixed output calculation and does not update if input data is changed.
On Error GoTo NotValidInput
Dim i As Integer 'counter
Dim Lrow As Integer 'lower cell of data column
Dim Urow As Integer 'uppper cell of data column
Dim Ic As Integer 'column NUMBER for input data
Dim Cnum As Integer 'column NUMBER for output data
Ic = InputBox("Enter column NUMBER of input data (A=1, B=2 etc.)")
Lrow = InputBox("Enter row number of first data cell.")
Urow = InputBox("Enter row number of last data cell.")
Cnum = InputBox("Enter column NUMBER for output data (A=1, B=2 etc.)")
For i = (Lrow + 14) To (Urow - 14)
Cells(i, Cnum).Value = (-351 * Cells(i - 14, Ic).Value - 216 * Cells(i - 13, Ic).Value + -91 * Cells(i - 12, Ic).Value _
+ 24 * Cells(i - 11, Ic).Value + 129 * Cells(i - 10, Ic).Value + 224 * Cells(i - 9, Ic).Value _
+ 309 * Cells(i - 8, Ic).Value + 384 * Cells(i - 7, Ic).Value + 449 * Cells(i - 6, Ic).Value _
+ 504 * Cells(i - 5, Ic).Value + 549 * Cells(i - 4, Ic).Value + 584 * Cells(i - 3, Ic).Value _
+ 609 * Cells(i - 2, Ic).Value + 624 * Cells(i - 1, Ic).Value + 629 * Cells(i, Ic).Value _
+ 624 * Cells(i + 1, Ic).Value + 609 * Cells(i + 2, Ic).Value + 584 * Cells(i + 3, Ic).Value _
+ 549 * Cells(i + 4, Ic).Value + 504 * Cells(i + 5, Ic).Value + 449 * Cells(i + 6, Ic).Value _
+ 384 * Cells(i + 7, Ic).Value + 309 * Cells(i + 8, Ic).Value + 224 * Cells(i + 9, Ic).Value _
+ 129 * Cells(i + 10, Ic).Value + 24 * Cells(i + 11, Ic).Value - 91 * Cells(i + 12, Ic).Value _
- 216 * Cells(i + 13, Ic).Value - 351 * Cells(i + 14, Ic).Value) / 8091
Next i
Exit Sub
NotValidInput:
MsgBox ("Non valid entry- terminating.")

End Sub
Replies continue below

### RE: Savitzky-Golay filter

Thanks for that.

I have converted it to a user defined function that I find more convenient for these things. The one function will call the formulae for either 5 (default), 11 or 29 points:

#### CODE --> VBA

Function Savitzky(Drange As Variant, Optional Points As Long = 5)
'5, 11 or 29 Point Savitzky-Golay Smoothing Filter

On Error GoTo NotValidInput
Dim i As Long, j As Long  'counters
Dim irows As Long 'Number of rows of data
Dim Res As Double, ResA() As Variant, FactA As Variant, div As Long

Drange = Drange.Value2

irows = UBound(Drange)
ReDim ResA(1 To irows, 1 To 1)
Select Case Points
Case 5
For i = 1 To 2
ResA(i, 1) = CVErr(xlErrNA)
Next i
FactA = Array(-3, 12, 17, 12, -3)
div = 35
For i = 3 To irows - 2
Res = 0
For j = 0 To 4
Res = Res + (FactA(j) * Drange(i + j - 2, 1))
Next j
ResA(i, 1) = Res / div
Next i
For i = irows - 1 To irows
ResA(i, 1) = CVErr(xlErrNA)
Next i
Case 11
For i = 1 To 5
ResA(i, 1) = CVErr(xlErrNA)
Next i
FactA = Array(-36, 9, 44, 69, 84, 89, 84, 69, 44, 9, -36)
div = 429
For i = 6 To irows - 5
Res = 0
For j = 0 To 10
Res = Res + (FactA(j) * Drange(i + j - 5, 1))
Next j
ResA(i, 1) = Res / div
Next i
For i = irows - 4 To irows
ResA(i, 1) = CVErr(xlErrNA)
Next i
Case 29
For i = 1 To 14
ResA(i, 1) = CVErr(xlErrNA)
Next i

FactA = Array(-351, -216, -91, 24, 129, 224, 309, 384, 449, 504, 549, 584, 609, 624, 629, 624, 609, 584, 549, 504, 449, _
384, 309, 224, 129, 24, -91, -216, -351)
div = 8091
For i = 15 To irows - 14
Res = 0
For j = 0 To 28
Res = Res + (FactA(j) * Drange(i + j - 14, 1))
Next j
ResA(i, 1) = Res / div
Next i
For i = irows - 14 To irows
ResA(i, 1) = CVErr(xlErrNA)
Next i

Case Else
Savitzky = "Points must be 5, 11, or 29"
Exit Function
End Select

Savitzky = ResA
Exit Function
NotValidInput:
Savitzky = ("Non valid entry- terminating.")

End Function 

To return all of the results enter as an array function:
Enter the formula in the top cell of the output range.
Select the entire output range
Press F2
Press Ctrl-Shift-Enter

Doug Jenkins
Interactive Design Services
http://newtonexcelbach.wordpress.com/

### RE: Savitzky-Golay filter

(OP)
Doug. Thank you for this. I hope it helps other users.

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!