diff options
| author | drowe67 <[email protected]> | 2023-07-20 08:59:48 +0930 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-20 08:59:48 +0930 |
| commit | 06d4c11e699b0351765f10398abb4f663a984f36 (patch) | |
| tree | 33e22af0814c5b6c3d676f096ae8c2ac8a3ed9f0 /src/lpc.c | |
| parent | 6588e77f38bdebd7adffe091b22e7760d95d0ccb (diff) | |
| parent | 4d6c143c0abec15e1d6ed1fd95d36f80e6cb7df8 (diff) | |
Merge pull request #3 from drowe67/dr-cleanup21.2.0
Cleanup Part 2
Diffstat (limited to 'src/lpc.c')
| -rw-r--r-- | src/lpc.c | 221 |
1 files changed, 96 insertions, 125 deletions
@@ -25,16 +25,18 @@ along with this program; if not, see <http://www.gnu.org/licenses/>. */ -#define LPC_MAX_N 512 /* maximum no. of samples in frame */ -#define PI 3.141592654 /* mathematical constant */ +#define LPC_MAX_N 512 /* maximum no. of samples in frame */ +#define PI 3.141592654 /* mathematical constant */ #define ALPHA 1.0 -#define BETA 0.94 +#define BETA 0.94 + +#include "lpc.h" #include <assert.h> #include <math.h> + #include "defines.h" -#include "lpc.h" /*---------------------------------------------------------------------------*\ @@ -42,29 +44,25 @@ Pre-emphasise (high pass filter with zero close to 0 Hz) a frame of speech samples. Helps reduce dynamic range of LPC spectrum, giving - greater weight and hense a better match to low energy formants. + greater weight and hence a better match to low energy formants. Should be balanced by de-emphasis of the output speech. \*---------------------------------------------------------------------------*/ -void pre_emp( - float Sn_pre[], /* output frame of speech samples */ - float Sn[], /* input frame of speech samples */ - float *mem, /* Sn[-1]single sample memory */ - int Nsam /* number of speech samples to use */ -) -{ - int i; - - for(i=0; i<Nsam; i++) { - Sn_pre[i] = Sn[i] - ALPHA * mem[0]; - mem[0] = Sn[i]; - } +void pre_emp(float Sn_pre[], /* output frame of speech samples */ + float Sn[], /* input frame of speech samples */ + float *mem, /* Sn[-1]single sample memory */ + int Nsam /* number of speech samples to use */ +) { + int i; + for (i = 0; i < Nsam; i++) { + Sn_pre[i] = Sn[i] - ALPHA * mem[0]; + mem[0] = Sn[i]; + } } - /*---------------------------------------------------------------------------*\ de_emp() @@ -73,23 +71,19 @@ void pre_emp( \*---------------------------------------------------------------------------*/ -void de_emp( - float Sn_de[], /* output frame of speech samples */ - float Sn[], /* input frame of speech samples */ - float *mem, /* Sn[-1]single sample memory */ - int Nsam /* number of speech samples to use */ -) -{ - int i; - - for(i=0; i<Nsam; i++) { - Sn_de[i] = Sn[i] + BETA * mem[0]; - mem[0] = Sn_de[i]; - } +void de_emp(float Sn_de[], /* output frame of speech samples */ + float Sn[], /* input frame of speech samples */ + float *mem, /* Sn[-1]single sample memory */ + int Nsam /* number of speech samples to use */ +) { + int i; + for (i = 0; i < Nsam; i++) { + Sn_de[i] = Sn[i] + BETA * mem[0]; + mem[0] = Sn_de[i]; + } } - /*---------------------------------------------------------------------------*\ hanning_window() @@ -98,16 +92,14 @@ void de_emp( \*---------------------------------------------------------------------------*/ -void hanning_window( - float Sn[], /* input frame of speech samples */ - float Wn[], /* output frame of windowed samples */ - int Nsam /* number of samples */ -) -{ - int i; /* loop variable */ +void hanning_window(float Sn[], /* input frame of speech samples */ + float Wn[], /* output frame of windowed samples */ + int Nsam /* number of samples */ +) { + int i; /* loop variable */ - for(i=0; i<Nsam; i++) - Wn[i] = Sn[i]*(0.5 - 0.5*cosf(2*PI*(float)i/(Nsam-1))); + for (i = 0; i < Nsam; i++) + Wn[i] = Sn[i] * (0.5 - 0.5 * cosf(2 * PI * (float)i / (Nsam - 1))); } /*---------------------------------------------------------------------------*\ @@ -119,19 +111,16 @@ void hanning_window( \*---------------------------------------------------------------------------*/ -void autocorrelate( - float Sn[], /* frame of Nsam windowed speech samples */ - float Rn[], /* array of P+1 autocorrelation coefficients */ - int Nsam, /* number of windowed samples to use */ - int order /* order of LPC analysis */ -) -{ - int i,j; /* loop variables */ +void autocorrelate(float Sn[], /* frame of Nsam windowed speech samples */ + float Rn[], /* array of P+1 autocorrelation coefficients */ + int Nsam, /* number of windowed samples to use */ + int order /* order of LPC analysis */ +) { + int i, j; /* loop variables */ - for(j=0; j<order+1; j++) { + for (j = 0; j < order + 1; j++) { Rn[j] = 0.0; - for(i=0; i<Nsam-j; i++) - Rn[j] += Sn[i]*Sn[i+j]; + for (i = 0; i < Nsam - j; i++) Rn[j] += Sn[i] * Sn[i + j]; } } @@ -150,36 +139,31 @@ void autocorrelate( \*---------------------------------------------------------------------------*/ -void levinson_durbin( - float R[], /* order+1 autocorrelation coeff */ - float lpcs[], /* order+1 LPC's */ - int order /* order of the LPC analysis */ -) -{ - float a[order+1][order+1]; +void levinson_durbin(float R[], /* order+1 autocorrelation coeff */ + float lpcs[], /* order+1 LPC's */ + int order /* order of the LPC analysis */ +) { + float a[order + 1][order + 1]; float sum, e, k; - int i,j; /* loop variables */ + int i, j; /* loop variables */ - e = R[0]; /* Equation 38a, Makhoul */ + e = R[0]; /* Equation 38a, Makhoul */ - for(i=1; i<=order; i++) { + for (i = 1; i <= order; i++) { sum = 0.0; - for(j=1; j<=i-1; j++) - sum += a[i-1][j]*R[i-j]; - k = -1.0*(R[i] + sum)/e; /* Equation 38b, Makhoul */ - if (fabsf(k) > 1.0) - k = 0.0; + for (j = 1; j <= i - 1; j++) sum += a[i - 1][j] * R[i - j]; + k = -1.0 * (R[i] + sum) / e; /* Equation 38b, Makhoul */ + if (fabsf(k) > 1.0) k = 0.0; a[i][i] = k; - for(j=1; j<=i-1; j++) - a[i][j] = a[i-1][j] + k*a[i-1][i-j]; /* Equation 38c, Makhoul */ + for (j = 1; j <= i - 1; j++) + a[i][j] = a[i - 1][j] + k * a[i - 1][i - j]; /* Equation 38c, Makhoul */ - e *= (1-k*k); /* Equation 38d, Makhoul */ + e *= (1 - k * k); /* Equation 38d, Makhoul */ } - for(i=1; i<=order; i++) - lpcs[i] = a[order][i]; + for (i = 1; i <= order; i++) lpcs[i] = a[order][i]; lpcs[0] = 1.0; } @@ -194,20 +178,17 @@ void levinson_durbin( \*---------------------------------------------------------------------------*/ -void inverse_filter( - float Sn[], /* Nsam input samples */ - float a[], /* LPCs for this frame of samples */ - int Nsam, /* number of samples */ - float res[], /* Nsam residual samples */ - int order /* order of LPC */ -) -{ - int i,j; /* loop variables */ - - for(i=0; i<Nsam; i++) { +void inverse_filter(float Sn[], /* Nsam input samples */ + float a[], /* LPCs for this frame of samples */ + int Nsam, /* number of samples */ + float res[], /* Nsam residual samples */ + int order /* order of LPC */ +) { + int i, j; /* loop variables */ + + for (i = 0; i < Nsam; i++) { res[i] = 0.0; - for(j=0; j<=order; j++) - res[i] += Sn[i-j]*a[j]; + for (j = 0; j <= order; j++) res[i] += Sn[i - j] * a[j]; } } @@ -231,21 +212,19 @@ void inverse_filter( \*---------------------------------------------------------------------------*/ void synthesis_filter( - float res[], /* Nsam input residual (excitation) samples */ - float a[], /* LPCs for this frame of speech samples */ - int Nsam, /* number of speech samples */ - int order, /* LPC order */ - float Sn_[] /* Nsam output synthesised speech samples */ -) -{ - int i,j; /* loop variables */ + float res[], /* Nsam input residual (excitation) samples */ + float a[], /* LPCs for this frame of speech samples */ + int Nsam, /* number of speech samples */ + int order, /* LPC order */ + float Sn_[] /* Nsam output synthesised speech samples */ +) { + int i, j; /* loop variables */ /* Filter Nsam samples */ - for(i=0; i<Nsam; i++) { - Sn_[i] = res[i]*a[0]; - for(j=1; j<=order; j++) - Sn_[i] -= Sn_[i-j]*a[j]; + for (i = 0; i < Nsam; i++) { + Sn_[i] = res[i] * a[0]; + for (j = 1; j <= order; j++) Sn_[i] -= Sn_[i - j] * a[j]; } } @@ -258,29 +237,25 @@ void synthesis_filter( \*---------------------------------------------------------------------------*/ -void find_aks( - float Sn[], /* Nsam samples with order sample memory */ - float a[], /* order+1 LPCs with first coeff 1.0 */ - int Nsam, /* number of input speech samples */ - int order, /* order of the LPC analysis */ - float *E /* residual energy */ -) -{ - float Wn[LPC_MAX_N]; /* windowed frame of Nsam speech samples */ - float R[order+1]; /* order+1 autocorrelation values of Sn[] */ +void find_aks(float Sn[], /* Nsam samples with order sample memory */ + float a[], /* order+1 LPCs with first coeff 1.0 */ + int Nsam, /* number of input speech samples */ + int order, /* order of the LPC analysis */ + float *E /* residual energy */ +) { + float Wn[LPC_MAX_N]; /* windowed frame of Nsam speech samples */ + float R[order + 1]; /* order+1 autocorrelation values of Sn[] */ int i; assert(Nsam < LPC_MAX_N); - hanning_window(Sn,Wn,Nsam); - autocorrelate(Wn,R,Nsam,order); - levinson_durbin(R,a,order); + hanning_window(Sn, Wn, Nsam); + autocorrelate(Wn, R, Nsam, order); + levinson_durbin(R, a, order); *E = 0.0; - for(i=0; i<=order; i++) - *E += a[i]*R[i]; - if (*E < 0.0) - *E = 1E-12; + for (i = 0; i <= order; i++) *E += a[i] * R[i]; + if (*E < 0.0) *E = 1E-12; } /*---------------------------------------------------------------------------*\ @@ -291,16 +266,12 @@ void find_aks( \*---------------------------------------------------------------------------*/ -void weight( - float ak[], /* vector of order+1 LPCs */ - float gamma, /* weighting factor */ - int order, /* num LPCs (excluding leading 1.0) */ - float akw[] /* weighted vector of order+1 LPCs */ -) -{ +void weight(float ak[], /* vector of order+1 LPCs */ + float gamma, /* weighting factor */ + int order, /* num LPCs (excluding leading 1.0) */ + float akw[] /* weighted vector of order+1 LPCs */ +) { int i; - for(i=1; i<=order; i++) - akw[i] = ak[i]*powf(gamma,(float)i); + for (i = 1; i <= order; i++) akw[i] = ak[i] * powf(gamma, (float)i); } - |
