Skip to content

Commit

Permalink
plotting AWGN and MPP curves
Browse files Browse the repository at this point in the history
  • Loading branch information
drowe67 committed Dec 17, 2024
1 parent 040d6c6 commit 465b347
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 32 deletions.
112 changes: 86 additions & 26 deletions asr_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,59 @@ function print_help {
echo
echo "Automated Speech Recognition (ASR) dataset processing for Radio Autoencoder testing"
echo
echo " usage ./asr_test.sh path/to/source dest [test option below]"
echo " usage ./ota_test.sh ~/.cache/LibriSpeech/test-clean ~/.cache/LibriSpeech/test-awgn-2dB --No -30"
echo " usage ./asr_test.sh ssb|rade [test option below]"
echo " usage ./ota_test.sh ssb --No -30"
echo " usage ./ota_test.sh rade --EbNodB 10"
echo
echo " --EbNodB EbNodB inference.py simulation noise level (experiment to get desired SNR)"
echo " --No NodB ch channel simulation No value (experiment to get desired SNR)"
echo " -n numSamples number of dataset samples to process (default all)"
echo " --results resultsFile name of results file (deafult results.txt)"
echo " -d verbose debug information"
exit
}

n_samples=0
No=-100
EbNodB=100
setpoint_rms=2048
comp_gain=6
mode="rade_inf"
results=asr_results.txt
inference_args=""
ch_args=""

POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--EbNodB)
EbNodB="$2"
shift
shift
;;
--g_file)
g_file="$2"
if [ ! -f $2 ]; then
echo "can't find $2"
exit 1
fi
inference_args="${inference_args} --g_file ${2}"
cp ${2} fast_fading_samples.float
ch_args="${ch_args} --fading_dir . --mpp --gain 0.5"
shift
shift
;;
--No)
No="$2"
shift
shift
;;
--results)
results="$2"
shift
shift
;;
-n)
n_samples="$2"
shift
Expand All @@ -61,17 +89,23 @@ esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters

