-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathfeature-spectrogram.cc
87 lines (67 loc) · 3.12 KB
/
feature-spectrogram.cc
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
// feat/feature-spectrogram.cc
// Copyright 2009-2012 Karel Vesely
// Copyright 2012 Navdeep Jaitly
// 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-spectrogram.h"
namespace kaldi {
SpectrogramComputer::SpectrogramComputer(const SpectrogramOptions &opts)
: opts_(opts), srfft_(NULL) {
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);
}
SpectrogramComputer::SpectrogramComputer(const SpectrogramComputer &other):
opts_(other.opts_), log_energy_floor_(other.log_energy_floor_), srfft_(NULL) {
if (other.srfft_ != NULL)
srfft_ = new SplitRadixRealFft<BaseFloat>(*other.srfft_);
}
SpectrogramComputer::~SpectrogramComputer() {
delete srfft_;
}
void SpectrogramComputer::Compute(BaseFloat signal_raw_log_energy,
BaseFloat vtln_warp,
VectorBase<BaseFloat> *signal_frame,
VectorBase<BaseFloat> *feature) {
KALDI_ASSERT(signal_frame->Dim() == opts_.frame_opts.PaddedWindowSize() &&
feature->Dim() == this->Dim());
// Compute energy after window function (not the raw one)
if (!opts_.raw_energy)
signal_raw_log_energy = Log(std::max<BaseFloat>(VecVec(*signal_frame, *signal_frame),
std::numeric_limits<float>::epsilon()));
if (srfft_ != NULL) // Compute FFT using split-radix algorithm.
srfft_->Compute(signal_frame->Data(), true);
else // An alternative algorithm that works for non-powers-of-two
RealFft(signal_frame, true);
if (opts_.return_raw_fft) {
feature->CopyFromVec(*signal_frame);
return;
}
// Convert the FFT into a power spectrum.
ComputePowerSpectrum(signal_frame);
SubVector<BaseFloat> power_spectrum(*signal_frame,
0, signal_frame->Dim() / 2 + 1);
power_spectrum.ApplyFloor(std::numeric_limits<float>::epsilon());
power_spectrum.ApplyLog();
feature->CopyFromVec(power_spectrum);
if (opts_.energy_floor > 0.0 && signal_raw_log_energy < log_energy_floor_)
signal_raw_log_energy = log_energy_floor_;
// The zeroth spectrogram component is always set to the signal energy,
// instead of the square of the constant component of the signal.
(*feature)(0) = signal_raw_log_energy;
}
} // namespace kaldi