To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / Cepstrum.h @ 51:0997774f5fdc
History | View | Annotate | Download (2.88 KB)
| 1 |
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
|---|---|
| 2 |
/*
|
| 3 |
This file is Copyright (c) 2012 Chris Cannam
|
| 4 |
|
| 5 |
Permission is hereby granted, free of charge, to any person
|
| 6 |
obtaining a copy of this software and associated documentation
|
| 7 |
files (the "Software"), to deal in the Software without
|
| 8 |
restriction, including without limitation the rights to use, copy,
|
| 9 |
modify, merge, publish, distribute, sublicense, and/or sell copies
|
| 10 |
of the Software, and to permit persons to whom the Software is
|
| 11 |
furnished to do so, subject to the following conditions:
|
| 12 |
|
| 13 |
The above copyright notice and this permission notice shall be
|
| 14 |
included in all copies or substantial portions of the Software.
|
| 15 |
|
| 16 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
| 17 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
| 18 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
| 19 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
| 20 |
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
| 21 |
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
| 22 |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
| 23 |
*/
|
| 24 |
|
| 25 |
#ifndef _CEPSTRUM_H_
|
| 26 |
#define _CEPSTRUM_H_
|
| 27 |
|
| 28 |
#include "vamp-sdk/FFT.h" |
| 29 |
#include <cmath> |
| 30 |
|
| 31 |
#include <iostream> |
| 32 |
#include <exception> |
| 33 |
|
| 34 |
class Cepstrum |
| 35 |
{
|
| 36 |
public:
|
| 37 |
/**
|
| 38 |
* Construct a cepstrum converter based on an n-point FFT.
|
| 39 |
*/
|
| 40 |
Cepstrum(int n) : m_n(n) {
|
| 41 |
if (n & (n-1)) { |
| 42 |
throw "N must be a power of two";
|
| 43 |
} |
| 44 |
} |
| 45 |
~Cepstrum() { }
|
| 46 |
|
| 47 |
/**
|
| 48 |
* Convert the given frequency-domain data to the cepstral domain.
|
| 49 |
*
|
| 50 |
* The input must be in the format used for FrequencyDomain data
|
| 51 |
* in the Vamp SDK: n/2+1 consecutive pairs of real and imaginary
|
| 52 |
* component floats corresponding to bins 0..(n/2) of the FFT
|
| 53 |
* output. Thus, n+2 values in total.
|
| 54 |
*
|
| 55 |
* The output consists of the raw cepstrum of length n.
|
| 56 |
*
|
| 57 |
* The cepstrum is calculated as the inverse FFT of a
|
| 58 |
* synthetically symmetrical base-10 log magnitude spectrum.
|
| 59 |
*
|
| 60 |
* Returns the mean magnitude of the input spectrum.
|
| 61 |
*/
|
| 62 |
double process(const float *in, double *out) { |
| 63 |
|
| 64 |
int hs = m_n/2 + 1; |
| 65 |
double *io = new double[m_n]; |
| 66 |
double *logmag = new double[m_n]; |
| 67 |
double epsilon = 1e-10; |
| 68 |
|
| 69 |
double magmean = 0.0; |
| 70 |
|
| 71 |
for (int i = 0; i < hs; ++i) { |
| 72 |
|
| 73 |
double power = in[i*2] * in[i*2] + in[i*2+1] * in[i*2+1]; |
| 74 |
double mag = sqrt(power);
|
| 75 |
magmean += mag; |
| 76 |
|
| 77 |
logmag[i] = log10(mag + epsilon); |
| 78 |
|
| 79 |
if (i > 0) { |
| 80 |
// make the log magnitude spectrum symmetrical
|
| 81 |
logmag[m_n - i] = logmag[i]; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
magmean /= hs; |
| 86 |
/*
|
| 87 |
std::cerr << "logmags:" << std::endl;
|
| 88 |
for (int i = 0; i < m_n; ++i) {
|
| 89 |
std::cerr << logmag[i] << " ";
|
| 90 |
}
|
| 91 |
std::cerr << std::endl;
|
| 92 |
*/
|
| 93 |
Vamp::FFT::inverse(m_n, logmag, 0, out, io);
|
| 94 |
|
| 95 |
delete[] logmag; |
| 96 |
delete[] io; |
| 97 |
|
| 98 |
return magmean;
|
| 99 |
} |
| 100 |
|
| 101 |
private:
|
| 102 |
int m_n;
|
| 103 |
}; |
| 104 |
|
| 105 |
#endif
|