janitor

janitor
git clone https://git.prime8.dev/janitor.git
Log | Files | Refs | README | LICENSE

feature-functions.cc (1604B)


      1 /**
      2  * Copyright (c)  2022  Xiaomi Corporation (authors: Fangjun Kuang)
      3  *
      4  * See LICENSE for clarification regarding multiple authors
      5  *
      6  * Licensed under the Apache License, Version 2.0 (the "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  */
     18 
     19 // This file is copied/modified from kaldi/src/feat/feature-functions.cc
     20 
     21 #include "kaldi-native-fbank/csrc/feature-functions.h"
     22 
     23 #include <cstdint>
     24 #include <vector>
     25 
     26 namespace knf {
     27 
     28 void ComputePowerSpectrum(std::vector<float> *complex_fft) {
     29   int32_t dim = complex_fft->size();
     30 
     31   // now we have in complex_fft, first half of complex spectrum
     32   // it's stored as [real0, realN/2, real1, im1, real2, im2, ...]
     33 
     34   float *p = complex_fft->data();
     35   int32_t half_dim = dim / 2;
     36   float first_energy = p[0] * p[0];
     37   float last_energy = p[1] * p[1];  // handle this special case
     38 
     39   for (int32_t i = 1; i < half_dim; ++i) {
     40     float real = p[i * 2];
     41     float im = p[i * 2 + 1];
     42     p[i] = real * real + im * im;
     43   }
     44   p[0] = first_energy;
     45   p[half_dim] = last_energy;  // Will actually never be used, and anyway
     46   // if the signal has been bandlimited sensibly this should be zero.
     47 }
     48 
     49 }  // namespace knf