Skip to content

Instantly share code, notes, and snippets.

@gauteh
Created September 14, 2015 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauteh/d257ee67b159c5bdef49 to your computer and use it in GitHub Desktop.
Save gauteh/d257ee67b159c5bdef49 to your computer and use it in GitHub Desktop.
#! /usr/bin/python
#
# Gaute Hope (gaute.hope@nersc.no) / 2015
#
# Uses a sinc-kernel to interpolate a discretely sampled signal by convolution.
#
import numpy as np
def conv_upsample (x, k, wn = None):
"""
Use a convolution filter to resample the signal x with factor k. the
resulting vector is not k-times bigger, but the sample rate is k-times
greater.
wn is window length. default is the length of the input signal.
"""
if k == 1:
return x
elif k < 1:
raise NotImplementedError ("k < 1")
n = len(x)
nn = (n+1) * k
if wn is None:
wn = n
wn = wn - 1
sx = np.arange (1/k, wn / 2, 1/k)
sx = np.r_[-sx[::-1], 0, sx]
si = np.sinc (sx)
y = np.zeros ((nn,))
y[k::k] = x
y = np.convolve (y, si, 'full')
j = int(len(si)/2)
y = y[j:-j] # cut off convultion residue
y = y[k:-k+1] # cut off sides outside the orignal first and last
# sample point
return y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment