1 | // Copyright (C) 2024 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #ifndef QSINEWAVEVALIDATOR_H |
5 | #define QSINEWAVEVALIDATOR_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <array> |
19 | |
20 | // sine wave validator: accumulating peak value and result of passing the signal through a notch |
21 | // filter. A sine wave of that frequency should not pass the notch filter, so if the peak exceeds a |
22 | // small threshold, it we can detect discontinuities for example. |
23 | struct QSineWaveValidator |
24 | { |
25 | QSineWaveValidator(const QSineWaveValidator &) = delete; |
26 | QSineWaveValidator(QSineWaveValidator &&) = delete; |
27 | QSineWaveValidator &operator=(const QSineWaveValidator &) = delete; |
28 | QSineWaveValidator &operator=(QSineWaveValidator &&) = delete; |
29 | |
30 | QSineWaveValidator(float notchFrequency, float sampleRate); |
31 | |
32 | void feedSample(float sample); |
33 | |
34 | float peak() const { return accumPeak; } |
35 | float notchPeak() const; |
36 | |
37 | private: |
38 | float a0, a1, a2; |
39 | float b0, b1, b2; |
40 | |
41 | std::array<float, 3> x{}, y{}; |
42 | int framesBeforeAnalysis = 128; // unscientific estimate, larger than the transient response |
43 | // time for the IIR filter |
44 | int pendingFramesBeforeAnalysis = framesBeforeAnalysis; |
45 | |
46 | float accumPeak{}; |
47 | float accumNotchPeak{}; |
48 | }; |
49 | |
50 | #endif // QSINEWAVEVALIDATOR_H |
51 |