Mercurial > hg > tipic
view src/Normalise.cpp @ 30:00df792783e3
Faster filter implementation (less copying)
author | Chris Cannam |
---|---|
date | Wed, 30 Sep 2015 13:45:08 +0100 |
parents | 3d1c5cabadcc |
children |
line wrap: on
line source
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ #include "Normalise.h" #include <cmath> using namespace std; double Normalise::norm(vector<double> v, int p) { double tot = 0.0; for (auto x: v) tot += abs(pow(x, p)); return pow(tot, 1.0 / p); } vector<double> Normalise::normalise(vector<double> v, int p, double threshold) { if (p == 0) return v; // no normalisation int n = v.size(); double nv = norm(v, p); if (nv < threshold) { return vector<double>(n, 1.0 / pow(n, 1.0 / p)); // unit vector } vector<double> out; for (auto x: v) out.push_back(x / nv); return out; }