Pitch

Pitch extraction — the equivalent of kaldi's compute-and-process-kaldi-pitch-feats (kaldi-native-fbank/csrc/feature-pitch.h, pitch-functions), implementing "A Pitch Extraction Algorithm Tuned for Automatic Speech Recognition", Ghahremani et al., ICASSP 2014.

The extractor downsamples the waveform, evaluates the normalized cross-correlation function (NCCF) at log-spaced lags between \(1/\mathrm{max\_f0}\) and \(1/\mathrm{min\_f0}\), picks a per-frame lag with a Viterbi search, and post-processes the raw \((\mathrm{NCCF}, \mathrm{pitch})\) pair into the configured features — by default the 3-dimensional (POV-feature, normalized-log-pitch, delta-log-pitch) used by kaldi.

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

import kaldi_native_fbank as knf

pitch_opts = knf.PitchExtractionOptions(min_f0=50, max_f0=500,
                                        snip_edges=True)
process_opts = knf.ProcessPitchOptions()  # 3-dim output
feats = knf.compute_and_process_kaldi_pitch(pitch_opts, process_opts,
                                            samples)  # (T, 3)

Streaming usage (same interface as kaldi_native_fbank.OnlineMfcc):

pitch = knf.OnlinePitch(pitch_opts, process_opts)
pitch.accept_waveform(16000, samples)
pitch.input_finished()
for t in range(pitch.num_frames_ready):
    frame = pitch.get_frame(t)  # np.ndarray of shape (3,)

Sample scale: feed samples at raw int16 amplitude (do not divide by 32768); kaldi's wav reader keeps raw magnitudes.

Determinism: the noise added to the delta-pitch feature (delta_pitch_noise_stddev) is drawn from a deterministic stream that reproduces the reference kaldi realization, so features are identical on every platform (and match the golden test data in tests/data/feat_golden).

API reference

class kaldi_native_fbank.PitchExtractionOptions(self: kaldi_native_fbank.PitchExtractionOptions, samp_freq: SupportsFloat = 16000, frame_shift_ms: SupportsFloat = 10.0, frame_length_ms: SupportsFloat = 25.0, preemph_coeff: SupportsFloat = 0.0, min_f0: SupportsFloat = 50, max_f0: SupportsFloat = 400, soft_min_f0: SupportsFloat = 10.0, penalty_factor: SupportsFloat = 0.1, lowpass_cutoff: SupportsFloat = 1000, resample_freq: SupportsFloat = 4000, delta_pitch: SupportsFloat = 0.005, nccf_ballast: SupportsFloat = 7000, lowpass_filter_width: SupportsInt = 1, upsample_filter_width: SupportsInt = 5, max_frames_latency: SupportsInt = 0, frames_per_chunk: SupportsInt = 0, simulate_first_pass_online: bool = False, recompute_frame: SupportsInt = 500, nccf_ballast_online: bool = False, snip_edges: bool = True)

Options for the raw pitch extraction stage.

Mirrors the flags of kaldi's compute-kaldi-pitch-feats and kaldi's PitchExtractionOptions.

Construct with the given options.

