Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/algorithm-evidence-matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The goal is to make the repository reviewable as an engineering portfolio artifa
| Goertzel detector | active | active | active | tone-detection plot | possible | add fixed-point detector notes |
| Batched Goertzel | active | active | active | benchmark report | possible | add SIMD comparison table |
| GCC-PHAT delay estimation | active | active | partial | delay-estimation plot | future | add noisy/multipath robustness sweep |
| Rational L/M resampler | active | active | active | generated response / demo | possible | add alias rejection metric |
| Rational L/M resampler | batch polyphase FIR | zero-stuffed reference, DC, tone and alias rejection | planned ratio sweep | [Lab 05 and downsampling measurement](labs/lab05_resampling_polyphase.md) | possible | sweep rejection across the transition band |
| IQ file analysis demo | active | smoke/demo | planned | demo output | not direct | add small public CI16 vector |
| SIMD / AVX2 path | optional | active where supported | active | performance report | not direct | add scalar-vs-AVX2 summary table |
| CMake package export | active | installed consumer smoke in CMake CI (Linux/Windows) | not applicable | README example | not applicable | document a tagged package-release check |
Expand Down
29 changes: 27 additions & 2 deletions docs/labs/lab05_resampling_polyphase.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,33 @@ Potential hardware mapping:

## Complexity discussion

The batch C++ function `resample_rational` returns `ceil(N * L / M)` samples.
For output index `m`, the delay-compensated position in the upsampled signal
is `t = m * M + (K - 1) / 2` for an odd FIR length `K`. Only taps at
`t mod L`, `(t mod L) + L`, and subsequent multiples of that stride can
contribute. Each output therefore takes at most `ceil(K / L)` multiplies.
Samples outside the input record are treated as zero. The implementation
stores the FIR and the output, and computes the phase as it advances.

### Verify the C++ resampler

```bash
cmake -S . -B build -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release
ctest --test-dir build -C Release -R DspResampler --output-on-failure
```

The equivalence test compares every output with explicit zero insertion, full
convolution and decimation, including both ends of short records and the
unreduced ratio 6/4. The downsampling test uses separate unit-amplitude tones
at 1 and 10 kHz, sampled at 48 kS/s and decimated to 16 kS/s. A 127-tap
Blackman-windowed FIR with cutoff scale 0.95 suppresses the 10 kHz tone,
which would otherwise fold to 6 kHz. After discarding 32 output samples at
each end, the test requires passband gain within 1% of unity and at least
60 dB of rejection, measured as `20 log10(RMS_pass / RMS_alias)`.
The double-precision run gave a gain of 0.999993 and 87.29 dB of rejection at
these two frequencies.

| Implementation | Computational cost | Comment |
|---|---:|---|
| Naive resampler | high | many operations on inserted zeros |
Expand All @@ -115,8 +142,6 @@ Potential hardware mapping:

## Extension tasks

