31 int exp_sign = (forward ? -1 : 1);
35 int twoN = in.
Dim(), N = twoN / 2;
36 const Real *data_in = in.
Data();
37 Real *data_out = out->
Data();
39 Real exp1N_re, exp1N_im;
40 Real fraction = exp_sign *
M_2PI /
static_cast<Real
>(N);
43 Real expm_re = 1.0, expm_im = 0.0;
45 for (
int two_m = 0; two_m < twoN; two_m+=2) {
46 Real expmn_re = 1.0, expmn_im = 0.0;
47 Real sum_re = 0.0, sum_im = 0.0;
48 for (
int two_n = 0; two_n < twoN; two_n+=2) {
52 ComplexMul(expm_re, expm_im, &expmn_re, &expmn_im);
54 data_out[two_m] = sum_re;
55 data_out[two_m + 1] = sum_im;
58 if (two_m % 10 == 0) {
60 int nextm = 1 + two_m/2;
61 Real fraction_mult = fraction * nextm;
64 ComplexMul(exp1N_re, exp1N_im, &expm_re, &expm_im);
77 #define KALDI_COMPLEXFFT_BLOCKSIZE 8192 96 template<
typename Real>
98 const int *factor_begin,
99 const int *factor_end,
bool forward,
101 if (factor_begin == factor_end) {
113 if (block_skip == 0) block_skip = 1;
114 if (block_skip < nffts) {
115 int blocks_left = nffts;
116 while (blocks_left > 0) {
117 int skip_now = std::min(blocks_left, block_skip);
119 blocks_left -= skip_now;
120 data += skip_now * N*2;
127 int P = *factor_begin;
132 if (P > 1 && Q > 1) {
134 Real *data_thisblock = data;
136 Real *data_tmp = tmp_vec->
Data();
137 for (
int thisfft = 0; thisfft < nffts; thisfft++, data_thisblock+=N*2) {
138 for (
int offset = 0; offset < 2; offset++) {
139 for (
int p = 0; p < P; p++) {
140 for (
int q = 0; q < Q; q++) {
141 int aidx = q*P + p, bidx = p*Q + q;
142 data_tmp[bidx] = data_thisblock[2*aidx+offset];
145 for (
int n = 0;
n < P*Q;
n++) data_thisblock[2*
n+offset] = data_tmp[
n];
154 int exp_sign = (forward ? -1 : 1);
155 Real rootN_re, rootN_im;
158 Real rootP_re, rootP_im;
165 Real *temp_a = tmp_vec->
Data();
167 Real *data_thisblock = data, *data_end = data+(N*2*nffts);
168 for (; data_thisblock != data_end; data_thisblock += N*2) {
169 Real qd_re = 1.0, qd_im = 0.0;
170 for (
int qd = 0; qd < Q; qd++) {
171 Real pdQ_qd_re = qd_re, pdQ_qd_im = qd_im;
173 for (
int pd = 0; pd < P; pd++) {
175 temp_a[pd*2] = data_thisblock[qd*2];
176 temp_a[pd*2 + 1] = data_thisblock[qd*2 + 1];
181 data_thisblock[(qd+Q)*2], data_thisblock[(qd+Q)*2 + 1],
182 &(temp_a[pd*2]), &(temp_a[pd*2 + 1]));
185 Real p_pdQ_qd_re = pdQ_qd_re, p_pdQ_qd_im = pdQ_qd_im;
186 for (
int p = 2; p < P; p++) {
187 ComplexMul(pdQ_qd_re, pdQ_qd_im, &p_pdQ_qd_re, &p_pdQ_qd_im);
188 int data_idx = p*Q + qd;
190 data_thisblock[data_idx*2], data_thisblock[data_idx*2 + 1],
191 &(temp_a[pd*2]), &(temp_a[pd*2 + 1]));
195 ComplexMul(rootP_re, rootP_im, &pdQ_qd_re, &pdQ_qd_im);
198 for (
int pd = 0; pd < P; pd++) {
199 data_thisblock[(pd*Q + qd)*2] = temp_a[pd*2];
200 data_thisblock[(pd*Q + qd)*2 + 1] = temp_a[pd*2 + 1];
202 ComplexMul(rootN_re, rootN_im, &qd_re, &qd_im);
337 if (v->
Dim()<=1)
return;
339 int N = v->
Dim() / 2;
340 std::vector<int> factors;
342 int *factor_beg = NULL;
343 if (factors.size() > 0)
344 factor_beg = &(factors[0]);
368 vtmp(2*
i) = (*v)(2*
i);
369 vtmp(2*
i+1) = (*v)(2*
i+1);
371 vtmp(2*(N-
i)) = (*v)(2*
i);
372 vtmp(2*(N-
i)+1) = -(*v)(2*
i+1);
399 Real *data = v->
Data();
400 Real rootN_re, rootN_im;
401 int forward_sign = forward ? -1 : 1;
403 Real kN_re = -forward_sign, kN_im = 0.0;
406 ComplexMul(rootN_re, rootN_im, &kN_re, &kN_im);
408 Real Ck_re, Ck_im, Dk_re, Dk_im;
410 Ck_re = 0.5 * (data[2*k] + data[N - 2*k]);
411 Ck_im = 0.5 * (data[2*k + 1] - data[N - 2*k + 1]);
413 Dk_re = 0.5 * (data[2*k + 1] + data[N - 2*k + 1]);
415 Dk_im =-0.5 * (data[2*k] - data[N - 2*k]);
429 data[2*kdash] = Ck_re;
430 data[2*kdash+1] = -Ck_im;
434 ComplexAddProduct(Dk_re, -Dk_im, -kN_re, kN_im, &(data[2*kdash]), &(data[2*kdash+1]));
444 Real zeroth = data[0] + data[1],
445 n2th = data[0] - data[1];
599 Real normalizer = std::sqrt(1.0 / static_cast<Real>(N));
602 normalizer = std::sqrt(2.0 / static_cast<Real>(N));
606 (*M)(k,
n) = normalizer
607 * std::cos( static_cast<double>(
M_PI)/N * (
n + 0.5) * k );
615 template<
typename Real>
649 U->
Row(g).CopyColFromMat(Utmp, g);
652 <<
"PCA eigenvalues are " << l;
675 KALDI_WARN <<
"In PCA, setting element " << l(g) <<
" to zero.";
686 Real sqrtlg = sqrt(l(g));
688 U->
Row(g).AddMatVec(1.0 / sqrtlg, X,
kTrans, Vtmp.
Row(g), 0.0);
695 (*A)(
n, g) = sqrtlg * Vtmp(g,
n);
702 KALDI_LOG <<
"(inner-product) PCA eigenvalues are " << l;
725 template<
typename Real>
734 mskip = minus->
Stride() - ncols;
735 const Real *adata = a.
Data(), *bdata = b.
Data();
736 Real *plusdata = plus->
Data(), *minusdata = minus->
Data();
739 const Real *btmp = bdata;
740 Real multiple = alpha * *adata;
741 if (multiple > 0.0) {
742 for (
int32 j = 0;
j < ncols;
j++, plusdata++, minusdata++, btmp++) {
743 if (*btmp > 0.0) *plusdata += multiple * *btmp;
744 else *minusdata -= multiple * *btmp;
747 for (
int32 j = 0;
j < ncols;
j++, plusdata++, minusdata++, btmp++) {
748 if (*btmp < 0.0) *plusdata += multiple * *btmp;
749 else *minusdata -= multiple * *btmp;
void AddMat2(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const Real beta)
rank-N update: if (transM == kNoTrans) (*this) = beta*(*this) + alpha * M * M^T, or (if transM == kTr...
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Packed symetric matrix class.
void ComplexFftRecursive(Real *data, int nffts, int N, const int *factor_begin, const int *factor_end, bool forward, Vector< Real > *tmp_vec)
ComplexFftRecursive is a recursive function that computes the complex FFT of size N...
void Transpose()
Transpose the matrix.
void RealFftInefficient(VectorBase< Real > *v, bool forward)
Inefficient version of Fourier transform, for testing purposes.
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Base class which provides matrix operations not involving resizing or allocation. ...
const Real * Data() const
Gives pointer to raw data (const).
void ComputeDctMatrix(Matrix< Real > *M)
ComputeDctMatrix computes a matrix corresponding to the DCT, such that M * v equals the DCT of vector...
void Eig(VectorBase< Real > *s, MatrixBase< Real > *P=NULL) const
Solves the symmetric eigenvalue problem: at end we should have (*this) = P * diag(s) * P^T...
A class for storing matrices.
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void DestructiveSvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt)
Singular value decomposition Major limitations: For nonsquare matrices, we assume m>=n (NumRows >= Nu...
void ComplexFt(const VectorBase< Real > &in, VectorBase< Real > *out, bool forward)
ComplexFt is the same as ComplexFft but it implements the Fourier transform in an inefficient way...
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
template void AddOuterProductPlusMinus< float >(float alpha, const VectorBase< float > &a, const VectorBase< float > &b, MatrixBase< float > *plus, MatrixBase< float > *minus)
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
void ComplexAddProduct(const Real &a_re, const Real &a_im, const Real &b_re, const Real &b_im, Real *c_re, Real *c_im)
ComplexMul implements, inline, the complex operation c += (a * b).
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
void TopEigs(VectorBase< Real > *s, MatrixBase< Real > *P, MatrixIndexT lanczos_dim=0) const
This function gives you, approximately, the largest eigenvalues of the symmetric matrix and the corre...
void ComputePca(const MatrixBase< Real > &X, MatrixBase< Real > *U, MatrixBase< Real > *A, bool print_eigs, bool exact)
ComputePCA does a PCA computation, using either outer products or inner products, whichever is more e...
Real * Data()
Returns a pointer to the start of the vector's data.
void ComplexMul(const Real &a_re, const Real &a_im, Real *b_re, Real *b_im)
ComplexMul implements, inline, the complex multiplication b *= a.
void AddOuterProductPlusMinus(Real alpha, const VectorBase< Real > &a, const VectorBase< Real > &b, MatrixBase< Real > *plus, MatrixBase< Real > *minus)
MatrixIndexT Dim() const
Returns the dimension of the vector.
void Scale(Real alpha)
Multiplies all elements by this constant.
#define KALDI_COMPLEXFFT_BLOCKSIZE
void ComplexFft(VectorBase< Real > *v, bool forward, Vector< Real > *tmp_in)
The function ComplexFft does an Fft on the vector argument v.
A class representing a vector.
#define KALDI_ASSERT(cond)
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
template void AddOuterProductPlusMinus< double >(double alpha, const VectorBase< double > &a, const VectorBase< double > &b, MatrixBase< double > *plus, MatrixBase< double > *minus)
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).
void OrthogonalizeRows()
This function orthogonalizes the rows of a matrix using the Gram-Schmidt process. ...
Provides a vector abstraction class.
void SortSvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt, bool sort_on_absolute_value)
Function to ensure that SVD is sorted.
void Factorize(I m, std::vector< I > *factors)
void ComplexImExp(Real x, Real *a_re, Real *a_im)
ComplexImExp implements a <– exp(i x), inline.
void RealFft(VectorBase< Real > *v, bool forward)
RealFft is a fourier transform of real inputs.
SubVector< Real > Range(const MatrixIndexT o, const MatrixIndexT l)
Returns a sub-vector of a vector (a range of elements).