feature-functions.cc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. // This file is copied/modified from kaldi/src/feat/feature-functions.cc
  19. #include "feature-functions.h"
  20. #include <cstdint>
  21. #include <vector>
  22. namespace knf {
  23. void ComputePowerSpectrum(std::vector<float> *complex_fft) {
  24. int32_t dim = complex_fft->size();
  25. // now we have in complex_fft, first half of complex spectrum
  26. // it's stored as [real0, realN/2, real1, im1, real2, im2, ...]
  27. float *p = complex_fft->data();
  28. int32_t half_dim = dim / 2;
  29. float first_energy = p[0] * p[0];
  30. float last_energy = p[1] * p[1]; // handle this special case
  31. for (int32_t i = 1; i < half_dim; ++i) {
  32. float real = p[i * 2];
  33. float im = p[i * 2 + 1];
  34. p[i] = real * real + im * im;
  35. }
  36. p[0] = first_energy;
  37. p[half_dim] = last_energy; // Will actually never be used, and anyway
  38. // if the signal has been bandlimited sensibly this should be zero.
  39. }
  40. } // namespace knf