Pitch

Full doxygen reference (auto-generated from the headers): C++ API doxygen

Header: kaldi-native-fbank/csrc/feature-pitch.h — the equivalent of kaldi's compute-and-process-kaldi-pitch-feats (pitch-functions, the ICASSP 2014 pitch algorithm of Ghahremani et al.).

Streaming:

knf::PitchExtractionOptions pitch_opts;
pitch_opts.min_f0 = 50;
pitch_opts.max_f0 = 500;
knf::ProcessPitchOptions process_opts;   // 3-dim output by default

knf::OnlinePitch pitch(pitch_opts, process_opts);
pitch.AcceptWaveform(pitch_opts.samp_freq, samples, n);
pitch.InputFinished();
for (int32_t t = 0; t != pitch.NumFramesReady(); ++t) {
  std::vector<float> feat = pitch.GetFrame(t);  // size == pitch.Dim()
}

Batch (kaldi's compute-and-process-kaldi-pitch-feats):

knf::MatrixF feats = knf::ComputeAndProcessKaldiPitch(
    pitch_opts, process_opts, samples, n);  // (num_frames, Dim())

API (doxygen)

struct PitchExtractionOptions

Options for the raw pitch extraction stage (kaldi's PitchExtractionOptions).

Mirrors the flags of kaldi's compute-kaldi-pitch-feats / OnlinePitchFeature; see src/feat/pitch-functions.h.

Public Functions

inline int32_t NccfWindowSize() const
Returns:

The NCCF window size in samples after resampling. This is the "basic window size", not the full window size after extending by max-lag. Because of floating point representation, it is more reliable to divide by 1000 instead of multiplying by 0.001, but it is a bit slower.

inline int32_t NccfWindowShift() const
Returns:

The NCCF window shift in samples after resampling.

Public Members

float samp_freq = 16000

Waveform data sample frequency in Hz (must match the waveform).

Kaldi --sample-frequency.

float frame_shift_ms = 10.0

Frame shift in milliseconds. Kaldi --frame-shift.

float frame_length_ms = 25.0

Frame length in milliseconds. Kaldi --frame-length.

float preemph_coeff = 0.0

Preemphasis coefficient (use is deprecated in kaldi; kept for parity).

Kaldi --preemphasis-coefficient.

float min_f0 = 50

Min F0 to search for (Hz). Kaldi --min-f0.

float max_f0 = 400

Max F0 to search for (Hz). Kaldi --max-f0.

float soft_min_f0 = 10.0

Minimum f0, applied in a soft way; must not exceed min_f0.

Kaldi --soft-min-f0.

float penalty_factor = 0.1

Cost factor for F0 change (Viterbi inter-frame penalty).

Kaldi --penalty-factor.

float lowpass_cutoff = 1000

Cutoff frequency for the low-pass filter (Hz). Kaldi --lowpass-cutoff.

float resample_freq = 4000

Frequency that we down-sample the signal to.

Must be more than twice lowpass_cutoff. Kaldi --resample-frequency.

float delta_pitch = 0.005

The pitch tolerance in pruning lags: the relative change in pitch that the algorithm measures (the lags are spaced in a geometric progression with ratio \( 1 + \mathrm{delta\_pitch} \)).

Kaldi --delta-pitch.

float nccf_ballast = 7000

Increasing this factor reduces NCCF for quiet frames, helping ensure pitch continuity in unvoiced regions.

Kaldi --nccf-ballast.

int32_t lowpass_filter_width = 1

Integer that determines the filter width of the low-pass filter; more gives a sharper filter.

Kaldi --lowpass-filter-width.

int32_t upsample_filter_width = 5

Integer that determines the filter width when upsampling the NCCF.

Kaldi --upsample-filter-width.

int32_t max_frames_latency = 0

The maximum number of frames of latency that we allow the pitch processing to introduce, for online operation.

If you set this to a large value, there would be no inaccuracy from the Viterbi traceback (but it might make you wait to see the pitch). Normalization right context is more relevant for online latency; you can leave this at zero. Kaldi --max-frames-latency.

int32_t frames_per_chunk = 0

Only relevant for ComputeAndProcessKaldiPitch.

If nonzero, we provide the input as chunks of this size. This affects the energy normalization which has a small effect on the resulting features, especially at the beginning of a file. For best compatibility with online operation, you might want to set this to a small value, like one frame. Kaldi --frames-per-chunk.

bool simulate_first_pass_online = false

Only relevant for ComputeAndProcessKaldiPitch, and only if frames_per_chunk is nonzero.

If true, we output the features as soon as they are available, which simulates the first-pass features you would get in online decoding. If false, the features you get will be the same as those available at the end of the utterance, after InputFinished() has been called (e.g. during lattice rescoring). Kaldi --simulate-first-pass-online.

int32_t recompute_frame = 500

Only relevant for online operation or when emulating online operation (e.g.

when setting frames_per_chunk). This is the frame-index on which we recompute the NCCF (e.g. frame-index 500 = after 5 seconds); if the segment ends before this we do it when the segment ends. We do this by re-computing the signal average energy, which affects the NCCF via the "ballast term", scaling the resampled NCCF by a factor derived from the average change in the "ballast term", and re-doing the backtrace computation. Making this infinity would be the most exact, but would introduce unwanted latency at the end of long utterances, for little benefit. Kaldi --recompute-frame.

bool nccf_ballast_online = false

This is a "hidden config" used only for testing the online pitch extraction.

If true, we compute the signal root-mean-squared for the ballast term only up to the current frame, rather than the end of the current chunk of signal. This makes the output insensitive to the chunking, which is useful for testing purposes. Kaldi --nccf-ballast-online.

bool snip_edges = true

If false, the incomplete frames near the ending edge won't be snipped, so that the number of frames is the file size divided by the frame-shift.

This makes different types of features give the same number of frames. Kaldi --snip-edges. (Note: unlike the other feature types, kaldi's pitch extractor defaults this to true.)

struct ProcessPitchOptions

Options for the pitch post-processing stage (kaldi's ProcessPitchOptions).

Determines which features are produced from the raw (NCCF, pitch) pair. With the defaults, each frame is the 3-dimensional (POV-feature, normalized-log-pitch, delta-log-pitch) used by kaldi's compute-and-process-kaldi-pitch-feats; see src/feat/pitch-functions.h.

Public Functions

inline int32_t Dim() const
Returns:

The output feature dimension implied by the add_* flags.

Public Members

float pitch_scale = 2.0

The final normalized-log-pitch feature is scaled with this value.

Kaldi --pitch-scale.

float pov_scale = 2.0

The final POV feature is scaled with this value. Kaldi --pov-scale.

float pov_offset = 0.0

An offset that can be added to the final POV feature (useful for online decoding, where we don't do CMN to the pitch-derived features).

Kaldi --pov-offset.

float delta_pitch_scale = 10.0

Term to scale the final delta log-pitch feature.

Kaldi --delta-pitch-scale.

float delta_pitch_noise_stddev = 0.005

Standard deviation for the noise we add to the delta log-pitch (before scaling); should be about the same as the delta_pitch option to pitch creation.

The purpose is to get rid of peaks in the delta-pitch caused by discretization of pitch values. (The noise is drawn from a deterministic stream — see ComputeAndProcessKaldiPitch — so the features are reproducible.) Kaldi --delta-pitch-noise-stddev.

int32_t normalization_left_context = 75

Left-context (in frames) for moving-window normalization of the log pitch.

Kaldi --normalization-left-context.

int32_t normalization_right_context = 75

Right-context (in frames) for moving-window normalization of the log pitch; should be reduced in online decoding to reduce latency.

Kaldi --normalization-right-context.

int32_t delta_window = 2

Number of frames on each side of the central frame to use for the delta window.

Kaldi --delta-window.

int32_t delay = 0

Number of frames by which the pitch information is delayed.

Kaldi --delay.

bool add_pov_feature = true

If true, the warped NCCF is added to the output features (1 dim).

Kaldi --add-pov-feature.

bool add_normalized_log_pitch = true

If true, the log-pitch with POV-weighted mean subtraction over a 1.5 second window is added to the output features (1 dim).

Kaldi --add-normalized-log-pitch.

bool add_delta_pitch = true

If true, the time derivative of log-pitch is added to the output features (1 dim).

Kaldi --add-delta-pitch.

bool add_raw_log_pitch = false

If true, log(pitch) is added to the output features (1 dim).

Kaldi --add-raw-log-pitch.

class OnlinePitch

Streaming pitch extraction with post-processing (kaldi's OnlinePitchFeature + OnlineProcessPitch).

This is the streaming interface of kaldi's compute-and-process-kaldi-pitch-feats: it consumes waveform chunks and produces the processed pitch features (by default 3-dimensional: POV, normalized-log-pitch, delta-pitch).

Usage mirrors OnlineMfcc: feed the waveform with AcceptWaveform(), call InputFinished() when no more data is coming, then read frames with GetFrame(). Frames may become available before InputFinished() (with latency: see normalization_right_context and max_frames_latency), and InputFinished() flushes the remainder.

Features for a given frame may be revised as more data arrives (the Viterbi traceback and the normalization window settle over time), except for the delta-pitch noise which is drawn once per frame. After InputFinished(), the features are final and stable.

Used by:

Batch extraction via ComputeAndProcessKaldiPitch; MFA-style feature pipelines that append pitch to MFCC (paste-feats).

Public Functions

explicit OnlinePitch(const PitchExtractionOptions &opts, const ProcessPitchOptions &popts = {})

Construct with the given extraction and post-processing options.

Parameters:
  • opts -- [in] Raw pitch extraction options.

  • popts -- [in] Post-processing options; at least one of the add_* flags must be true.

int32_t Dim() const
Returns:

The output feature dimension (= popts.Dim(), 3 by default).

float FrameShiftInSeconds() const
Returns:

The frame shift in seconds.

int32_t NumFramesReady() const
Returns:

The number of frames that are already computed and readable via GetFrame().

bool IsLastFrame(int32_t frame) const
Returns:

True if frame is the last frame (only true after InputFinished() has been called and this is the final frame).

std::vector<float> GetFrame(int32_t frame) const

Return the frame-th feature vector.

The frame is computed on demand and cached internally; calling this repeatedly for the same frame returns the same values (as long as frame < NumFramesReady()).

Parameters:

frame -- [in] Frame index, must be < NumFramesReady().

Returns:

Feature vector of size Dim().

void AcceptWaveform(float sampling_rate, const float *waveform, int32_t n)

Add waveform samples to the extractor.

Parameters:
  • sampling_rate -- [in] Sample rate of the waveform in Hz; must match opts.samp_freq.

  • waveform -- [in] Pointer to a 1-D array of n samples.

  • n -- [in] Number of samples in waveform.

void InputFinished()

Tell the extractor no more waveform will be added; this flushes out the last frames and fixes the features of all frames.

MatrixF knf::ComputeAndProcessKaldiPitch(const PitchExtractionOptions &pitch_opts, const ProcessPitchOptions &process_opts, const float *wave, int32_t n)

Batch pitch extraction (kaldi's ComputeAndProcessKaldiPitch).

Combines the raw pitch extraction and the post-processing in one call; the output equals what kaldi's compute-and-process-kaldi-pitch-feats produces for the same input and options. It is sensitive to the variables in pitch_opts that relate to online processing, i.e. max_frames_latency, frames_per_chunk, simulate_first_pass_online, recompute_frame.

Parameters:
  • pitch_opts -- [in] Raw pitch extraction options.

  • process_opts -- [in] Post-processing options.

  • wave -- [in] Pointer to a 1-D array of n waveform samples (as returned by a WAV reader; kaldi semantics, i.e. raw int16 magnitudes, not divided by 32768).

  • n -- [in] Number of samples in wave.

Returns:

Feature matrix of shape (num_frames, process_opts.Dim()).