Skip to content

Instantly share code, notes, and snippets.

@addisonElliott
Created January 26, 2018 18:47
Show Gist options
  • Save addisonElliott/2196a9f8da23ae14ba5df95999e7863d to your computer and use it in GitHub Desktop.
Save addisonElliott/2196a9f8da23ae14ba5df95999e7863d to your computer and use it in GitHub Desktop.
import numpy as np
from scipy.signal import upfirdn
# This is the 1D array that you want to interpolate between
# In this case, it is just 1-15 counting by 1. (16 is one plus stop value)
# MAINLY NEED TO CHANGE THIS AND UPSAMPLING FACTOR
oneDArray = np.arange(1, 16)
# METHOD 1
# ------------
# As the name suggests, this does an upsample, FIR filter, and then downsample. So downsample is set to 1 meaning do nothing.
# Upsample is set to 3. But that just inserts zeros between the values. The FIR is set to a ramp function from 1/upsample to 1 in increments of 1/upsample
# and then it goes back down to 1/upsample. Look at the coefficients I gave. If you do the convolution, you will notice it is the same as averaging
interpValues2 = upfirdn([1/3, 2/3, 1, 2/3, 1/3], oneDArray, 3)
# Note: The resulting signal is larger than necessary. You will need to cut off some of the beginning and end of it.
# There's a math equation for this but I'll have to think about it...
# Method 2
# ------------
# Upsample is the factor that we want to upsample by. So, this means the array will be 3 times bigger
upsample = 3
# This is the X array that matches the oneDArray (Y-values).
# So basically this is spaced out by the upsampling factor and then the values in between will be interpolated.
# So in this case, the arange will number the X-values from 0-45 in increments of 3.
actualXArray = np.arange(0, len(oneDArray) * 3, 3)
# The desired X array that we want to get the interpolated values for are just every integer value.
# So this time it counts from 0-45 but we increment by 1.
desiredXArray = np.arange(0, len(oneDArray) * 3)
# The interpolated values returns the y values for desiredXArray given the actualXArray and oneDArray
interpValues = np.interp(desiredXArray, actualXArray, oneDArray)
# Also may need to cut some of these values off.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment