Mercurial > hg > qm-dsp
comparison dsp/signalconditioning/FiltFilt.cpp @ 506:285f18c0992a
Tests and fixes for FiltFilt: Fix overrun; reset filter between forwards and backwards processes
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Wed, 05 Jun 2019 15:50:38 +0100 |
parents | b1f72e469ec8 |
children | d7b9691817a3 |
comparison
equal
deleted
inserted
replaced
505:930b5b0f707d | 506:285f18c0992a |
---|---|
13 COPYING included with this distribution for more information. | 13 COPYING included with this distribution for more information. |
14 */ | 14 */ |
15 | 15 |
16 #include "FiltFilt.h" | 16 #include "FiltFilt.h" |
17 | 17 |
18 ////////////////////////////////////////////////////////////////////// | |
19 // Construction/Destruction | |
20 ////////////////////////////////////////////////////////////////////// | |
21 | |
22 FiltFilt::FiltFilt(Filter::Parameters parameters) : | 18 FiltFilt::FiltFilt(Filter::Parameters parameters) : |
23 m_filter(parameters) | 19 m_filter(parameters) |
24 { | 20 { |
25 m_ord = m_filter.getOrder(); | 21 m_ord = m_filter.getOrder(); |
26 } | 22 } |
27 | 23 |
28 FiltFilt::~FiltFilt() | 24 FiltFilt::~FiltFilt() |
29 { | 25 { |
30 } | 26 } |
31 | 27 |
32 void FiltFilt::process(double *src, double *dst, int length) | 28 void FiltFilt::process(const double *const QM_R__ src, |
29 double *const QM_R__ dst, | |
30 const int length) | |
33 { | 31 { |
34 int i; | 32 int i; |
35 | 33 |
36 if (length == 0) return; | 34 if (length == 0) return; |
37 | 35 |
51 double sample0 = 2 * src[ 0 ]; | 49 double sample0 = 2 * src[ 0 ]; |
52 double sampleN = 2 * src[ length - 1 ]; | 50 double sampleN = 2 * src[ length - 1 ]; |
53 | 51 |
54 int index = 0; | 52 int index = 0; |
55 for (i = nFact; i > 0; i--) { | 53 for (i = nFact; i > 0; i--) { |
56 filtScratchIn[ index++ ] = sample0 - src[ i ]; | 54 if (i < length) { |
55 filtScratchIn[index] = sample0 - src[ i ]; | |
56 } | |
57 ++index; | |
57 } | 58 } |
58 index = 0; | 59 index = 0; |
59 for (i = 0; i < nFact; i++) { | 60 for (i = 0; i < nFact; i++) { |
60 filtScratchIn[ (nExt - nFact) + index++ ] = | 61 if (i < length) { |
61 sampleN - src[ (length - 2) - i ]; | 62 filtScratchIn[(nExt - nFact) + index] = |
63 sampleN - src[ (length - 2) - i ]; | |
64 } | |
65 ++index; | |
62 } | 66 } |
63 | 67 |
64 index = 0; | 68 index = 0; |
65 for (i = 0; i < length; i++) { | 69 for (i = 0; i < length; i++) { |
66 filtScratchIn[ i + nFact ] = src[ i ]; | 70 filtScratchIn[ i + nFact ] = src[ i ]; |
67 } | 71 } |
68 | 72 |
69 //////////////////////////////// | 73 //////////////////////////////// |
70 // Do 0Ph filtering | 74 // Do 0Ph filtering |
71 m_filter.process(filtScratchIn, filtScratchOut, nExt); | 75 m_filter.process(filtScratchIn, filtScratchOut, nExt); |
72 | 76 |
73 // reverse the series for FILTFILT | 77 // reverse the series for FILTFILT |
74 for (i = 0; i < nExt; i++) { | 78 for (i = 0; i < nExt; i++) { |
75 filtScratchIn[ i ] = filtScratchOut[ nExt - i - 1]; | 79 filtScratchIn[ i ] = filtScratchOut[ nExt - i - 1]; |
76 } | 80 } |
77 | 81 |
82 // clear filter state | |
83 m_filter.reset(); | |
84 | |
78 // do FILTER again | 85 // do FILTER again |
79 m_filter.process(filtScratchIn, filtScratchOut, nExt); | 86 m_filter.process(filtScratchIn, filtScratchOut, nExt); |
80 | 87 |
81 // reverse the series back | 88 // reverse the series back |
82 for (i = 0; i < nExt; i++) { | 89 for (i = 0; i < nExt; i++) { |
83 filtScratchIn[ i ] = filtScratchOut[ nExt - i - 1 ]; | 90 filtScratchIn[ i ] = filtScratchOut[ nExt - i - 1 ]; |
84 } | 91 } |
85 for (i = 0; i < nExt; i++) { | 92 for (i = 0; i < nExt; i++) { |
93 | 100 |
94 delete [] filtScratchIn; | 101 delete [] filtScratchIn; |
95 delete [] filtScratchOut; | 102 delete [] filtScratchOut; |
96 } | 103 } |
97 | 104 |
98 void FiltFilt::reset() | |
99 { | |
100 | |
101 } |