From: drowe67 Date: Fri, 20 Jul 2012 03:31:05 +0000 (+0000) Subject: inital framework for phase experiments X-Git-Url: http://git.whiteaudio.com/gitweb/?a=commitdiff_plain;h=33f9d06a0efd737b522d2dd3ed1c93742cfaf770;p=freetel-svn-tracking.git inital framework for phase experiments git-svn-id: https://svn.code.sf.net/p/freetel/code@594 01035d8c-6547-0410-b346-abe4f91aad63 --- diff --git a/codec2-dev/src/c2sim.c b/codec2-dev/src/c2sim.c index 5a412ea2..62b79098 100644 --- a/codec2-dev/src/c2sim.c +++ b/codec2-dev/src/c2sim.c @@ -50,6 +50,7 @@ void synth_one_frame(kiss_fft_cfg fft_inv_cfg, short buf[], MODEL *model, float Sn_[], float Pn[]); void print_help(const struct option *long_options, int num_opts, char* argv[]); + /*---------------------------------------------------------------------------*\ MAIN @@ -94,7 +95,7 @@ int main(int argc, char *argv[]) int postfilt; float bg_est; - int hand_voicing = 0, phasetest = 0; + int hand_voicing = 0, phaseexp = 0; FILE *fvoicing = 0; MODEL prev_model, interp_model; @@ -118,6 +119,7 @@ int main(int argc, char *argv[]) #ifdef DUMP int dump; #endif + struct PEXP *pexp = NULL; char* opt_string = "ho:"; struct option long_options[] = { @@ -131,7 +133,7 @@ int main(int argc, char *argv[]) { "lspdt_mode", required_argument, NULL, 0 }, { "lspjvm", no_argument, &lspjvm, 1 }, { "phase0", no_argument, &phase0, 1 }, - { "phasetest", no_argument, &phasetest, 1 }, + { "phaseexp", no_argument, &phaseexp, 1 }, { "postfilter", no_argument, &postfilt, 1 }, { "hand_voicing", required_argument, &hand_voicing, 1 }, { "dec", no_argument, &decimate, 1 }, @@ -314,6 +316,8 @@ int main(int argc, char *argv[]) make_analysis_window(fft_fwd_cfg, w, W); make_synthesis_window(Pn); quantise_init(); + if (phaseexp) + pexp = phase_experiment_create(); /*----------------------------------------------------------------*\ @@ -352,6 +356,9 @@ int main(int argc, char *argv[]) dump_Sn(Sn); dump_Sw(Sw); dump_model(&model); #endif + if (phaseexp) + phase_experiment(pexp, &model); + /*------------------------------------------------------------*\ Zero-phase modelling @@ -724,6 +731,8 @@ int main(int argc, char *argv[]) if (lpc_model) printf("SNR av = %5.2f dB\n", sum_snr/frames); + if (phaseexp) + phase_experiment_destroy(pexp); #ifdef DUMP if (dump) dump_off(); diff --git a/codec2-dev/src/dump.c b/codec2-dev/src/dump.c index ba406a34..aca0b29c 100644 --- a/codec2-dev/src/dump.c +++ b/codec2-dev/src/dump.c @@ -55,6 +55,7 @@ static FILE *fak = NULL; static FILE *fbg = NULL; static FILE *fE = NULL; static FILE *frk = NULL; +static FILE *fhephase = NULL; static char prefix[MAX_STR]; @@ -104,6 +105,8 @@ void dump_off(){ fclose(fE); if (frk != NULL) fclose(frk); + if (fhephase != NULL) + fclose(fhephase); } void dump_Sn(float Sn[]) { @@ -262,6 +265,25 @@ void dump_phase_(float phase_[], int L) { fprintf(fphase_,"\n"); } + +void dump_hephase(int ind[], int dim) { + int m; + char s[MAX_STR]; + + if (!dumpon) return; + + if (fhephase == NULL) { + sprintf(s,"%s_hephase.txt", prefix); + fhephase = fopen(s, "wt"); + assert(fhephase != NULL); + } + + for(m=0; mphi_prev[i] = 0.0; + pexp->Wo_prev = 0.0; + + return pexp; +} + + +/*---------------------------------------------------------------------------* \ + + phase_experiment_destroy() + +\*---------------------------------------------------------------------------*/ + +void phase_experiment_destroy(struct PEXP *pexp) { + assert(pexp != NULL); + free(pexp); +} + + +struct AMPINDEX { + float amp; + int index; +}; + +static void bubbleSort(struct AMPINDEX numbers[], int array_size) +{ + int i, j; + struct AMPINDEX temp; + + for (i = (array_size - 1); i > 0; i--) + { + for (j = 1; j <= i; j++) + { + if (numbers[j-1].amp < numbers[j].amp) + { + temp = numbers[j-1]; + numbers[j-1] = numbers[j]; + numbers[j] = temp; + } + } + } +} + +/*---------------------------------------------------------------------------* \ + + phase_experiment() + + Phase quantisation experiments. + +\*---------------------------------------------------------------------------*/ + +void phase_experiment(struct PEXP *pexp, MODEL *model) { + int i; + + assert(pexp != NULL); + + //#define AMP + #ifdef AMP + { + int r = 0; + float max = 0; + for(i=1; i max) max = model.A[i]; + for(i=1; i dim) + n_harm = dim; + + for(i=st; i<=st+dim; i++) { + top = 0; + for(j=1; j<=n_harm; j++) + if (model.A[i] == sorted[j].amp) + top = 1; + + if (!top) { + model.phi[i] = 0.0; /* make sure */ + //model.phi[i] = PI*(1.0 - 2.0*(float)rand()/RAND_MAX); + model.phi[i] = phi_prev[i] + i*N*model.Wo; + r++; + } + else + nr++; + } + printf("r %d nr %d dim %d\n", r, nr, dim); + + for(i=0; iL/4+1; i<=model->L/2; i++) { + float pred = pexp->phi_prev[i] + N*i*(model->Wo); + float err = pred - model->phi[i]; + err = atan2(sin(err),cos(err)); + printf("%f\n",err); + } + #endif + + //#define PRED2 + #ifdef PRED2 + /* + if (fabs(model.Wo - Wo_prev) > 0.1*model.Wo) { + for(i=1; i<=model.L/2; i++) + phi_prev[i] = (PI/8)*(1.0 - 2.0*(float)rand()/RAND_MAX); + } + else + printf("%d\n", frames); + */ + + for(i=1; i<=model.L/4; i++) { + model.phi[i] = phi_prev[i] + N*i*(model.Wo); + } + #ifdef OLD + if ((frames % 2) != 0) { + /* predict on even frames */ + for(i=model.L/4+1; i<=model.L; i++) + model.phi[i] = phi_prev[i] + N*i*(model.Wo); + } + else { + /* 3 bit quantise on odd */ + for(i=model.L/4+1; i<=model.L; i++) { + float pred = phi_prev[i] + N*i*(model.Wo); + if (pred > model.phi[i]) + model.phi[i] = pred - PI/8; + else + model.phi[i] = pred + PI/8; + + //model.phi[i] += (PI/8)*(1.0 - 2.0*(float)rand()/RAND_MAX); + } + } + #endif + + #ifdef QUANT + for(i=model.L/4+1; i<=model.L/2; i++) { + float pred = phi_prev[i] + N*i*(model.Wo); + float err = pred - model.phi[i]; + err = atan2(sin(err),cos(err)); + float interval = 0.25; + int ind = floor(err/(interval*PI)+0.5); + float qerr = ind*interval*PI; + //printf("%f %d %f\n", err, ind, ind*0.25*PI); + if (pred > model.phi[i]) + model.phi[i] = pred - qerr; + else + model.phi[i] = pred + qerr; + + } + + for(i=model.L/2+1; i<=7*model.L/8; i++) { + float pred = phi_prev[i] + N*i*(model.Wo); + float err = pred - model.phi[i]; + err = atan2(sin(err),cos(err)); + float interval = 0.5; + int ind = floor(err/(interval*PI)+0.5); + float qerr = ind*interval*PI; + //printf("%f %d %f\n", err, ind, ind*0.25*PI); + if (pred > model.phi[i]) + model.phi[i] = pred - qerr; + else + model.phi[i] = pred + qerr; + + } + #endif + + /* + for(i=1; i<=model.L/4; i++) { + model.A[i] = 0.0; + } + for(i=model.L/2+1; i<=model.L; i++) { + model.A[i] = 0.0; + } + */ + + #endif + + for(i=1; iL; i++) + pexp->phi_prev[i] = model->phi[i]; + pexp->Wo_prev = model->Wo; + + +} + diff --git a/codec2-dev/src/phase.h b/codec2-dev/src/phase.h index 725698d9..d849f6b3 100644 --- a/codec2-dev/src/phase.h +++ b/codec2-dev/src/phase.h @@ -33,4 +33,10 @@ void phase_synth_zero_order(kiss_fft_cfg fft_dec_cfg, MODEL *model, float aks[], float *ex_phase, int order); +struct PEXP; + +struct PEXP * phase_experiment_create(); +void phase_experiment_destroy(struct PEXP *pexp); +void phase_experiment(struct PEXP *pexp, MODEL *model); + #endif