UPFIRDN
This function combines three multirate operations into one efficient polyphase routine: insert samples during upsampling, apply an FIR filter, and then retain samples according to the downsampling factor. It is a core building block for interpolation, decimation, and rational sample-rate conversion.
If the interpolation factor is u and the decimation factor is d, the output sampling rate satisfies:
f_{s,\text{out}} = f_{s,\text{in}} \cdot \frac{u}{d}
Compared with explicitly inserting zeros, filtering, and decimating in separate passes, the polyphase implementation reduces redundant work and is therefore more efficient for many signal-processing pipelines.
Excel Usage
=UPFIRDN(h, x, up, down, upfirdn_mode, cval)
h(list[list], required): 1-D FIR filter coefficients (Excel range).x(list[list], required): Input signal array.up(int, optional, default: 1): Upsampling rate.down(int, optional, default: 1): Downsampling rate.upfirdn_mode(str, optional, default: “constant”): The signal extension mode to use.cval(float, optional, default: 0): The constant value to use when mode is ‘constant’.
Returns (list[list]): A 2D array of the output signal.
Example 1: Simple FIR filter
Inputs:
| h | x | ||||
|---|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 1 | 1 |
Excel formula:
=UPFIRDN({1,1,1}, {1,1,1})
Expected output:
| Result | ||||
|---|---|---|---|---|
| 1 | 2 | 3 | 2 | 1 |
Example 2: Upsample with zeros
Inputs:
| h | x | up | ||
|---|---|---|---|---|
| 1 | 1 | 2 | 3 | 3 |
Excel formula:
=UPFIRDN({1}, {1,2,3}, 3)
Expected output:
| Result | ||||||
|---|---|---|---|---|---|---|
| 1 | 0 | 0 | 2 | 0 | 0 | 3 |
Example 3: FIR filter with decimation by 3
Inputs:
| h | x | down | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 3 |
Excel formula:
=UPFIRDN({1}, {0,1,2,3,4,5,6,7,8,9}, 3)
Expected output:
| Result | |||
|---|---|---|---|
| 0 | 3 | 6 | 9 |
Example 4: Linear boundary extension during interpolation
Inputs:
| h | x | up | upfirdn_mode | ||||
|---|---|---|---|---|---|---|---|
| 0.5 | 1 | 0.5 | 1 | 2 | 3 | 2 | line |
Excel formula:
=UPFIRDN({0.5,1,0.5}, {1,2,3}, 2, "line")
Expected output:
| Result | ||||||
|---|---|---|---|---|---|---|
| 0.5 | 1 | 1.5 | 2 | 2.5 | 3 | 3.5 |
Python Code
Show Code
import numpy as np
from scipy.signal import upfirdn as scipy_upfirdn
def upfirdn(h, x, up=1, down=1, upfirdn_mode='constant', cval=0):
"""
Upsample, FIR filter, and downsample.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.upfirdn.html
This example function is provided as-is without any representation of accuracy.
Args:
h (list[list]): 1-D FIR filter coefficients (Excel range).
x (list[list]): Input signal array.
up (int, optional): Upsampling rate. Default is 1.
down (int, optional): Downsampling rate. Default is 1.
upfirdn_mode (str, optional): The signal extension mode to use. Valid options: Constant, Symmetric, Reflect, Anti-Reflect, Anti-Symmetric, Edge, Wrap, Smooth, Line. Default is 'constant'.
cval (float, optional): The constant value to use when mode is 'constant'. Default is 0.
Returns:
list[list]: A 2D array of the output signal.
"""
try:
def to_1d(v):
if isinstance(v, list):
return np.array([float(x) for row in v for x in row])
return np.array([float(v)])
h_arr = to_1d(h)
x_arr = to_1d(x)
result = scipy_upfirdn(
h_arr,
x_arr,
up=int(up),
down=int(down),
mode=upfirdn_mode,
cval=float(cval)
)
return [result.tolist()]
except Exception as e:
return f"Error: {str(e)}"