janitor

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

rfft.cc (1668B)


      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 #include "kaldi-native-fbank/csrc/rfft.h"
     20 
     21 #include <algorithm>
     22 #include <cmath>
     23 #include <vector>
     24 
     25 #include "kaldi-native-fbank/csrc/log.h"
     26 
     27 namespace knf {
     28 
     29 // see fftsg.cc
     30 void rdft(int n, int isgn, double *a, int *ip, double *w);
     31 
     32 class Rfft::RfftImpl {
     33  public:
     34   explicit RfftImpl(int32_t n) : n_(n), ip_(2 + std::sqrt(n / 2)), w_(n / 2) {
     35     KNF_CHECK_EQ(n & (n - 1), 0);
     36   }
     37 
     38   void Compute(float *in_out) {
     39     std::vector<double> d(in_out, in_out + n_);
     40 
     41     Compute(d.data());
     42 
     43     std::copy(d.begin(), d.end(), in_out);
     44   }
     45 
     46   void Compute(double *in_out) {
     47     // 1 means forward fft
     48     rdft(n_, 1, in_out, ip_.data(), w_.data());
     49   }
     50 
     51  private:
     52   int32_t n_;
     53   std::vector<int32_t> ip_;
     54   std::vector<double> w_;
     55 };
     56 
     57 Rfft::Rfft(int32_t n) : impl_(std::make_unique<RfftImpl>(n)) {}
     58 
     59 Rfft::~Rfft() = default;
     60 
     61 void Rfft::Compute(float *in_out) { impl_->Compute(in_out); }
     62 void Rfft::Compute(double *in_out) { impl_->Compute(in_out); }
     63 
     64 }  // namespace knf