diff options
author | Paul Davis <paul@linuxaudiosystems.com> | 2011-03-02 12:37:39 +0000 |
---|---|---|
committer | Paul Davis <paul@linuxaudiosystems.com> | 2011-03-02 12:37:39 +0000 |
commit | 3deba1921bcf5bddd8bea9846a203c92b6c9239d (patch) | |
tree | 8b2e00405337396918ff28e282df14e958b84bb9 /libs/qm-dsp/dsp/signalconditioning | |
parent | fa41cfef580b2c8c04adec5b47d6cfa415ca6251 (diff) |
add queen mary DSP library
git-svn-id: svn://localhost/ardour2/branches/3.0@9029 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs/qm-dsp/dsp/signalconditioning')
-rw-r--r-- | libs/qm-dsp/dsp/signalconditioning/DFProcess.cpp | 187 | ||||
-rw-r--r-- | libs/qm-dsp/dsp/signalconditioning/DFProcess.h | 69 | ||||
-rw-r--r-- | libs/qm-dsp/dsp/signalconditioning/FiltFilt.cpp | 130 | ||||
-rw-r--r-- | libs/qm-dsp/dsp/signalconditioning/FiltFilt.h | 50 | ||||
-rw-r--r-- | libs/qm-dsp/dsp/signalconditioning/Filter.cpp | 87 | ||||
-rw-r--r-- | libs/qm-dsp/dsp/signalconditioning/Filter.h | 53 | ||||
-rw-r--r-- | libs/qm-dsp/dsp/signalconditioning/Framer.cpp | 109 | ||||
-rw-r--r-- | libs/qm-dsp/dsp/signalconditioning/Framer.h | 52 |
8 files changed, 737 insertions, 0 deletions
diff --git a/libs/qm-dsp/dsp/signalconditioning/DFProcess.cpp b/libs/qm-dsp/dsp/signalconditioning/DFProcess.cpp new file mode 100644 index 0000000000..bbe308b985 --- /dev/null +++ b/libs/qm-dsp/dsp/signalconditioning/DFProcess.cpp @@ -0,0 +1,187 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + QM DSP Library + + Centre for Digital Music, Queen Mary, University of London. + This file 2005-2006 Christian Landone. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#include "DFProcess.h" +#include "maths/MathUtilities.h" + +#include <cstring> + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +DFProcess::DFProcess( DFProcConfig Config ) +{ + filtSrc = NULL; + filtDst = NULL; + m_filtScratchIn = NULL; + m_filtScratchOut = NULL; + + m_FFOrd = 0; + + initialise( Config ); +} + +DFProcess::~DFProcess() +{ + deInitialise(); +} + +void DFProcess::initialise( DFProcConfig Config ) +{ + m_length = Config.length; + m_winPre = Config.winPre; + m_winPost = Config.winPost; + m_alphaNormParam = Config.AlphaNormParam; + + m_isMedianPositive = Config.isMedianPositive; + + filtSrc = new double[ m_length ]; + filtDst = new double[ m_length ]; + + + //Low Pass Smoothing Filter Config + m_FilterConfigParams.ord = Config.LPOrd; + m_FilterConfigParams.ACoeffs = Config.LPACoeffs; + m_FilterConfigParams.BCoeffs = Config.LPBCoeffs; + + m_FiltFilt = new FiltFilt( m_FilterConfigParams ); +} + +void DFProcess::deInitialise() +{ + delete [] filtSrc; + + delete [] filtDst; + + delete [] m_filtScratchIn; + + delete [] m_filtScratchOut; + + delete m_FiltFilt; +} + +void DFProcess::process(double *src, double* dst) +{ + if (m_length == 0) return; + + removeDCNormalize( src, filtSrc ); + + m_FiltFilt->process( filtSrc, filtDst, m_length ); + + medianFilter( filtDst, dst ); +} + + +void DFProcess::medianFilter(double *src, double *dst) +{ + int i,k,j,l; + int index = 0; + + double val = 0; + + double* y = new double[ m_winPost + m_winPre + 1]; + memset( y, 0, sizeof( double ) * ( m_winPost + m_winPre + 1) ); + + double* scratch = new double[ m_length ]; + + for( i = 0; i < m_winPre; i++) + { + if (index >= m_length) break; + + k = i + m_winPost + 1; + + for( j = 0; j < k; j++) + { + y[ j ] = src[ j ]; + } + scratch[ index ] = MathUtilities::median( y, k ); + index++; + } + + for( i = 0; i + m_winPost + m_winPre < m_length; i ++) + { + if (index >= m_length) break; + + + l = 0; + for( j = i; j < ( i + m_winPost + m_winPre + 1); j++) + { + y[ l ] = src[ j ]; + l++; + } + + scratch[ index++ ] = MathUtilities::median( y, (m_winPost + m_winPre + 1 )); + } + + for( i = std::max( m_length - m_winPost, 1); i < m_length; i++) + { + if (index >= m_length) break; + + k = std::max( i - m_winPre, 1); + + l = 0; + for( j = k; j < m_length; j++) + { + y[ l ] = src[ j ]; + + l++; + } + + scratch[ index++ ] = MathUtilities::median( y, l); + } + + + for( i = 0; i < m_length; i++ ) + { + val = src[ i ] - scratch[ i ];// - 0.033; + + if( m_isMedianPositive ) + { + if( val > 0 ) + { + dst[ i ] = val; + } + else + { + dst[ i ] = 0; + } + } + else + { + dst[ i ] = val; + } + } + + delete [] y; + delete [] scratch; +} + + +void DFProcess::removeDCNormalize( double *src, double*dst ) +{ + double DFmax = 0; + double DFMin = 0; + double DFAlphaNorm = 0; + + MathUtilities::getFrameMinMax( src, m_length, &DFMin, &DFmax ); + + MathUtilities::getAlphaNorm( src, m_length, m_alphaNormParam, &DFAlphaNorm ); + + for( unsigned int i = 0; i< m_length; i++) + { + dst[ i ] = ( src[ i ] - DFMin ) / DFAlphaNorm; + } +} diff --git a/libs/qm-dsp/dsp/signalconditioning/DFProcess.h b/libs/qm-dsp/dsp/signalconditioning/DFProcess.h new file mode 100644 index 0000000000..cba779f02f --- /dev/null +++ b/libs/qm-dsp/dsp/signalconditioning/DFProcess.h @@ -0,0 +1,69 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + QM DSP Library + + Centre for Digital Music, Queen Mary, University of London. + This file 2005-2006 Christian Landone. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#ifndef CDFPROCESS_H +#define CDFPROCESS_H + +#include <stdio.h> +#include "FiltFilt.h" + +struct DFProcConfig{ + unsigned int length; + unsigned int LPOrd; + double *LPACoeffs; + double *LPBCoeffs; + unsigned int winPre; + unsigned int winPost; + double AlphaNormParam; + bool isMedianPositive; +}; + +class DFProcess +{ +public: + DFProcess( DFProcConfig Config ); + virtual ~DFProcess(); + + void process( double* src, double* dst ); + + +private: + void initialise( DFProcConfig Config ); + void deInitialise(); + void removeDCNormalize( double *src, double*dst ); + void medianFilter( double* src, double* dst ); + + int m_length; + int m_FFOrd; + + int m_winPre; + int m_winPost; + + double m_alphaNormParam; + + double* filtSrc; + double* filtDst; + + double* m_filtScratchIn; + double* m_filtScratchOut; + + FiltFiltConfig m_FilterConfigParams; + + FiltFilt* m_FiltFilt; + + bool m_isMedianPositive; +}; + +#endif diff --git a/libs/qm-dsp/dsp/signalconditioning/FiltFilt.cpp b/libs/qm-dsp/dsp/signalconditioning/FiltFilt.cpp new file mode 100644 index 0000000000..3163bfe0e3 --- /dev/null +++ b/libs/qm-dsp/dsp/signalconditioning/FiltFilt.cpp @@ -0,0 +1,130 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + QM DSP Library + + Centre for Digital Music, Queen Mary, University of London. + This file 2005-2006 Christian Landone. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#include "FiltFilt.h" + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +FiltFilt::FiltFilt( FiltFiltConfig Config ) +{ + m_filtScratchIn = NULL; + m_filtScratchOut = NULL; + m_ord = 0; + + initialise( Config ); +} + +FiltFilt::~FiltFilt() +{ + deInitialise(); +} + +void FiltFilt::initialise( FiltFiltConfig Config ) +{ + m_ord = Config.ord; + m_filterConfig.ord = Config.ord; + m_filterConfig.ACoeffs = Config.ACoeffs; + m_filterConfig.BCoeffs = Config.BCoeffs; + + m_filter = new Filter( m_filterConfig ); +} + +void FiltFilt::deInitialise() +{ + delete m_filter; +} + + +void FiltFilt::process(double *src, double *dst, unsigned int length) +{ + unsigned int i; + + if (length == 0) return; + + unsigned int nFilt = m_ord + 1; + unsigned int nFact = 3 * ( nFilt - 1); + unsigned int nExt = length + 2 * nFact; + + m_filtScratchIn = new double[ nExt ]; + m_filtScratchOut = new double[ nExt ]; + + + for( i = 0; i< nExt; i++ ) + { + m_filtScratchIn[ i ] = 0.0; + m_filtScratchOut[ i ] = 0.0; + } + + // Edge transients reflection + double sample0 = 2 * src[ 0 ]; + double sampleN = 2 * src[ length - 1 ]; + + unsigned int index = 0; + for( i = nFact; i > 0; i-- ) + { + m_filtScratchIn[ index++ ] = sample0 - src[ i ]; + } + index = 0; + for( i = 0; i < nFact; i++ ) + { + m_filtScratchIn[ (nExt - nFact) + index++ ] = sampleN - src[ (length - 2) - i ]; + } + + index = 0; + for( i = 0; i < length; i++ ) + { + m_filtScratchIn[ i + nFact ] = src[ i ]; + } + + //////////////////////////////// + // Do 0Ph filtering + m_filter->process( m_filtScratchIn, m_filtScratchOut, nExt); + + // reverse the series for FILTFILT + for ( i = 0; i < nExt; i++) + { + m_filtScratchIn[ i ] = m_filtScratchOut[ nExt - i - 1]; + } + + // do FILTER again + m_filter->process( m_filtScratchIn, m_filtScratchOut, nExt); + + // reverse the series back + for ( i = 0; i < nExt; i++) + { + m_filtScratchIn[ i ] = m_filtScratchOut[ nExt - i - 1 ]; + } + for ( i = 0;i < nExt; i++) + { + m_filtScratchOut[ i ] = m_filtScratchIn[ i ]; + } + + index = 0; + for( i = 0; i < length; i++ ) + { + dst[ index++ ] = m_filtScratchOut[ i + nFact ]; + } + + delete [] m_filtScratchIn; + delete [] m_filtScratchOut; + +} + +void FiltFilt::reset() +{ + +} diff --git a/libs/qm-dsp/dsp/signalconditioning/FiltFilt.h b/libs/qm-dsp/dsp/signalconditioning/FiltFilt.h new file mode 100644 index 0000000000..a6cdbacce9 --- /dev/null +++ b/libs/qm-dsp/dsp/signalconditioning/FiltFilt.h @@ -0,0 +1,50 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + QM DSP Library + + Centre for Digital Music, Queen Mary, University of London. + This file 2005-2006 Christian Landone. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#ifndef FILTFILT_H +#define FILTFILT_H + +#include "Filter.h" + +struct FiltFiltConfig{ + unsigned int ord; + double* ACoeffs; + double* BCoeffs; +}; + +class FiltFilt +{ +public: + FiltFilt( FiltFiltConfig Config ); + virtual ~FiltFilt(); + + void reset(); + void process( double* src, double* dst, unsigned int length ); + +private: + void initialise( FiltFiltConfig Config ); + void deInitialise(); + + unsigned int m_ord; + + Filter* m_filter; + + double* m_filtScratchIn; + double* m_filtScratchOut; + + FilterConfig m_filterConfig; +}; + +#endif diff --git a/libs/qm-dsp/dsp/signalconditioning/Filter.cpp b/libs/qm-dsp/dsp/signalconditioning/Filter.cpp new file mode 100644 index 0000000000..fcc12e590a --- /dev/null +++ b/libs/qm-dsp/dsp/signalconditioning/Filter.cpp @@ -0,0 +1,87 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + QM DSP Library + + Centre for Digital Music, Queen Mary, University of London. + This file 2005-2006 Christian Landone. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#include "Filter.h" + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +Filter::Filter( FilterConfig Config ) +{ + m_ord = 0; + m_outBuffer = NULL; + m_inBuffer = NULL; + + initialise( Config ); +} + +Filter::~Filter() +{ + deInitialise(); +} + +void Filter::initialise( FilterConfig Config ) +{ + m_ord = Config.ord; + m_ACoeffs = Config.ACoeffs; + m_BCoeffs = Config.BCoeffs; + + m_inBuffer = new double[ m_ord + 1 ]; + m_outBuffer = new double[ m_ord + 1 ]; + + reset(); +} + +void Filter::deInitialise() +{ + delete[] m_inBuffer; + delete[] m_outBuffer; +} + +void Filter::reset() +{ + for( unsigned int i = 0; i < m_ord+1; i++ ){ m_inBuffer[ i ] = 0.0; } + for(unsigned int i = 0; i < m_ord+1; i++ ){ m_outBuffer[ i ] = 0.0; } +} + +void Filter::process( double *src, double *dst, unsigned int length ) +{ + unsigned int SP,i,j; + + double xin,xout; + + for (SP=0;SP<length;SP++) + { + xin=src[SP]; + /* move buffer */ + for ( i = 0; i < m_ord; i++) {m_inBuffer[ m_ord - i ]=m_inBuffer[ m_ord - i - 1 ];} + m_inBuffer[0]=xin; + + xout=0.0; + for (j=0;j< m_ord + 1; j++) + xout = xout + m_BCoeffs[ j ] * m_inBuffer[ j ]; + for (j = 0; j < m_ord; j++) + xout= xout - m_ACoeffs[ j + 1 ] * m_outBuffer[ j ]; + + dst[ SP ] = xout; + for ( i = 0; i < m_ord - 1; i++ ) { m_outBuffer[ m_ord - i - 1 ] = m_outBuffer[ m_ord - i - 2 ];} + m_outBuffer[0]=xout; + + } /* end of SP loop */ +} + + + diff --git a/libs/qm-dsp/dsp/signalconditioning/Filter.h b/libs/qm-dsp/dsp/signalconditioning/Filter.h new file mode 100644 index 0000000000..b0ff3dc964 --- /dev/null +++ b/libs/qm-dsp/dsp/signalconditioning/Filter.h @@ -0,0 +1,53 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + QM DSP Library + + Centre for Digital Music, Queen Mary, University of London. + This file 2005-2006 Christian Landone. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#ifndef FILTER_H +#define FILTER_H + +#ifndef NULL +#define NULL 0 +#endif + +struct FilterConfig{ + unsigned int ord; + double* ACoeffs; + double* BCoeffs; +}; + +class Filter +{ +public: + Filter( FilterConfig Config ); + virtual ~Filter(); + + void reset(); + + void process( double *src, double *dst, unsigned int length ); + + +private: + void initialise( FilterConfig Config ); + void deInitialise(); + + unsigned int m_ord; + + double* m_inBuffer; + double* m_outBuffer; + + double* m_ACoeffs; + double* m_BCoeffs; +}; + +#endif diff --git a/libs/qm-dsp/dsp/signalconditioning/Framer.cpp b/libs/qm-dsp/dsp/signalconditioning/Framer.cpp new file mode 100644 index 0000000000..e352278ada --- /dev/null +++ b/libs/qm-dsp/dsp/signalconditioning/Framer.cpp @@ -0,0 +1,109 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + QM DSP Library + + Centre for Digital Music, Queen Mary, University of London. + This file 2005-2006 Christian Landone. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#include "Framer.h" +#include <math.h> + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +Framer::Framer() +{ + m_dataFrame = NULL; + m_strideFrame = NULL; +} + +Framer::~Framer() +{ + if( m_dataFrame != NULL ) + delete [] m_dataFrame; + + if( m_strideFrame != NULL ) + delete [] m_strideFrame; +} + +void Framer::configure( unsigned int frameLength, unsigned int hop ) +{ + m_frameLength = frameLength; + m_stepSize = hop; + + resetCounters(); + + if( m_dataFrame != NULL ) + { + delete [] m_dataFrame; + m_dataFrame = NULL; + } + m_dataFrame = new double[ m_frameLength ]; + + if( m_strideFrame != NULL ) + { + delete [] m_strideFrame; + m_strideFrame = NULL; + } + m_strideFrame = new double[ m_stepSize ]; +} + +void Framer::getFrame(double *dst) +{ + + if( (m_ulSrcIndex + ( m_frameLength) ) < m_ulSampleLen ) + { + for( unsigned int u = 0; u < m_frameLength; u++) + { + dst[ u ] = m_srcBuffer[ m_ulSrcIndex++ ]; + } + m_ulSrcIndex -= ( m_frameLength - m_stepSize ); + } + else + { + unsigned int rem = (m_ulSampleLen - m_ulSrcIndex ); + unsigned int zero = m_frameLength - rem; + + for( unsigned int u = 0; u < rem; u++ ) + { + dst[ u ] = m_srcBuffer[ m_ulSrcIndex++ ]; + } + + for( unsigned int u = 0; u < zero; u++ ) + { + dst[ rem + u ] = 0; + } + + m_ulSrcIndex -= (( rem - m_stepSize ) ); + } + + m_framesRead++; +} + +void Framer::resetCounters() +{ + m_framesRead = 0; + m_ulSrcIndex = 0; +} + +unsigned int Framer::getMaxNoFrames() +{ + return m_maxFrames; +} + +void Framer::setSource(double *src, unsigned int length) +{ + m_srcBuffer = src; + m_ulSampleLen = length; + + m_maxFrames = (unsigned int)ceil( (double)m_ulSampleLen/(double)m_stepSize ) ; +} diff --git a/libs/qm-dsp/dsp/signalconditioning/Framer.h b/libs/qm-dsp/dsp/signalconditioning/Framer.h new file mode 100644 index 0000000000..7da0718b2b --- /dev/null +++ b/libs/qm-dsp/dsp/signalconditioning/Framer.h @@ -0,0 +1,52 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + QM DSP Library + + Centre for Digital Music, Queen Mary, University of London. + This file 2005-2006 Christian Landone. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#ifndef FRAMER_H +#define FRAMER_H + +//#include <io.h> +#include <fcntl.h> +#include <stdio.h> + + +class Framer +{ +public: + void setSource( double* src, unsigned int length ); + unsigned int getMaxNoFrames(); + void getFrame( double* dst ); + void configure( unsigned int frameLength, unsigned int hop ); + Framer(); + virtual ~Framer(); + + void resetCounters(); + +private: + + unsigned long m_ulSampleLen; // DataLength (samples) + unsigned int m_framesRead; // Read Frames Index + + double* m_srcBuffer; + double* m_dataFrame; // Analysis Frame Buffer + double* m_strideFrame; // Stride Frame Buffer + unsigned int m_frameLength; // Analysis Frame Length + unsigned int m_stepSize; // Analysis Frame Stride + + unsigned int m_maxFrames; + + unsigned long m_ulSrcIndex; +}; + +#endif |