OnlineCmnInput Class Reference

#include <online-feat-input.h>

Inheritance diagram for OnlineCmnInput:
Collaboration diagram for OnlineCmnInput:

Public Member Functions

 OnlineCmnInput (OnlineFeatInputItf *input, int32 cmn_window, int32 min_window)
 
virtual bool Compute (Matrix< BaseFloat > *output)
 
virtual int32 Dim () const
 
- Public Member Functions inherited from OnlineFeatInputItf
virtual ~OnlineFeatInputItf ()
 

Private Member Functions

bool ComputeInternal (Matrix< BaseFloat > *output)
 
void AcceptFrame (const VectorBase< BaseFloat > &input)
 
void OutputFrame (VectorBase< BaseFloat > *output)
 
int32 NumOutputFrames (int32 num_new_frames, bool more_data) const
 
 KALDI_DISALLOW_COPY_AND_ASSIGN (OnlineCmnInput)
 

Private Attributes

OnlineFeatInputItfinput_
 
const int32 cmn_window_
 
const int32 min_window_
 
Matrix< BaseFloathistory_
 
int64 t_in_
 
int64 t_out_
 
Vector< double > sum_
 

Detailed Description

Definition at line 80 of file online-feat-input.h.

Constructor & Destructor Documentation

◆ OnlineCmnInput()

OnlineCmnInput ( OnlineFeatInputItf input,
int32  cmn_window,
int32  min_window 
)
inline

Definition at line 88 of file online-feat-input.h.

References OnlineFeatInputItf::Compute(), and KALDI_ASSERT.

89  : input_(input), cmn_window_(cmn_window), min_window_(min_window),
90  history_(cmn_window + 1, input->Dim()), t_in_(0), t_out_(0),
91  sum_(input->Dim()) { KALDI_ASSERT(cmn_window >= min_window && min_window > 0); }
OnlineFeatInputItf * input_
Matrix< BaseFloat > history_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

Member Function Documentation

◆ AcceptFrame()

void AcceptFrame ( const VectorBase< BaseFloat > &  input)
private

Definition at line 109 of file online-feat-input.cc.

References OnlineCmnInput::cmn_window_, OnlineCmnInput::history_, KALDI_ASSERT, MatrixBase< Real >::Row(), OnlineCmnInput::t_in_, and OnlineCmnInput::t_out_.

Referenced by OnlineCmnInput::ComputeInternal().

109  {
111  history_.Row(t_in_ % (cmn_window_ + 1)).CopyFromVec(input);
112  t_in_++;
113 }
Matrix< BaseFloat > history_
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Compute()

bool Compute ( Matrix< BaseFloat > *  output)
virtual

Implements OnlineFeatInputItf.

Definition at line 35 of file online-feat-input.cc.

References OnlineCmnInput::ComputeInternal(), MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), Matrix< Real >::Resize(), and OnlineCmnInput::t_in_.

35  {
36 
37  int32 orig_nr = output->NumRows(), orig_nc = output->NumCols();
38  int32 initial_t_in = t_in_;
39  bool ans;
40  while ((ans = ComputeInternal(output))) {
41  if (output->NumRows() == 0 &&
42  t_in_ != initial_t_in) {
43  // we produced no output but added to our internal buffer.
44  // Call ComputeInternal again.
45  initial_t_in = t_in_;
46  output->Resize(orig_nr, orig_nc); // make the same request.
47  } else {
48  return ans;
49  }
50  }
51  return ans;
52  // ans = false. If ComputeInternal returned false,
53  // it means we are done, so no point calling it again.
54 }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
kaldi::int32 int32
bool ComputeInternal(Matrix< BaseFloat > *output)
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).

◆ ComputeInternal()

bool ComputeInternal ( Matrix< BaseFloat > *  output)
private

Definition at line 76 of file online-feat-input.cc.

References OnlineCmnInput::AcceptFrame(), OnlineCmnInput::cmn_window_, OnlineFeatInputItf::Compute(), OnlineCmnInput::Dim(), rnnlm::i, OnlineCmnInput::input_, KALDI_ASSERT, MatrixBase< Real >::NumCols(), OnlineCmnInput::NumOutputFrames(), MatrixBase< Real >::NumRows(), OnlineCmnInput::OutputFrame(), Matrix< Real >::Resize(), Matrix< Real >::Swap(), OnlineCmnInput::t_in_, and OnlineCmnInput::t_out_.

Referenced by OnlineCmnInput::Compute().

