annotate dsp/signalconditioning/FiltFilt.cpp @ 417:fa851e147e3f

Faster filter implementation with explicit FIR support
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 07 Oct 2015 10:36:09 +0100
parents f5b5f64835b9
children fdaa63607c15
rev   line source
c@225 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@225 2
c@225 3 /*
c@225 4 QM DSP Library
c@225 5
c@225 6 Centre for Digital Music, Queen Mary, University of London.
c@309 7 This file 2005-2006 Christian Landone.
c@309 8
c@309 9 This program is free software; you can redistribute it and/or
c@309 10 modify it under the terms of the GNU General Public License as
c@309 11 published by the Free Software Foundation; either version 2 of the
c@309 12 License, or (at your option) any later version. See the file
c@309 13 COPYING included with this distribution for more information.
c@225 14 */
c@225 15
c@225 16 #include "FiltFilt.h"
c@225 17
c@225 18 //////////////////////////////////////////////////////////////////////
c@225 19 // Construction/Destruction
c@225 20 //////////////////////////////////////////////////////////////////////
c@225 21
c@417 22 FiltFilt::FiltFilt(Filter::Parameters parameters) :
c@417 23 m_filter(parameters)
c@225 24 {
c@417 25 m_ord = m_filter.getOrder();
c@225 26 }
c@225 27
c@225 28 FiltFilt::~FiltFilt()
c@225 29 {
c@225 30 }
c@225 31
c@225 32 void FiltFilt::process(double *src, double *dst, unsigned int length)
c@225 33 {
c@225 34 unsigned int i;
c@225 35
c@283 36 if (length == 0) return;
c@283 37
c@225 38 unsigned int nFilt = m_ord + 1;
c@225 39 unsigned int nFact = 3 * ( nFilt - 1);
c@225 40 unsigned int nExt = length + 2 * nFact;
c@225 41
c@417 42 double *filtScratchIn = new double[ nExt ];
c@417 43 double *filtScratchOut = new double[ nExt ];
c@225 44
c@225 45 for( i = 0; i< nExt; i++ )
c@225 46 {
c@417 47 filtScratchIn[ i ] = 0.0;
c@417 48 filtScratchOut[ i ] = 0.0;
c@225 49 }
c@225 50
c@225 51 // Edge transients reflection
c@225 52 double sample0 = 2 * src[ 0 ];
c@225 53 double sampleN = 2 * src[ length - 1 ];
c@225 54
c@225 55 unsigned int index = 0;
c@225 56 for( i = nFact; i > 0; i-- )
c@225 57 {
c@417 58 filtScratchIn[ index++ ] = sample0 - src[ i ];
c@225 59 }
c@225 60 index = 0;
c@225 61 for( i = 0; i < nFact; i++ )
c@225 62 {
c@417 63 filtScratchIn[ (nExt - nFact) + index++ ] = sampleN - src[ (length - 2) - i ];
c@225 64 }
c@225 65
c@225 66 index = 0;
c@225 67 for( i = 0; i < length; i++ )
c@225 68 {
c@417 69 filtScratchIn[ i + nFact ] = src[ i ];
c@225 70 }
c@225 71
c@225 72 ////////////////////////////////
c@225 73 // Do 0Ph filtering
c@417 74 m_filter.process( filtScratchIn, filtScratchOut, nExt);
c@225 75
c@225 76 // reverse the series for FILTFILT
c@225 77 for ( i = 0; i < nExt; i++)
c@225 78 {
c@417 79 filtScratchIn[ i ] = filtScratchOut[ nExt - i - 1];
c@225 80 }
c@225 81
c@225 82 // do FILTER again
c@417 83 m_filter.process( filtScratchIn, filtScratchOut, nExt);
c@225 84
c@225 85 // reverse the series back
c@225 86 for ( i = 0; i < nExt; i++)
c@225 87 {
c@417 88 filtScratchIn[ i ] = filtScratchOut[ nExt - i - 1 ];
c@225 89 }
c@225 90 for ( i = 0;i < nExt; i++)
c@225 91 {
c@417 92 filtScratchOut[ i ] = filtScratchIn[ i ];
c@225 93 }
c@225 94
c@225 95 index = 0;
c@225 96 for( i = 0; i < length; i++ )
c@225 97 {
c@417 98 dst[ index++ ] = filtScratchOut[ i + nFact ];
c@225 99 }
c@225 100
c@417 101 delete [] filtScratchIn;
c@417 102 delete [] filtScratchOut;
c@225 103
c@225 104 }
c@225 105
c@225 106 void FiltFilt::reset()
c@225 107 {
c@225 108
c@225 109 }