Engee documentation

resample

Resampling evenly or unevenly distributed data with a new fixed sampling rate.

Library

EngeeDSP

Syntax

Function call

  • y = resample(x,p,q) — performs re-sampling of the input sequence x with a sampling rate, in p/q times higher than the original one. Function resample applies to x Low-pass smoothing filter with a finite impulse response (FIR) and compensates for the delay introduced by the filter. The function works on the first dimension of the array, the size of which is larger 1.

  • y = resample(x,p,q,n) — uses an order smoothing filter 2 × n × max(p,q).

  • y = resample(x,p,q,n,beta) — defines the shape parameter of the Kaiser window used to design the low-pass filter.

  • y = resample(x,p,q,b) — filters x using the filter coefficients specified in the argument b.

  • y = resample(x,tx) — performs re-sampling of the signal values x, measured at time points specified in the vector tx. The function linearly interpolates x into a vector of uniformly distributed time points with the same endpoints and number of samples as tx. Values NaN they are considered as missing data and are ignored.

  • y = resample(x,tx,fs) — uses a polyphase smoothing filter to resample the signal with a uniform sampling rate specified in the argument fs.

  • y = resample(x,tx,fs,p,q) — interpolates the input signal onto an intermediate uniform grid with a sampling step (p/q)/fs. The function then filters the result by increasing its sampling rate by p and reducing by q, which results in the final sampling rate fs. For best results, make sure that fs × q/p at least twice as much as the highest frequency component x.

  • y = resample(x,tx,___,method) — defines the interpolation method along with any arguments from the previous syntaxes of this group. The interpolation method can be "linear", "pchip" or "spline".

  • y,ty = resample(x,tx,___) — returns to ty the time points corresponding to the resampled signal.

  • y,b = resample(x,p,q,___) — also returns the coefficients of the filter applied to x during re-sampling.

  • y,ty,b = resample(x,tx,___) — returns the coefficients of the smoothing filter in the argument b.

  • ___ = resample(___,Dimension=dim) — performs re-sampling of the measurement input data dim.

Arguments

Input arguments

# x — input signal

+ vector | the matrix | An N-dimensional array

Details

An input signal specified as a vector, matrix, or N-dimensional array. Argument x may contain values NaN if there is information about the time. Values NaN They are considered as missing data and excluded from re-sampling.

Типы данных

Float32, Float64

# p is the resampling coefficient

+ a positive integer

Details

The resampling coefficient, set as a positive integer.

Типы данных

Float32, Float64

# q is the resampling coefficient

+ a positive integer

Details

The resampling coefficient, set as a positive integer.

Типы данных

Float32, Float64

# n is the number of the neighboring element

+ 10 (by default) | a non-negative integer

Details

The number of the neighboring element, set as a non-negative integer. If n = 0, function resample performs nearest neighbor interpolation. The length of the smoothing FIR filter is proportional to n. Large values n they provide higher accuracy by increasing the calculation time.

Типы данных

Float32, Float64

# beta — Kaiser window shape parameter

+ 5 (by default) | positive real scalar

Details

The Kaiser window shape parameter, set as a positive real scalar. Increasing the argument beta expands the main lobe of the window used to build the smoothing filter, and reduces the amplitude of the side lobes of the window.

Типы данных

Float32, Float64

# b — FIR filter coefficients

+ vector

Details

FIR filter coefficients, specified as a vector. The default function is resample designs a filter using the function firls and the Kaiser’s window. When compensating for the delay, the function resample assumes that the input argument is b it has an odd length and a linear phase. For more information, see Low-pass smoothing filter.

Типы данных

Float32, Float64

# tx — time points
a non-negative real vector | DateTime

Details

Time points specified as a non-negative real vector or array DateTime. Argument tx It should increase monotonously, but it doesn’t have to be evenly distributed. tx may contain the values NaN. These values are treated as missing data and excluded from resampling. Argument tx let’s say only for the input value x.

Типы данных

Float32, Float64, DateTime

# fs — sampling rate
positive scalar

Details

The sampling rate, set as a positive scalar. The sampling rate is the number of samples per unit of time. If the unit of measurement of time is seconds, then the sampling frequency is indicated in Hz.

Типы данных

Float32, Float64

# method — interpolation method

+ "linear" (default) | "pchip" | "spline"

Details

The interpolation method, defined as "linear", "pchip" or "spline":

  • "linear" — linear interpolation;

  • "pchip" — piecewise cubic shape-preserving interpolation;

  • "spline" — spline interpolation using "not-a-knot" conditions at the ends.

For more information, see the page interp1.

If x If it doesn’t change slowly, consider using the function interp1 with the interpolation method "pchip".

