Skip to content

Commit 98e88c7

Browse files
committed
Audio: MFCC: Refactor run_mfcc.sh and add decode_all.m
Refactor run_mfcc.sh into functions for input conversion and testbench execution to reduce code duplication. Add Xtensa testbench support when XTENSA_PATH environment variable is set, producing xt_ prefixed output files. Add decode_all.m Octave script to decode and plot all MFCC cepstral and Mel spectrogram output files from run_mfcc.sh, including Xtensa variants. Update README.txt to document the current run_mfcc.sh output files, Xtensa support, and decode_all.m usage. Export XTENSA_PATH in rebuild-testbench.sh so that run_mfcc.sh can find the Xtensa toolchain path for the testbench build. Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
1 parent 52fa825 commit 98e88c7

4 files changed

Lines changed: 110 additions & 55 deletions

File tree

scripts/rebuild-testbench.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export_xtensa_setup()
9797
cat <<EOFSETUP > "$export_script"
9898
export XTENSA_TOOLS_ROOT=$XTENSA_TOOLS_ROOT
9999
export XTENSA_CORE=$XTENSA_CORE
100-
XTENSA_PATH=$tools_bin
100+
export XTENSA_PATH=$tools_bin
101101
EOFSETUP
102102
}
103103

src/audio/mfcc/tune/README.txt

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,45 @@ need to be created with "scripts/build-tools.sh -t". Next the testbench
88
is build with "scripts/rebuild-testbench.sh".
99

1010
Once the previous steps are done, a sample wav file can be processed
11-
into stream of cepstral coefficients with script run_mfcc.sh. E.g.
12-
next command processes an ALSA test file with speech clip "front center".
13-
The output file is hard-coded to mfcc.raw.
11+
with script run_mfcc.sh. The script converts the input to raw 16 kHz
12+
stereo format and runs the testbench for S16, S24, and S32 bit depths,
13+
producing both cepstral coefficient (MFCC) and Mel spectrogram outputs.
1414

1515
./run_mfcc.sh /usr/share/sounds/alsa/Front_Center.wav
1616

17-
The output can be plotted and retrieved with Matlab or Octave command:
17+
Output files from host testbench:
18+
mfcc_s16.raw, mfcc_s24.raw, mfcc_s32.raw - cepstral coefficients
19+
mel_s16.raw, mel_s24.raw, mel_s32.raw - Mel spectrogram
20+
21+
If the XTENSA_PATH environment variable is set, the script also runs
22+
the Xtensa build of the testbench (via xt-run) and produces additional
23+
output files prefixed with "xt_":
24+
xt_mfcc_s16.raw, xt_mfcc_s24.raw, xt_mfcc_s32.raw
25+
xt_mel_s16.raw, xt_mel_s24.raw, xt_mel_s32.raw
26+
27+
All output files can be decoded and plotted at once in Matlab or Octave
28+
with the decode_all.m script:
29+
30+
decode_all
31+
32+
This calls decode_ceps for each MFCC file (13 cepstral coefficients) and
33+
decode_mel for each Mel file (80 Mel bins), plotting spectrograms for all
34+
files that exist including the Xtensa variants.
35+
36+
Individual files can also be decoded manually:
1837

1938
[ceps, t, n] = decode_ceps('mfcc_s16.raw', 13);
2039

2140
In the above it's known from configuration script that MFCC was set up to
2241
output 13 cepstral coefficients from each FFT -> Mel -> DCT -> Cepstral
2342
coefficients computation run.
2443

44+
The 80 bands Mel output can be visualized with command:
45+
46+
[mel, t, n] = decode_mel('mel_s16.raw', 80);
47+
2548
Other kind of signals have quite big visual difference in audio features. Try
2649
e.g. other sound files found in computer.
2750

2851
./run_mfcc.sh /usr/share/sounds/gnome/default/alerts/bark.ogg
2952
./run_mfcc.sh /usr/share/sounds/gnome/default/alerts/sonar.ogg
30-
31-
The script runs the same input sample with s16/24/32 formats for
32-
cepstral coefficients data output and Mel frequency spectrogram
33-
output. The 80 bands Mel output can be visualized with command:
34-
35-
[ceps, t, n] = decode_mel('mel_s16.raw', 80);

src/audio/mfcc/tune/decode_all.m

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
% decode_all.m - Decode all MFCC and Mel raw output files from run_mfcc.sh
2+
%
3+
% SPDX-License-Identifier: BSD-3-Clause
4+
% Copyright(c) 2026 Intel Corporation.
5+
6+
num_ceps = 13;
7+
num_mel = 80;
8+
9+
% MFCC cepstral output files
10+
ceps_files = {'mfcc_s16.raw', 'mfcc_s24.raw', 'mfcc_s32.raw'};
11+
12+
% Mel output files with corresponding format
13+
mel_files = {'mel_s16.raw', 'mel_s24.raw', 'mel_s32.raw'};
14+
mel_fmts = {'s16', 's24', 's32'};
15+
16+
% Xtensa prefixed variants
17+
xt_ceps_files = {'xt_mfcc_s16.raw', 'xt_mfcc_s24.raw', 'xt_mfcc_s32.raw'};
18+
xt_mel_files = {'xt_mel_s16.raw', 'xt_mel_s24.raw', 'xt_mel_s32.raw'};
19+
20+
all_ceps_files = [ceps_files, xt_ceps_files];
21+
all_mel_files = [mel_files, xt_mel_files];
22+
all_mel_fmts = [mel_fmts, mel_fmts];
23+
24+
for i = 1:length(all_ceps_files)
25+
fn = all_ceps_files{i};
26+
if exist(fn, 'file')
27+
fprintf('Decoding MFCC ceps: %s\n', fn);
28+
[ceps, t, n] = decode_ceps(fn, num_ceps);
29+
end
30+
end
31+
32+
for i = 1:length(all_mel_files)
33+
fn = all_mel_files{i};
34+
fmt = all_mel_fmts{i};
35+
if exist(fn, 'file')
36+
fprintf('Decoding Mel: %s\n', fn);
37+
[mel, t, n] = decode_mel(fn, num_mel, fmt);
38+
end
39+
end

src/audio/mfcc/tune/run_mfcc.sh

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,49 @@ set -e
77
RAW_INPUT_S16=in_s16.raw
88
RAW_INPUT_S24=in_s24.raw
99
RAW_INPUT_S32=in_s32.raw
10-
RAW_OUTPUT_S16=mfcc_s16.raw
11-
RAW_OUTPUT_S24=mfcc_s24.raw
12-
RAW_OUTPUT_S32=mfcc_s32.raw
1310

1411
VALGRIND="valgrind --leak-check=full"
12+
#VALGRIND=""
1513
TESTBENCH=$SOF_WORKSPACE/sof/tools/testbench/build_testbench/install/bin/sof-testbench4
16-
TOPOLOGY_S16=$SOF_WORKSPACE/sof/tools/build_tools/topology/topology2/development/sof-hda-benchmark-mfcc16.tplg
17-
TOPOLOGY_S24=$SOF_WORKSPACE/sof/tools/build_tools/topology/topology2/development/sof-hda-benchmark-mfcc24.tplg
18-
TOPOLOGY_S32=$SOF_WORKSPACE/sof/tools/build_tools/topology/topology2/development/sof-hda-benchmark-mfcc32.tplg
19-
OPT_S16="-r 16000 -c 2 -b S16_LE -p 3,4 -t $TOPOLOGY_S16"
20-
OPT_S24="-r 16000 -c 2 -b S24_LE -p 3,4 -t $TOPOLOGY_S24"
21-
OPT_S32="-r 16000 -c 2 -b S32_LE -p 3,4 -t $TOPOLOGY_S32"
22-
23-
# Convert input audio file raw 16 kHz 2 channel 16 bit
24-
sox -R --encoding signed-integer "$1" -L -r 16000 -c 2 -b 16 "$RAW_INPUT_S16"
25-
sox -R --no-dither --encoding signed-integer -L -r 16000 -c 2 -b 16 "$RAW_INPUT_S16" -b 32 "$RAW_INPUT_S32"
26-
sox -R --no-dither --encoding signed-integer -L -r 16000 -c 2 -b 16 "$RAW_INPUT_S16" -b 32 "$RAW_INPUT_S24" vol 0.003906250000
27-
28-
# Run testbench
29-
$VALGRIND $TESTBENCH $OPT_S16 -i "$RAW_INPUT_S16" -o "$RAW_OUTPUT_S16"
30-
$VALGRIND $TESTBENCH $OPT_S24 -i "$RAW_INPUT_S24" -o "$RAW_OUTPUT_S24"
31-
$VALGRIND $TESTBENCH $OPT_S32 -i "$RAW_INPUT_S32" -o "$RAW_OUTPUT_S32"
32-
33-
echo ----------------------------------------------------------------------------------
34-
echo The MFCC data was output to file $RAW_OUTPUT_S16, $RAW_OUTPUT_S24, $RAW_OUTPUT_S32
35-
echo ----------------------------------------------------------------------------------
36-
37-
RAW_OUTPUT_S16=mel_s16.raw
38-
RAW_OUTPUT_S24=mel_s24.raw
39-
RAW_OUTPUT_S32=mel_s32.raw
40-
41-
TESTBENCH=$SOF_WORKSPACE/sof/tools/testbench/build_testbench/install/bin/sof-testbench4
42-
TOPOLOGY_S16=$SOF_WORKSPACE/sof/tools/build_tools/topology/topology2/development/sof-hda-benchmark-mfccmel16.tplg
43-
TOPOLOGY_S24=$SOF_WORKSPACE/sof/tools/build_tools/topology/topology2/development/sof-hda-benchmark-mfccmel24.tplg
44-
TOPOLOGY_S32=$SOF_WORKSPACE/sof/tools/build_tools/topology/topology2/development/sof-hda-benchmark-mfccmel32.tplg
45-
OPT_S16="-r 16000 -c 2 -b S16_LE -p 3,4 -t $TOPOLOGY_S16"
46-
OPT_S24="-r 16000 -c 2 -b S24_LE -p 3,4 -t $TOPOLOGY_S24"
47-
OPT_S32="-r 16000 -c 2 -b S32_LE -p 3,4 -t $TOPOLOGY_S32"
48-
49-
# Run testbench
50-
$VALGRIND $TESTBENCH $OPT_S16 -i "$RAW_INPUT_S16" -o "$RAW_OUTPUT_S16"
51-
$VALGRIND $TESTBENCH $OPT_S24 -i "$RAW_INPUT_S24" -o "$RAW_OUTPUT_S24"
52-
$VALGRIND $TESTBENCH $OPT_S32 -i "$RAW_INPUT_S32" -o "$RAW_OUTPUT_S32"
53-
54-
echo ----------------------------------------------------------------------------------
55-
echo The MFCC Mel data was output to file $RAW_OUTPUT_S16, $RAW_OUTPUT_S24, $RAW_OUTPUT_S32
56-
echo ----------------------------------------------------------------------------------
14+
TESTBENCH_RUN="$VALGRIND $TESTBENCH"
15+
16+
convert_input() {
17+
sox -R --encoding signed-integer "$1" -L -r 16000 -c 2 -b 16 "$RAW_INPUT_S16"
18+
sox -R --no-dither --encoding signed-integer -L -r 16000 -c 2 -b 16 \
19+
"$RAW_INPUT_S16" -b 32 "$RAW_INPUT_S32"
20+
sox -R --no-dither --encoding signed-integer -L -r 16000 -c 2 -b 16 \
21+
"$RAW_INPUT_S16" -b 32 "$RAW_INPUT_S24" vol 0.003906250000
22+
}
23+
24+
run_testbench() {
25+
local tplg_base="$1"
26+
local out_s16="$2"
27+
local out_s24="$3"
28+
local out_s32="$4"
29+
local label="$5"
30+
local tplg_s16="${SOF_WORKSPACE}/sof/tools/build_tools/topology/topology2/development/${tplg_base}16.tplg"
31+
local tplg_s24="${SOF_WORKSPACE}/sof/tools/build_tools/topology/topology2/development/${tplg_base}24.tplg"
32+
local tplg_s32="${SOF_WORKSPACE}/sof/tools/build_tools/topology/topology2/development/${tplg_base}32.tplg"
33+
34+
$TESTBENCH_RUN -r 16000 -c 2 -b S16_LE -p 3,4 -t "$tplg_s16" -i "$RAW_INPUT_S16" -o "$out_s16"
35+
$TESTBENCH_RUN -r 16000 -c 2 -b S24_LE -p 3,4 -t "$tplg_s24" -i "$RAW_INPUT_S24" -o "$out_s24"
36+
$TESTBENCH_RUN -r 16000 -c 2 -b S32_LE -p 3,4 -t "$tplg_s32" -i "$RAW_INPUT_S32" -o "$out_s32"
37+
38+
echo ----------------------------------------------------------------------------------
39+
echo "The ${label} data was output to file ${out_s16}, ${out_s24}, ${out_s32}"
40+
echo ----------------------------------------------------------------------------------
41+
}
42+
43+
main() {
44+
convert_input "$1"
45+
run_testbench "sof-hda-benchmark-mfcc" mfcc_s16.raw mfcc_s24.raw mfcc_s32.raw "MFCC"
46+
run_testbench "sof-hda-benchmark-mfccmel" mel_s16.raw mel_s24.raw mel_s32.raw "MFCC Mel"
47+
48+
if [ -n "$XTENSA_PATH" ]; then
49+
TESTBENCH_RUN="$XTENSA_PATH/xt-run $SOF_WORKSPACE/sof/tools/testbench/build_xt_testbench/sof-testbench4"
50+
run_testbench "sof-hda-benchmark-mfcc" xt_mfcc_s16.raw xt_mfcc_s24.raw xt_mfcc_s32.raw "Xtensa MFCC"
51+
run_testbench "sof-hda-benchmark-mfccmel" xt_mel_s16.raw xt_mel_s24.raw xt_mel_s32.raw "Xtensa MFCC Mel"
52+
fi
53+
}
54+
55+
main "$@"

0 commit comments

Comments
 (0)