在gnuplot中绘制音频数据

alg*_*ine 16 audio gnuplot

如何使用gnuplot将音频文件(如aiff)转换为svg?我使用sox(声音交换)将.aiff转换为.dat,我现在可以在gnuplot中加载.

我做了类似的事情:

set terminal svg
set output "test.svg"
plot "test.dat"
Run Code Online (Sandbox Code Playgroud)

我得到一个svg文件,但只有点/或大量的x.我怎么能连接点?

nei*_*llb 26

要在点之间绘制线条,请使用

plot "test.dat" with lines
Run Code Online (Sandbox Code Playgroud)

或者保持点标记以及线条,使用

plot "test.dat" with linespoints
Run Code Online (Sandbox Code Playgroud)

你的榜样就变成了

set terminal svg    
set output "test.svg"
plot "test.dat" with lines
Run Code Online (Sandbox Code Playgroud)

进一步提示:

不要考虑每个样本:

对于大文件,您可能还会发现仅使用"每n"绘制每个第n个样本很有用.这将使绘图生成更快,并且还将产生更小(但不太详细)的svg文件.

例如

plot "test.dat" every 100 with lines
Run Code Online (Sandbox Code Playgroud)

忽略.dat文件头:

如果你的sox生成的.dat文件有一些介绍元数据,比如

; Sample Rate 44100
; Channels 2
Run Code Online (Sandbox Code Playgroud)

你可以添加以下内容让gnuplot考虑那些行注释并忽略它们.

set datafile commentschars ";"
Run Code Online (Sandbox Code Playgroud)

这将节省您必须预处理.dat文件,以便在gnuplot阻塞之前删除这些行.

绘制立体声音频的左右声道:

如果您正在使用立体声文件,您可能希望看到两个频道.

我们可以使用"multiplot"在共享的x轴上一个在另一个上面布置以下两个图(左侧和右侧通道),就像许多声音编辑程序一样.

set multiplot layout 2,1
plot "test.dat" using 1:2 with lines
plot ""         using 1:3 with lines
Run Code Online (Sandbox Code Playgroud)

1:2和1:3指示gnuplot将dat文件的哪些列用作x和y源.我假设你的sox生成的立体声.dat文件看起来像我的那样,列为 - 1:从第一个样本开始的时间 - 2:左通道的标准化样本值 - 3:右通道的标准化样本值

示例代码段:

   10.840113       0.20101929      0.17840576 
   10.840136       0.26062012      0.14831543 
   10.840159       0.23779297      0.13146973 
Run Code Online (Sandbox Code Playgroud)

把它放在一起: 这是一个将所有上述内容放在一起的脚本.如果您没有要使用的立体声数据文件,则需要删除1:3的绘图和多色绘图设置.

#!/usr/bin/env gnuplot
set datafile commentschars ";"

set terminal svg
set output "test.svg"

set multiplot layout 2,1
plot "test.dat" using 1:2 every 100 with lines
plot ""         using 1:3 every 100 with lines
unset multiplot
Run Code Online (Sandbox Code Playgroud)

Prettification

最后,我调整了脚本进行演示(借用了Philipp K. Janert的优秀"gnuplot in action"一书):

#!/usr/bin/env gnuplot
set datafile commentschars ";"

set terminal svg
set output "test.svg"

set multiplot layout 2,1

set ylabel "sample value"
set bmargin 0
set format x ""
set ytics -0.8,0.2
set key bottom
plot "test.dat" using 1:2 every 100 with lines lc rgbcolor "#a0a0b0" title "left channel"

set xlabel "time (s)"
set bmargin
set tmargin 0
set format x "%g"
set ytics -1.0,0.2,0.8
set key top

plot ""         using 1:3 every 100 with lines lc rgbcolor "#a0a0b0" title "right channel"
unset multiplot
Run Code Online (Sandbox Code Playgroud)

这是一个示例输出(尽管是png): gnuplotted音频数据(立体声示例)

如何制作.dat文件

对于在家中随行的任何人,您可以使用sox通过以下命令从音频文件生成.dat文件:

sox input.wav output.dat
Run Code Online (Sandbox Code Playgroud)

大文件警告:在40kHz转换即使只有10秒的立体声音频也会产生25Mb的输出文件.

  • 只是在"跳过"样本或在大文件上运行时的注意事项 - 大多数可视化显示将采用rms和min/max的一系列样本而不是跳过样本(因为这会产生明显的不一致性.) (2认同)