if [ $# -lt 2 ]; then
if [ $# -lt 1 ]; then
print_help
fi
mode=$1

source=$1
dest=$2
source=~/.cache/LibriSpeech/test-clean
if [ ! -d $source ]; then
echo "cant find Librispeech source directory" $source
exit 1
fi
# results must be written to a directory known by Librispeech package (can't be any name)
dest=~/.cache/LibriSpeech/test-other
rm -Rf $dest

# cp translation files to new dataset directory
function cp_translation_files {
pushd $source; trans=$(find . -name '*.txt'); popd
pushd $source > /dev/null; trans=$(find . -name '*.txt'); popd > /dev/null
for f in $trans
do
d=$(dirname $f)
Expand All @@ -80,18 +114,18 @@ function cp_translation_files {
done
}

function mean_text_file {
file_name=#1
python3 <<EOF
import numpy as np
s=np.loadtxt(${file_name})
print(f"{np.mean(s):5.2f}")
EOF
function print_mean_text_file {
file_name=$1
python3 - <<END
import numpy as np
s=np.loadtxt("${file_name}")
print(f"{np.mean(s):5.2f}", end='')
END
}

# process audio files and place in new dataset directory
function process {
pushd $source; flac=$(find . -name '*.flac'); popd
pushd $source > /dev/null; flac=$(find . -name '*.flac'); popd > /dev/null
if [ $n_samples -ne 0 ]; then
flac=$(echo "$flac" | head -n $n_samples)
fi
Expand All @@ -102,29 +136,44 @@ function process {
in=in.raw
comp=comp.raw
ch_log=ch_log.txt
rade_log=rade_log.txt
snr_log=snr_log.txt
asr_log=asr.txt
rm -f ${snr_log}
CNo_log=CNo_log.txt
rm -f ${CNo_log}

if [ $mode == "ssb" ]; then


fading_adv=0
for f in $flac
do
d=$(dirname $f)
mkdir -p ${dest}/${d}
sox ${source}/${f} -t .s16 -r 8000 ${in}
# AGC and Hilbert compression
set_rms ${in} $setpoint_rms
analog_compressor ${in} ${comp} ${comp_gain}
papr=$(measure_cpapr ${comp})
ch ${comp} - --No ${No} 2>${ch_log} | sox -t .s16 -r 8000 -c 1 - -r 16000 ${dest}/${f}
analog_compressor ${in} ${comp} ${comp_gain} 2>/dev/null
ch ${comp} - --No ${No} ${ch_args} --fading_adv ${fading_adv} 2>${ch_log} | sox -t .s16 -r 8000 -c 1 - -r 16000 ${dest}/${f}
grep "Fading file finished" $ch_log
if [ $? -eq 0 ]; then
echo "Error - fading file too short after" $fading_adv " seconds"
exit 1
fi
snr=$(cat $ch_log | grep "SNR3k" | tr -s ' ' | cut -d' ' -f3)
CNo=$(cat $ch_log | grep "SNR3k" | tr -s ' ' | cut -d' ' -f5)
echo $snr >> ${snr_log}
echo ${dest}/${f} ${snr} ${papr}
print_mean_text_file ${snr_log}
echo $CNo >> ${CNo_log}

# advance through fading simulation file
dur=$(sox --info -D ${source}/${f})
fading_adv=$(python3 -c "print(${fading_adv} + ${dur})")
done
fi
SNR_mean=$(print_mean_text_file ${snr_log})
CNo_mean=$(print_mean_text_file ${CNo_log})
fi

if [ $mode == "rade_inf" ]; then
if [ $mode == "rade" ]; then
# find length of each file
duration_log=""
flac_full=""
Expand All @@ -143,7 +192,16 @@ function process {

# process all samples as one file to save time
./inference.sh model19_check3/checkpoints/checkpoint_epoch_100.pth ${in} out.wav \
--rate_Fs --pilots --pilot_eq --eq_ls --cp 0.004 --bottleneck 3 --auxdata
--rate_Fs --pilots --pilot_eq --eq_ls --cp 0.004 --bottleneck 3 --auxdata --time_offset -16 \
--EbNodB $EbNodB ${inference_args} | tee ${rade_log}
grep "Multipath Doppler spread file too short" $rade_log
if [ $? -eq 0 ]; then
echo "Error - fading file too short"
exit 1
fi

SNR_mean=$(cat $rade_log | grep "Measured" | tr -s ' ' | cut -d' ' -f4)
CNo_mean=$(cat $rade_log | grep "Measured" | tr -s ' ' | cut -d' ' -f3)

# extract individual output files
duration_array=( ${duration_log} )
Expand All @@ -152,7 +210,7 @@ function process {
for f in $flac
do
dur=${duration_array[i]}
printf "%4d %s %5.2f %5.2f\n" $i $f $st $dur
#printf "%4d %s %5.2f %5.2f\n" $i $f $st $dur
((i++))
if [ $i -eq ${#duration_array[@]} ]; then
sox out.wav ${dest}/${f} trim $st
Expand All @@ -163,7 +221,9 @@ function process {
done
fi


python3 asr_wer.py test-other -n $n_samples | tee > $asr_log
wer=$(tail -n1 $asr_log | tr -s ' ' | cut -d' ' -f2)
printf "%-4s %5.2f %5.2f %5.2f\n" $mode $SNR_mean $CNo_mean $wer | tee -a $results
}

cp_translation_files
Expand Down
30 changes: 30 additions & 0 deletions asr_test_awgn.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
# asr_test_awgn.sh
#
# Top level ASR test script for AWGN channels

results_file=241217_asr_awgn
n=100

function ssb_awgn {
No_range="-100 -39 -36 -33 -30 -25 -20"
for No in $No_range
do
./asr_test.sh ssb --No $No -n $n --results ${results_file}_ssb.txt
done
cat ${results_file}_ssb.txt | grep ssb | sed -e "s/ssb//" > tmp.txt
mv tmp.txt ${results_file}_ssb.txt
}

function rade_awgn {
EbNodB_range="100 15 10 5 2.5 0 -2.5"
for EbNodB in $EbNodB_range
do
./asr_test.sh rade --EbNodB $EbNodB -n $n --results ${results_file}_rade.txt
done
cat ${results_file}_rade.txt | grep rade | sed -e "s/rade//" > tmp.txt
mv tmp.txt ${results_file}_rade.txt
}

ssb_awgn
rade_awgn
14 changes: 8 additions & 6 deletions multipath_samples.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ function multipath_samples(ch, Fs, Rs, Nc, Nseconds, H_fn, G_fn="")
LCR_meas = LC/Nseconds
subplot(211); hold on; stem(LC_log,sqrt(P)*ones(length(LC_log))); hold off; axis([0 Nsecplot*Rs 0 3]);
end
printf("H file size is Nseconds*Rs*Nc*(4 bytes/sample) = %d*%d*%d*4 = %d bytes\n", Nseconds,Rs,Nc,Nseconds*Rs*Nc*4)
f=fopen(H_fn,"wb");
[r c] = size(H);
Hflat = reshape(H', 1, r*c);
fwrite(f, Hflat, 'float32');
fclose(f);
if length(H_fn)
printf("H file size is Nseconds*Rs*Nc*(4 bytes/sample) = %d*%d*%d*4 = %d bytes\n", Nseconds,Rs,Nc,Nseconds*Rs*Nc*4)
f=fopen(H_fn,"wb");
[r c] = size(H);
Hflat = reshape(H', 1, r*c);
fwrite(f, Hflat, 'float32');
fclose(f);
end

if length(G_fn)
% G matrix cols are G1 G2, rows timesteps, with hf_gain the first row,
Expand Down
32 changes: 32 additions & 0 deletions radae_plots.m
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,35 @@ function plot_sample_spec(wav_fn,png_spec_fn="")
print("-dpng",png_spec_fn,"-S800,600");
end
end

function plot_wer(ssb_awgn_fn,rade_awgn_fn, ssb_mpp_fn="",rade_mpp_fn="")
ssb_awgn = load(ssb_awgn_fn);
rade_awgn = load(rade_awgn_fn);
if length(ssb_mpp_fn)
ssb_mpp = load(ssb_mpp_fn);
rade_mpp = load(rade_mpp_fn);
end

figure(1); clf;
plot(ssb_awgn(:,2),ssb_awgn(:,3),'b+-;SSB AWGN;');
hold on;
plot(rade_awgn(:,2),rade_awgn(:,3),'g+-;RADE AWGN;');
if length(ssb_mpp_fn)
plot(ssb_mpp(:,2),ssb_mpp(:,3),'bo-;SSB MPP;');
plot(rade_mpp(:,2),rade_mpp(:,3),'go-;RADE MPP;');
end
hold off;
axis([30,60,0,60]); grid; ylabel('WER %'); xlabel("C/No (dB)");

figure(2); clf;
plot(ssb_awgn(:,1),ssb_awgn(:,3),'b+-;SSB AWGN;');
hold on;
plot(rade_awgn(:,1),rade_awgn(:,3),'g+-;RADE AWGN;');
if length(ssb_mpp_fn)
plot(ssb_mpp(:,1),ssb_mpp(:,3),'bo-;SSB MPP;');
plot(rade_mpp(:,1),rade_mpp(:,3),'go-;RADE MPP;');
end
hold off;
axis([-5,20,0,60]); grid; ylabel('WER %'); xlabel("SNR3k (dB)");
print("-dpng","asr_test","-S800,600");
end

0 comments on commit 465b347

Please sign in to comment.