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 @ 73:939cf0e86268
History | View | Annotate | Download (2.93 KB)
| 1 | 51:0997774f5fdc | Chris | /* -*- 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 | 73:939cf0e86268 | Chris | #ifndef CEPSTRUM_H
|
| 26 | #define CEPSTRUM_H
|
||
| 27 | 51:0997774f5fdc | Chris | |
| 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 | 73:939cf0e86268 | Chris | double re = in[i*2]; |
| 74 | double im = in[i*2+1]; |
||
| 75 | double power = re * re + im * im;
|
||
| 76 | 51:0997774f5fdc | Chris | double mag = sqrt(power);
|
| 77 | magmean += mag; |
||
| 78 | |||
| 79 | logmag[i] = log10(mag + epsilon); |
||
| 80 | |||
| 81 | if (i > 0) { |
||
| 82 | // make the log magnitude spectrum symmetrical
|
||
| 83 | logmag[m_n - i] = logmag[i]; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | magmean /= hs; |
||
| 88 | /*
|
||
| 89 | std::cerr << "logmags:" << std::endl;
|
||
| 90 | for (int i = 0; i < m_n; ++i) {
|
||
| 91 | std::cerr << logmag[i] << " ";
|
||
| 92 | }
|
||
| 93 | std::cerr << std::endl;
|
||
| 94 | */
|
||
| 95 | Vamp::FFT::inverse(m_n, logmag, 0, out, io);
|
||
| 96 | |||
| 97 | delete[] logmag; |
||
| 98 | delete[] io; |
||
| 99 | |||
| 100 | return magmean;
|
||
| 101 | } |
||
| 102 | |||
| 103 | private:
|
||
| 104 | int m_n;
|
||
| 105 | }; |
||
| 106 | |||
| 107 | #endif |