dsplib 1.1.0
C++ DSP library for MATLAB-like coding
Loading...
Searching...
No Matches
resample.h
1#pragma once
2
3#include <memory>
4
5#include <dsplib/utils.h>
6#include <dsplib/array.h>
7#include <dsplib/math.h>
8
9namespace dsplib {
10
11//TODO: complex type implementation
12
13//multirate FIR filter design (similar to implementation in MATLAB)
14arr_real design_multirate_fir(int interp, int decim, int hlen = 12, real_t astop = 90);
15
16//------------------------------------------------------------------------------
17//base resample class
19{
20public:
21 virtual ~IResampler() = default;
22
23 virtual arr_real process(span_real sig) = 0;
24
25 [[nodiscard]] virtual int delay() const noexcept {
26 return 0;
27 }
28
29 [[nodiscard]] virtual int decim_rate() const noexcept {
30 return 1;
31 }
32
33 [[nodiscard]] virtual int interp_rate() const noexcept {
34 return 1;
35 }
36
37 [[nodiscard]] int next_size(int size) const noexcept {
38 return IResampler::next_size(size, this->interp_rate(), this->decim_rate());
39 }
40
41 [[nodiscard]] int prev_size(int size) const noexcept {
42 return IResampler::prev_size(size, this->interp_rate(), this->decim_rate());
43 }
44
45 //polyphase decomposition of multirate filter
46 static std::vector<arr_real> polyphase(span_real h, int m, real_t gain = 1.0, bool flip_coeffs = false);
47
48 //nearest multiple of frame size to process
49 static int next_size(int size, int p, int q);
50 static int prev_size(int size, int p, int q);
51
52 //simplify the p/q ratio
53 static std::pair<int, int> simplify(int p, int q);
54};
55
56//------------------------------------------------------------------------------
57//polyphase FIR decimation
59{
60public:
61 explicit FIRDecimator(int decim);
62
63 //decim - decimation factor
64 //h - custom multirate fir filter
65 explicit FIRDecimator(int decim, span_real h);
66
67 //in.size() must be a multiple of the decim
68 arr_real process(span_real in) final;
69
70 [[nodiscard]] int delay() const noexcept final;
71 [[nodiscard]] int decim_rate() const noexcept final;
72
73private:
74 std::shared_ptr<IResampler> d_;
75};
76
77//------------------------------------------------------------------------------
78//polyphase FIR interpolation
80{
81public:
82 explicit FIRInterpolator(int interp);
83
84 //interp - interpolation factor
85 //h - custom multirate fir filter
86 explicit FIRInterpolator(int interp, span_real h);
87
88 arr_real process(span_real in) final;
89
90 [[nodiscard]] int delay() const noexcept final;
91 [[nodiscard]] int interp_rate() const noexcept final;
92
93private:
94 std::vector<arr_real> h_;
95 arr_real d_;
96 int interp_;
97 int sublen_;
98};
99
100//------------------------------------------------------------------------------
101//polyphase FIR sample rate conversion
103{
104public:
105 explicit FIRRateConverter(int interp, int decim);
106
107 //interp - interpolation factor
108 //decim - decimation factor
109 //h - custom multirate fir filter
110 explicit FIRRateConverter(int interp, int decim, span_real h);
111
112 //in.size() must be a multiple of the decim
113 arr_real process(span_real in) final;
114
115 [[nodiscard]] int delay() const noexcept final;
116 [[nodiscard]] int interp_rate() const noexcept final;
117 [[nodiscard]] int decim_rate() const noexcept final;
118
119private:
120 std::vector<arr_real> h_;
121 arr_real d_;
122 int interp_;
123 int decim_;
124 int sublen_;
125 std::vector<uint16_t> xidxs_;
126};
127
128//------------------------------------------------------------------------------
129//Wrapper over FIRRateConverter, FIRInterpolator and FIRDecimator
130//with calculation of optimal L/M coefficients for sample rate conversion
131//TODO: filter transition band setting
133{
134public:
135 //out_fs - output sample rate (Hz)
136 //in_fs - input sample rate (Hz)
137 explicit FIRResampler(int out_fs, int in_fs);
138
139 explicit FIRResampler(int out_fs, int in_fs, span_real h);
140
141 enum class Mode
142 {
143 Bypass,
144 Decimator,
145 Interpolator,
146 Resampler
147 };
148
149 arr_real process(span_real sig) final;
150
151 [[nodiscard]] int delay() const noexcept final;
152 [[nodiscard]] int interp_rate() const noexcept final;
153 [[nodiscard]] int decim_rate() const noexcept final;
154
155private:
156 Mode mode_{Mode::Bypass};
157 std::shared_ptr<IResampler> rsmp_;
158};
159
160//------------------------------------------------------------------------------
161//resamples the input sequence, x, at p/q times the original sample rate
162//as p/q coefficients, you can use out_fs/in_fs
163
164//n - filter len, uses an antialiasing filter of order 2 × n × max(p,q)
165//beta - shape parameter of Kaiser window
166arr_real resample(span_real x, int p, int q, int n = 10, real_t beta = 5.0);
167
168//h - resample FIR filter coefficients
169arr_real resample(span_real x, int p, int q, span_real h);
170
171} // namespace dsplib
Definition resample.h:59
Definition resample.h:80
Definition resample.h:103
Definition resample.h:133
Definition resample.h:19
Definition span.h:295