Learn R Programming

soundgen (version 3.0.0)

interpolMatrix: Interpolate matrix

Description

Performs a chosen type of separable interpolation across both rows and columns of a matrix, in effect up- or downsampling a matrix to required dimensions. Rownames and colnames are also interpolated as needed. Make sure there are no NAs in the input to avoid unpredictable behavior (dropped by approx and spline, propagated by resample).

Usage

interpolMatrix(
  m,
  nr = NULL,
  nc = NULL,
  interpol = c("approx", "spline", "resample")
)

Arguments

m

input matrix of numeric values

nr, nc

target dimensions

interpol

interpolation method ('approx' for linear, 'spline' for spline, 'resample' for more nuanced resampling with low-pass filtering). NB: only linear interpolation is implemented for matrices of complex numbers

Examples

Run this code
m = matrix(1:12 + rnorm(12, 0, .2), nrow = 3)
rownames(m) = 1:3; colnames(m) = 1:4
soundgen:::interpolMatrix(m)  # just returns the original
soundgen:::interpolMatrix(m, nr = 10, nc = 7)
soundgen:::interpolMatrix(m, nr = 10, nc = 7, interpol = 'spline')
soundgen:::interpolMatrix(m, nr = 2, nc = 7)
soundgen:::interpolMatrix(m, nr = 2, nc = 3)

# input matrices can have a single row/column
soundgen:::interpolMatrix(matrix(1:5, nrow = 1), nc = 9)
soundgen:::interpolMatrix(matrix(1:5, ncol = 1), nr = 5, nc = 3)

# use resample() with a low-pass filter to avoid artifacts
a = matrix(c(rep(0, 10), 1, rep(0, 10)))
soundgen:::interpolMatrix(a, nr = 5)  # ok
soundgen:::interpolMatrix(a, nr = 6)  # spike lost
soundgen:::interpolMatrix(a, nr = 6, interpol = 'resample')  # ok

# complex values
cm = matrix(complex(real = 1:9, imaginary = 1:9), nrow = 3)
soundgen:::interpolMatrix(cm, nr = 5)

Run the code above in your browser using DataLab