2024/02/02(Fri) 20:41:44 編集(投稿者)
■No102920 (kiku さん) に返信
> エラーを解消するだけでしたら、
> 下記に変更すれば良いと思います。
> Dim w(0 To 2 * N, 0 To M) As Double
小出しですみません。
Copilotで聞いても中々解消しないとこもあります。
SGFilter関数でSavitzky-Golayフィルターを適用し、入力配列 y8 を平滑化し結果を返すものでした。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim y8(0 To 100) As Double
For i = 0 To 100
y8(i) = i
Next
Dim N As Integer = 5
Dim M As Integer = 2
Dim result() As Double
result = SGFilter(y8, N, M)
End Sub
Private Function SGFilter(ByVal y() As Double, ByVal N As Integer, ByVal M As Integer) As Double()
Dim i As Integer, j As Integer, k As Integer
Dim w(0 To 2 * N, 0 To M) As Double 'Dim w(0 To N, 0 To M) As Double
Dim sum As Double
Dim y2() As Double
ReDim y2(0 To UBound(y))
For i = 0 To UBound(y)
sum = 0
For j = -N To N
For k = 0 To M
w(N + j, k) = ((j + k) ^ (2 * M))
Next k ' ※ ここので「インデックスが配列の境界外です。」は解消しました。
sum = sum + w(N + j, 0)
Next j
For j = -N To N
For k = 0 To M
w(N + j, k) = w(N + j, k) / sum
Next k
Next j
For j = -N To N
sum = 0
For k = 0 To M
sum = sum + w(N + j, k) * y(i + j) ' ※ 今度はここに「インデックスが配列の境界外です。」
Next k
y2(i) = y2(i) + sum
Next j
Next i
SGFilter = y2
End Function