Mercurial > hg > qm-dsp
changeset 308:25af9a1e4ec3
* Remove some unused code; minor tidy
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Wed, 01 Dec 2010 14:05:25 +0000 |
parents | f26e2d35dde9 |
children | d5014ab8b0e5 |
files | dsp/chromagram/ChromaProcess.cpp dsp/chromagram/ChromaProcess.h dsp/segmentation/SavedFeatureSegmenter.cpp dsp/segmentation/SavedFeatureSegmenter.h dsp/segmentation/Segmenter.cpp dsp/segmentation/segment.h hmm/hmm.c hmm/hmm.h maths/Histogram.h maths/pca/pca.h |
diffstat | 10 files changed, 0 insertions(+), 800 deletions(-) [+] |
line wrap: on
line diff
--- a/dsp/chromagram/ChromaProcess.cpp Tue Jul 13 11:35:13 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,213 +0,0 @@ -/* -*- 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 copyright 2005-2006 Christian Landone. - All rights reserved. -*/ - - -#include "ChromaProcess.h" -#include "maths/Histogram.h" -#include <math.h> -////////////////////////////////////////////////////////////////////// -// Construction/Destruction -////////////////////////////////////////////////////////////////////// - -ChromaProcess::ChromaProcess() -{ - -} - -ChromaProcess::~ChromaProcess() -{ - -} - -int ChromaProcess::findChromaBias( vector<double> chromaVector, unsigned int BPO, unsigned int frames ) -{ - vector<double> newChroma; - vector<int> peakIndex; - vector<int> modPeakIndex; - - unsigned int chromaLength = chromaVector.size(); - - unsigned int newLength = chromaLength + (2*BPO); - - newChroma.resize( newLength ); - newChroma.clear(); - - modPeakIndex.resize( newLength ); - modPeakIndex.clear(); - - //adds last row at the top and first row at the bottom to create - //circularity - effectively adds 2 to the bpo-length of the vectors: - - for( unsigned int i = 0; i < BPO; i++ ) - { - newChroma.push_back( chromaVector[ chromaLength - BPO + i ] ); - } - - for( unsigned i = 0; i < chromaLength; i++ ) - { - newChroma.push_back( chromaVector[ i ] ); - } - - for( unsigned i = 0; i < BPO; i++ ) - { - newChroma.push_back( chromaVector[ i ] ); - } - - // pick peaks in the chroma - peakIndex = getPeaks( newChroma, BPO ); - - // modularises to res = bpo/12 bins: - // corrects the mod value for bin 3 - modPeakIndex = mod( peakIndex, 3 ); - - // finds the highest concentration of peaks on the bpo/12 bin resolution - THistogram<int> m_hist(3); - - double ave, adev, sdev, var, skew, ccurt; - - m_hist.compute(modPeakIndex); - - m_hist.getMoments( modPeakIndex, ave, adev, sdev, var, skew, ccurt ); - - vector <double> histogram = m_hist.geTHistogramD(); - ////////////////////////////////////////////////////////////////////////////// - - /////////////////////////////////////////////////////////////////////////// - // Find actual bias from histogram - int minIdx, maxIdx; - double min, max; - - findHistMaxMin( histogram, &max, &maxIdx, &min, &minIdx ); - -/* - FILE* foutchroma = fopen("../testdata/newchroma.bin","wb"); - FILE* foutpeaks = fopen("../testdata/peaks.bin","wb"); - - - fwrite( &chromaVector[0], sizeof(double), chromaVector.size(), foutchroma ); - fwrite( &histogram[0], sizeof(double), histogram.size(), foutpeaks ); - - fclose( foutchroma ); - fclose( foutpeaks ); -*/ - return maxIdx - 1; -} - - -vector <int> ChromaProcess::getPeaks(vector <double> chroma, unsigned int BPO) -{ - vector <int> peaks; - - double pre = 0; - double post = 0; - double current = 0; - - unsigned int BPOCounter = 0; - unsigned int mult = 0; - unsigned int idx = 0; - - for( unsigned int i = 0; i < chroma.size() - 0; i++ ) - { - BPOCounter++; - - pre = chroma[ i ]; - current = chroma[ i + 1 ]; - post = chroma[ i + 2 ]; - - if( (current > 0) && (current > pre) && (current > post) ) - { - peaks.push_back( BPOCounter + 1); - } - - - if( BPOCounter == (BPO - 2 ) ) - { - BPOCounter = 0; - i+=2; - } - - } - - /* - for( unsigned int i = 1; i < chroma.size() - 1; i++ ) - { - BPOCounter++ ; - - pre = chroma[ i - 1 ]; - current = chroma[ i ]; - post = chroma[ i + 1 ]; - - if( (current > 0) && (current > pre) && (current > post) ) - { - peaks.push_back( BPOCounter + 1 ); - } - - if( BPOCounter == (PO - 1) ) - { - BPOCounter = 1; - i+=2; - } - } - */ - return peaks; -} - -vector <int> ChromaProcess::mod(vector <int> input, int res) -{ - vector <int> result; - - for( unsigned int i = 0; i < input.size(); i++ ) - { - int val = input[ i ]; - int res = val - res * floor( (double)val / (double)res ); - - if( val != 0 ) - { - if( res == 0 ) - res = 3; - - result.push_back( res ); - } - else - { - result.push_back( val ); - } - } - return result; -} - -void ChromaProcess::findHistMaxMin( vector<double> hist, double* max, int* maxIdx, double* min, int* minIdx ) -{ - double temp = 0.0; - unsigned int vecLength = hist.size(); - - *minIdx = 0; - *maxIdx = 0; - - *min = hist[0]; - *max = *min; - - for( unsigned int u = 0; u < vecLength; u++ ) - { - temp = hist[ u ]; - - if( temp < *min ) - { - *min = temp ; - *minIdx = u; - } - if( temp > *max ) - { - *max = temp ; - *maxIdx = u; - } - - } -}
--- a/dsp/chromagram/ChromaProcess.h Tue Jul 13 11:35:13 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -/* -*- 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 copyright 2005-2006 Christian Landone. - All rights reserved. -*/ - -#ifndef CHROMAPROCESS_H -#define CHROMAPROCESS_H - -#include <vector> - -using namespace std; - -class ChromaProcess -{ -public: - void findHistMaxMin( vector<double> hist, double* max, int*maxIdx, double* min, int* minIdx ); - vector <int> mod( vector <int> input, int res ); - vector <int> getPeaks( vector <double> chroma, unsigned int BPO ); - int findChromaBias( vector<double> chromaVector, unsigned int BPO, unsigned int frames ); - ChromaProcess(); - virtual ~ChromaProcess(); - -}; - -#endif // !defined(CHROMAPROCESS_H)
--- a/dsp/segmentation/SavedFeatureSegmenter.cpp Tue Jul 13 11:35:13 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,97 +0,0 @@ -/* - * SavedFeatureSegmenter.cpp - * soundbite - * - * Created by Mark Levy on 23/03/2006. - * Copyright 2006 Centre for Digital Music, Queen Mary, University of London. All rights reserved. - * - */ - -#include <cfloat> -#include <cmath> - -#include "SavedFeatureSegmenter.h" -#include "cluster_segmenter.h" -#include "segment.h" - -SavedFeatureSegmenter::SavedFeatureSegmenter(SavedFeatureSegmenterParams params) : windowSize(params.windowSize), -hopSize(params.hopSize), -nHMMStates(params.nHMMStates), -nclusters(params.nclusters), -histogramLength(params.histogramLength), -neighbourhoodLimit(params.neighbourhoodLimit) -{ -} - -void SavedFeatureSegmenter::initialise(int fs) -{ - samplerate = fs; -} - -SavedFeatureSegmenter::~SavedFeatureSegmenter() -{ -} - -void SavedFeatureSegmenter::segment(int m) -{ - nclusters = m; - segment(); -} - -void SavedFeatureSegmenter::setFeatures(const vector<vector<double> >& f) -{ - features = f; -} - -void SavedFeatureSegmenter::segment() -{ - // for now copy the features to a native array and use the existing C segmenter... - double** arrFeatures = new double*[features.size()]; - for (int i = 0; i < features.size(); i++) - { - arrFeatures[i] = new double[features[0].size()]; // allow space for the normalised envelope - for (int j = 0; j < features[0].size(); j++) - arrFeatures[i][j] = features[i][j]; - } - - q = new int[features.size()]; - - cluster_segment(q, arrFeatures, features.size(), features[0].size(), nHMMStates, histogramLength, - nclusters, neighbourhoodLimit); - // convert the cluster assignment sequence to a segmentation - makeSegmentation(q, features.size()); - - // de-allocate arrays - delete [] q; - for (int i = 0; i < features.size(); i++) - delete [] arrFeatures[i]; - delete [] arrFeatures; - - // clear the features - clear(); -} - -void SavedFeatureSegmenter::makeSegmentation(int* q, int len) -{ - segmentation.segments.clear(); - segmentation.nsegtypes = nclusters; - segmentation.samplerate = samplerate; - - Segment segment; - segment.start = 0; - segment.type = q[0]; - - for (int i = 1; i < len; i++) - { - if (q[i] != q[i-1]) - { - segment.end = i * getHopsize(); - segmentation.segments.push_back(segment); - segment.type = q[i]; - segment.start = segment.end; - } - } - segment.end = len * getHopsize(); - segmentation.segments.push_back(segment); -} -
--- a/dsp/segmentation/SavedFeatureSegmenter.h Tue Jul 13 11:35:13 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,62 +0,0 @@ -/* - * SavedFeatureSegmenter.h - * soundbite - * - * Created by Mark Levy on 23/03/2006. - * Copyright 2006 Centre for Digital Music, Queen Mary, University of London. All rights reserved. - * - */ - -#include <vector> - -#include "segment.h" -#include "Segmenter.h" -#include "hmm/hmm.h" - -using std::vector; - -class SavedFeatureSegmenterParams -{ -public: - SavedFeatureSegmenterParams() : hopSize(0.2), windowSize(0.6), - nHMMStates(40), nclusters(10), histogramLength(15), neighbourhoodLimit(20) { } - double hopSize; // in secs - double windowSize; // in secs - int nHMMStates; - int nclusters; - int histogramLength; - int neighbourhoodLimit; -}; - -class SavedFeatureSegmenter : public Segmenter -{ -public: - SavedFeatureSegmenter(SavedFeatureSegmenterParams params); - virtual ~SavedFeatureSegmenter(); - virtual void initialise(int samplerate); - virtual int getWindowsize() { return static_cast<int>(windowSize * samplerate); } - virtual int getHopsize() { return static_cast<int>(hopSize * samplerate); } - virtual void extractFeatures(double* samples, int nsamples) { } - void setFeatures(const vector<vector<double> >& f); // provide the features yourself - virtual void segment(); // segment into default number of segment-types - void segment(int m); // segment into m segment-types - int getNSegmentTypes() { return nclusters; } -protected: - void makeSegmentation(int* q, int len); - - model_t* model; // the HMM - int* q; // the decoded HMM state sequence - vector<vector<double> > histograms; - - double hopSize; // in seconds - double windowSize; // in seconds - - // HMM parameters - int nHMMStates; - - // clustering parameters - int nclusters; - int histogramLength; - int neighbourhoodLimit; -}; -
--- a/dsp/segmentation/Segmenter.cpp Tue Jul 13 11:35:13 2010 +0000 +++ b/dsp/segmentation/Segmenter.cpp Wed Dec 01 14:05:25 2010 +0000 @@ -1,6 +1,5 @@ /* * Segmenter.cpp - * soundbite * * Created by Mark Levy on 04/04/2006. * Copyright 2006 Centre for Digital Music, Queen Mary, University of London. All rights reserved.
--- a/dsp/segmentation/segment.h Tue Jul 13 11:35:13 2010 +0000 +++ b/dsp/segmentation/segment.h Wed Dec 01 14:05:25 2010 +0000 @@ -7,7 +7,6 @@ /* * segment.h - * soundbite * * Created by Mark Levy on 06/04/2006. * Copyright 2006 Centre for Digital Music, Queen Mary, University of London. All rights reserved.
--- a/hmm/hmm.c Tue Jul 13 11:35:13 2010 +0000 +++ b/hmm/hmm.c Wed Dec 01 14:05:25 2010 +0000 @@ -1,6 +1,5 @@ /* * hmm.c - * soundbite * * Created by Mark Levy on 12/02/2006. * Copyright 2006 Centre for Digital Music, Queen Mary, University of London. All rights reserved.
--- a/hmm/hmm.h Tue Jul 13 11:35:13 2010 +0000 +++ b/hmm/hmm.h Wed Dec 01 14:05:25 2010 +0000 @@ -7,7 +7,6 @@ /* * hmm.h - * soundbite * * Created by Mark Levy on 12/02/2006. * Copyright 2006 Centre for Digital Music, Queen Mary, University of London. All rights reserved.
--- a/maths/Histogram.h Tue Jul 13 11:35:13 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,393 +0,0 @@ -/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ - -// Histogram.h: interface for the THistogram class. -// -////////////////////////////////////////////////////////////////////// - - -#ifndef HISTOGRAM_H -#define HISTOGRAM_H - - -#include <valarray> - -/*! \brief A histogram class - - This class computes the histogram of a vector. - -\par Template parameters - - - T type of input data (can be any: float, double, int, UINT, etc...) - - TOut type of output data: float or double. (default is double) - -\par Moments: - - The moments (average, standard deviation, skewness, etc.) are computed using -the algorithm of the Numerical recipies (see Numerical recipies in C, Chapter 14.1, pg 613). - -\par Example: - - This example shows the typical use of the class: -\code - // a vector containing the data - vector<float> data; - // Creating histogram using float data and with 101 containers, - THistogram<float> histo(101); - // computing the histogram - histo.compute(data); -\endcode - -Once this is done, you can get a vector with the histogram or the normalized histogram (such that it's area is 1): -\code - // getting normalized histogram - vector<float> v=histo.getNormalizedHistogram(); -\endcode - -\par Reference - - Equally spaced acsissa function integration (used in #GetArea): Numerical Recipies in C, Chapter 4.1, pg 130. - -\author Jonathan de Halleux, dehalleux@auto.ucl.ac.be, 2002 -*/ - -template<class T, class TOut = double> -class THistogram -{ -public: - //! \name Constructors - //@{ - /*! Default constructor - \param counters the number of histogram containers (default value is 10) - */ - THistogram(unsigned int counters = 10); - virtual ~THistogram() { clear();}; - //@} - - //! \name Histogram computation, update - //@{ - /*! Computes histogram of vector v - \param v vector to compute histogram - \param computeMinMax set to true if min/max of v have to be used to get the histogram limits - - This function computes the histogram of v and stores it internally. - \sa Update, GeTHistogram - */ - void compute( const std::vector<T>& v, bool computeMinMax = true); - //! Update histogram with the vector v - void update( const std::vector<T>& v); - //! Update histogram with t - void update( const T& t); - //@} - - //! \name Resetting functions - //@{ - //! Resize the histogram. Warning this function clear the histogram. - void resize( unsigned int counters ); - //! Clears the histogram - void clear() { m_counters.clear();}; - //@} - - //! \name Setters - //@{ - /*! This function sets the minimum of the histogram spectrum. - The spectrum is not recomputed, use it with care - */ - void setMinSpectrum( const T& min ) { m_min = min; computeStep();}; - /*! This function sets the minimum of the histogram spectrum. - The spectrum is not recomputed, use it with care - */ - void setMaxSpectrum( const T& max ) { m_max = max; computeStep();}; - //@} - //! \name Getters - //@{ - //! return minimum of histogram spectrum - const T& getMinSpectrum() const { return m_min;}; - //! return maximum of histogram spectrum - const T& getMaxSpectrum() const { return m_max;}; - //! return step size of histogram containers - TOut getStep() const { return m_step;}; - //! return number of points in histogram - unsigned int getSum() const; - /*! \brief returns area under the histogram - - The Simpson rule is used to integrate the histogram. - */ - TOut getArea() const; - - /*! \brief Computes the moments of the histogram - - \param data dataset - \param ave mean - \f[ \bar x = \frac{1}{N} \sum_{j=1}^N x_j\f] - \param adev mean absolute deviation - \f[ adev(X) = \frac{1}{N} \sum_{j=1}^N | x_j - \bar x |\f] - \param var average deviation: - \f[ \mbox{Var}(X) = \frac{1}{N-1} \sum_{j=1}^N (x_j - \bar x)^2\f] - \param sdev standard deviation: - \f[ \sigma(X) = \sqrt{var(\bar x) }\f] - \param skew skewness - \f[ \mbox{Skew}(X) = \frac{1}{N}\sum_{j=1}^N \left[ \frac{x_j - \bar x}{\sigma}\right]^3\f] - \param kurt kurtosis - \f[ \mbox{Kurt}(X) = \left\{ \frac{1}{N}\sum_{j=1}^N \left[ \frac{x_j - \bar x}{\sigma}\right]^4 \right\} - 3\f] - - */ - static void getMoments(const std::vector<T>& data, TOut& ave, TOut& adev, TOut& sdev, TOut& var, TOut& skew, TOut& kurt); - - //! return number of containers - unsigned int getSize() const { return m_counters.size();}; - //! returns i-th counter - unsigned int operator [] (unsigned int i) const { -// ASSERT( i < m_counters.size() ); - return m_counters[i]; - }; - //! return the computed histogram - const std::vector<unsigned int>& geTHistogram() const { return m_counters;}; - //! return the computed histogram, in TOuts - std::vector<TOut> geTHistogramD() const; - /*! return the normalized computed histogram - - \return the histogram such that the area is equal to 1 - */ - std::vector<TOut> getNormalizedHistogram() const; - //! returns left containers position - std::vector<TOut> getLeftContainers() const; - //! returns center containers position - std::vector<TOut> getCenterContainers() const; - //@} -protected: - //! Computes the step - void computeStep() { m_step = (TOut)(((TOut)(m_max-m_min)) / (m_counters.size()-1));}; - //! Data accumulators - std::vector<unsigned int> m_counters; - //! minimum of dataset - T m_min; - //! maximum of dataset - T m_max; - //! width of container - TOut m_step; -}; - -template<class T, class TOut> -THistogram<T,TOut>::THistogram(unsigned int counters) - : m_counters(counters,0), m_min(0), m_max(0), m_step(0) -{ - -} - -template<class T, class TOut> -void THistogram<T,TOut>::resize( unsigned int counters ) -{ - clear(); - - m_counters.resize(counters,0); - - computeStep(); -} - -template<class T, class TOut> -void THistogram<T,TOut>::compute( const std::vector<T>& v, bool computeMinMax) -{ - using namespace std; - unsigned int i; - int index; - - if (m_counters.empty()) - return; - - if (computeMinMax) - { - m_max = m_min = v[0]; - for (i=1;i<v.size();i++) - { - m_max = std::max( m_max, v[i]); - m_min = std::min( m_min, v[i]); - } - } - - computeStep(); - - for (i = 0;i < v.size() ; i++) - { - index=(int) floor( ((TOut)(v[i]-m_min))/m_step ) ; - - if (index >= m_counters.size() || index < 0) - return; - - m_counters[index]++; - } -} - -template<class T, class TOut> -void THistogram<T,TOut>::update( const std::vector<T>& v) -{ - if (m_counters.empty()) - return; - - computeStep(); - - TOut size = m_counters.size(); - - int index; - for (unsigned int i = 0;i < size ; i++) - { - index = (int)floor(((TOut)(v[i]-m_min))/m_step); - - if (index >= m_counters.size() || index < 0) - return; - - m_counters[index]++; - } -} - -template<class T, class TOut> -void THistogram<T,TOut>::update( const T& t) -{ - int index=(int) floor( ((TOut)(t-m_min))/m_step ) ; - - if (index >= m_counters.size() || index < 0) - return; - - m_counters[index]++; -}; - -template<class T, class TOut> -std::vector<TOut> THistogram<T,TOut>::geTHistogramD() const -{ - std::vector<TOut> v(m_counters.size()); - for (unsigned int i = 0;i<m_counters.size(); i++) - v[i]=(TOut)m_counters[i]; - - return v; -} - -template <class T, class TOut> -std::vector<TOut> THistogram<T,TOut>::getLeftContainers() const -{ - std::vector<TOut> x( m_counters.size()); - - for (unsigned int i = 0;i<m_counters.size(); i++) - x[i]= m_min + i*m_step; - - return x; -} - -template <class T, class TOut> -std::vector<TOut> THistogram<T,TOut>::getCenterContainers() const -{ - std::vector<TOut> x( m_counters.size()); - - for (unsigned int i = 0;i<m_counters.size(); i++) - x[i]= m_min + (i+0.5)*m_step; - - return x; -} - -template <class T, class TOut> -unsigned int THistogram<T,TOut>::getSum() const -{ - unsigned int sum = 0; - for (unsigned int i = 0;i<m_counters.size(); i++) - sum+=m_counters[i]; - - return sum; -} - -template <class T, class TOut> -TOut THistogram<T,TOut>::getArea() const -{ - const size_t n=m_counters.size(); - TOut area=0; - - if (n>6) - { - area=3.0/8.0*(m_counters[0]+m_counters[n-1]) - +7.0/6.0*(m_counters[1]+m_counters[n-2]) - +23.0/24.0*(m_counters[2]+m_counters[n-3]); - for (unsigned int i=3;i<n-3;i++) - { - area+=m_counters[i]; - } - } - else if (n>4) - { - area=5.0/12.0*(m_counters[0]+m_counters[n-1]) - +13.0/12.0*(m_counters[1]+m_counters[n-2]); - for (unsigned int i=2;i<n-2;i++) - { - area+=m_counters[i]; - } - } - else if (n>1) - { - area=1/2.0*(m_counters[0]+m_counters[n-1]); - for (unsigned int i=1;i<n-1;i++) - { - area+=m_counters[i]; - } - } - else - area=0; - - return area*m_step; -} - -template <class T, class TOut> -std::vector<TOut> THistogram<T,TOut>::getNormalizedHistogram() const -{ - std::vector<TOut> normCounters( m_counters.size()); - TOut area = (TOut)getArea(); - - for (unsigned int i = 0;i<m_counters.size(); i++) - { - normCounters[i]= (TOut)m_counters[i]/area; - } - - return normCounters; -}; - -template <class T, class TOut> -void THistogram<T,TOut>::getMoments(const std::vector<T>& data, TOut& ave, TOut& adev, TOut& sdev, TOut& var, TOut& skew, TOut& kurt) -{ - int j; - double ep=0.0,s,p; - const size_t n = data.size(); - - if (n <= 1) - // nrerror("n must be at least 2 in moment"); - return; - - s=0.0; // First pass to get the mean. - for (j=0;j<n;j++) - s += data[j]; - - ave=s/(n); - adev=var=skew=kurt=0.0; - /* Second pass to get the first (absolute), second, - third, and fourth moments of the - deviation from the mean. */ - - for (j=0;j<n;j++) - { - adev += fabs(s=data[j]-(ave)); - ep += s; - var += (p=s*s); - skew += (p *= s); - kurt += (p *= s); - } - - - adev /= n; - var=(var-ep*ep/n)/(n-1); // Corrected two-pass formula. - sdev=sqrt(var); // Put the pieces together according to the conventional definitions. - if (var) - { - skew /= (n*(var)*(sdev)); - kurt=(kurt)/(n*(var)*(var))-3.0; - } - else - //nrerror("No skew/kurtosis when variance = 0 (in moment)"); - return; -} - -#endif -