|
| 1 | +#!/bin/bash |
| 2 | +# subsetvq.sh |
| 3 | +# David Rowe August 2021 |
| 4 | +# |
| 5 | +# Script to support: |
| 6 | +# 1. Subset VQ training and listening |
| 7 | +# 1. Training Trellis Vector Quantiser for Codec 2 newamp1, supports octave/trellis.m |
| 8 | +# 2. VQ sorting/optimisation experiments, octave/vq_compare.m |
| 9 | + |
| 10 | +TRAIN=~/Downloads/all_speech_8k.sw |
| 11 | +CODEC2_PATH=$HOME/codec2 |
| 12 | +PATH=$PATH:$CODEC2_PATH/build_linux/src:$CODEC2_PATH/build_linux/misc |
| 13 | +K=20 |
| 14 | +Kst=2 |
| 15 | +Ken=16 |
| 16 | + |
| 17 | +# train a new VQ and generate quantised training material |
| 18 | +function train() { |
| 19 | + fullfile=$TRAIN |
| 20 | + filename=$(basename -- "$fullfile") |
| 21 | + extension="${filename##*.}" |
| 22 | + filename="${filename%.*}" |
| 23 | + |
| 24 | + c2sim $fullfile --rateK --rateKout ${filename}.f32 |
| 25 | + echo "ratek=load_f32('../build_linux/${filename}.f32',20); vq_700c_eq; ratek_lim=limit_vec(ratek, 0, 40); save_f32('../build_linux/${filename}_lim.f32', ratek_lim); quit" | \ |
| 26 | + octave -p ${CODEC2_PATH}/octave -qf |
| 27 | + vqtrain ${filename}_lim.f32 $K 4096 vq_stage1.f32 -s 1e-3 --st $Kst --en $Ken |
| 28 | + |
| 29 | + # VQ the training file |
| 30 | + cat ${filename}_lim.f32 | vq_mbest --st $Kst --en $Ken -k $K -q vq_stage1.f32 > ${filename}_test.f32 |
| 31 | +} |
| 32 | + |
| 33 | +function listen() { |
| 34 | + vq_fn=$1 |
| 35 | + dec=$2 |
| 36 | + EbNodB=$3 |
| 37 | + fullfile=$4 |
| 38 | + filename=$(basename -- "$fullfile") |
| 39 | + extension="${filename##*.}" |
| 40 | + filename="${filename%.*}" |
| 41 | + |
| 42 | + fullfile_out=$5 |
| 43 | + sox_options='-t raw -e signed-integer -b 16' |
| 44 | + sox $fullfile $sox_options - | c2sim - --rateK --rateKout ${filename}.f32 |
| 45 | + |
| 46 | + echo "ratek=load_f32('../build_linux/${filename}.f32',20); vq_700c_eq; ratek_lim=limit_vec(ratek, 0, 40); save_f32('../build_linux/${filename}_lim.f32', ratek_lim); quit" | \ |
| 47 | + octave -p ${CODEC2_PATH}/octave -qf |
| 48 | + |
| 49 | + echo "pkg load statistics; vq_compare(action='vq_file', '${vq_fn}', ${dec}, ${EbNodB}, '${filename}_lim.f32', '${filename}_test.f32'); quit" \ | |
| 50 | + octave -p ${CODEC2_PATH}/octave -qf |
| 51 | + |
| 52 | + if [ "$fullfile_out" = "aplay" ]; then |
| 53 | + sox $fullfile $sox_options - | c2sim - --rateK --rateKin ${filename}_test.f32 -o - | aplay -f S16_LE |
| 54 | + else |
| 55 | + sox $fullfile $sox_options - | c2sim - --rateK --rateKin ${filename}_test.f32 -o - | sox -t .s16 -r 8000 -c 1 - ${fullfile_out} |
| 56 | + fi |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +function print_help { |
| 61 | + echo |
| 62 | + echo "Trellis/VQ optimisation support script" |
| 63 | + echo |
| 64 | + echo " usage ./train_trellis.sh [-x] [-t] [-v vq.f32 in.wav out.wav] [-e EbNodB] [-d dec]" |
| 65 | + echo |
| 66 | + echo " -x debug mode; trace script execution" |
| 67 | + echo " -t train VQ and generate a fully quantised version of training vectors" |
| 68 | + echo " -v in.wav out.wav vq.f32 synthesise an output file out.wav from in.raw, using the VQ vq.f32" |
| 69 | + echo " -v in.wav aplay vq.f32 synthesise output, play immediately using aplay, using the VQ vq.f32" |
| 70 | + echo " -e EbNodB Eb/No in dB for AWGn channel simulation (error insertion)" |
| 71 | + echo " -d dec decimation/interpolation rate" |
| 72 | + echo |
| 73 | + exit |
| 74 | +} |
| 75 | + |
| 76 | +# command line arguments to select function |
| 77 | + |
| 78 | +if [ $# -lt 1 ]; then |
| 79 | + print_help |
| 80 | +fi |
| 81 | + |
| 82 | +do_train=0 |
| 83 | +do_vq=0 |
| 84 | +EbNodB=100 |
| 85 | +dec=1 |
| 86 | +POSITIONAL=() |
| 87 | +while [[ $# -gt 0 ]] |
| 88 | +do |
| 89 | +key="$1" |
| 90 | +case $key in |
| 91 | + -x) |
| 92 | + set -x |
| 93 | + shift |
| 94 | + ;; |
| 95 | + -t) |
| 96 | + do_train=1 |
| 97 | + shift |
| 98 | + ;; |
| 99 | + -v) |
| 100 | + do_vq=1 |
| 101 | + vq_fn="$2" |
| 102 | + in_wav="$3" |
| 103 | + out_wav="$4" |
| 104 | + shift |
| 105 | + shift |
| 106 | + shift |
| 107 | + shift |
| 108 | + ;; |
| 109 | + -d) |
| 110 | + dec="$2" |
| 111 | + shift |
| 112 | + shift |
| 113 | + ;; |
| 114 | + -e) |
| 115 | + EbNodB="$2" |
| 116 | + shift |
| 117 | + shift |
| 118 | + ;; |
| 119 | + -h) |
| 120 | + print_help |
| 121 | + ;; |
| 122 | + *) |
| 123 | + POSITIONAL+=("$1") # save it in an array for later |
| 124 | + shift |
| 125 | + ;; |
| 126 | +esac |
| 127 | +done |
| 128 | +set -- "${POSITIONAL[@]}" # restore positional parameters |
| 129 | + |
| 130 | +if [ $do_train -eq 1 ]; then |
| 131 | + train |
| 132 | +fi |
| 133 | + |
| 134 | +if [ $do_vq -eq 1 ]; then |
| 135 | + listen ${vq_fn} ${dec} ${EbNodB} ${in_wav} ${out_wav} |
| 136 | +fi |
0 commit comments