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 <memory>
16#include <span>
17#include <vector>
18
19#include "core/config.h"
20
21namespace fers_signal
22{
23 /**
24 * @brief Upsamples a signal by a given ratio.
25 *
26 * @param in Input span of complex samples.
27 * @param size Size of the input signal.
28 * @param out Output span for upsampled complex samples.
29 * @throws std::invalid_argument if the input or output spans are empty or the ratio is zero.
30 */
31 void upsample(std::span<const ComplexType> in, unsigned size, std::span<ComplexType> out);
32
33 /**
34 * @brief Downsamples a signal by a given ratio.
35 *
36 * @param in Input span of complex samples.
37 * @throws std::invalid_argument if the input or output spans are empty or the ratio is zero.
38 */
39 std::vector<ComplexType> downsample(std::span<const ComplexType> in);
40
41 /**
42 * @class DspFilter
43 * @brief Abstract base class for digital filters.
44 */
46 {
47 public:
48 DspFilter() = default;
49
50 virtual ~DspFilter() = default;
51
52 /**
53 * @brief Filters a single sample.
54 *
55 * @param sample A single real-valued sample to be filtered.
56 * @return The filtered sample.
57 */
58 virtual RealType filter(RealType sample) = 0;
59
60 /**
61 * @brief Filters a block of samples.
62 *
63 * @param samples Span of real-valued samples to be filtered.
64 */
65 virtual void filter(std::span<RealType> samples) = 0;
66
67 DspFilter(const DspFilter&) = delete;
68
69 DspFilter& operator=(const DspFilter&) = delete;
70
71 DspFilter(DspFilter&&) noexcept = default;
72
73 DspFilter& operator=(DspFilter&&) noexcept = default;
74 };
75
76 /**
77 * @class IirFilter
78 * @brief Implements an Infinite Impulse Response (IIR) filter.
79 */
80 class IirFilter final : public DspFilter
81 {
82 public:
83 /**
84 * @brief Constructs an IIR filter with given numerator and denominator coefficients and order.
85 *
86 * @param denCoeffs Pointer to the denominator coefficients array.
87 * @param numCoeffs Pointer to the numerator coefficients array.
88 * @param order The order of the filter.
89 */
90 IirFilter(const RealType* denCoeffs, const RealType* numCoeffs, unsigned order) noexcept;
91
92 ~IirFilter() override = default;
93
94 /**
95 * @brief Filters a single sample.
96 *
97 * @param sample The sample to be filtered.
98 * @return The filtered sample.
99 */
100 RealType filter(RealType sample) noexcept override;
101
102 /**
103 * @brief Filters a block of samples.
104 *
105 * @param samples Span of samples to be filtered.
106 */
107 void filter(std::span<RealType> samples) noexcept override;
108
109 private:
110 std::vector<RealType> _a; ///< Denominator coefficients
111 std::vector<RealType> _b; ///< Numerator coefficients
112 std::vector<RealType> _w; ///< Internal state
113 unsigned _order{}; ///< Filter order
114 };
115
116 /**
117 * @class FirFilter
118 * @brief Implements a Finite Impulse Response (FIR) filter.
119 */
120 class FirFilter final : public DspFilter
121 {
122 public:
123 /**
124 * @brief Constructs an FIR filter with the given coefficients.
125 *
126 * @param coeffs Span of filter coefficients.
127 */
128 explicit FirFilter(std::span<const RealType> coeffs) :
129 _filter(coeffs.begin(), coeffs.end()), _w(coeffs.size()), _order(coeffs.size())
130 {
131 }
132
133 ~FirFilter() override = default;
134
135 RealType filter(RealType) override { return 0; }
136
137 void filter(std::span<RealType> /*samples*/) noexcept override {}
138
139 /**
140 * @brief Filters a block of complex samples.
141 *
142 * @param samples Span of complex samples to be filtered.
143 */
144 void filter(std::vector<ComplexType>& samples) const;
145
146 private:
147 std::vector<RealType> _filter; ///< Filter coefficients
148 std::vector<RealType> _w; ///< Internal state
149 unsigned _order{}; ///< Filter order
150 };
151
152 /**
153 * @class DecadeUpsampler
154 * @brief Implements a specialized upsampler with a fixed upsampling factor of 10.
155 */
157 {
158 public:
160
161 ~DecadeUpsampler() = default;
162
163 /**
164 * @brief Upsamples a single sample.
165 *
166 * @param sample The sample to be upsampled.
167 * @param out Span of output samples.
168 */
169 void upsample(RealType sample, std::span<RealType> out) const;
170
172
174
175 DecadeUpsampler(DecadeUpsampler&&) noexcept = default;
176
177 DecadeUpsampler& operator=(DecadeUpsampler&&) noexcept = default;
178
179 private:
180 std::unique_ptr<IirFilter> _filter; ///< IIR filter for upsampling
181 };
182}
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:46
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:81
~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.