-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResampler.cpp
More file actions
95 lines (80 loc) · 3.27 KB
/
Copy pathResampler.cpp
File metadata and controls
95 lines (80 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright (C) 2026 by Adrian Musceac YO8RZZ
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "Resampler.h"
#include <cassert>
Resampler::Resampler(unsigned int interp, unsigned int decim, float bw, unsigned int num_channels) :
m_decim(decim),
m_interp(interp),
m_activeChannels(num_channels)
{
assert(num_channels <= MAX_MMDVM_CHANNELS);
assert(decim > 0U);
assert(interp > 0U);
for (unsigned int i = 0U; i < m_activeChannels; i++) {
m_upsampler[i] = ::rresamp_crcf_create_kaiser(interp, decim, RESAMPLER_FILTER_DELAY, bw, 70.0F);
m_downsampler[i] = ::rresamp_crcf_create_kaiser(decim, interp, RESAMPLER_FILTER_DELAY, bw, 70.0F);
}
}
Resampler::~Resampler()
{
for (unsigned int i = 0U; i < m_activeChannels; i++) {
::rresamp_crcf_destroy(m_upsampler[i]);
::rresamp_crcf_destroy(m_downsampler[i]);
}
}
void Resampler::upsample(unsigned int channel, std::complex<float>* in_samples,
unsigned int num_samples, std::complex<float>* out_samples)
{
assert(in_samples != nullptr);
assert(out_samples != nullptr);
unsigned int p_in = num_samples / m_decim;
std::complex<float>* in_buf = new std::complex<float>[m_decim];
std::complex<float>* out_buf = new std::complex<float>[m_interp];
for (unsigned int i = 0U; i < p_in; i++) {
::memcpy(in_buf, in_samples + (i * m_decim), m_decim * sizeof(std::complex<float>));
::rresamp_crcf_execute(m_upsampler[channel], in_buf, out_buf);
::memcpy(out_samples + (i * m_interp), out_buf, m_interp * sizeof(std::complex<float>));
}
delete[] in_buf;
delete[] out_buf;
}
void Resampler::downsample(unsigned int channel, std::complex<float>* in_samples,
unsigned int num_samples, std::complex<float>* out_samples)
{
assert(in_samples != nullptr);
assert(out_samples != nullptr);
// Interpolation and decimation are reversed when downsampling
unsigned int p_in = num_samples / m_interp;
std::complex<float>* in_buf = new std::complex<float>[m_interp];
std::complex<float>* out_buf = new std::complex<float>[m_decim];
for (unsigned int i = 0U; i < p_in; i++) {
::memcpy(in_buf, in_samples + (i * m_interp), m_interp * sizeof(std::complex<float>));
::rresamp_crcf_execute(m_downsampler[channel], in_buf, out_buf);
::memcpy(out_samples + (i * m_decim), out_buf, m_decim * sizeof(std::complex<float>));
}
delete[] in_buf;
delete[] out_buf;
}
unsigned int Resampler::getDecim() const
{
return m_decim;
}
unsigned int Resampler::getInterp() const
{
return m_interp;
}