From ac7c48b4dee99d4c772f133d70d8d1b38262fcd2 Mon Sep 17 00:00:00 2001 From: Author Name Date: Fri, 7 Jul 2023 12:20:59 +0930 Subject: shallow zip-file copy from codec2 e9d726bf20 --- src/lpcnet_freq.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/lpcnet_freq.c (limited to 'src/lpcnet_freq.c') diff --git a/src/lpcnet_freq.c b/src/lpcnet_freq.c new file mode 100644 index 0000000..7945ee1 --- /dev/null +++ b/src/lpcnet_freq.c @@ -0,0 +1,92 @@ +/* + lpcnet_freq.c + + freq.c from LPCnet project, I think this code originally came from + Opus. +*/ + +/* Copyright (c) 2017-2018 Mozilla */ +/* + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include +#include +#include +#include +#include "comp.h" +#include "lpcnet_freq.h" + +#define SQUARE(x) ((x)*(x)) + +/* FFT bin index of centre of each band, assuming an 80 sample time + domain window (5ms at 16 kHz), which results in 40 samples in the + positive freq side of the FFT. TODO - refactor this to something more generic */ +static float eband5ms[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40 +}; + +/* bandE[i] is the sum of energy in a triangular window centred on + eband5ms[i], with adjustments for first and last band */ +int lpcnet_compute_band_energy(float *bandE, float *bandCentrekHz, COMP *X, float Fs, int Nfft) { + float sum[LPCNET_FREQ_MAX_BANDS] = {0}; + int nb_bands; + float scale; + + assert((Fs == 8000) || (Fs == 16000)); + if (Fs == 8000) + nb_bands = 14; + else + nb_bands = 18; + + /* map eband5ms[] bins to our FFT size and Fs */ + scale = ((float)Nfft/2)/eband5ms[nb_bands-1]; + + /* sum energy from either side of band centre */ + for (int i=0;i