- implement a polyphase resampler in C++;
- compare naive and polyphase output numerically;
- add fixed-point coefficient quantization;
- benchmark different `L/M` ratios;
- add SDR decimation example;
Expand Down
31 changes: 21 additions & 10 deletions dsp_core/src/dsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,28 @@ Signal resample_rational(const Signal& input, int up_factor, int down_factor, in
const double cutoff = 0.5 / static_cast<double>(std::max(up_factor, down_factor)) * cutoff_scale;
auto anti_alias = design_lowpass_fir(filter_taps, cutoff);
for (double& tap : anti_alias) tap *= static_cast<double>(up_factor);
Signal upsampled(input.size() * static_cast<std::size_t>(up_factor), 0.0);
for (std::size_t i = 0; i < input.size(); ++i) upsampled[i * static_cast<std::size_t>(up_factor)] = input[i];
const Signal filtered = convolve(upsampled, anti_alias);
const std::size_t expected = static_cast<std::size_t>(std::ceil(static_cast<double>(input.size()) * up_factor / down_factor));
const auto up = static_cast<std::size_t>(up_factor);
const auto down = static_cast<std::size_t>(down_factor);
const std::size_t delay = (anti_alias.size() - 1) / 2;
Signal output;
output.reserve(expected);
const std::size_t delay = static_cast<std::size_t>((filter_taps - 1) / 2);
for (std::size_t i = 0; i < expected; ++i) {
const std::size_t index = delay + i * static_cast<std::size_t>(down_factor);
if (index >= filtered.size()) break;
output.push_back(filtered[index]);
// input_index and phase represent output time in units of 1/up input samples.
std::size_t input_index = 0;
std::size_t phase = 0;
while (input_index < input.size()) {
const std::size_t delayed_phase = phase + delay;
const std::size_t newest = input_index + delayed_phase / up;
double sum = 0.0;
std::size_t offset = 0;
for (std::size_t tap = delayed_phase % up; tap < anti_alias.size() && offset <= newest;
tap += up, ++offset) {
if (newest - offset < input.size()) {
sum += input[newest - offset] * anti_alias[tap];
}
}
output.push_back(sum);
phase += down;
input_index += phase / up;
phase %= up;
}
return output;
}
Expand Down
41 changes: 41 additions & 0 deletions dsp_core/tests/dsp_resampler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstddef>
#include <cmath>
#include <numeric>
#include <utility>

#include <gtest/gtest.h>

Expand Down Expand Up @@ -53,3 +54,43 @@ TEST(DspResampler, MaintainsDcLevelInSteadyState) {
output.end() - static_cast<std::ptrdiff_t>(80));
EXPECT_NEAR(mean(steady_state), 1.0, 0.03);
}

TEST(DspResampler, MatchesZeroStuffedConvolutionThroughBothEdges) {
using namespace dsp_core::dsp;
constexpr int taps = 15;
for (auto [up, down] : {std::pair{3, 2}, {2, 3}, {1, 4}, {4, 1}, {6, 4}, {19, 2}, {1, 1}}) {
for (const Signal& input : {Signal{}, Signal{0.75}, Signal{1.0, -0.5, 0.25, 0.875, -1.0}}) {
SCOPED_TRACE(::testing::Message() << up << "/" << down << ", N=" << input.size());
Signal stuffed(input.size() * up, 0.0);
for (std::size_t n = 0; n < input.size(); ++n) stuffed[n * up] = input[n];
auto h = design_lowpass_fir(taps, 0.45 / std::max(up, down));
for (auto& coefficient : h) coefficient *= up;
const auto full = convolve(stuffed, h);
const auto actual = resample_rational(input, up, down, taps, 0.9);
ASSERT_EQ(actual.size(), (stuffed.size() + down - 1) / down);
for (std::size_t n = 0; n < actual.size(); ++n) {
EXPECT_NEAR(actual[n], full[taps / 2 + n * down], 1e-12);
}
}
}
}

TEST(DspResampler, RejectsAnAliasedToneWhenDecimatingByThree) {
using namespace dsp_core::dsp;
auto decimate_tone = [](double frequency_hz) {
const auto tone = generate_tone(3072, 48'000.0, frequency_hz);
return resample_rational(tone, 1, 3, 127, 0.95);
};
const auto passband = decimate_tone(1'000.0);
const auto alias = decimate_tone(10'000.0); // folds to 6 kHz at 16 kS/s
ASSERT_EQ(passband.size(), 1024U);
ASSERT_EQ(alias.size(), passband.size());
auto steady_rms = [](const Signal& output) {
// 960 steady-state samples contain whole periods of both output tones.
return rms(Signal(output.begin() + 32, output.end() - 32));
};
const double passband_rms = steady_rms(passband);
const double alias_rms = steady_rms(alias);
EXPECT_NEAR(passband_rms * std::sqrt(2.0), 1.0, 0.01);
EXPECT_LT(alias_rms, passband_rms * 0.001); // at least 60 dB of rejection
}