在gnuplot中绘制音频数据

时间:2011-04-29 00:48:54

标签: audio gnuplot

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

我做了类似的事情:

set terminal svg
set output "test.svg"
plot "test.dat"

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

3 个答案:

答案 0 :(得分:26)

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

plot "test.dat" with lines

要保留点标记以及线条,请使用

plot "test.dat" with linespoints

所以你的例子变成了

set terminal svg    
set output "test.svg"
plot "test.dat" with lines

进一步提示:

不要考虑每个样本:

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

e.g。

plot "test.dat" every 100 with lines

忽略.dat文件标题:

如果您的sox生成的.dat文件包含一些介绍性元数据,例如

; Sample Rate 44100
; Channels 2

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

set datafile commentschars ";"

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

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

如果你正在使用立体声文件,你可能想看到两个频道。

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

set multiplot layout 2,1
plot "test.dat" using 1:2 with lines
plot ""         using 1:3 with lines

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 

把它放在一起 这是一个将所有上述内容放在一起的脚本。如果你没有立体声数据文件试试这个,你需要删除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

<强> 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

这是一个示例输出(尽管是png): gnuplotted audio data (stereo example)

如何制作.dat文件

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

sox input.wav output.dat

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

答案 1 :(得分:4)

请注意,您也可以直接绘制二进制数据:

set terminal svg
set output "test.svg"
plot '< sox test.aiff -t s32 -' binary format='%int32' using 0:1 with lines

答案 2 :(得分:1)

只想记录下来 - 好吧,我正在寻找一个Linux命令行音频波形查看器很长一段时间,它可以从命令行调用,原始二进制文件作为输入,其格式为可以在命令行上指定数据。

Audacity可以导入原始数据,但只能从GUI导入(无法通过命令行选项指定原始数据文件格式);而gwavegtkwaveGaw - Gtk Analog Wave viewer等波形观看者可以阅读正确的.wav或基于SPICE的格式。

感谢answer by @Thor,现在我知道我可以将gnuplot用于此目的。这是一个示例命令行,它将原始二进制数据解释为16位立体声:

gnuplot -p -e "set terminal x11 ; set multiplot layout 2,1 ; plot 0 ls 2, 'data.raw' binary format='%int16%int16' using 0:1 with lines ls 1; plot 0 ls 2, 'data.raw' binary format='%int16%int16' using 0:2 with lines ls 1 ; unset multiplot"

...或分成几行:

gnuplot -p -e "set terminal x11 ; set multiplot layout 2,1 ; \
plot 0 ls 2, 'data.raw' binary format='%int16%int16' using 0:1 with lines ls 1; \
plot 0 ls 2, 'data.raw' binary format='%int16%int16' using 0:2 with lines ls 1; \
unset multiplot"

请注意:

  • 如果要从shell命令输出,则只应使用管道"< ..." - 如果您有文件(如上所述),请不要使用管道(否则获得权限被拒绝)
  • 注意格式'%int16%int16'将使字节流“分组”为2个字节,表示列(通道)1,接下来的2个字节表示为列(通道)2,接下来的2个字节再次作为列1 ,依此类推......请参阅gnuplot docs_4.2: Binary General - Format(也相关:Gnuplot: How to plot multiple time series from a binary format
  • 最后有两个独立的plot,一个using 0:1和另一个using 0:2,我们可以得到一个典型的波形渲染(如接受的答案) - 一个频道高于另一个< / LI>
  • 由于上面使用了--persist选项,gnuplot将退出,而x11wxt)窗口将保留 - 因此,典型的{{1}与窗口的交互将工作

无论如何,很高兴我发现了这个,会节省我很多时间,我想gnuplot