Skip to content

Commit

Permalink
[src] Adding some more feature extraction options (needed by some use…
Browse files Browse the repository at this point in the history
…rs..) (kaldi-asr#3724)
  • Loading branch information
jtrmal authored and Bar-BY committed Jan 21, 2020
1 parent e6b5c5e commit 6840b39
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/feat/feature-spectrogram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ void SpectrogramComputer::Compute(BaseFloat signal_raw_log_energy,
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,
Expand Down
16 changes: 14 additions & 2 deletions src/feat/feature-spectrogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ struct SpectrogramOptions {
FrameExtractionOptions frame_opts;
BaseFloat energy_floor;
bool raw_energy; // If true, compute energy before preemphasis and windowing
bool return_raw_fft; // If true, return the raw FFT spectrum
// Note that in that case the Dim() will return double
// the expected dimension (because of the complex domain of it)

SpectrogramOptions() :
energy_floor(0.0),
raw_energy(true) {}
raw_energy(true),
return_raw_fft(false) {}

void Register(OptionsItf *opts) {
frame_opts.Register(opts);
Expand All @@ -54,6 +58,8 @@ struct SpectrogramOptions {
"std::numeric_limits<float>::epsilon().");
opts->Register("raw-energy", &raw_energy,
"If true, compute energy before preemphasis and windowing");
opts->Register("return-raw-fft", &return_raw_fft,
"If true, return raw FFT complex numbers instead of log magnitudes");
}
};

Expand All @@ -68,7 +74,13 @@ class SpectrogramComputer {
return opts_.frame_opts;
}

int32 Dim() const { return opts_.frame_opts.PaddedWindowSize() / 2 + 1; }
int32 Dim() const {
if (opts_.return_raw_fft) {
return opts_.frame_opts.PaddedWindowSize();
} else {
return opts_.frame_opts.PaddedWindowSize() / 2 + 1;
}
}

bool NeedRawLogEnergy() const { return opts_.raw_energy; }

Expand Down
4 changes: 4 additions & 0 deletions src/feat/feature-window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ FeatureWindowFunction::FeatureWindowFunction(const FrameExtractionOptions &opts)
double i_fl = static_cast<double>(i);
if (opts.window_type == "hanning") {
window(i) = 0.5 - 0.5*cos(a * i_fl);
} else if (opts.window_type == "sine") {
// when you are checking ws wikipedia, please
// note that 0.5 * a = M_PI/(frame_length-1)
window(i) = sin(0.5 * a * i_fl);
} else if (opts.window_type == "hamming") {
window(i) = 0.54 - 0.46*cos(a * i_fl);
} else if (opts.window_type == "povey") { // like hamming but goes to zero at edges.
Expand Down
4 changes: 2 additions & 2 deletions src/feat/feature-window.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct FrameExtractionOptions {
BaseFloat preemph_coeff; // Preemphasis coefficient.
bool remove_dc_offset; // Subtract mean of wave before FFT.
std::string window_type; // e.g. Hamming window
// May be "hamming", "rectangular", "povey", "hanning", "blackman"
// May be "hamming", "rectangular", "povey", "hanning", "sine", "blackman"
// "povey" is a window I made to be similar to Hamming but to go to zero at the
// edges, it's pow((0.5 - 0.5*cos(n/N*2*pi)), 0.85)
// I just don't think the Hamming window makes sense as a windowing function.
Expand Down Expand Up @@ -81,7 +81,7 @@ struct FrameExtractionOptions {
"option, e.g. to 1.0 or 0.1");
opts->Register("window-type", &window_type, "Type of window "
"(\"hamming\"|\"hanning\"|\"povey\"|\"rectangular\""
"|\"blackmann\")");
"|\"sine\"|\"blackmann\")");
opts->Register("blackman-coeff", &blackman_coeff,
"Constant coefficient for generalized Blackman window.");
opts->Register("round-to-power-of-two", &round_to_power_of_two,
Expand Down

0 comments on commit 6840b39

Please sign in to comment.