From 30325d24d107dbf133da39f7c96d1510fd1c9449 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Fri, 15 Aug 2025 12:42:40 +0200 Subject: Bump to codec2 version 1.2.0 --- interp.c | 247 +++++++++++++++++++++++++++++---------------------------------- 1 file changed, 112 insertions(+), 135 deletions(-) (limited to 'interp.c') diff --git a/interp.c b/interp.c index 2109acb..08a9518 100644 --- a/interp.c +++ b/interp.c @@ -25,13 +25,14 @@ along with this program; if not, see . */ +#include "interp.h" + #include #include -#include #include +#include #include "defines.h" -#include "interp.h" #include "lsp.h" #include "quantise.h" @@ -44,7 +45,7 @@ float sample_log_amp(MODEL *model, float w); AUTHOR......: David Rowe DATE CREATED: 22/8/10 - Given two frames decribed by model parameters 20ms apart, determines + Given two frames described by model parameters 20ms apart, determines the model parameters of the 10ms frame between them. Assumes voicing is available for middle (interpolated) frame. Outputs are amplitudes and Wo for the interpolated frame. @@ -107,29 +108,27 @@ void interpolate( \*---------------------------------------------------------------------------*/ -float sample_log_amp(MODEL *model, float w) -{ - int m; - float f, log_amp; +float sample_log_amp(MODEL *model, float w) { + int m; + float f, log_amp; - assert(w > 0.0); assert (w <= PI); + assert(w > 0.0); + assert(w <= PI); - m = floorf(w/model->Wo + 0.5); - f = (w - m*model->Wo)/w; - assert(f <= 1.0); + m = floorf(w / model->Wo + 0.5); + f = (w - m * model->Wo) / w; + assert(f <= 1.0); - if (m < 1) { - log_amp = f*log10f(model->A[1] + 1E-6); - } - else if ((m+1) > model->L) { - log_amp = (1.0-f)*log10f(model->A[model->L] + 1E-6); - } - else { - log_amp = (1.0-f)*log10f(model->A[m] + 1E-6) + - f*log10f(model->A[m+1] + 1E-6); - } + if (m < 1) { + log_amp = f * log10f(model->A[1] + 1E-6); + } else if ((m + 1) > model->L) { + log_amp = (1.0 - f) * log10f(model->A[model->L] + 1E-6); + } else { + log_amp = (1.0 - f) * log10f(model->A[m] + 1E-6) + + f * log10f(model->A[m + 1] + 1E-6); + } - return log_amp; + return log_amp; } #ifdef NOT_NEEDED @@ -140,7 +139,7 @@ float sample_log_amp(MODEL *model, float w) AUTHOR......: David Rowe DATE CREATED: 10 Nov 2010 - Given two frames decribed by model parameters 20ms apart, determines + Given two frames described by model parameters 20ms apart, determines the model parameters of the 10ms frame between them. Assumes voicing is available for middle (interpolated) frame. Outputs are amplitudes and Wo for the interpolated frame. @@ -150,65 +149,59 @@ float sample_log_amp(MODEL *model, float w) \*---------------------------------------------------------------------------*/ -void interpolate_lsp( - codec2_fft_cfg fft_fwd_cfg, - MODEL *interp, /* interpolated model params */ - MODEL *prev, /* previous frames model params */ - MODEL *next, /* next frames model params */ - float *prev_lsps, /* previous frames LSPs */ - float prev_e, /* previous frames LPC energy */ - float *next_lsps, /* next frames LSPs */ - float next_e, /* next frames LPC energy */ - float *ak_interp, /* interpolated aks for this frame */ - float *lsps_interp, /* interpolated lsps for this frame */ - float Wo_min -) -{ - int i; - float e; - float snr; - - /* trap corner case where V est is probably wrong */ - - if (interp->voiced && !prev->voiced && !next->voiced) { - interp->voiced = 0; - } - - /* Wo depends on voicing of this and adjacent frames */ - - if (interp->voiced) { - if (prev->voiced && next->voiced) - interp->Wo = (prev->Wo + next->Wo)/2.0; - if (!prev->voiced && next->voiced) - interp->Wo = next->Wo; - if (prev->voiced && !next->voiced) - interp->Wo = prev->Wo; - } - else { - interp->Wo = Wo_min; - } - interp->L = PI/interp->Wo; - - //printf(" interp: prev_v: %d next_v: %d prev_Wo: %f next_Wo: %f\n", - // prev->voiced, next->voiced, prev->Wo, next->Wo); - //printf(" interp: Wo: %1.5f L: %d\n", interp->Wo, interp->L); - - /* interpolate LSPs */ - - for(i=0; iA[1]); +void interpolate_lsp(codec2_fft_cfg fft_fwd_cfg, + MODEL *interp, /* interpolated model params */ + MODEL *prev, /* previous frames model params */ + MODEL *next, /* next frames model params */ + float *prev_lsps, /* previous frames LSPs */ + float prev_e, /* previous frames LPC energy */ + float *next_lsps, /* next frames LSPs */ + float next_e, /* next frames LPC energy */ + float *ak_interp, /* interpolated aks for this frame */ + float *lsps_interp, /* interpolated lsps for this frame */ + float Wo_min) { + int i; + float e; + float snr; + + /* trap corner case where V est is probably wrong */ + + if (interp->voiced && !prev->voiced && !next->voiced) { + interp->voiced = 0; + } + + /* Wo depends on voicing of this and adjacent frames */ + + if (interp->voiced) { + if (prev->voiced && next->voiced) interp->Wo = (prev->Wo + next->Wo) / 2.0; + if (!prev->voiced && next->voiced) interp->Wo = next->Wo; + if (prev->voiced && !next->voiced) interp->Wo = prev->Wo; + } else { + interp->Wo = Wo_min; + } + interp->L = PI / interp->Wo; + + // printf(" interp: prev_v: %d next_v: %d prev_Wo: %f next_Wo: %f\n", + // prev->voiced, next->voiced, prev->Wo, next->Wo); + // printf(" interp: Wo: %1.5f L: %d\n", interp->Wo, interp->L); + + /* interpolate LSPs */ + + for (i = 0; i < LPC_ORD; i++) { + lsps_interp[i] = (prev_lsps[i] + next_lsps[i]) / 2.0; + } + + /* Interpolate LPC energy in log domain */ + + e = powf(10.0, (log10f(prev_e) + log10f(next_e)) / 2.0); + // printf(" interp: e: %f\n", e); + + /* convert back to amplitudes */ + + lsp_to_lpc(lsps_interp, ak_interp, LPC_ORD); + aks_to_M2(fft_fwd_cfg, ak_interp, LPC_ORD, interp, e, &snr, 0, 0, 1, 1, + LPCPF_BETA, LPCPF_GAMMA); + // printf(" interp: ak[1]: %f A[1] %f\n", ak_interp[1], interp->A[1]); } #endif @@ -224,14 +217,11 @@ void interpolate_lsp( \*---------------------------------------------------------------------------*/ -void interp_Wo( - MODEL *interp, /* interpolated model params */ - MODEL *prev, /* previous frames model params */ - MODEL *next, /* next frames model params */ - float Wo_min - ) -{ - interp_Wo2(interp, prev, next, 0.5, Wo_min); +void interp_Wo(MODEL *interp, /* interpolated model params */ + MODEL *prev, /* previous frames model params */ + MODEL *next, /* next frames model params */ + float Wo_min) { + interp_Wo2(interp, prev, next, 0.5, Wo_min); } /*---------------------------------------------------------------------------*\ @@ -244,37 +234,29 @@ void interp_Wo( \*---------------------------------------------------------------------------*/ -void interp_Wo2( - MODEL *interp, /* interpolated model params */ - MODEL *prev, /* previous frames model params */ - MODEL *next, /* next frames model params */ - float weight, - float Wo_min -) -{ - /* trap corner case where voicing est is probably wrong */ - - if (interp->voiced && !prev->voiced && !next->voiced) { - interp->voiced = 0; - } - - /* Wo depends on voicing of this and adjacent frames */ - - if (interp->voiced) { - if (prev->voiced && next->voiced) - interp->Wo = (1.0 - weight)*prev->Wo + weight*next->Wo; - if (!prev->voiced && next->voiced) - interp->Wo = next->Wo; - if (prev->voiced && !next->voiced) - interp->Wo = prev->Wo; - } - else { - interp->Wo = Wo_min; - } - interp->L = PI/interp->Wo; +void interp_Wo2(MODEL *interp, /* interpolated model params */ + MODEL *prev, /* previous frames model params */ + MODEL *next, /* next frames model params */ + float weight, float Wo_min) { + /* trap corner case where voicing est is probably wrong */ + + if (interp->voiced && !prev->voiced && !next->voiced) { + interp->voiced = 0; + } + + /* Wo depends on voicing of this and adjacent frames */ + + if (interp->voiced) { + if (prev->voiced && next->voiced) + interp->Wo = (1.0 - weight) * prev->Wo + weight * next->Wo; + if (!prev->voiced && next->voiced) interp->Wo = next->Wo; + if (prev->voiced && !next->voiced) interp->Wo = prev->Wo; + } else { + interp->Wo = Wo_min; + } + interp->L = PI / interp->Wo; } - /*---------------------------------------------------------------------------*\ FUNCTION....: interp_energy() @@ -286,13 +268,12 @@ void interp_Wo2( \*---------------------------------------------------------------------------*/ -float interp_energy(float prev_e, float next_e) -{ - //return powf(10.0, (log10f(prev_e) + log10f(next_e))/2.0); - return sqrtf(prev_e * next_e); //looks better is math. identical and faster math +float interp_energy(float prev_e, float next_e) { + // return powf(10.0, (log10f(prev_e) + log10f(next_e))/2.0); + return sqrtf(prev_e * + next_e); // looks better is math. identical and faster math } - /*---------------------------------------------------------------------------*\ FUNCTION....: interp_energy2() @@ -304,13 +285,10 @@ float interp_energy(float prev_e, float next_e) \*---------------------------------------------------------------------------*/ -float interp_energy2(float prev_e, float next_e, float weight) -{ - return POW10F((1.0 - weight)*log10f(prev_e) + weight*log10f(next_e)); - +float interp_energy2(float prev_e, float next_e, float weight) { + return POW10F((1.0 - weight) * log10f(prev_e) + weight * log10f(next_e)); } - /*---------------------------------------------------------------------------*\ FUNCTION....: interpolate_lsp_ver2() @@ -321,11 +299,10 @@ float interp_energy2(float prev_e, float next_e, float weight) \*---------------------------------------------------------------------------*/ -void interpolate_lsp_ver2(float interp[], float prev[], float next[], float weight, int order) -{ - int i; +void interpolate_lsp_ver2(float interp[], float prev[], float next[], + float weight, int order) { + int i; - for(i=0; i