76  {
77  KALDI_ASSERT(output->NumRows() > 0 && output->NumCols() == Dim());
78 
79  Matrix<BaseFloat> input;
80  input.Swap(output);
81 
82  bool more_data = input_->Compute(&input);
83 
84  int32 num_input_frames = input.NumRows();
85 
86  int32 output_frames = NumOutputFrames(num_input_frames,
87  more_data);
88  output->Resize(output_frames,
89  output_frames == 0 ? 0 : Dim());
90 
91  int32 output_counter = 0;
92  for (int32 i = 0; i < num_input_frames; i++) {
93  AcceptFrame(input.Row(i));
94  while (t_in_ >= cmn_window_ && t_out_ < t_in_) {
95  // We must output a frame now or we'll overwrite
96  // frames we need in the buffer.
97  SubVector<BaseFloat> this_frame(*output, output_counter);
98  OutputFrame(&this_frame);
99  output_counter++;
100  }
101  }
102  for (; output_counter < output_frames; output_counter++) {
103  SubVector<BaseFloat> this_frame(*output, output_counter);
104  OutputFrame(&this_frame);
105  }
106  return more_data;
107 }
void OutputFrame(VectorBase< BaseFloat > *output)
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
OnlineFeatInputItf * input_
kaldi::int32 int32
void AcceptFrame(const VectorBase< BaseFloat > &input)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
virtual bool Compute(Matrix< BaseFloat > *output)=0
int32 NumOutputFrames(int32 num_new_frames, bool more_data) const
virtual int32 Dim() const

◆ Dim()

virtual int32 Dim ( ) const
inlinevirtual

Implements OnlineFeatInputItf.

Definition at line 95 of file online-feat-input.h.

Referenced by OnlineCmnInput::ComputeInternal().

95 { return input_->Dim(); }
virtual int32 Dim() const =0
OnlineFeatInputItf * input_

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( OnlineCmnInput  )
private

◆ NumOutputFrames()

int32 NumOutputFrames ( int32  num_new_frames,
bool  more_data 
) const
private

Definition at line 57 of file online-feat-input.cc.

References OnlineCmnInput::min_window_, OnlineCmnInput::t_in_, and OnlineCmnInput::t_out_.

Referenced by OnlineCmnInput::ComputeInternal().

58  {
59  // Tells the caller, assuming we get given "num_new_frames" of input (and
60  // given knowledge of whether there is more data coming), how many frames
61  // would we be able to output?
62 
63  int32 max_t = t_in_ + num_new_frames;
64  if (max_t >= min_window_ || !more_data) {
65  // If this takes us to "min_window_" frames, we'll output all we have.
66  return num_new_frames + t_in_ - t_out_;
67  } else {
68  return 0; // We'll wait till we have at least "min_window_" frames.
69  }
70 }
kaldi::int32 int32

◆ OutputFrame()

void OutputFrame ( VectorBase< BaseFloat > *  output)
private

Definition at line 116 of file online-feat-input.cc.

References VectorBase< Real >::AddVec(), OnlineCmnInput::cmn_window_, VectorBase< Real >::CopyFromVec(), OnlineCmnInput::history_, rnnlm::i, KALDI_ASSERT, OnlineCmnInput::min_window_, MatrixBase< Real >::Row(), OnlineCmnInput::sum_, OnlineCmnInput::t_in_, and OnlineCmnInput::t_out_.

Referenced by OnlineCmnInput::ComputeInternal().

116  {
117  KALDI_ASSERT(t_out_ < t_in_); // or there is nothing to output.
118  // First set "sum_".
119  if (t_out_ == 0) { // This is the first request for an output frame,
120  // so in general we need to set sum_ to the sum of the first "min_window_"
121  // frames. We will have less than min_window_ frames if the input finished
122  // before then (if the input were not finished, we'd not have reached this
123  // code).
124  int32 num_frames = t_in_ < min_window_ ? t_in_ : min_window_;
125  for (int32 i = 0; i < num_frames; i++)
126  sum_.AddVec(1.0, history_.Row(i));
127  }
128  int32 num_history_frames;
129  if (t_out_ >= cmn_window_) num_history_frames = cmn_window_;
130  else if (t_out_ < min_window_)
131  num_history_frames = (t_in_ < min_window_ ? t_in_ : min_window_);
132  else
133  num_history_frames = t_out_;
134 
135  SubVector<BaseFloat> input_frame(history_, t_out_ % (cmn_window_ + 1));
136  output->CopyFromVec(input_frame);
137  output->AddVec(-1.0 / num_history_frames, sum_); // Apply CMN to the output.
138 
139  // Update sum.
140  if (t_out_ >= min_window_)
141  sum_.AddVec(1.0, input_frame);
142  if (t_out_ >= cmn_window_) { // Remove the frame from "cmn_window_" frames ago.
143  sum_.AddVec(-1.0, history_.Row((t_out_ - cmn_window_) % (cmn_window_ + 1)));
144  KALDI_ASSERT(t_in_ == t_out_ + 1); // or else the frame indexed t_out_ -
145  // cmn_window_ would not be the right one.
146  }
147  t_out_++;
148 }
kaldi::int32 int32
Matrix< BaseFloat > history_
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...

Member Data Documentation

◆ cmn_window_

const int32 cmn_window_
private

◆ history_

Matrix<BaseFloat> history_
private

◆ input_

OnlineFeatInputItf* input_
private

Definition at line 101 of file online-feat-input.h.

Referenced by OnlineCmnInput::ComputeInternal().

◆ min_window_

const int32 min_window_
private

◆ sum_

Vector<double> sum_
private

Definition at line 123 of file online-feat-input.h.

Referenced by OnlineCmnInput::OutputFrame().

◆ t_in_

◆ t_out_


The documentation for this class was generated from the following files: