FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
dsp_filters.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-only
2//
3// Copyright (c) 2007-2008 Marc Brooker and Michael Inggs
4// Copyright (c) 2008-present FERS Contributors (see AUTHORS.md).
5//
6// See the GNU GPLv2 LICENSE file in the FERS project root for more information.
7
8/**
9 * @file dsp_filters.h
10 * @brief Header file for Digital Signal Processing (DSP) filters and upsampling/downsampling functionality.
11 */
12
13#pragma once
14
15#include <cstddef>
16#include <memory>
17#include <span>
18#include <vector>
19
20#include "core/config.h"
21
22namespace fers_signal
23{
24 /**
25 * @brief Upsamples a signal by a given ratio.
26 *
27 * @param in Input span of complex samples.
28 * @param size Size of the input signal.
29 * @param out Output span for upsampled complex samples.
30 * @throws std::invalid_argument if the input or output spans are empty or the ratio is zero.
31 */
32 void upsample(std::span<const ComplexType> in, unsigned size, std::span<ComplexType> out);
33
34 /**
35 * @brief Downsamples a signal by a given ratio.
36 *
37 * @param in Input span of complex samples.
38 * @throws std::invalid_argument if the input or output spans are empty or the ratio is zero.
39 */
40 std::vector<ComplexType> downsample(std::span<const ComplexType> in);
41
42 /**
43 * @class DspFilter
44 * @brief Abstract base class for digital filters.
45 */
47 {
48 public:
49 DspFilter() = default;
50
51 virtual ~DspFilter() = default;
52
53 /**
54 * @brief Filters a single sample.
55 *
56 * @param sample A single real-valued sample to be filtered.
57 * @return The filtered sample.
58 */
59 virtual RealType filter(RealType sample) = 0;
60
61 /**
62 * @brief Filters a block of samples.
63 *
64 * @param samples Span of real-valued samples to be filtered.
65 */
66 virtual void filter(std::span<RealType> samples) = 0;
67
68 DspFilter(const DspFilter&) = delete;
69
70 DspFilter& operator=(const DspFilter&) = delete;
71
72 DspFilter(DspFilter&&) noexcept = default;
73
74 DspFilter& operator=(DspFilter&&) noexcept = default;
75 };
76
77 /**
78 * @class IirFilter
79 * @brief Implements an Infinite Impulse Response (IIR) filter.
80 */
81 class IirFilter final : public DspFilter
82 {
83 public:
84 /**
85 * @brief Constructs an IIR filter with given numerator and denominator coefficients and order.
86 *
87 * @param denCoeffs Pointer to the denominator coefficients array.
88 * @param numCoeffs Pointer to the numerator coefficients array.
89 * @param order The order of the filter.
90 */
91 IirFilter(const RealType* denCoeffs, const RealType* numCoeffs, unsigned order) noexcept;
92
93 ~IirFilter() override = default;
94
95 /**
96 * @brief Filters a single sample.
97 *
98 * @param sample The sample to be filtered.
99 * @return The filtered sample.
100 */
101 RealType filter(RealType sample) noexcept override;
102
103 /**
104 * @brief Filters a block of samples.
105 *
106 * @param samples Span of samples to be filtered.
107 */
108 void filter(std::span<RealType> samples) noexcept override;
109
110 private:
111 std::vector<RealType> _a; ///< Denominator coefficients
112 std::vector<RealType> _b; ///< Numerator coefficients
113 std::vector<RealType> _w; ///< Internal state
114 unsigned _order{}; ///< Filter order
115 };
116
117 /**
118 * @class FirFilter
119 * @brief Implements a Finite Impulse Response (FIR) filter.
120 */
121 class FirFilter final : public DspFilter
122 {
123 public:
124 /**
125 * @brief Constructs an FIR filter with the given coefficients.
126 *
127 * @param coeffs Span of filter coefficients.
128 */
129 explicit FirFilter(std::span<const RealType> coeffs) :
130 _filter(coeffs.begin(), coeffs.end()), _w(coeffs.size()), _order(coeffs.size())
131 {
132 }
133
134 ~FirFilter() override = default;
135
136 RealType filter(RealType) override { return 0; }
137
138 void filter(std::span<RealType> /*samples*/) noexcept override {}
139
140 /**
141 * @brief Filters a block of complex samples.
142 *
143 * @param samples Span of complex samples to be filtered.
144 */
145 void filter(std::vector<ComplexType>& samples) const;
146
147 private:
148 std::vector<RealType> _filter; ///< Filter coefficients
149 std::vector<RealType> _w; ///< Internal state
150 std::size_t _order{}; ///< Filter order
151 };
152
153 /**
154 * @class DecadeUpsampler
155 * @brief Implements a specialized upsampler with a fixed upsampling factor of 10.
156 */
158 {
159 public:
161
162 ~DecadeUpsampler() = default;
163
164 /**
165 * @brief Upsamples a single sample.
166 *
167 * @param sample The sample to be upsampled.
168 * @param out Span of output samples.
169 */
170 void upsample(RealType sample, std::span<RealType> out) const;
171
173
175
176 DecadeUpsampler(DecadeUpsampler&&) noexcept = default;
177
178 DecadeUpsampler& operator=(DecadeUpsampler&&) noexcept = default;
179
180 private:
181 std::unique_ptr<IirFilter> _filter; ///< IIR filter for upsampling
182 };
183}
Implements a specialized upsampler with a fixed upsampling factor of 10.
DecadeUpsampler & operator=(const DecadeUpsampler &)=delete
DecadeUpsampler(const DecadeUpsampler &)=delete
DecadeUpsampler(DecadeUpsampler &&) noexcept=default
Abstract base class for digital filters.
Definition dsp_filters.h:47
DspFilter(const DspFilter &)=delete
DspFilter & operator=(const DspFilter &)=delete
virtual ~DspFilter()=default
DspFilter(DspFilter &&) noexcept=default
virtual void filter(std::span< RealType > samples)=0
Filters a block of samples.
virtual RealType filter(RealType sample)=0
Filters a single sample.
Implements a Finite Impulse Response (FIR) filter.
void filter(std::span< RealType >) noexcept override
Filters a block of samples.
~FirFilter() override=default
FirFilter(std::span< const RealType > coeffs)
Constructs an FIR filter with the given coefficients.
RealType filter(RealType) override
Filters a single sample.
Implements an Infinite Impulse Response (IIR) filter.
Definition dsp_filters.h:82
~IirFilter() override=default
Global configuration file for the project.
double RealType
Type for real numbers.
Definition config.h:27
void upsample(const std::span< const ComplexType > in, const unsigned size, std::span< ComplexType > out)
Upsamples a signal by a given ratio.
std::vector< ComplexType > downsample(std::span< const ComplexType > in)
Downsamples a signal by a given ratio.