Skip to content

Instantly share code, notes, and snippets.

@jshahbazi
Last active September 24, 2023 13:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jshahbazi/5289503 to your computer and use it in GitHub Desktop.
Save jshahbazi/5289503 to your computer and use it in GitHub Desktop.
A simple convolution of two 1D vectors (signals) done in Fortran
function convolve(x, h)
implicit none
!x is the signal array
!h is the noise/impulse array
real, dimension(:), allocatable :: convolve, y
real, dimension(:) :: x, h
integer :: kernelsize, datasize
integer :: i,j,k
datasize = size(x)
kernelsize = size(h)
allocate(y(datasize))
allocate(convolve(datasize))
!last part
do i=kernelsize,datasize
y(i) = 0.0
j=i
do k=1,kernelsize
y(i) = y(i) + x(j)*h(k)
j = j-1
end do
end do
!first part
do i=1,kernelsize
y(i) = 0.0
j=i
k=1
do while (j > 0)
y(i) = y(i) + x(j)*h(k)
j = j-1
k = k+1
end do
end do
convolve = y
end function convolve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment