-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.sh
executable file
·68 lines (59 loc) · 2.05 KB
/
utils.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# utils.sh
#
# Bash include file with reusable utility functions
# Approximation of Hilbert clipper type compressor. Could do with some HF boost
function analog_compressor {
input_file=$1
output_file=$2
gain=$3
cat $input_file | ch - - 2>/dev/null | \
ch - - --No -100 --clip 16384 --gain $gain 2>/dev/null | \
# final line prints peak and CPAPR for SSB
ch - - --clip 16384 --ssbfilt 0 |
sox -t .s16 -r 8000 -c 1 - -t .s16 $output_file
}
# Note "--clip 16384 --ssbfilt 0" prevents ringing from input HT and SSB filter
# that affects peak values and PAPR. This could use some review and tests
function measure_rms() {
ch_log=$(mktemp)
raw=$1
shift
ch $raw /dev/null --clip 16384 --ssbfilt 0 $@ 2>${ch_log}
rms=$(cat $ch_log| grep "RMS" | tr -s ' ' | cut -d' ' -f5)
echo $rms
}
function measure_peak() {
ch_log=$(mktemp)
ch $1 /dev/null --clip 16384 --ssbfilt 0 2>${ch_log}
peak=$(cat $ch_log | grep "peak" | tr -s ' ' | cut -d' ' -f3)
echo $peak
}
function measure_cpapr() {
ch_log=$(mktemp)
ch $1 /dev/null --clip 16384 --ssbfilt 0 2>${ch_log}
cpapr=$(cat $ch_log | grep "CPAPR" | tr -s ' ' | cut -d' ' -f7)
echo $cpapr
}
# Make power of a raw file $1 equal to the setpoint $2, buy adjusting the RMS level
function set_rms() {
raw=$1
setpoint_rms=$2
rms=$(measure_rms $raw)
gain=$(python3 -c "gain=${setpoint_rms}/${rms}; print(\"%f\" % gain)")
raw_gain=$(mktemp)
sox -t .s16 -r 8k -c 1 -v $gain $raw -t .s16 -r 8k -c 1 $raw_gain
cp $raw_gain $raw
}
# Make peak power of a raw file $1 equal to the setpoint $2, buy adjusting the peak level
function set_peak() {
raw=$1
setpoint_peak=$2
peak=$(measure_peak $raw)
gain=$(python3 -c "gain=${setpoint_peak}/${peak}; print(\"%f\" % gain)")
raw_gain=$(mktemp)
sox -t .s16 -r 8k -c 1 -v $gain $raw -t .s16 -r 8k -c 1 $raw_gain
cp $raw_gain $raw
}
function spectrogram() {
echo "pkg load signal; rx=load_raw(\"$1\"); plot_specgram(rx, Fs=8000, 0, 3000); print('-dpng',\"$2\"); quit" | octave-cli -qf
}