Name-value input arguments

# dim — the measurement for which the operation is performed

+ a positive integer scalar

Details

The dimension that the operation is performed on, specified as a positive integer scalar. If the argument is dim not specified, function resample it is performed on the first dimension of the array, the size of which is larger 1.

Типы данных

Float32, Float64

Output arguments

# y is a resampled signal

+ vector | the matrix | An N-dimensional array

Details

A resampled signal returned as a vector, matrix, or N-dimensional array. If the input argument is x has a length of by measurement dim, and also the arguments are specified p and q, then the output argument is y has a length of by measurement dim.

# b — FIR filter coefficients

+ vector

Details

FIR filter coefficients returned as a vector.

# ty — output data

+ a non-negative real vector

Details

The output data returned as a non-negative real vector. Argument ty applies only to the input value x.

Examples

Re-sampling of a linear sequence

Details

Let’s re-sample a simple linear sequence with a frequency 3/2 from the original 10 Hz. Let’s plot the original sequence and the sequence with repeated sampling in one figure.

import EngeeDSP.Functions: resample

using Plots

fs = 10
t1 = 0:1/fs:1
x = t1

result = resample(x, 3, 2)
y = result.y

t2 = (0:(length(y)-1)) * 2 / (3 * fs)

plot(t1, x,
     seriestype = :scatter,
     marker = :xcross,
     markerstrokecolor = :blue,
     label = "Original",
     xlabel = "Time (s)",
     ylabel = "Signal",
     legend = :topleft)
plot!(t2, y,
      seriestype = :scatter,
      marker = :circle,
      markerstrokewidth = 1,
      markerstrokecolor = :orange,
      markercolor = :white,
      label = "Resampled")

resample 1

Re-sampling of a multi-channel signal

Details

We will generate a five-channel sinusoidal signal with 100 by counting down. Time increases in columns, and frequency increases in rows. Let’s plot the signal.

import EngeeDSP.Functions: resample

using Plots

p = 3
q = 2

tx = 0:p:300-p

x = [cos.(2π * tx ./ (freq * 100)) for freq in 1:5]
x = hcat(x...)

plot(tx, x,
     marker = :circle,
     markersize = 2,
     linestyle = :dot,
     seriestype = :path,
     title = "Original",
     ylims = (-1.5, 1.5),
     legend = false)

resample 2

Increase the sampling frequency of the sine wave by 3/2 in the second dimension. Let’s put the re-sampled signal on the graph.

ty = 0:q:300-q
result = resample(x, p, q)
y = result.y

plot(ty, y,
     marker = :circle,
     markersize = 2,
     linestyle = :dot,
     seriestype = :path,
     title = "Upsampled",
     ylims = (-1.5, 1.5),
     legend = false)

resample 2 1

Let’s change the shape of the re-sampled signal so that time passes along the third dimension.

y = reshape(y, 1, size(y, 1), size(y, 2))
y = permutedims(y, [3, 1, 2])
size(y)
(5, 1, 150)

Let’s lower the sampling frequency of the signal to the initial one and build a graph.

result_z = resample(y, q, p, Dimension=3)
z = result_z.y
z = dropdims(z, dims=2)

plot(tx, z',
     marker = :circle,
     markersize = 2,
     linestyle = :dot,
     seriestype = :path,
     title = "Downsampled",
     ylims = (-1.5, 1.5),
     legend = false)

resample 2 2

Additional Info

Low-pass smoothing filter

Details

To resample a signal with a rational coefficient p/q function resample calls the function upfirdn, which performs the following actions:

  1. Inserts zeros to increase the sampling of the signal by p.

  2. Applies an anti-aliasing FIR filter to the high-sampling signal.

  3. Drops samples to lower the sampling of the filtered signal by q.

An ideal smoothing filter has a normalized cutoff frequency. rad/count and gain p. To approximate the smoothing filter, the function resample uses the Kaiser window method.

  • The filter order is 2 × n × max(p,q). Meaning n the default value is 10.

  • The Kaiser window has a shape parameter beta, which determines the trade-off between transition width and attenuation in the delay band. Meaning beta the default value is 5.

  • The filter coefficients are normalized to account for the window gain.

As an example, we will design an anti-aliasing filter to resample the signal to the sampling frequency, in 3/2 twice the initial value:

p = 3
q = 2
maxpq = max(p, q)

fc = 1 / maxpq
n = 10
order = 2 * n * maxpq
beta = 5

b = fir1(order, fc, kaiser(order + 1, beta))
b = p * b / sum(b)

Algorithms

Function resample performs FIR design using the function firls, normalizes the result to account for the window gain, and then implements a change in the sampling rate using the function upfirdn.