From 984c7265dcf61123bec42f383ef4678f44008fdd Mon Sep 17 00:00:00 2001 From: drowe67 Date: Wed, 9 Sep 2009 08:58:15 +0000 Subject: [PATCH] phase model uses fractional pitch, error measure matches Octave plamp.m for consistency git-svn-id: https://svn.code.sf.net/p/freetel/code@52 01035d8c-6547-0410-b346-abe4f91aad63 --- codec2/octave/phase.m | 2 +- codec2/octave/plamp.m | 36 +++++++++++++++++++++++++++++------- codec2/src/dump.c | 17 +++++++++++++++++ codec2/src/dump.h | 4 ++++ codec2/src/phase.c | 42 +++++++++++++++++++++++++++--------------- codec2/src/phase.h | 4 ++-- codec2/src/sinedec.c | 18 ++++++++++-------- 7 files changed, 90 insertions(+), 33 deletions(-) diff --git a/codec2/octave/phase.m b/codec2/octave/phase.m index 537e193b..fc13b47b 100644 --- a/codec2/octave/phase.m +++ b/codec2/octave/phase.m @@ -10,7 +10,7 @@ function phase(samname, F0, randphase, png) A = 10000/L; phi = zeros(1,L); for m=1:L - phi(m) = m*Wo*0.3*m; + phi(m) = m*Wo*50.125; end if (randphase == 1) rand("seed",0); diff --git a/codec2/octave/plamp.m b/codec2/octave/plamp.m index cf7618ce..cdf6176f 100644 --- a/codec2/octave/plamp.m +++ b/codec2/octave/plamp.m @@ -45,6 +45,11 @@ function plamp(samname, f) phase_ = load(phase_name_); endif + snr_name = strcat(samname,"_snr.txt"); + if (file_in_path(".",snr_name)) + snr = load(snr_name); + endif + do figure(1); clg; @@ -74,16 +79,21 @@ function plamp(samname, f) plot((1:L)*Wo*4000/pi, 20*log10(Amq) - 20*log10(Am), Am_err_label); endif - % phase model - determine SNR and error spectrum + if (file_in_path(".",snr_name)) + snr_label = sprintf(";phase SNR %4.2f dB;",snr(f)); + plot(1,1,snr_label); + endif + + % phase model - determine SNR and error spectrum for phase model 1 if (file_in_path(".",phase_name_)) orig = Am.*exp(j*phase(f,1:L)); synth = Am.*exp(j*phase_(f,1:L)); - signal = orig * orig' - noise = (orig-synth) * (orig-synth)' - snr = 10*log10(signal/noise); + signal = orig * orig'; + noise = (orig-synth) * (orig-synth)'; + snr_phase = 10*log10(signal/noise); - phase_err_label = sprintf(";phase_err SNR %4.2f dB;",snr); + phase_err_label = sprintf(";phase_err SNR %4.2f dB;",snr_phase); plot((1:L)*Wo*4000/pi, 20*log10(orig-synth), phase_err_label); endif @@ -97,16 +107,28 @@ function plamp(samname, f) if (file_in_path(".",phase_name)) figure(3); - plot((1:L)*Wo*4000/pi, unwrap(phase(f,1:L)), ";phase;"); + plot((1:L)*Wo*4000/pi, phase(f,1:L), ";phase;"); axis; if (file_in_path(".",phase_name_)) hold on; - plot((1:L)*Wo*4000/pi, unwrap(phase_(f,1:L)), ";phase_;"); + plot((1:L)*Wo*4000/pi, phase_(f,1:L), ";phase_;"); hold off; endif figure(2); endif + % autocorrelation function to research voicing est + + %M = length(s); + %sw = s .* hanning(M)'; + %for k=0:159 + % R(k+1) = sw(1:320-k) * sw(1+k:320)'; + %endfor + %figure(4); + %R_label = sprintf(";R(k) %3.2f;",max(R(20:159))/R(1)); + %plot(R/R(1),R_label); + %grid + % interactive menu printf("\rframe: %d menu: n-next b-back p-png q-quit ", f); diff --git a/codec2/src/dump.c b/codec2/src/dump.c index 02a354d5..c52d574b 100644 --- a/codec2/src/dump.c +++ b/codec2/src/dump.c @@ -45,6 +45,7 @@ static FILE *ffw = NULL; static FILE *fe = NULL; static FILE *fsq = NULL; static FILE *fdec = NULL; +static FILE *fsnr = NULL; static char prefix[MAX_STR]; @@ -80,6 +81,8 @@ void dump_off(){ fclose(fsq); if (fdec != NULL) fclose(fdec); + if (fsnr != NULL) + fclose(fsnr); } void dump_Sn(float Sn[]) { @@ -223,6 +226,20 @@ void dump_phase_(float phase_[]) { fprintf(fphase_,"\n"); } +void dump_snr(float snr) { + char s[MAX_STR]; + + if (!dumpon) return; + + if (fphase_ == NULL) { + sprintf(s,"%s_snr.txt", prefix); + fsnr = fopen(s, "wt"); + assert(fsnr != NULL); + } + + fprintf(fsnr,"%f\n",snr); +} + void dump_Pw(COMP Pw[]) { int i; char s[MAX_STR]; diff --git a/codec2/src/dump.h b/codec2/src/dump.h index 031445af..2d3bbc25 100644 --- a/codec2/src/dump.h +++ b/codec2/src/dump.h @@ -38,6 +38,10 @@ void dump_model(MODEL *m); void dump_quantised_model(MODEL *m); void dump_Pw(COMP Pw[]); void dump_lsp(float lsp[]); + +/* phase modelling */ + +void dump_snr(float snr); void dump_phase(float phase[]); void dump_phase_(float phase[]); diff --git a/codec2/src/phase.c b/codec2/src/phase.c index 42b1c658..ffb132af 100644 --- a/codec2/src/phase.c +++ b/codec2/src/phase.c @@ -126,10 +126,10 @@ int order; \*---------------------------------------------------------------------------*/ float phase_model_first_order( - float aks[], /* LPC coeffs for this frame */ - COMP H[], /* LPC filter freq doamin samples */ - int *i_min, /* pulse position for min error */ - COMP *minAm /* complex gain for min error */ + float aks[], /* LPC coeffs for this frame */ + COMP H[], /* LPC filter freq doamin samples */ + float *n_min, /* pulse position for min error */ + COMP *minAm /* complex gain for min error */ ) { float G; /* LPC gain */ @@ -144,11 +144,15 @@ float phase_model_first_order( COMP Em; /* error for m-th band */ float den; /* energy of synthesised */ float snr; /* snr of each excitation source */ + int Lmax; + float n; + + Lmax = model.L; /* Construct target vector */ sig = 0.0; - for(m=1; m<=model.L; m++) { + for(m=1; m<=Lmax; m++) { A[m].real = model.A[m]*cos(model.phi[m]); A[m].imag = model.A[m]*sin(model.phi[m]); sig += model.A[m]*model.A[m]; @@ -156,6 +160,11 @@ float phase_model_first_order( /* Sample LPC model at harmonics */ + #ifdef NO_LPC_PHASE + /* useful for testing with Sn[] an impulse train */ + for(i=1; i<=PHASE_LPC_ORD; i++) + aks[i] = 0; + #endif G = 1.0; aks_to_H(&model,aks,G,H,PHASE_LPC_ORD); @@ -163,16 +172,16 @@ float phase_model_first_order( Emin = 1E32; P = floor(TWO_PI/model.Wo + 0.5); - for(i=0; ireal = Am.real; minAm->imag = Am.imag; } @@ -370,7 +382,7 @@ void phase_synth_zero_order( void phase_synth_first_order( float snr, /* SNR from first order model */ COMP H[], /* LPC spectra samples */ - int i_min, /* best pulse position */ + float n_min, /* best pulse position */ COMP minAm /* best complex gain */ ) { @@ -397,8 +409,8 @@ void phase_synth_first_order( /* generate excitation */ if (m <= Lrand) { - Ex[m].real = cos(model.Wo*m*i_min); - Ex[m].imag = sin(-model.Wo*m*i_min); + Ex[m].real = cos(model.Wo*m*n_min); + Ex[m].imag = sin(-model.Wo*m*n_min); } else { float phi = TWO_PI*(float)rand()/RAND_MAX; diff --git a/codec2/src/phase.h b/codec2/src/phase.h index e692f938..5dd4ad69 100644 --- a/codec2/src/phase.h +++ b/codec2/src/phase.h @@ -31,8 +31,8 @@ #define PHASE_LPC_ORD 10 -float phase_model_first_order(float aks[], COMP H[], int *i_min, COMP *min_Am); +float phase_model_first_order(float aks[], COMP H[], float *n_min, COMP *min_Am); void phase_synth_zero_order(float snr, COMP H[], float *prev_Wo, float *ex_phase); -void phase_synth_first_order(float snr, COMP H[], int i_min, COMP min_Am); +void phase_synth_first_order(float snr, COMP H[], float n_min, COMP min_Am); #endif diff --git a/codec2/src/sinedec.c b/codec2/src/sinedec.c index 54083330..9d72a274 100644 --- a/codec2/src/sinedec.c +++ b/codec2/src/sinedec.c @@ -200,7 +200,7 @@ int main(int argc, char *argv[]) float Wn[AW_ENC]; /* windowed speech samples */ float Rk[PHASE_LPC_ORD+1]; /* autocorrelation coeffs */ COMP H[MAX_AMP]; /* LPC freq domain samples */ - int i_min; + float n_min; COMP min_Am; dump_phase(&model.phi[0]); @@ -217,14 +217,16 @@ int main(int argc, char *argv[]) else assert(order == PHASE_LPC_ORD); - snr = phase_model_first_order(ak, H, &i_min, &min_Am); - snr = 5; - if (phase_model == 0) + snr = phase_model_first_order(ak, H, &n_min, &min_Am); + dump_snr(snr); + if (phase_model == 0) { phase_synth_zero_order(snr, H, &prev_Wo, &ex_phase); - if (phase_model == 1) - phase_synth_first_order(snr, H, i_min, min_Am); - - dump_phase_(&model.phi[0]); + } + + if (phase_model == 1) { + phase_synth_first_order(snr, H, n_min, min_Am); + dump_phase_(&model.phi[0]); + } } /* Synthesise speech */ -- 2.25.1