提取快速傅立叶从文件中的数据转换 [英] Extract Fast Fourier Transform data from file
问题描述
我建立这应该在服务器上运行,并分析声音文件的工具。我想我的其他所有工具都写在红宝石以及为此在红宝石。但我无法找到实现这一点的一个好方法。
I am building a tool which is supposed to run on a server and analyze sound files. I want to do this in Ruby as all my other tools are written in Ruby as well. But I am having trouble finding a good way of accomplishing this.
很多我发现一直在做可视化工具和图形化的东西的例子。我只需要FFT数据,仅此而已。我需要都得到音频数据,并做就可以了FFT。我的最终目标是要计算一些东西,如平均/中位数/模式,第25百分位和第75百分位在所有频率(加权幅度),BPM的,也许还有一些其他好的特性以后还可以集群类似的声音一起
A lot of the examples I've found has been doing visualizers and graphical stuff. I just need the FFT data, nothing more. I need to both get the audio data, and do a FFT on it. My end goal is to calculate some stuff like the mean/median/mode, 25th-percentile, and 75th-percentile over all frequencies (weighted amplitude), the BPM, and perhaps some other good characteristic to later be able to cluster similar sounds together.
首先,我试图使用红宝石音频和 fftw3 ,但我从来不去这两个真正携手合作。该文档是也不好,所以我真的不知道什么样的数据正在抛去。
接下来我试图使用 bplay / BREC 并限制我的Ruby脚本只使用STDIN和上(仍在使用fftw3)执行FFT。但我不能让bplay / BREC工作,因为服务器没有声卡,我没能只是直接将音频输出到标准输出而不去音频设备第一。
First I tried to use ruby-audio and fftw3 but I never go the two to really work together. The documentation wasn't good either so I really didn't know what data was being shuffled around. Next I tried to use bplay / brec and limit my Ruby script to just use STDIN and perform an FFT on that (still using fftw3). But I couldn't get bplay/brec to work since the server doesn't have a sound card and I didn't manage to just get the audio directly to STDOUT without going to an audio device first.
下面是我得到的最接近:
Here's the closest I've gotten:
# extracting audio from wav with ruby-audio
buf = RubyAudio::Buffer.float(1024)
RubyAudio::Sound.open(fname) do |snd|
while snd.read(buf) != 0
# ???
end
end
# performing FFT on audio
def get_fft(input, window_size)
data = input.read(window_size).unpack("s*")
na = NArray.to_na(data)
fft = FFTW3.fft(na).to_a[0, window_size/2]
return fft
end
所以,现在我卡住了,无法找到在谷歌更多的好成绩。因此,也许你这样的家伙能帮助我吗?
So now I'm stuck and can't find any more good results on Google. So perhaps you SO guys can help me out?
谢谢!
推荐答案
下面是最终的解决方案,我试图实现的,非常感谢兰德尔库克的有益的建议。在code提取WAV文件的声波和FFT在Ruby中:
Here's the final solution to what I was trying to achieve, thanks a lot to Randall Cook's helpful advice. The code to extract sound wave and FFT of a wav file in Ruby:
require "ruby-audio"
require "fftw3"
fname = ARGV[0]
window_size = 1024
wave = Array.new
fft = Array.new(window_size/2,[])
begin
buf = RubyAudio::Buffer.float(window_size)
RubyAudio::Sound.open(fname) do |snd|
while snd.read(buf) != 0
wave.concat(buf.to_a)
na = NArray.to_na(buf.to_a)
fft_slice = FFTW3.fft(na).to_a[0, window_size/2]
j=0
fft_slice.each { |x| fft[j] << x; j+=1 }
end
end
rescue => err
log.error "error reading audio file: " + err
exit
end
# now I can work on analyzing the "fft" and "wave" arrays...
这篇关于提取快速傅立叶从文件中的数据转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!