Is there a Fortran subroutine which performs linear interpolation in one-dimenional data? I need something similar to MATLAB function interp1.
There is no built-in Fortran functionality to do linear interpolation. You could either use a library or write your own routine. I haven't tried compiling or testing and my fortran may be a bit rusty, but something like the following should work.
subroutine interp1( xData, yData, xVal, yVal ) ! Inputs: xData = a vector of the x-values of the data to be interpolated ! yData = a vector of the y-values of the data to be interpolated ! xVal = a vector of the x-values where interpolation should be performed ! Output: yVal = a vector of the resulting interpolated values implicit none real, intent(in) :: xData(:), yData(:), xVal(:) real, intent(out) :: yVal(:) integer :: inputIndex, dataIndex real :: minXdata, minYdata, xRange, weight ! Possible checks on inputs could go here ! Things you may want to check: ! monotonically increasing xData ! size(xData) == size(yData) ! size(xVal) == size(yVal) minXData = xData(1) maxXData = xData(size(xData)) xRange = maxXData - minXData do inputIndex = 1, size(xVal) ! possible checks for out of range xVal could go here ! this will work if x is uniformly spaced, otherwise increment ! dataIndex until xData(dataIndex+1)>xVal(inputIndex) dataIndex = floor((xVal(inputIndex)-minXData)/xRange); weight = (xVal - xData(dataIndex))/(xData(dataIndex+1)-xData(dataIndex)); yVal(inputIndex) = (1.0-weight)*yData(dataIndex) + ... weight*yData(dataIndex+1); end do end subroutine
Take a look at the Numerical Recipes book, a classic, found here. Look in the interpolation chapter. You can find an explanation, plus the code. It's in pdf. A simple google search also produced a github library by Thomas Robitaille, where a routine called interp1d exists.