Engee documentation

upfirdn

Increasing the sampling rate, using an FIR filter, and lowering the sampling rate.

Library

EngeeDSP

Syntax

Function call

  • yout = upfirdn(xin,h) — filters the input signal xin using a FIR filter with an impulse response h. Increasing and decreasing the sampling rate is not implemented in this syntax.

  • yout = upfirdn(xin,h,p) — uses an integer sampling rate boost factor p.

  • yout = upfirdn(xin,h,p,q) — uses an integer sampling rate reduction factor q.

Arguments

Input arguments

# xin — input signal

+ vector | the matrix

Details

An input signal specified as a vector or matrix. If xin If it is a vector, then it represents a single signal. If xin — matrix, then each column is filtered independently. For more information, see Recommendations.

Типы данных

Float32, Float64

# h — pulse response

+ vector | the matrix

Details

The impulse response of the filter, specified as a vector or matrix. If h — vector, then it represents one FIR filter. If h — matrix, then each column represents the impulse response of a separate FIR filter. For more information, see Recommendations.

Типы данных

Float32, Float64

# p is the sampling rate boost factor
1 (default) | a positive integer

Details

The sampling rate boost factor, set as a positive integer.

Типы данных

Float32, Float64

# q is the sampling rate reduction factor
1 (by default) | a positive integer

Details

The sampling rate reduction factor, set as a positive integer.

Типы данных

Float32, Float64

Output arguments

# yout — output signal

+ vector | the matrix

Details

The output signal returned as a vector or matrix. Each column yout has a length of ceil(length(xin) − 1) * p + length(h / q).

If the input signal is xin has a single precision, then yout it will also have single precision.

Because upfirdn performs convolution and change of sampling rate, then the output signals yout have a length different from the length of the input signals xin. Number of rows yout approximately in p/q times the number of rows xin.

Examples

Converting the DAT sampling rate to the CD sampling rate

Details

Let’s change the sampling frequency of the signal using a rational conversion factor from the sampling frequency DAT 48 kHz to CD sampling rate 44.1 kHz. Using the function rationalize let’s find the numerator L and the denominator M the rational coefficient.

import EngeeDSP.Functions: upfirdn, kaiserord, fir1, kaiser

Fdat = 48e3
Fcd = 44.1e3
r = rationalize(Fcd/Fdat)
L = numerator(r)
M = denominator(r)

println("L=",L,", M=",M)
L=147, M=160

Generate a sinusoid with frequency 1.5 kHz with sampling period equal 0.25 C. Let’s plot the first millisecond of the signal.

t = 0:1/Fdat:0.25-1/Fdat
x = sin.(2π * 1.5e3 * t)
scatter(t, x, markershape=:circle, markersize=3, line=:stem, label="signal")
xlims!(0, 0.001)

upfirdn 1

Let’s design a low-pass filter with smoothing using the Kaiser window. Let’s set the boundaries of the filter band at 90% and 110% of the cutoff frequency, . Setting the ripple in the bandwidth 5 dB and attenuation in the delay band 40 dB. Let’s set the gain in the bandwidth to L.

Fs = 48e3
f = (Fdat/2) * min(1/L, 1/M)

Wp = 0.9*f / (Fs/2)
Ws = 1.1*f / (Fs/2)
Rp = 5
Rs = 40

n, Wn, beta = kaiserord([Wp, Ws], [1, 0], [10^(-Rp/20), 10^(-Rs/20)])
b = fir1(n, Wn, kaiser(n+1, beta))
h = L * b

Using the function upfirdn with a filter h to resample the sine wave. Calculate and compensate for the delay introduced by the filter. We will generate the corresponding oversampled time vector.

y = upfirdn(x, h, L, M)

delay_float = ((length(h) - 1) / 2 - (L - 1)) / L
delay = floor(Int, delay_float)
y = y[delay+1:end]


t_res = (0:length(y)-1) / Fcd

Let’s display the original and oversampled signal on the graph.

plot(t, x, line=:stem, marker=:circle, markersize=2,
     label="Original (48 kHz)", color=:red,
     xlims=(0, 0.001), title="Signal Comparison",
     xlabel="Time (s)", ylabel="Amplitude")

scatter!(t_res, y, markershape=:circle, markersize=2,
         label="Resampled (44.1 kHz)", color=:blue)

upfirdn 2

Recommendations

Acceptable size combinations xin and h are:

  1. xin — vector and h — vector.

    There is one filter and one signal at the input, so the function collapses xin with h. The output signal yout is a string vector if xin — vector-string; otherwise yout — column vector.

  2. xin — the matrix, and h — vector.

    There is one filter and many signals at the input, so the function collapses h with each column xin. The resulting yout — a matrix with the same number of columns as xin.

  3. xin — vector, eh h — the matrix.

    There are several filters and one signal at the input, so the function collapses each column. h with xin. The resulting yout — a matrix with the same number of columns as h.

  4. xin — the matrix and h — a matrix, both with the same number of columns.

    There are several filters and several signals at the input, so the function collapses the corresponding columns. xin and h. The resulting yout — a matrix with the same number of columns as xin and h.

Algorithms

Function upfirdn uses a polyphase interpolation structure. The number of multiplication-summation operations in a polyphase structure is approximately equal to , where and — lengths and accordingly. For long signals, this formula is often accurate.

Function upfirdn performs a cascade of three operations:

  1. Increasing the sampling rate of the input data in the matrix xin by an integer coefficient p (adding zeros).

  2. FIR filtering for a signal with an increased sampling frequency with a sequence of pulse characteristics specified as a vector or matrix h.

  3. Lowering the sampling rate by an integer factor q (dropping counts).

A FIR filter is usually a low-pass filter that needs to be designed using another function, such as firpm or fir1.

Function resample performs the design of the FIR filter using firls and then changing the sampling rate using upfirdn.

Literature

  1. Crochiere, R. E. A General Program to Perform Sampling Rate Conversion of Data by Rational Ratios. Programs for Digital Signal Processing (Digital Signal Processing Committee of the IEEE Acoustics, Speech, and Signal Processing Society, eds.). New York: IEEE Press, 1979, Programs 8.2-1–8.2-7.

  2. Crochiere, R. E., and Lawrence R. Rabiner. Multirate Digital Signal Processing. Englewood Cliffs, NJ: Prentice-Hall, 1983.