All fields are optional keyword arguments; the defaults are shown in the signature (they are kaldi's defaults).

Parameters:
  • samp_freq -- Waveform sample frequency in Hz (kaldi --sample-frequency).

  • frame_shift_ms -- Frame shift in milliseconds (kaldi --frame-shift).

  • frame_length_ms -- Frame length in milliseconds (kaldi --frame-length).

  • preemph_coeff -- Preemphasis coefficient; deprecated in kaldi (kaldi --preemphasis-coefficient).

  • min_f0 -- Min F0 to search for, in Hz (kaldi --min-f0).

  • max_f0 -- Max F0 to search for, in Hz (kaldi --max-f0).

  • soft_min_f0 -- Minimum f0 applied in a soft way; must not exceed min_f0 (kaldi --soft-min-f0).

  • penalty_factor -- Cost factor for F0 change (kaldi --penalty-factor).

  • lowpass_cutoff -- Low-pass filter cutoff in Hz (kaldi --lowpass-cutoff).

  • resample_freq -- Frequency the signal is downsampled to; must be more than twice lowpass_cutoff (kaldi --resample-frequency).

  • delta_pitch -- The pitch tolerance in pruning lags (kaldi --delta-pitch).

  • nccf_ballast -- Reduces NCCF for quiet frames when increased (kaldi --nccf-ballast).

  • lowpass_filter_width -- Low-pass filter width (kaldi --lowpass-filter-width).

  • upsample_filter_width -- NCCF upsampling filter width (kaldi --upsample-filter-width).

  • max_frames_latency -- Maximum frames of latency allowed for pitch tracking (kaldi --max-frames-latency).

  • frames_per_chunk -- Chunk size for compute_and_process_kaldi_pitch() (kaldi --frames-per-chunk).

  • simulate_first_pass_online -- Output first-pass online features in compute_and_process_kaldi_pitch() (kaldi --simulate-first-pass-online).

  • recompute_frame -- Frame at which online NCCF recomputation happens (kaldi --recompute-frame).

  • nccf_ballast_online -- Debug flag for how the NCCF ballast is computed (kaldi --nccf-ballast-online).

  • snip_edges -- If false, incomplete frames near the ending edge are not snipped (kaldi --snip-edges). Note: unlike other feature types, kaldi's pitch extractor defaults this to true.

__init__(self: kaldi_native_fbank.PitchExtractionOptions, samp_freq: SupportsFloat = 16000, frame_shift_ms: SupportsFloat = 10.0, frame_length_ms: SupportsFloat = 25.0, preemph_coeff: SupportsFloat = 0.0, min_f0: SupportsFloat = 50, max_f0: SupportsFloat = 400, soft_min_f0: SupportsFloat = 10.0, penalty_factor: SupportsFloat = 0.1, lowpass_cutoff: SupportsFloat = 1000, resample_freq: SupportsFloat = 4000, delta_pitch: SupportsFloat = 0.005, nccf_ballast: SupportsFloat = 7000, lowpass_filter_width: SupportsInt = 1, upsample_filter_width: SupportsInt = 5, max_frames_latency: SupportsInt = 0, frames_per_chunk: SupportsInt = 0, simulate_first_pass_online: bool = False, recompute_frame: SupportsInt = 500, nccf_ballast_online: bool = False, snip_edges: bool = True) → None

Construct with the given options.

All fields are optional keyword arguments; the defaults are shown in the signature (they are kaldi's defaults).

Parameters:
  • samp_freq -- Waveform sample frequency in Hz (kaldi --sample-frequency).

  • frame_shift_ms -- Frame shift in milliseconds (kaldi --frame-shift).

  • frame_length_ms -- Frame length in milliseconds (kaldi --frame-length).

  • preemph_coeff -- Preemphasis coefficient; deprecated in kaldi (kaldi --preemphasis-coefficient).

  • min_f0 -- Min F0 to search for, in Hz (kaldi --min-f0).

  • max_f0 -- Max F0 to search for, in Hz (kaldi --max-f0).

  • soft_min_f0 -- Minimum f0 applied in a soft way; must not exceed min_f0 (kaldi --soft-min-f0).

  • penalty_factor -- Cost factor for F0 change (kaldi --penalty-factor).

  • lowpass_cutoff -- Low-pass filter cutoff in Hz (kaldi --lowpass-cutoff).

  • resample_freq -- Frequency the signal is downsampled to; must be more than twice lowpass_cutoff (kaldi --resample-frequency).

  • delta_pitch -- The pitch tolerance in pruning lags (kaldi --delta-pitch).

  • nccf_ballast -- Reduces NCCF for quiet frames when increased (kaldi --nccf-ballast).

  • lowpass_filter_width -- Low-pass filter width (kaldi --lowpass-filter-width).

  • upsample_filter_width -- NCCF upsampling filter width (kaldi --upsample-filter-width).

  • max_frames_latency -- Maximum frames of latency allowed for pitch tracking (kaldi --max-frames-latency).

  • frames_per_chunk -- Chunk size for compute_and_process_kaldi_pitch() (kaldi --frames-per-chunk).

  • simulate_first_pass_online -- Output first-pass online features in compute_and_process_kaldi_pitch() (kaldi --simulate-first-pass-online).

  • recompute_frame -- Frame at which online NCCF recomputation happens (kaldi --recompute-frame).

  • nccf_ballast_online -- Debug flag for how the NCCF ballast is computed (kaldi --nccf-ballast-online).

  • snip_edges -- If false, incomplete frames near the ending edge are not snipped (kaldi --snip-edges). Note: unlike other feature types, kaldi's pitch extractor defaults this to true.

property delta_pitch

The pitch tolerance in pruning lags.

property frame_length_ms

Frame length in milliseconds.

property frame_shift_ms

Frame shift in milliseconds.

property frames_per_chunk

Chunk size for compute_and_process_kaldi_pitch().

property lowpass_cutoff

Low-pass filter cutoff in Hz.

property lowpass_filter_width

Low-pass filter width.

property max_f0

Max F0 to search for, in Hz.

property max_frames_latency

Maximum frames of latency allowed for pitch tracking.

property min_f0

Min F0 to search for, in Hz.

property nccf_ballast

Reduces NCCF for quiet frames when increased.

property nccf_ballast_online

Debug flag for how the NCCF ballast is computed.

property nccf_window_shift

The NCCF window shift in samples after resampling.

property nccf_window_size

The NCCF window size in samples after resampling.

property penalty_factor

Cost factor for F0 change.

property preemph_coeff

Preemphasis coefficient (deprecated in kaldi).

property recompute_frame

Frame at which online NCCF recomputation happens.

property resample_freq

Frequency the signal is downsampled to.

property samp_freq

Waveform sample frequency in Hz.

property simulate_first_pass_online

Output first-pass online features (batch mode only).

property snip_edges

If false, incomplete frames near the ending edge are not snipped.

property soft_min_f0

Minimum f0 applied in a soft way.

property upsample_filter_width

NCCF upsampling filter width.

class kaldi_native_fbank.ProcessPitchOptions(self: kaldi_native_fbank.ProcessPitchOptions, pitch_scale: SupportsFloat = 2.0, pov_scale: SupportsFloat = 2.0, pov_offset: SupportsFloat = 0.0, delta_pitch_scale: SupportsFloat = 10.0, delta_pitch_noise_stddev: SupportsFloat = 0.005, normalization_left_context: SupportsInt = 75, normalization_right_context: SupportsInt = 75, delta_window: SupportsInt = 2, delay: SupportsInt = 0, add_pov_feature: bool = True, add_normalized_log_pitch: bool = True, add_delta_pitch: bool = True, add_raw_log_pitch: bool = False)

Options for the pitch post-processing stage.

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) output of kaldi's compute-and-process-kaldi-pitch-feats; see kaldi's ProcessPitchOptions.

Construct with the given options.

All fields are optional keyword arguments; the defaults are shown in the signature (they are kaldi's defaults).

Parameters:
  • pitch_scale -- Scaling of the normalized log-pitch feature (kaldi --pitch-scale).

  • pov_scale -- Scaling of the POV feature (kaldi --pov-scale).

  • pov_offset -- Offset added to the POV feature (kaldi --pov-offset).

  • delta_pitch_scale -- Scaling of the delta log-pitch feature (kaldi --delta-pitch-scale).

  • delta_pitch_noise_stddev -- Stddev of the noise added to the delta log-pitch (kaldi --delta-pitch-noise-stddev).

  • normalization_left_context -- Left context (frames) for log-pitch normalization (kaldi --normalization-left-context).

  • normalization_right_context -- Right context (frames) for log-pitch normalization (kaldi --normalization-right-context).

  • delta_window -- Frames on each side used for the delta window (kaldi --delta-window).

  • delay -- Frames by which the pitch information is delayed (kaldi --delay).

  • add_pov_feature -- Add the warped NCCF to the output (1 dim).

  • add_normalized_log_pitch -- Add the mean-subtracted log-pitch (1 dim).

  • add_delta_pitch -- Add the time derivative of log-pitch (1 dim).

  • add_raw_log_pitch -- Add log(pitch) (1 dim).

__init__(self: kaldi_native_fbank.ProcessPitchOptions, pitch_scale: SupportsFloat = 2.0, pov_scale: SupportsFloat = 2.0, pov_offset: SupportsFloat = 0.0, delta_pitch_scale: SupportsFloat = 10.0, delta_pitch_noise_stddev: SupportsFloat = 0.005, normalization_left_context: SupportsInt = 75, normalization_right_context: SupportsInt = 75, delta_window: SupportsInt = 2, delay: SupportsInt = 0, add_pov_feature: bool = True, add_normalized_log_pitch: bool = True, add_delta_pitch: bool = True, add_raw_log_pitch: bool = False) → None

Construct with the given options.

All fields are optional keyword arguments; the defaults are shown in the signature (they are kaldi's defaults).

Parameters:
  • pitch_scale -- Scaling of the normalized log-pitch feature (kaldi --pitch-scale).

  • pov_scale -- Scaling of the POV feature (kaldi --pov-scale).

  • pov_offset -- Offset added to the POV feature (kaldi --pov-offset).

  • delta_pitch_scale -- Scaling of the delta log-pitch feature (kaldi --delta-pitch-scale).

  • delta_pitch_noise_stddev -- Stddev of the noise added to the delta log-pitch (kaldi --delta-pitch-noise-stddev).

  • normalization_left_context -- Left context (frames) for log-pitch normalization (kaldi --normalization-left-context).

  • normalization_right_context -- Right context (frames) for log-pitch normalization (kaldi --normalization-right-context).

  • delta_window -- Frames on each side used for the delta window (kaldi --delta-window).

  • delay -- Frames by which the pitch information is delayed (kaldi --delay).

  • add_pov_feature -- Add the warped NCCF to the output (1 dim).

  • add_normalized_log_pitch -- Add the mean-subtracted log-pitch (1 dim).

  • add_delta_pitch -- Add the time derivative of log-pitch (1 dim).

  • add_raw_log_pitch -- Add log(pitch) (1 dim).

property add_delta_pitch

Add the time derivative of log-pitch (1 dim).

property add_normalized_log_pitch

Add the mean-subtracted log-pitch (1 dim).

property add_pov_feature

Add the warped NCCF to the output (1 dim).

property add_raw_log_pitch

Add log(pitch) (1 dim).

property delay

Frames by which the pitch information is delayed.

property delta_pitch_noise_stddev

Stddev of the noise added to the delta log-pitch.

The noise is drawn from a deterministic stream, so the features are reproducible on every platform.

property delta_pitch_scale

Scaling of the delta log-pitch feature.

property delta_window

Frames on each side used for the delta window.

property dim

The output feature dimension.

property normalization_left_context

Left context (frames) for log-pitch normalization.

property normalization_right_context

Right context (frames) for log-pitch normalization.

property pitch_scale

Scaling of the normalized log-pitch feature.

property pov_offset

Offset added to the POV feature.

property pov_scale

Scaling of the POV feature.

class kaldi_native_fbank.OnlinePitch(self: kaldi_native_fbank.OnlinePitch, opts: kaldi_native_fbank.PitchExtractionOptions, popts: kaldi_native_fbank.ProcessPitchOptions = ProcessPitchOptions())

Streaming pitch extraction with post-processing.

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

Usage mirrors kaldi_native_fbank.OnlineMfcc: feed the waveform with accept_waveform(), call input_finished() when no more data is coming, then read frames with get_frame().

Construct with the given extraction and post-processing options.

Parameters:
__init__(self: kaldi_native_fbank.OnlinePitch, opts: kaldi_native_fbank.PitchExtractionOptions, popts: kaldi_native_fbank.ProcessPitchOptions = ProcessPitchOptions()) → None

Construct with the given extraction and post-processing options.

Parameters:
accept_waveform(self: kaldi_native_fbank.OnlinePitch, sampling_rate: SupportsFloat, waveform: collections.abc.Sequence[SupportsFloat]) → None

Add more waveform samples.

Parameters:
  • sampling_rate -- The sampling rate of waveform; must match opts.samp_freq.

  • waveform -- 1-D float32 samples (kaldi semantics: raw WAV magnitudes, not divided by 32768).

property dim

The dimension of the output features (3 by default).

property frame_shift_in_seconds

The frame shift in seconds.

get_frame(self: kaldi_native_fbank.OnlinePitch, frame: SupportsInt) → numpy.typing.NDArray[numpy.float32]

Return the frame-th feature vector.

Parameters:

frame -- Frame index, must be less than num_frames_ready.

Returns:

np.ndarray of shape (dim,) and dtype float32.

input_finished(self: kaldi_native_fbank.OnlinePitch) → None

Tell the extractor that no more waveform will be added.

This flushes out the last frames and fixes the features of all frames.

is_last_frame(self: kaldi_native_fbank.OnlinePitch, frame: SupportsInt) → bool

Return true if frame is the last frame.

Note: this can only return true after input_finished() has been called.

Parameters:

frame -- Frame index.

Returns:

True if the frame is the last one.

property num_frames_ready

The number of frames that have already been computed.

kaldi_native_fbank.compute_and_process_kaldi_pitch(pitch_opts: kaldi_native_fbank.PitchExtractionOptions, process_opts: kaldi_native_fbank.ProcessPitchOptions, wave: collections.abc.Sequence[SupportsFloat]) → Annotated[numpy.typing.NDArray[numpy.float32], '[m, n]']

Compute processed pitch features for a whole utterance.

This is kaldi's compute-and-process-kaldi-pitch-feats (same arithmetic): the raw (NCCF, pitch) extraction of Ghahremani et al., ICASSP 2014 followed by post-processing into the configured features (by default POV, normalized-log-pitch, delta-pitch). The output equals the kaldi binary's output value-by-value 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.

The noise added to the delta-pitch feature (process_opts.delta_pitch_noise_stddev) is drawn from a deterministic stream that reproduces the reference kaldi realization, so the result is identical on every platform.

Parameters:
  • pitch_opts -- Raw pitch extraction options.

  • process_opts -- Post-processing options.

  • wave (numpy.ndarray) -- 1-D float32 samples (kaldi semantics: raw WAV magnitudes, not divided by 32768).

Returns:

Feature matrix of shape (num_frames, process_opts.dim), float32.

Return type:

numpy.ndarray

Example

import wave
import numpy as np
import kaldi_native_fbank as knf

with wave.open("zh0_1s.wav") as f:
    samples = np.frombuffer(f.readframes(f.getnframes()), dtype=np.int16)
samples = samples.astype(np.float32)  # raw magnitudes, no /32768

pitch_opts = knf.PitchExtractionOptions(min_f0=50, max_f0=500,
                                        snip_edges=True)
feats = knf.compute_and_process_kaldi_pitch(pitch_opts,
                                            knf.ProcessPitchOptions(),
                                            samples)
# feats.shape == (num_frames, 3): POV, norm-log-pitch, delta-pitch