计算卷积的物理意义与公式网上与书上很多,这里不再赘述。
下面是计算两个向量u和v的fortran代码
program test_conv
!// 计算向量u和v的卷积
implicit none
integer :: i, k
integer, parameter :: m = 3, n = 8 !// 向量u和向量v的长度
real :: u(0:m-1) = [1, 1, 1], v(0:n-1) = [1, 1, 0, 0, 0, 1, 1, 1]
real :: conv(0:m+n-2)
real :: s1, s2
conv = 0.
do i = 0, m+n-2
do k = 0, m+n-2
if (k >= 0 .and. k <= m-1) then
s1 = u(k)
else
s1 = 0.
end if
if (i-k >=0 .and. i-k <= n-1) then
s2 = v(i-k)
else
s2 = 0.
end if
conv(i) = conv(i) + s1*s2
end do
end do
print*, conv
end program test_conv