forked from kaldi-asr/kaldi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature-mfcc.cc
More file actions
191 lines (156 loc) · 6.63 KB
/
feature-mfcc.cc
File metadata and controls
191 lines (156 loc) · 6.63 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// feat/feature-mfcc.cc
// Copyright 2009-2011 Karel Vesely; Petr Motlicek
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#include "feat/feature-mfcc.h"
namespace kaldi {
Mfcc::Mfcc(const MfccOptions &opts)
: opts_(opts), feature_window_function_(opts.frame_opts), srfft_(NULL) {
int32 num_bins = opts.mel_opts.num_bins;
Matrix<BaseFloat> dct_matrix(num_bins, num_bins);
ComputeDctMatrix(&dct_matrix);
// Note that we include zeroth dct in either case. If using the
// energy we replace this with the energy. This means a different
// ordering of features than HTK.
SubMatrix<BaseFloat> dct_rows(dct_matrix, 0, opts.num_ceps, 0, num_bins);
dct_matrix_.Resize(opts.num_ceps, num_bins);
dct_matrix_.CopyFromMat(dct_rows); // subset of rows.
if (opts.cepstral_lifter != 0.0) {
lifter_coeffs_.Resize(opts.num_ceps);
ComputeLifterCoeffs(opts.cepstral_lifter, &lifter_coeffs_);
}
if (opts.energy_floor > 0.0)
log_energy_floor_ = Log(opts.energy_floor);
int32 padded_window_size = opts.frame_opts.PaddedWindowSize();
if ((padded_window_size & (padded_window_size-1)) == 0) // Is a power of two...
srfft_ = new SplitRadixRealFft<BaseFloat>(padded_window_size);
// We'll definitely need the filterbanks info for VTLN warping factor 1.0.
// [note: this call caches it.] The reason we call this here is to
// improve the efficiency of the "const" version of Compute().
GetMelBanks(1.0);
}
Mfcc::~Mfcc() {
for (std::map<BaseFloat, MelBanks*>::iterator iter = mel_banks_.begin();
iter != mel_banks_.end();
++iter)
delete iter->second;
delete srfft_;
}
const MelBanks *Mfcc::GetMelBanks(BaseFloat vtln_warp) {
MelBanks *this_mel_banks = NULL;
std::map<BaseFloat, MelBanks*>::iterator iter = mel_banks_.find(vtln_warp);
if (iter == mel_banks_.end()) {
this_mel_banks = new MelBanks(opts_.mel_opts,
opts_.frame_opts,
vtln_warp);
mel_banks_[vtln_warp] = this_mel_banks;
} else {
this_mel_banks = iter->second;
}
return this_mel_banks;
}
const MelBanks *Mfcc::GetMelBanks(BaseFloat vtln_warp, bool *must_delete) const {
MelBanks *this_mel_banks = NULL;
std::map<BaseFloat, MelBanks*>::const_iterator iter =
mel_banks_.find(vtln_warp);
if (iter == mel_banks_.end()) {
this_mel_banks = new MelBanks(opts_.mel_opts,
opts_.frame_opts,
vtln_warp);
*must_delete = true;
} else {
this_mel_banks = iter->second;
*must_delete = false;
}
return this_mel_banks;
}
void Mfcc::Compute(const VectorBase<BaseFloat> &wave,
BaseFloat vtln_warp,
Matrix<BaseFloat> *output,
Vector<BaseFloat> *wave_remainder) {
const MelBanks *this_mel_banks = GetMelBanks(vtln_warp);
ComputeInternal(wave, *this_mel_banks, output, wave_remainder);
}
void Mfcc::Compute(const VectorBase<BaseFloat> &wave,
BaseFloat vtln_warp,
Matrix<BaseFloat> *output,
Vector<BaseFloat> *wave_remainder) const {
bool must_delete_mel_banks;
const MelBanks *mel_banks = GetMelBanks(vtln_warp,
&must_delete_mel_banks);
ComputeInternal(wave, *mel_banks, output, wave_remainder);
if (must_delete_mel_banks)
delete mel_banks;
}
void Mfcc::ComputeInternal(const VectorBase<BaseFloat> &wave,
const MelBanks &mel_banks,
Matrix<BaseFloat> *output,
Vector<BaseFloat> *wave_remainder) const {
KALDI_ASSERT(output != NULL);
int32 rows_out = NumFrames(wave.Dim(), opts_.frame_opts),
cols_out = opts_.num_ceps;
if (rows_out == 0) {
output->Resize(0, 0);
if (wave_remainder != NULL)
*wave_remainder = wave;
return;
}
output->Resize(rows_out, cols_out);
if (wave_remainder != NULL)
ExtractWaveformRemainder(wave, opts_.frame_opts, wave_remainder);
Vector<BaseFloat> window; // windowed waveform.
Vector<BaseFloat> mel_energies;
std::vector<BaseFloat> temp_buffer; // used by srfft.
for (int32 r = 0; r < rows_out; r++) { // r is frame index..
BaseFloat log_energy;
ExtractWindow(wave, r, opts_.frame_opts, feature_window_function_, &window,
(opts_.use_energy && opts_.raw_energy ? &log_energy : NULL));
if (opts_.use_energy && !opts_.raw_energy)
log_energy = Log(std::max(VecVec(window, window),
std::numeric_limits<BaseFloat>::min()));
if (srfft_ != NULL) // Compute FFT using the split-radix algorithm.
srfft_->Compute(window.Data(), true, &temp_buffer);
else // An alternative algorithm that works for non-powers-of-two.
RealFft(&window, true);
// Convert the FFT into a power spectrum.
ComputePowerSpectrum(&window);
SubVector<BaseFloat> power_spectrum(window, 0, window.Dim()/2 + 1);
mel_banks.Compute(power_spectrum, &mel_energies);
// avoid log of zero (which should be prevented anyway by dithering).
mel_energies.ApplyFloor(std::numeric_limits<BaseFloat>::min());
mel_energies.ApplyLog(); // take the log.
SubVector<BaseFloat> this_mfcc(output->Row(r));
// this_mfcc = dct_matrix_ * mel_energies [which now have log]
this_mfcc.AddMatVec(1.0, dct_matrix_, kNoTrans, mel_energies, 0.0);
if (opts_.cepstral_lifter != 0.0)
this_mfcc.MulElements(lifter_coeffs_);
if (opts_.use_energy) {
if (opts_.energy_floor > 0.0 && log_energy < log_energy_floor_)
log_energy = log_energy_floor_;
this_mfcc(0) = log_energy;
}
if (opts_.htk_compat) {
BaseFloat energy = this_mfcc(0);
for (int32 i = 0; i < opts_.num_ceps-1; i++)
this_mfcc(i) = this_mfcc(i+1);
if (!opts_.use_energy)
energy *= M_SQRT2; // scale on C0 (actually removing scale
// we previously added that's part of one common definition of
// cosine transform.)
this_mfcc(opts_.num_ceps-1) = energy;
}
}
}
} // namespace kaldi