From ed9494deadabd1ff6311a53dd5cdca0ba4925536 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Sun, 12 May 2019 07:29:26 +0930 Subject: [PATCH] building up test framework to track down clicking bug on Windows --- src/diff32.c | 18 +++++++---- src/linux_v_windows.m | 70 ++++++++++++++++++++++++++++++++++++++++++ src/linux_v_windows.sh | 24 +++++++++++++++ src/lpcnet.c | 28 +++++++++++++++++ src/lpcnet.h | 2 ++ src/test_lpcnet.c | 6 +++- 6 files changed, 141 insertions(+), 7 deletions(-) create mode 100644 src/linux_v_windows.m create mode 100755 src/linux_v_windows.sh diff --git a/src/diff32.c b/src/diff32.c index 161c00b..bab19d6 100644 --- a/src/diff32.c +++ b/src/diff32.c @@ -1,3 +1,5 @@ +/* test tool to diffs two .f32 files */ + #include #include #include @@ -6,13 +8,17 @@ int main(int argc, char *argv[]) { float f1[NB_FEATURES],f2[NB_FEATURES],fdiff=0.0; - int ret,f=0,i; + int f=0; + unsigned int ret, i, stride = NB_FEATURES; FILE *file1 = fopen(argv[1],"rb"); FILE *file2 = fopen(argv[2],"rb"); - while(fread(&f1,sizeof(float),NB_FEATURES,file1) == NB_FEATURES) { - ret = fread(&f2,sizeof(float),NB_FEATURES,file2); - if (ret != NB_FEATURES) break; - for(i=0; i 1E-6)) { @@ -22,6 +28,6 @@ int main(int argc, char *argv[]) { } f++; } - fprintf(stderr,"f: %d fdiff: %f\n", f, fdiff); + fprintf(stderr,"stride: %d f: %d fdiff: %f\n", stride, f, fdiff); fclose(file1); fclose(file2); } diff --git a/src/linux_v_windows.m b/src/linux_v_windows.m new file mode 100644 index 0000000..4fd77fb --- /dev/null +++ b/src/linux_v_windows.m @@ -0,0 +1,70 @@ +% linux_v_windows.m +% David Rowe May 2019 +% +% Part of system to generate and compare Linux and Windows test files +% that contain LPCNet states. Use to track down issue with Windows version +% +% Run in Octave from LPCNet/src + +1; + +function check_vec(fig_num, name, vec1, vec2) + figure(fig_num); clf; + plot(vec1); hold on; plot(vec2); hold off; + title(name); + diff = var(vec1-vec2); + if diff < 1E-3 + printf("%s [PASS]\n", name); + else + printf("%s [FAIL]\n", name); + end +endfunction + +function check_matrix(fig_num, name, mat1, mat2) + figure(fig_num); clf; + mesh(mat1); hold on; mesh(mat2); hold off; + title(name); + mdiff = mat1-mat2; diff = var(mdiff(:)); + if diff < 1E-3 + printf("%s [PASS]\n", name); + else + printf("%s [FAIL]\n", name); + % find first row where problem occurs + [rows cols]= size(mat1); + first_error = 1; + for r=1:rows + e = var(mat1(r,:) - mat2(r,:)); + if e > 1E-3 + printf("error %f in row: %d\n", e, r); + if (first_error) + clf; plot(mat1(r,:)); hold on; plot(mat2(r,:)); plot(mat1(r,:) - mat2(r,:)); hold off; + first_error = 0; + end + end + end + end +endfunction + +n_pitch_embed = 64; +n_pitch = 1; +n_pitch_gain = 1; +n_lpc = 16; +n_condition = 128; +n_cols = n_pitch_embed + n_pitch + n_pitch_gain + n_lpc + n_condition; + +linux=load_f32("../build_linux/src/test_lpcnet_states.f32", n_cols); +[r c]=size(linux); +printf("linux %d x %d\n", r, c); +windows=load_f32("../build_win/src/test_lpcnet_states.f32", n_cols); +[r c]=size(windows); +printf("windows %d x %d\n", r, c); + +fig = 1; +st = 1; +en = st + n_pitch_embed-1; check_matrix(fig++, "pitch_embed", linux(:,st:en), windows(:,st:en)); st += n_pitch_embed; +check_vec(fig++, "pitch", linux(:,st), windows(:,st)); st += n_pitch; +check_vec(fig++, "pitch_gain", linux(:,st), windows(:,st)); st += n_pitch_gain; +en = st + n_lpc-1; check_matrix(fig++, "lpc", linux(:,st:en), windows(:,st:en)); st += n_lpc; +en = st + n_condition-1; check_matrix(fig++, "condition", linux(:,st:en), windows(:,st:en)); st += n_condition; + + diff --git a/src/linux_v_windows.sh b/src/linux_v_windows.sh new file mode 100755 index 0000000..75945ef --- /dev/null +++ b/src/linux_v_windows.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# linux_v_windows.sh +# David Rowe May 2019 +# +# Part of system to generate and compare Linux and Windows test files +# that contain LPCNet states. Use to track down issue with Windows version + +export WINEPATH=$HOME/freedv-gui/codec2/build_win/src';'$HOME/freedv-gui/build_win/_CPack_Packages/win64/NSIS/FreeDV-1.4.0-devel-win64/bin/ + +# start in LPCNet dir +p=$PWD + +# Windows +cd build_win/src && make test_lpcnet +wine test_lpcnet.exe wia.f32 wia_out.raw +cd $p + +# Linux +cd build_linux/src && make test_lpcnet +./test_lpcnet ../../build_win/src/wia.f32 wia_out.raw +./diff32 test_lpcnet_states.f32 ../../build_win/src/test_lpcnet_states.f32 210 +cd $p + + diff --git a/src/lpcnet.c b/src/lpcnet.c index 1ac41c1..60dbb95 100644 --- a/src/lpcnet.c +++ b/src/lpcnet.c @@ -53,6 +53,7 @@ struct LPCNetState { float old_gain[FEATURES_DELAY]; int frame_count; float deemph_mem; + FILE *ftest; /* used to dump states for automates tests */ }; @@ -86,6 +87,10 @@ void run_frame_network(LPCNetState *lpcnet, float *condition, float *gru_a_condi compute_dense(&feature_dense2, condition, dense1_out); compute_dense(&gru_a_dense_feature, gru_a_condition, condition); if (lpcnet->frame_count < 1000) lpcnet->frame_count++; + + if (lpcnet->ftest) { + fwrite(&in[NB_FEATURES], sizeof(float), EMBED_PITCH_OUT_SIZE, lpcnet->ftest); + } } void run_sample_network(NNetState *net, float *pdf, const float *condition, const float *gru_a_condition, int last_exc, int last_sig, int pred) @@ -109,14 +114,24 @@ LPCNetState *lpcnet_create() LPCNetState *lpcnet; lpcnet = (LPCNetState *)calloc(sizeof(LPCNetState), 1); lpcnet->last_exc = 128; + lpcnet->ftest = NULL; return lpcnet; } void lpcnet_destroy(LPCNetState *lpcnet) { + if (lpcnet->ftest) fclose(lpcnet->ftest); free(lpcnet); } +void lpcnet_open_test_file(LPCNetState *lpcnet, char file_name[]) { + lpcnet->ftest = fopen(file_name, "wb"); + if (lpcnet->ftest == NULL) { + fprintf(stderr, "Error opening LPCNet test file: %s\n", file_name); + exit(1); + } +} + void lpcnet_synthesize(LPCNetState *lpcnet, short *output, const float *features, int N) { int i; @@ -137,6 +152,19 @@ void lpcnet_synthesize(LPCNetState *lpcnet, short *output, const float *features memcpy(lpc, lpcnet->old_lpc[FEATURES_DELAY-1], LPC_ORDER*sizeof(lpc[0])); memmove(lpcnet->old_lpc[1], lpcnet->old_lpc[0], (FEATURES_DELAY-1)*LPC_ORDER*sizeof(lpc[0])); lpc_from_cepstrum(lpcnet->old_lpc[0], features); + + if (lpcnet->ftest) { + float pitch_f = pitch; + fwrite(&pitch_f, sizeof(float), 1, lpcnet->ftest); + fwrite(&pitch_gain, sizeof(float), 1, lpcnet->ftest); + fwrite(lpc, sizeof(float), LPC_ORDER, lpcnet->ftest); + fwrite(condition, sizeof(float), FEATURE_DENSE2_OUT_SIZE, lpcnet->ftest); + if (lpcnet->frame_count==1) { + fprintf(stderr, "%d %d %d %d %d\n", EMBED_PITCH_OUT_SIZE, 1,1,LPC_ORDER,FEATURE_DENSE2_OUT_SIZE); + fprintf(stderr, "ftest cols = %d\n", EMBED_PITCH_OUT_SIZE+1+1+LPC_ORDER+FEATURE_DENSE2_OUT_SIZE); + } + } + if (lpcnet->frame_count <= FEATURES_DELAY) { RNN_CLEAR(output, N); diff --git a/src/lpcnet.h b/src/lpcnet.h index 2744b08..002e43e 100644 --- a/src/lpcnet.h +++ b/src/lpcnet.h @@ -36,4 +36,6 @@ LPCNetState *lpcnet_create(); void lpcnet_destroy(LPCNetState *lpcnet); void lpcnet_synthesize(LPCNetState *lpcnet, short *output, const float *features, int N); +void lpcnet_open_test_file(LPCNetState *lpcnet, char file_name[]); + #endif diff --git a/src/test_lpcnet.c b/src/test_lpcnet.c index 8841de4..f748167 100644 --- a/src/test_lpcnet.c +++ b/src/test_lpcnet.c @@ -34,7 +34,10 @@ int main(int argc, char **argv) { FILE *fin, *fout; LPCNetState *net; + net = lpcnet_create(); + lpcnet_open_test_file(net, "test_lpcnet_states.f32"); + if (argc != 3) { fprintf(stderr, "usage: test_lpcnet \n"); @@ -58,7 +61,8 @@ int main(int argc, char **argv) { } } - while (1) { + + while (1) { float in_features[NB_TOTAL_FEATURES]; float features[NB_FEATURES]; short pcm[FRAME_SIZE];