double baum_welch_Ntrain (Hmm *H) { int **stab; int a, b; double cP,bP; UNPACK_HMM(H); stab=declare_int (nrounds,L+1); for (a=0; a<nrounds; a++) { model2modelR(H); cP=baum_welch_train(H, a+1); viterbi (H); posterior(H); for (b=1; b<L; b++)stab[a][b]=H->VT[b]; if (a==0 || cP>bP) { dump_model (H); dump_viterbi (H); dump_posterior(H); bP=cP; } } fprintf (stderr, "---- SUMMARY: Best: %10.3f STABILITY: %.3f\n", bP,analyze_stability (stab,nrounds, L, 100)); H->P=bP; return H->P; }
///////////////////////////////////////////////////////////////////////////// // // // // // Decoding // // // // // ///////////////////////////////////////////////////////////////////////////// double decode (Hmm *H) { posterior (H); viterbi (H); dump_model(H); dump_viterbi(H); dump_posterior(H); return H->PP; }
void analyse_one_frame(struct CODEC2 *c2, MODEL *model, short speech[]) { COMP Sw[FFT_ENC]; COMP Sw_[FFT_ENC]; COMP Ew[FFT_ENC]; float pitch; int i; PROFILE_VAR(dft_start, nlp_start, model_start, two_stage, estamps); /* Read input speech */ for(i=0; i<M-N; i++) c2->Sn[i] = c2->Sn[i+N]; for(i=0; i<N; i++) c2->Sn[i+M-N] = speech[i]; PROFILE_SAMPLE(dft_start); dft_speech(c2->fft_fwd_cfg, Sw, c2->Sn, c2->w); PROFILE_SAMPLE_AND_LOG(nlp_start, dft_start, " dft_speech"); /* Estimate pitch */ nlp(c2->nlp,c2->Sn,N,P_MIN,P_MAX,&pitch,Sw, c2->W, &c2->prev_Wo_enc); PROFILE_SAMPLE_AND_LOG(model_start, nlp_start, " nlp"); model->Wo = TWO_PI/pitch; model->L = PI/model->Wo; /* estimate model parameters */ two_stage_pitch_refinement(model, Sw); PROFILE_SAMPLE_AND_LOG(two_stage, model_start, " two_stage"); estimate_amplitudes(model, Sw, c2->W, 0); PROFILE_SAMPLE_AND_LOG(estamps, two_stage, " est_amps"); est_voicing_mbe(model, Sw, c2->W, Sw_, Ew); c2->prev_Wo_enc = model->Wo; PROFILE_SAMPLE_AND_LOG2(estamps, " est_voicing"); #ifdef DUMP dump_model(model); #endif }
int main() { set_sample_method("metropolis-hastings"); set_sample_iterations(NSAMPLES); set_mh_burn_in(BURN_IN); set_mh_lag(LAG); set_prompt_per_round(50); pp_state_t* state; struct pp_instance_t* instance; pp_query_t* query; pp_trace_store_t* traces; state = pp_new_state(); pp_load_file(state, "parse/models/naive.model"); ModelNode* model = model_map_find(state->model_map, state->symbol_table, "naive_bayes"); if (!model) { printf("error: model not found\n"); return 1; } printf(dump_model(model)); pp_variable_t* param[6] = { new_pp_int(K), /* K : #classes */ new_pp_int(N), /* N : #docs */ new_pp_int(NWORDS), /* nwords: #words per doc */ new_pp_int(V), /* V: vocabulary size */ new_pp_float(ALPHA), /* alpha */ new_pp_float(BETA), /* beta*/ }; FILE* fin = fopen("test/naive_data.txt", "r"); if (!fin) { printf("data not found\n"); return 1; } for (int i = 0; i < N; ++i) { for (int j = 0; j < NWORDS; ++j) { fscanf(fin, "%d", &X[i][j]); } } fclose(fin); fin = fopen("test/naive_class.txt", "r"); if (!fin) { printf("data not found\n"); return 1; } for (int i = 0; i < N; ++i) { fscanf(fin, "%d", &c[i]); } fclose(fin); query = pp_query_composite( pp_query_observe_int_array(state, "c", c, NTRAIN), pp_query_observe_int_array_2D(state, "X", &X[0][0], N, NWORDS) ); int result = pp_sample_f(state, "naive_bayes", param, query, stat_sample, 0); pp_variable_destroy_all(param, 6); pp_query_destroy(query); pp_free(state); FILE* fout = fopen("test/naive_inferred.txt", "w"); fprintf(fout, "%-8s%10s%10s%4s%4s\n", "no.", "p(0)", "p(1)", "tru", "inf"); int cnt[K][K]; memset(cnt, 0, sizeof(int) * K * K); for (int i = 0; i < N; ++i) { fprintf(fout, "%-8d", i); int cat = 0; for (int j = 0; j < K; ++j) { if (num[i][j] > num[i][cat]) cat = j; fprintf(fout, "%10f", num[i][j] / (float) NSAMPLES); //printf("num[%d][%d] = %d\n", i, j, num[i][j]); } fprintf(fout, "%4d%4d\n", c[i], cat); if (i >= NTRAIN) { cnt[cat][c[i]]++; } } fprintf(fout, "\ntp = %d, fp = %d, fn = %d\n", cnt[1][1], cnt[1][0], cnt[0][1]); printf("tp = %d, fp = %d, fn = %d\n", cnt[1][1], cnt[1][0], cnt[0][1]); float prec = ((float) cnt[1][1]) / (cnt[1][1] + cnt[1][0]); float recall = ((float) cnt[1][1]) / (cnt[1][1] + cnt[0][1]); float accuracy = ((float) cnt[0][0] + cnt[1][1]) / NTEST; fprintf(fout, "accuracy = %f\n", accuracy); printf("accuracy = %f\n", accuracy); fprintf(fout, "precision = %f, recall = %f\n", prec, recall); printf("precision = %f, recall = %f\n", prec, recall); float f1_score = 2 * (prec * recall) / (prec + recall); fprintf(fout, "f1 score = %f\n", f1_score); printf("f1 score = %f\n", f1_score); fclose(fout); return 0; }
int main(int argc, char *argv[]) { FILE *fout = NULL; /* output speech file */ FILE *fin; /* input speech file */ short buf[N]; /* input/output buffer */ float buf_float[N]; float Sn[M]; /* float input speech samples */ float Sn_pre[N]; /* pre-emphasised input speech samples */ COMP Sw[FFT_ENC]; /* DFT of Sn[] */ kiss_fft_cfg fft_fwd_cfg; kiss_fft_cfg fft_inv_cfg; float w[M]; /* time domain hamming window */ COMP W[FFT_ENC]; /* DFT of w[] */ MODEL model; float Pn[2*N]; /* trapezoidal synthesis window */ float Sn_[2*N]; /* synthesised speech */ int i,m; /* loop variable */ int frames; float prev_Wo, prev__Wo, prev_uq_Wo; float pitch; char out_file[MAX_STR]; char ampexp_arg[MAX_STR]; char phaseexp_arg[MAX_STR]; float snr; float sum_snr; int orderi; int lpc_model = 0, order = LPC_ORD; int lsp = 0, lspd = 0, lspvq = 0; int lspres = 0; int lspjvm = 0, lspjnd = 0, lspmel = 0; #ifdef __EXPERIMENTAL__ int lspanssi = 0, #endif int prede = 0; float pre_mem = 0.0, de_mem = 0.0; float ak[order]; COMP Sw_[FFT_ENC]; COMP Ew[FFT_ENC]; int phase0 = 0; float ex_phase[MAX_AMP+1]; int postfilt; int hand_voicing = 0, phaseexp = 0, ampexp = 0, hi = 0, simlpcpf = 0; int lpcpf = 0; FILE *fvoicing = 0; MODEL prev_model; int dec; int decimate = 1; float lsps[order]; float e, prev_e; int lsp_indexes[order]; float lsps_[order]; float Woe_[2]; float lsps_dec[4][LPC_ORD], e_dec[4], weight, weight_inc, ak_dec[4][LPC_ORD]; MODEL model_dec[4], prev_model_dec; float prev_lsps_dec[order], prev_e_dec; void *nlp_states; float hpf_states[2]; int scalar_quant_Wo_e = 0; int vector_quant_Wo_e = 0; int dump_pitch_e = 0; FILE *fjvm = NULL; #ifdef DUMP int dump; #endif struct PEXP *pexp = NULL; struct AEXP *aexp = NULL; float gain = 1.0; int bpf_en = 0; float bpf_buf[BPF_N+N]; char* opt_string = "ho:"; struct option long_options[] = { { "lpc", required_argument, &lpc_model, 1 }, { "lspjnd", no_argument, &lspjnd, 1 }, { "lspmel", no_argument, &lspmel, 1 }, { "lsp", no_argument, &lsp, 1 }, { "lspd", no_argument, &lspd, 1 }, { "lspvq", no_argument, &lspvq, 1 }, { "lspres", no_argument, &lspres, 1 }, { "lspjvm", no_argument, &lspjvm, 1 }, #ifdef __EXPERIMENTAL__ { "lspanssi", no_argument, &lspanssi, 1 }, #endif { "phase0", no_argument, &phase0, 1 }, { "phaseexp", required_argument, &phaseexp, 1 }, { "ampexp", required_argument, &exp, 1 }, { "postfilter", no_argument, &postfilt, 1 }, { "hand_voicing", required_argument, &hand_voicing, 1 }, { "dec", required_argument, &dec, 1 }, { "hi", no_argument, &hi, 1 }, { "simlpcpf", no_argument, &simlpcpf, 1 }, { "lpcpf", no_argument, &lpcpf, 1 }, { "prede", no_argument, &prede, 1 }, { "dump_pitch_e", required_argument, &dump_pitch_e, 1 }, { "sq_pitch_e", no_argument, &scalar_quant_Wo_e, 1 }, { "vq_pitch_e", no_argument, &vector_quant_Wo_e, 1 }, { "rate", required_argument, NULL, 0 }, { "gain", required_argument, NULL, 0 }, { "bpf", no_argument, &bpf_en, 1 }, #ifdef DUMP { "dump", required_argument, &dump, 1 }, #endif { "help", no_argument, NULL, 'h' }, { NULL, no_argument, NULL, 0 } }; int num_opts=sizeof(long_options)/sizeof(struct option); COMP Aw[FFT_ENC]; for(i=0; i<M; i++) { Sn[i] = 1.0; Sn_pre[i] = 1.0; } for(i=0; i<2*N; i++) Sn_[i] = 0; prev_uq_Wo = prev_Wo = prev__Wo = TWO_PI/P_MAX; prev_model.Wo = TWO_PI/P_MIN; prev_model.L = floor(PI/prev_model.Wo); for(i=1; i<=prev_model.L; i++) { prev_model.A[i] = 0.0; prev_model.phi[i] = 0.0; } for(i=1; i<=MAX_AMP; i++) { //ex_phase[i] = (PI/3)*(float)rand()/RAND_MAX; ex_phase[i] = 0.0; } e = prev_e = 1; hpf_states[0] = hpf_states[1] = 0.0; nlp_states = nlp_create(M); if (argc < 2) { print_help(long_options, num_opts, argv); } /*----------------------------------------------------------------*\ Interpret Command Line Arguments \*----------------------------------------------------------------*/ while(1) { int option_index = 0; int opt = getopt_long(argc, argv, opt_string, long_options, &option_index); if (opt == -1) break; switch (opt) { case 0: if(strcmp(long_options[option_index].name, "lpc") == 0) { orderi = atoi(optarg); if((orderi < 4) || (orderi > order)) { fprintf(stderr, "Error in LPC order (4 to %d): %s\n", order, optarg); exit(1); } order = orderi; #ifdef DUMP } else if(strcmp(long_options[option_index].name, "dump") == 0) { if (dump) dump_on(optarg); #endif } else if(strcmp(long_options[option_index].name, "lsp") == 0 || strcmp(long_options[option_index].name, "lspd") == 0 || strcmp(long_options[option_index].name, "lspvq") == 0) { assert(order == LPC_ORD); } else if(strcmp(long_options[option_index].name, "dec") == 0) { decimate = atoi(optarg); if ((decimate != 2) && (decimate != 4)) { fprintf(stderr, "Error in --dec, must be 2 or 4\n"); exit(1); } if (!phase0) { printf("needs --phase0 to resample phase when using --dec\n"); exit(1); } if (!lpc_model) { printf("needs --lpc [order] to resample amplitudes when using --dec\n"); exit(1); } } else if(strcmp(long_options[option_index].name, "hand_voicing") == 0) { if ((fvoicing = fopen(optarg,"rt")) == NULL) { fprintf(stderr, "Error opening voicing file: %s: %s.\n", optarg, strerror(errno)); exit(1); } } else if(strcmp(long_options[option_index].name, "dump_pitch_e") == 0) { if ((fjvm = fopen(optarg,"wt")) == NULL) { fprintf(stderr, "Error opening pitch & energy dump file: %s: %s.\n", optarg, strerror(errno)); exit(1); } } else if(strcmp(long_options[option_index].name, "phaseexp") == 0) { strcpy(phaseexp_arg, optarg); } else if(strcmp(long_options[option_index].name, "ampexp") == 0) { strcpy(ampexp_arg, optarg); } else if(strcmp(long_options[option_index].name, "gain") == 0) { gain = atof(optarg); } else if(strcmp(long_options[option_index].name, "rate") == 0) { if(strcmp(optarg,"3200") == 0) { lpc_model = 1; scalar_quant_Wo_e = 1; lspd = 1; phase0 = 1; postfilt = 1; decimate = 1; lpcpf = 1; } else if(strcmp(optarg,"2400") == 0) { lpc_model = 1; vector_quant_Wo_e = 1; lsp = 1; phase0 = 1; postfilt = 1; decimate = 2; lpcpf = 1; } else if(strcmp(optarg,"1400") == 0) { lpc_model = 1; vector_quant_Wo_e = 1; lsp = 1; phase0 = 1; postfilt = 1; decimate = 4; lpcpf = 1; } else if(strcmp(optarg,"1300") == 0) { lpc_model = 1; scalar_quant_Wo_e = 1; lsp = 1; phase0 = 1; postfilt = 1; decimate = 4; lpcpf = 1; } else if(strcmp(optarg,"1200") == 0) { lpc_model = 1; scalar_quant_Wo_e = 1; lspjvm = 1; phase0 = 1; postfilt = 1; decimate = 4; lpcpf = 1; } else { fprintf(stderr, "Error: invalid output rate (3200|2400|1400|1200) %s\n", optarg); exit(1); } } break; case 'h': print_help(long_options, num_opts, argv); break; case 'o': if (strcmp(optarg, "-") == 0) fout = stdout; else if ((fout = fopen(optarg,"wb")) == NULL) { fprintf(stderr, "Error opening output speech file: %s: %s.\n", optarg, strerror(errno)); exit(1); } strcpy(out_file,optarg); break; default: /* This will never be reached */ break; } } /* Input file */ if (strcmp(argv[optind], "-") == 0) fin = stdin; else if ((fin = fopen(argv[optind],"rb")) == NULL) { fprintf(stderr, "Error opening input speech file: %s: %s.\n", argv[optind], strerror(errno)); exit(1); } ex_phase[0] = 0; Woe_[0] = Woe_[1] = 1.0; /* printf("lspd: %d lspdt: %d lspdt_mode: %d phase0: %d postfilt: %d " "decimate: %d dt: %d\n",lspd,lspdt,lspdt_mode,phase0,postfilt, decimate,dt); */ /* Initialise ------------------------------------------------------------*/ fft_fwd_cfg = kiss_fft_alloc(FFT_ENC, 0, NULL, NULL); /* fwd FFT,used in several places */ fft_inv_cfg = kiss_fft_alloc(FFT_DEC, 1, NULL, NULL); /* inverse FFT, used just for synth */ make_analysis_window(fft_fwd_cfg, w, W); make_synthesis_window(Pn); quantise_init(); if (phaseexp) pexp = phase_experiment_create(); if (ampexp) aexp = amp_experiment_create(); if (bpf_en) { for(i=0; i<BPF_N; i++) bpf_buf[i] = 0.0; } for(i=0; i<LPC_ORD; i++) { prev_lsps_dec[i] = i*PI/(LPC_ORD+1); } prev_e_dec = 1; for(m=1; m<=MAX_AMP; m++) prev_model_dec.A[m] = 0.0; prev_model_dec.Wo = TWO_PI/P_MAX; prev_model_dec.L = PI/prev_model_dec.Wo; prev_model_dec.voiced = 0; /*----------------------------------------------------------------* \ Main Loop \*----------------------------------------------------------------*/ frames = 0; sum_snr = 0; while(fread(buf,sizeof(short),N,fin)) { frames++; for(i=0; i<N; i++) buf_float[i] = buf[i]; /* optionally filter input speech */ if (prede) { pre_emp(Sn_pre, buf_float, &pre_mem, N); for(i=0; i<N; i++) buf_float[i] = Sn_pre[i]; } if (bpf_en) { for(i=0; i<BPF_N; i++) bpf_buf[i] = bpf_buf[N+i]; for(i=0; i<N; i++) bpf_buf[BPF_N+i] = buf_float[i]; inverse_filter(&bpf_buf[BPF_N], bpf, N, buf_float, BPF_N); } /* shift buffer of input samples, and insert new samples */ for(i=0; i<M-N; i++) { Sn[i] = Sn[i+N]; } for(i=0; i<N; i++) Sn[i+M-N] = buf_float[i]; /*------------------------------------------------------------*\ Estimate Sinusoidal Model Parameters \*------------------------------------------------------------*/ nlp(nlp_states,Sn,N,P_MIN,P_MAX,&pitch,Sw,W,&prev_uq_Wo); model.Wo = TWO_PI/pitch; dft_speech(fft_fwd_cfg, Sw, Sn, w); two_stage_pitch_refinement(&model, Sw); estimate_amplitudes(&model, Sw, W, 1); #ifdef DUMP dump_Sn(Sn); dump_Sw(Sw); dump_model(&model); #endif if (ampexp) amp_experiment(aexp, &model, ampexp_arg); if (phaseexp) { #ifdef DUMP dump_phase(&model.phi[0], model.L); #endif phase_experiment(pexp, &model, phaseexp_arg); #ifdef DUMP dump_phase_(&model.phi[0], model.L); #endif } if (hi) { int m; for(m=1; m<model.L/2; m++) model.A[m] = 0.0; for(m=3*model.L/4; m<=model.L; m++) model.A[m] = 0.0; } /*------------------------------------------------------------*\ Zero-phase modelling \*------------------------------------------------------------*/ if (phase0) { float Wn[M]; /* windowed speech samples */ float Rk[order+1]; /* autocorrelation coeffs */ COMP a[FFT_ENC]; #ifdef DUMP dump_phase(&model.phi[0], model.L); #endif /* find aks here, these are overwritten if LPC modelling is enabled */ for(i=0; i<M; i++) Wn[i] = Sn[i]*w[i]; autocorrelate(Wn,Rk,M,order); levinson_durbin(Rk,ak,order); /* determine voicing */ snr = est_voicing_mbe(&model, Sw, W, Sw_, Ew); if (dump_pitch_e) fprintf(fjvm, "%f %f %d ", model.Wo, snr, model.voiced); //printf("snr %3.2f v: %d Wo: %f prev_Wo: %f\n", snr, model.voiced, // model.Wo, prev_uq_Wo); #ifdef DUMP dump_Sw_(Sw_); dump_Ew(Ew); dump_snr(snr); #endif /* just to make sure we are not cheating - kill all phases */ for(i=0; i<=MAX_AMP; i++) model.phi[i] = 0; /* Determine DFT of A(exp(jw)), which is needed for phase0 model when LPC is not used, e.g. indecimate=1 (10ms) frames with no LPC */ for(i=0; i<FFT_ENC; i++) { a[i].real = 0.0; a[i].imag = 0.0; } for(i=0; i<=order; i++) a[i].real = ak[i]; kiss_fft(fft_fwd_cfg, (kiss_fft_cpx *)a, (kiss_fft_cpx *)Aw); if (hand_voicing) { fscanf(fvoicing,"%d\n",&model.voiced); } } /*------------------------------------------------------------*\ LPC model amplitudes and LSP quantisation \*------------------------------------------------------------*/ if (lpc_model) { e = speech_to_uq_lsps(lsps, ak, Sn, w, order); for(i=0; i<LPC_ORD; i++) lsps_[i] = lsps[i]; #ifdef DUMP dump_ak(ak, order); dump_E(e); #endif /* tracking down -ve energy values with BW expansion */ /* if (e < 0.0) { int i; FILE*f=fopen("x.txt","wt"); for(i=0; i<M; i++) fprintf(f,"%f\n", Sn[i]); fclose(f); printf("e = %f frames = %d\n", e, frames); for(i=0; i<order; i++) printf("%f ", ak[i]); exit(0); } */ if (dump_pitch_e) fprintf(fjvm, "%f\n", e); #ifdef DUMP dump_lsp(lsps); #endif /* various LSP quantisation schemes */ if (lsp) { encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD); decode_lsps_scalar(lsps_, lsp_indexes, LPC_ORD); bw_expand_lsps(lsps_, LPC_ORD, 50.0, 100.0); lsp_to_lpc(lsps_, ak, LPC_ORD); } if (lspd) { encode_lspds_scalar(lsp_indexes, lsps, LPC_ORD); decode_lspds_scalar(lsps_, lsp_indexes, LPC_ORD); lsp_to_lpc(lsps_, ak, LPC_ORD); } #ifdef __EXPERIMENTAL__ if (lspvq) { lspvq_quantise(lsps, lsps_, LPC_ORD); bw_expand_lsps(lsps_, LPC_ORD, 50.0, 100.0); lsp_to_lpc(lsps_, ak, LPC_ORD); } #endif if (lspjvm) { /* Jean-Marc's multi-stage, split VQ */ lspjvm_quantise(lsps, lsps_, LPC_ORD); { float lsps_bw[LPC_ORD]; memcpy(lsps_bw, lsps_, sizeof(float)*LPC_ORD); bw_expand_lsps(lsps_bw, LPC_ORD, 50.0, 100.0); lsp_to_lpc(lsps_bw, ak, LPC_ORD); } } #ifdef __EXPERIMENTAL__ if (lspanssi) { /* multi-stage VQ from Anssi Ramo OH3GDD */ lspanssi_quantise(lsps, lsps_, LPC_ORD, 5); bw_expand_lsps(lsps_, LPC_ORD, 50.0, 100.0); lsp_to_lpc(lsps_, ak, LPC_ORD); } #endif /* experimenting with non-linear LSP spacing to see if it's just noticable */ if (lspjnd) { for(i=0; i<LPC_ORD; i++) lsps_[i] = lsps[i]; locate_lsps_jnd_steps(lsps_, LPC_ORD); lsp_to_lpc(lsps_, ak, LPC_ORD); } /* Another experiment with non-linear LSP spacing, this time using a scaled version of mel frequency axis warping. The scaling is such that the integer output can be directly sent over the channel. */ if (lspmel) { float f, f_; float mel[LPC_ORD]; int mel_indexes[LPC_ORD]; for(i=0; i<order; i++) { f = (4000.0/PI)*lsps[i]; mel[i] = floor(2595.0*log10(1.0 + f/700.0) + 0.5); } for(i=1; i<order; i++) { if (mel[i] == mel[i-1]) mel[i]++; } encode_mels_scalar(mel_indexes, mel, 6); decode_mels_scalar(mel, mel_indexes, 6); #ifdef DUMP dump_mel(mel, order); #endif for(i=0; i<LPC_ORD; i++) { f_ = 700.0*( pow(10.0, (float)mel[i]/2595.0) - 1.0); lsps_[i] = f_*(PI/4000.0); } lsp_to_lpc(lsps_, ak, order); } if (scalar_quant_Wo_e) { e = decode_energy(encode_energy(e, E_BITS), E_BITS); model.Wo = decode_Wo(encode_Wo(model.Wo, WO_BITS), WO_BITS); model.L = PI/model.Wo; /* if we quantise Wo re-compute L */ } if (vector_quant_Wo_e) { /* JVM's experimental joint Wo & LPC energy quantiser */ quantise_WoE(&model, &e, Woe_); } } /*------------------------------------------------------------*\ Synthesise and optional decimation to 20 or 40ms frame rate \*------------------------------------------------------------*/ /* if decimate == 2, we interpolate frame n from frame n-1 and n+1 if decimate == 4, we interpolate frames n, n+1, n+2, from frames n-1 and n+3 This is meant to give identical results to the implementations of various modes in codec2.c */ /* delay line to keep frame by frame voicing decisions */ for(i=0; i<decimate-1; i++) model_dec[i] = model_dec[i+1]; model_dec[decimate-1] = model; if ((frames % decimate) == 0) { for(i=0; i<order; i++) lsps_dec[decimate-1][i] = lsps_[i]; e_dec[decimate-1] = e; model_dec[decimate-1] = model; /* interpolate the model parameters */ weight_inc = 1.0/decimate; for(i=0, weight=weight_inc; i<decimate-1; i++, weight += weight_inc) { //model_dec[i].voiced = model_dec[decimate-1].voiced; interpolate_lsp_ver2(&lsps_dec[i][0], prev_lsps_dec, &lsps_dec[decimate-1][0], weight, order); interp_Wo2(&model_dec[i], &prev_model_dec, &model_dec[decimate-1], weight); e_dec[i] = interp_energy2(prev_e_dec, e_dec[decimate-1],weight); } /* then recover spectral amplitudes and synthesise */ for(i=0; i<decimate; i++) { if (lpc_model) { lsp_to_lpc(&lsps_dec[i][0], &ak_dec[i][0], order); aks_to_M2(fft_fwd_cfg, &ak_dec[i][0], order, &model_dec[i], e_dec[i], &snr, 0, simlpcpf, lpcpf, 1, LPCPF_BETA, LPCPF_GAMMA, Aw); apply_lpc_correction(&model_dec[i]); #ifdef DUMP dump_lsp_(&lsps_dec[i][0]); dump_ak_(&ak_dec[i][0], order); sum_snr += snr; dump_quantised_model(&model_dec[i]); #endif } if (phase0) phase_synth_zero_order(fft_fwd_cfg, &model_dec[i], ex_phase, Aw); synth_one_frame(fft_inv_cfg, buf, &model_dec[i], Sn_, Pn, prede, &de_mem, gain); if (fout != NULL) fwrite(buf,sizeof(short),N,fout); } /* for(i=0; i<decimate; i++) { printf("%d Wo: %f L: %d v: %d\n", frames, model_dec[i].Wo, model_dec[i].L, model_dec[i].voiced); } if (frames == 4*50) exit(0); */ /* update memories for next frame ----------------------------*/ prev_model_dec = model_dec[decimate-1]; prev_e_dec = e_dec[decimate-1]; for(i=0; i<LPC_ORD; i++) prev_lsps_dec[i] = lsps_dec[decimate-1][i]; } } /*----------------------------------------------------------------*\ End Main Loop \*----------------------------------------------------------------*/ fclose(fin); if (fout != NULL) fclose(fout); if (lpc_model) printf("SNR av = %5.2f dB\n", sum_snr/frames); if (phaseexp) phase_experiment_destroy(pexp); if (ampexp) amp_experiment_destroy(aexp); #ifdef DUMP if (dump) dump_off(); #endif if (hand_voicing) fclose(fvoicing); nlp_destroy(nlp_states); return 0; }
xmlChar *get_uri(fsp_link *link, fs_rid rid) { if (cache[rid & CACHE_MASK].rid == rid) { return (xmlChar *) cache[rid & CACHE_MASK].lex; } fs_rid_vector onerid = { .length = 1, .size = 1, .data = &rid }; fs_resource resource; fsp_resolve(link, FS_RID_SEGMENT(rid, segments), &onerid, &resource); return (xmlChar *) resource.lex; } xmlChar *get_attr(fsp_link *link, fs_rid rid) { if (attr_cache[rid & CACHE_MASK].rid == rid) { return (xmlChar *) attr_cache[rid & CACHE_MASK].lex; } fs_rid_vector onerid = { .length = 1, .size = 1, .data = &rid }; fs_resource resource; fsp_resolve(link, FS_RID_SEGMENT(rid, segments), &onerid, &resource); memcpy(&attr_cache[rid & ATTR_CACHE_MASK], &resource, sizeof(fs_resource)); return (xmlChar *) resource.lex; } xmlChar *get_literal(fsp_link *link, fs_rid rid, fs_rid *attr) { if (cache[rid & CACHE_MASK].rid == rid) { *attr = cache[rid & CACHE_MASK].attr; return (xmlChar *) cache[rid & CACHE_MASK].lex; } fs_rid_vector onerid = { .length = 1, .size = 1, .data = &rid }; fs_resource resource; fsp_resolve(link, FS_RID_SEGMENT(rid, segments), &onerid, &resource); *attr = resource.attr; return (xmlChar *) resource.lex; } void resolve_triples(fsp_link *link, fs_rid_vector **rids) { int quads = rids[0]->length; fs_rid_vector *todo[segments]; fs_segment segment; for (segment = 0; segment < segments; ++segment) { todo[segment] = fs_rid_vector_new(0); } for (int c = 0; c < 3; ++c) { for (int k = 0; k < quads; ++k) { const fs_rid rid = rids[c]->data[k]; if (FS_IS_BNODE(rid) || cache[rid & CACHE_MASK].rid == rid) continue; fs_rid_vector_append(todo[FS_RID_SEGMENT(rid, segments)], rid); cache[rid & CACHE_MASK].rid = rid; /* well, it will be soon */ } } int length[segments]; fs_resource *resources[segments]; for (segment = 0; segment < segments; ++segment) { length[segment] = todo[segment]->length; resources[segment] = calloc(length[segment], sizeof(fs_resource)); } fsp_resolve_all(link, todo, resources); for (segment = 0; segment < segments; ++segment) { fs_resource *res = resources[segment]; for (int k = 0; k < length[segment]; ++k) { free(cache[res[k].rid & CACHE_MASK].lex); memcpy(&cache[res[k].rid & CACHE_MASK], &res[k], sizeof(fs_resource)); } fs_rid_vector_free(todo[segment]); free(resources[segment]); } } void dump_model(fsp_link *link, fs_rid model, xmlTextWriterPtr xml) { fs_rid_vector none = { .length = 0, .size = 0, .data = 0 }; fs_rid_vector one = { .length = 1, .size = 1, .data = &model }; fs_rid_vector **results; double then; /* for time keeping */ then = fs_time(); fsp_bind_first_all(link, BIND_SPO, &one, &none, &none, &none, &results, QUAD_LIMIT); time_bind_first += (fs_time() - then); while (results != NULL) { long length = results[0]->length; if (length == 0) break; then = fs_time(); resolve_triples(link, results); time_resolving += (fs_time() - then); then = fs_time(); for (int k = 0; k < length; ++k) { xmlTextWriterStartElement(xml, (xmlChar *) "triple"); for (int r = 0; r < 3; ++r) { fs_rid rid = results[r]->data[k]; if (FS_IS_BNODE(rid)) { unsigned long long node = FS_BNODE_NUM(rid); xmlTextWriterWriteFormatElement(xml, (xmlChar *) "id", "%llu", node); } else if (FS_IS_URI(rid)) { xmlChar *uri = get_uri(link, rid); xmlTextWriterWriteElement(xml, (xmlChar *) "uri", uri); } else if (FS_IS_LITERAL(rid)) { fs_rid attr; xmlChar *lex = get_literal(link, rid, &attr); if (attr == fs_c.empty) { xmlTextWriterWriteElement(xml, (xmlChar *) "plainLiteral", lex); } else if (FS_IS_URI(attr)) { xmlChar *type = get_uri(link, attr); xmlTextWriterStartElement(xml, (xmlChar *) "typedLiteral"); xmlTextWriterWriteString(xml, (xmlChar *) lex); xmlTextWriterWriteAttribute(xml, (xmlChar *) "datatype", type); xmlTextWriterEndElement(xml); } else if (FS_IS_LITERAL(attr)) { xmlChar *lang = get_attr(link, attr); xmlTextWriterStartElement(xml, (xmlChar *) "plainLiteral"); xmlTextWriterWriteAttribute(xml, (xmlChar *) "xml:lang", lang); xmlTextWriterWriteString(xml, (xmlChar *) lex); xmlTextWriterEndElement(xml); } } } xmlTextWriterEndElement(xml); xmlTextWriterWriteString(xml, (xmlChar *) "\n"); } time_write_out += (fs_time() - then); fs_rid_vector_free(results[0]); fs_rid_vector_free(results[1]); fs_rid_vector_free(results[2]); free(results); then = fs_time(); fsp_bind_next_all(link, BIND_SPO, &results, QUAD_LIMIT); time_bind_next += (fs_time() - then); } fsp_bind_done_all(link); } void dump_trix(fsp_link *link, xmlTextWriterPtr xml) { fs_rid_vector **models; fs_rid_vector none = { .length = 0, .size = 0, .data = 0 }; fsp_bind_all(link, FS_BIND_DISTINCT | FS_BIND_MODEL | FS_BIND_BY_SUBJECT, &none, &none, &none, &none, &models); fs_rid_vector_sort(models[0]); fs_rid_vector_uniq(models[0], 1); long length = models[0]->length; for (int k = 0; k < length; ++k) { fs_rid model = models[0]->data[k]; xmlChar *model_uri = get_uri(link, model); xmlTextWriterStartElement(xml, (xmlChar *) "graph"); if (FS_IS_URI(model)) { xmlTextWriterWriteElement(xml, (xmlChar *) "uri", model_uri); } else { fs_error(LOG_WARNING, "model %lld is not a URI", model); } dump_model(link, model, xml); xmlTextWriterEndElement(xml); xmlTextWriterWriteString(xml, (xmlChar *) "\n"); printf("%5d/%ld: %4.5f %4.5f %4.5f %4.5f\n", k + 1, length, time_resolving, time_bind_first, time_bind_next, time_write_out); } } void dump_file(fsp_link *link, char *filename) { xmlTextWriterPtr xml = xmlNewTextWriterFilename(filename, TRUE); if (!xml) { fs_error(LOG_ERR, "Couldn't write output file, giving up"); exit(4); } xmlTextWriterStartDocument(xml, NULL, NULL, NULL); xmlTextWriterStartElement(xml, (xmlChar *) "TriX"); dump_trix(link, xml); xmlTextWriterEndDocument(xml); /* also closes TriX */ xmlFreeTextWriter(xml); } int main(int argc, char *argv[]) { char *password = fsp_argv_password(&argc, argv); if (argc != 3) { fprintf(stderr, "%s revision %s\n", argv[0], FS_FRONTEND_VER); fprintf(stderr, "Usage: %s <kbname> <uri>\n", argv[0]); exit(1); } fsp_link *link = fsp_open_link(argv[1], password, FS_OPEN_HINT_RO); if (!link) { fs_error (LOG_ERR, "couldn't connect to “%s”", argv[1]); exit(2); } fs_hash_init(fsp_hash_type(link)); segments = fsp_link_segments(link); dump_file(link, argv[2]); fsp_close_link(link); }
int main() { #ifdef ENABLE_MEM_PROFILE mem_profile_init(); #endif set_sample_method("Metropolis-hastings"); set_sample_iterations(200); set_mh_burn_in(200); set_mh_lag(50); set_mh_max_initial_round(2000); /* use pointers to structs because the client doesn't need to know the struct sizes */ struct pp_state_t* state; struct pp_instance_t* instance; struct pp_query_t* query; struct pp_trace_store_t* traces; float result; state = pp_new_state(); printf("> state created\n"); pp_load_file(state, "parse/models/lda.model"); printf("> file loaded\n"); ModelNode* model = model_map_find(state->model_map, state->symbol_table, "latent_dirichlet_allocation"); printf(dump_model(model)); //query = pp_compile_string_query(""); //printf("> condition compiled\n"); pp_variable_t** param = malloc(sizeof(pp_variable_t*) * 4); param[0] = new_pp_int(2); param[1] = new_pp_int(2); param[2] = new_pp_vector(2); PP_VARIABLE_VECTOR_LENGTH(param[2]) = 2; for (int i = 0; i < 2; ++i) { PP_VARIABLE_VECTOR_VALUE(param[2])[i] = new_pp_int(2); } param[3] = new_pp_int(3); int X[2][2] = { {0, 0}, {1, 1}, }; query = pp_query_observe_int_array_2D(state, "X", &X[0][0], 2, 2); if (!query) return 1; traces = pp_sample_v(state, "latent_dirichlet_allocation", param, query, 1, "topic"); printf("> traces sampled\n"); if (!traces) { printf("ERROR encountered!!\n"); return 1; } char buffer[8096]; size_t max_index = 0; for (size_t i = 1; i < traces->n; ++i) { if (traces->trace[i]->logprob > traces->trace[max_index]->logprob) { max_index = i; } } printf("\nsample with max logprob:\n"); pp_trace_dump(buffer, 8096, traces->trace[max_index]); printf(buffer); printf("\nlast sample:\n"); pp_trace_dump(buffer, 8096, traces->trace[traces->n - 1]); printf(buffer); FILE* trace_dump_file = fopen("trace_dump.txt", "w"); for (size_t i = 0; i != traces->n; ++i) { pp_trace_dump(buffer, 8096, traces->trace[i]); fprintf(trace_dump_file, "[trace %u]\n", i); fprintf(trace_dump_file, buffer); } fclose(trace_dump_file); int nwords[] = {2, 2}; /* parameter estimation */ estimate_parameters(traces, 1.0, 1.0, 2, 2, nwords, 3, &X[0][0], 2); // pp_free is broken pp_free(state); /* free memory, associated models, instances, queries, and trace stores are deallocated */ pp_trace_store_destroy(traces); pp_query_destroy(query); for (int i = 0; i < 4; ++i) pp_variable_destroy(param[i]); free(param); #ifdef ENABLE_MEM_PROFILE mem_profile_print(); mem_profile_destroy(); #endif return 0; }
void train_crbm(dataset_blas *train_set, int nvisible, int nhidden, int nlabel, int epoch, double lr, int minibatch, double momentum, char *model_file){ crbm m; int i, j, k; int nepoch; double *v1, *y1; uint8_t *l; int batch_size, niter; int wc; int err; double lik; double delta; int *idx; time_t start_t, end_t; init_crbm(&m, nvisible, nhidden, nlabel); //wc = (int)cblas_dasum(train_set->N * train_set->n_feature, train_set->input, 1); niter = (train_set->N-1)/minibatch + 1; delta = lr / (1.0*minibatch); /* * shuffle training data v1 = (double*)malloc(minibatch * m.nvisible * sizeof(double)); y1 = (double*)malloc(minibatch * m.ncat * sizeof(double)); l = (uint8_t*)malloc(minibatch * sizeof(uint8_t)); idx = (int*)malloc(train_set->N * sizeof(int)); for(i = 0; i < train_set->N; i++) idx[i] = i;*/ bzero(w_u, sizeof(w_u)); bzero(u_u, sizeof(u_u)); bzero(bh_u, sizeof(bh_u)); bzero(bv_u, sizeof(bv_u)); bzero(by_u, sizeof(by_u)); //shuffle(idx, train_set->N); for(nepoch = 0; nepoch < epoch; nepoch++){ lik = 0; err = 0; start_t = time(NULL); for(k = 0; k < niter; k++){ #ifdef DEBUG if((k+1) % 200 == 0){ printf("batch %d\n", k+1); } #endif if(k == niter - 1){ batch_size = train_set->N - minibatch * (niter-1); }else{ batch_size = minibatch; } v1 = train_set->input + train_set->n_feature * minibatch * k; y1 = train_set->label + train_set->nlabel * minibatch * k; l = train_set->output + minibatch * k; /* * shuffle training data for(i = 0; i < batch_size; i++){ cblas_dcopy(m.nvisible, train_set->input + m.nvisible * idx[k*minibatch+i], 1, v1 + m.nvisible * i, 1); cblas_dcopy(m.ncat, train_set->label + m.ncat * idx[k*minibatch+i], 1, y1 + m.ncat * i, 1); l[i] = train_set->output[idx[k*minibatch+i]]; }*/ get_hidden(&m, v1, y1, ph1, batch_size); sample_hidden(&m, ph1, h1, batch_size); get_visible(&m, h1, pv, batch_size); sample_visible(&m, pv, v2, batch_size); get_class(&m, h1, py, batch_size); sample_class(&m, py, y2, batch_size); get_hidden(&m, v2, y2, ph2, batch_size); sample_hidden(&m, ph2, h2, batch_size); //lik += get_likelihood(&m, v1, pv, batch_size); err += get_error(&m, y2, l, batch_size); //update w_u cblas_dgemm(CblasRowMajor, CblasTrans, CblasNoTrans, m.nhidden, m.nvisible, batch_size, 1.0, ph2, m.nhidden, v2, m.nvisible, 0, a, m.nvisible); cblas_dgemm(CblasRowMajor, CblasTrans, CblasNoTrans, m.nhidden, m.nvisible, batch_size, 1.0, ph1, m.nhidden, v1, m.nvisible, -1, a, m.nvisible); cblas_daxpy(m.nvisible * m.nhidden, momentum, w_u, 1, a, 1); cblas_dcopy(m.nvisible * m.nhidden, a, 1, w_u, 1); //update u_u cblas_dgemm(CblasRowMajor, CblasTrans, CblasNoTrans, m.nhidden, m.ncat, batch_size, 1.0, ph2, m.nhidden, y2, m.ncat, 0, a, m.ncat); cblas_dgemm(CblasRowMajor, CblasTrans, CblasNoTrans, m.nhidden, m.ncat, batch_size, 1.0, ph1, m.nhidden, y1, m.ncat, -1, a, m.ncat); cblas_daxpy(m.ncat * m.nhidden, momentum, u_u, 1, a, 1); cblas_dcopy(m.ncat * m.nhidden, a, 1, u_u, 1); //update bv_u cblas_dgemm(CblasRowMajor, CblasTrans, CblasNoTrans, m.nvisible, 1, batch_size, 1.0, v2, m.nvisible, I, 1, 0, a, 1); cblas_dgemm(CblasRowMajor, CblasTrans, CblasNoTrans, m.nvisible, 1, batch_size, 1.0, v1, m.nvisible, I, 1, -1, a, 1); cblas_daxpy(m.nvisible, momentum, bv_u, 1, a, 1); cblas_dcopy(m.nvisible, a, 1, bv_u, 1); //update by_u cblas_dgemm(CblasRowMajor, CblasTrans, CblasNoTrans, m.ncat, 1, batch_size, 1.0, y2, m.ncat, I, 1, 0, a, 1); cblas_dgemm(CblasRowMajor, CblasTrans, CblasNoTrans, m.ncat, 1, batch_size, 1.0, y1, m.ncat, I, 1, -1, a, 1); cblas_daxpy(m.ncat, momentum, by_u, 1, a, 1); cblas_dcopy(m.ncat, a, 1, by_u, 1); //update bh_u cblas_dgemm(CblasRowMajor, CblasTrans, CblasNoTrans, m.nhidden, 1, batch_size, 1.0, ph2, m.nhidden, I, 1, 0, a, 1); cblas_dgemm(CblasRowMajor, CblasTrans, CblasNoTrans, m.nhidden, 1, batch_size, 1.0, ph1, m.nhidden, I, 1, -1, a, 1); cblas_daxpy(m.nhidden, momentum, bh_u, 1, a, 1); cblas_dcopy(m.nhidden, a, 1, bh_u, 1); //change parameter cblas_daxpy(m.nvisible * m.nhidden, delta, w_u, 1, m.w, 1); cblas_daxpy(m.ncat * m.nhidden, delta, u_u, 1, m.u, 1); cblas_daxpy(m.nvisible, delta, bv_u, 1, m.bv, 1); cblas_daxpy(m.ncat, delta, by_u, 1, m.by, 1); cblas_daxpy(m.nhidden, delta, bh_u, 1, m.bh, 1); } end_t = time(NULL); printf("[epoch %d] error:%.5lf%%\ttime:%.2fmin\n", nepoch + 1, err * 100.0 / train_set->N, (end_t - start_t) / 60.0); } dump_model(&m, model_file); //print_prob(&m, train_set, "../data/rsm/test.prob"); /* * shuffle training data free(v1); free(y1); free(l);*/ free_crbm(&m); }