Farrow arbitrary resampler

import numpy as np
import matplotlib.pyplot as plt

import sdr

%config InlineBackend.print_figure_kwargs = {"facecolor" : "w"}
%matplotlib inline
# %matplotlib widget

sdr.plot.use_style()

Construct an input signal, \(x[n] = x(n T_s)\)

Create a discrete-time signal \(x[n]\) that sample the continuous-time signal \(x(t)\) at a sample rate \(f_s = 1 / T_s\).

sample_rate = 1  # samples/s
N = 100  # samples
freq = 0.05  # Hz
tx = np.arange(N) / sample_rate  # Time axis for the input signal
x = np.exp(1j * 2 * np.pi * freq * tx)  # Complex exponential input signal
x *= np.exp(-np.arange(N) / 100)  # Exponential decay
plt.figure()
sdr.plot.time_domain(tx, x, marker="o", fillstyle="none")
plt.title("Original signal, $x(t)$")
plt.show()
../../_images/e96a66da9e79b6834e98c3c244b6a2df2cba75ff3255221dcb68ecb11d62a700.png

Resample the input signal with rate \(r\), \(y[n] = x(n \frac{T_s}{r})\)

Now, resample \(x[n]\) such that the output \(y[n]\) is equivalent to sampling the continuous-time \(x(t)\) at sample rate \(f_s = r/T_s\). This is equivalent to sampling \(x[n]\) at \(1/r\) fractional samples. This is accomplished, for arbitrary \(r\), with a Farrow arbitrary resampler.

In the sdr library, the Farrow arbitrary resampler is implemented in sdr.FarrowResampler.

def resample_signal(rate):
    farrow = sdr.FarrowResampler()
    y = farrow(x, rate)
    new_sample_rate = rate * sample_rate
    ty = np.arange(y.size) / new_sample_rate  # Time axis for output signal

    print(f"Input signal length: {x.size}")
    print(f"Output signal length: {y.size}")

    plt.figure()
    sdr.plot.time_domain(tx, x, linestyle="none", marker="o", fillstyle="none", label="Input")
    plt.gca().set_prop_cycle(None)
    sdr.plot.time_domain(ty, y, linestyle="none", marker=".", label="Output")
    plt.title(f"Original $x(t)$ and resampled signal $y(t)$, rate = {rate}")
    plt.show()

Upsample the signal by an integer rate

When upsampling by 2, notice there are two output samples for every input sample.

resample_signal(2)
Input signal length: 100
Output signal length: 198
../../_images/ebdfa5683b95c9d1dc13b894257a5a6b85f7dbf3ab48a4885787b832d93179ad.png

When upsampling by 4, notice there are four output samples for every input sample.

resample_signal(4)
Input signal length: 100
Output signal length: 396
../../_images/1b1e39b19b11c2139a622a0bdc55b8a064c096456de6951d8c2dcc379c93cca0.png

Downsample the signal by an integer rate

When downsampling by 2, notice every other sample of the input appears at the output.

resample_signal(1 / 2)
Input signal length: 100
Output signal length: 50
../../_images/1a24099151d60e8b91f8fea76eb517b5837753685c7feeddebc4147e13267caf.png

When downsampling by 4, notice every fourth sample of the input appears at the output.

resample_signal(1 / 4)
Input signal length: 100
Output signal length: 25
../../_images/80fd3cde9202fb1d1bdcc4d0f65b5e8126f165c7eaedbfacdb0c2e195aef92b7.png

Upsample by an irrational rate

When upsampling by \(\pi\), notice there are roughly three output samples for every input sample. However, these samples often do not align with the original input samples.

resample_signal(np.pi)
Input signal length: 100
Output signal length: 312
../../_images/f5d3e4a77998ab0badf42529485a90b130b5882db5ac4c90410d9a7837572cc0.png

Downsample by an irrational rate

When downsampling by \(\pi\), notice there are roughly three input samples for every output sample. However, these samples often do not align with the original input samples.

resample_signal(1 / np.pi)
Input signal length: 100
Output signal length: 32
../../_images/b02b8d734cc498529b99797bd233b420af12c567181389975674a3620da17cd6.png

Last update: Jun 15, 2024