-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCepster.h
executable file
·89 lines (69 loc) · 1.7 KB
/
Cepster.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#pragma once
#include <cassert>
#include <cmath>
#include <complex>
#include <memory>
#include <span>
#include <vector>
#include <StftPitchShift/FFT.h>
namespace stftpitchshift
{
template<class T>
class Cepster
{
public:
Cepster(const std::shared_ptr<FFT> fft, const size_t framesize, const double samplerate) :
fft(fft),
framesize(framesize),
samplerate(samplerate),
spectrum(framesize / 2 + 1),
cepstrum(framesize)
{
}
double quefrency() const
{
return value;
}
void quefrency(const double quefrency)
{
value = quefrency;
cutoff = static_cast<size_t>(quefrency * samplerate);
}
void lifter(const std::span<T> envelope)
{
assert(envelope.size() == spectrum.size());
for (size_t i = 0; i < envelope.size(); ++i)
{
const T value = envelope[i];
spectrum[i] = value ? std::log10(value) : -12;
}
fft->ifft(spectrum, cepstrum);
lowpass(cepstrum, cutoff);
fft->fft(cepstrum, spectrum);
for (size_t i = 0; i < envelope.size(); ++i)
{
const T value = spectrum[i].real();
envelope[i] = std::pow(T(10), value);
}
}
private:
const std::shared_ptr<FFT> fft;
const size_t framesize;
const double samplerate;
double value;
size_t cutoff;
std::vector<std::complex<T>> spectrum;
std::vector<T> cepstrum;
static void lowpass(const std::span<T> cepstrum, const size_t cutoff)
{
for (size_t i = 1; i < std::min(cutoff, cepstrum.size()); ++i)
{
cepstrum[i] *= 2;
}
for (size_t i = cutoff + 1; i < cepstrum.size(); ++i)
{
cepstrum[i] = 0;
}
}
};
}