view src/DistanceMetric.h @ 172:30d59e1e4232 structure

Minor tidy
author Chris Cannam
date Fri, 06 Feb 2015 18:09:18 +0000
parents d6c1556fadd0
children 24ddab06aace
line wrap: on
line source
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Vamp feature extraction plugin using the MATCH audio alignment
    algorithm.

    Centre for Digital Music, Queen Mary, University of London.
    This file copyright 2007 Simon Dixon, Chris Cannam and QMUL.
    
    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 DISTANCE_METRIC_H
#define DISTANCE_METRIC_H

#include <vector>

class DistanceMetric
{
public:
    enum Metric {

        /** Calculate the Manhattan distance between feature
         *  vectors. If the vectors contain energy, as the default
         *  MATCH feature does, this could be considered as a squared
         *  Euclidean distance metric. */
        Manhattan,

        /** Calculate the Euclidean distance between feature vectors. */
        Euclidean,

        /** Calculate the cosine distance between feature vectors. The
         *  normalisation setting will be ignored as the result is
         *  already magnitude-independent. */
        Cosine,
    };

    enum DistanceNormalisation {
            
        /** Do not normalise distance metrics */
        NoDistanceNormalisation,

        /** Normalise distance metric for pairs of frames by the sum
         *  of the two frames. */
        NormaliseDistanceToSum,

        /** Normalise distance metric for pairs of frames by the log
         *  of the sum of the frames. */
        NormaliseDistanceToLogSum,
    };
    
    enum NoiseAddition {

        /** Don't add noise. */
        NoNoise,

        /** Add a constant noise term. This can help avoid
         *  mis-tracking when one file contains a lot of silence. */
        AddNoise,
    };
    
    struct Parameters {

        Parameters() :
            metric(Manhattan),
            norm(NormaliseDistanceToLogSum),
            noise(AddNoise)
        {}

        Metric metric;
        DistanceNormalisation norm;
        NoiseAddition noise;
    };
    
    DistanceMetric(Parameters params);
    
    /** Calculates the distance in some metric between two vectors,
     *  with an optional normalisation by the combined values in the
     *  vectors. Note that normalisation assumes the values are all
     *  non-negative.
     *
     *  @param f1 one of the vectors involved in the distance calculation
     *  @param f2 one of the vectors involved in the distance calculation
     *  @return the distance
     */
    double calcDistance(const std::vector<double> &f1,
			const std::vector<double> &f2);
    
private:
    Parameters m_params;
};

#endif