dsplib 1.1.0
C++ DSP library for MATLAB-like coding
Loading...
Searching...
No Matches
tuner.h
1#pragma once
2
3#include <dsplib/array.h>
4
5#include <cmath>
6
7namespace dsplib {
8
9//up/down freq converter
10//out = in * exp(1i * 2 * pi * freq * t)
11class Tuner
12{
13public:
14 //sample_rate - sample rate of signal (Hz)
15 //freq - tune freq in range (-sample_rate/2 : sample_rate/2) (Hz)
16 explicit Tuner(int sample_rate, real_t freq)
17 : _fs{sample_rate}
18 , _freq{freq} {
19 DSPLIB_ASSERT(std::abs(_freq) <= (_fs / 2), "tuner freq must be in range (-fs/2 : fs/2)");
20 }
21
22 arr_cmplx process(span_cmplx x) {
23 const int n = x.size();
24 arr_cmplx r(n);
25 for (int i = 0; i < n; i++) {
26 const real_t phase = 2 * pi * _freq * _phase / _fs;
27 const cmplx_t w = {std::cos(phase), std::sin(phase)};
28 r[i] = x[i] * w;
29 ++_phase;
30 _phase = (_phase < _fs) ? _phase : 0;
31 }
32 return r;
33 }
34
35 [[nodiscard]] real_t freq() const noexcept {
36 return _freq;
37 }
38
39 [[nodiscard]] int sample_rate() const noexcept {
40 return _fs;
41 }
42
43 arr_cmplx operator()(span_cmplx x) {
44 return this->process(x);
45 }
46
47private:
48 int _fs;
49 real_t _freq;
50 int _phase{0};
51};
52
53} // namespace dsplib
Definition tuner.h:12
Definition span.h:295
Definition types.h:102