如何从 .wav 文件中提取特定频率范围?
我对声音处理真的很陌生,所以也许我的问题很简单。 我想要做的是使用 R 从 wav 文件中提取特定的频率范围(假设为 150-400 Hz)。换句话说,我想创建另一个波形文件 (wave2),其中仅包含我想要的频率分量。指定(150 到 400 Hz,或其他)。
我在网上读到一些东西,我发现这可以通过 FFT 分析来完成,但问题来了。
假设我有这样的代码:
library(sound)
s1 <- Sine(440, 1)
s2 <- Sine(880, 1)
s3 <- s1 + s2
s3.s <- as.vector(s3$sound)
# s3.s is now a vector, with length 44100;
# bitrate is 44100 (by default)
# so total time of s3 is 1sec.
# now I calculate frequencies
N <- length(s3.s) # 44100
k <- c(0:(N-1))
Fs <- 44100 # sampling rate
T <- N / Fs
freq <- k / T
x <- fft(s3.s) / N
plot(freq[1:22050], x[1:22050], type="l") # we need just the first half of FFT computation
我们获得的图是:
嗯,有两个峰值。如果我们想知道它们对应的频率,只需找到:
order(Mod(x)[1:22050], decreasing=T)[1:10]
[1] 441 881 882 880 883 442 440 879 884 878
前两个值非常接近我用来创建声音的频率:
real computed
Freq1: 440 | 441
Freq2: 880 | 881
那么,现在问题来了: 如果我想从我的声音中删除,如何继续发出范围内的频率,例如 (1, 500)
?以及如何仅选择(并保存)范围 (1, 500)
? 我所关注的是,我的新声音(已删除频率)将接近于简单的Sine(freq=880,duration=1)
(我知道,它不可能完全像这样!)。 这可能吗?
我很确定 fft(DATA, inverse = TRUE)
就是我所需要的。但我不确定,但我不知道如何继续。
I'm really new on sound processing, so maybe my question will be trivial.
What I want to do is to extract a specific frequency range (let's say 150-400 Hz) from a wav file, using R. In other words, I want to create another wave file (wave2) that contains only the frequency component that I specify (150 to 400 Hz, or what else).
I read something on the net, and I discovered out that this can be done with a FFT analysis, and here's come the problems.
Suppose I've this code:
library(sound)
s1 <- Sine(440, 1)
s2 <- Sine(880, 1)
s3 <- s1 + s2
s3.s <- as.vector(s3$sound)
# s3.s is now a vector, with length 44100;
# bitrate is 44100 (by default)
# so total time of s3 is 1sec.
# now I calculate frequencies
N <- length(s3.s) # 44100
k <- c(0:(N-1))
Fs <- 44100 # sampling rate
T <- N / Fs
freq <- k / T
x <- fft(s3.s) / N
plot(freq[1:22050], x[1:22050], type="l") # we need just the first half of FFT computation
The plot we obtain is:
Well, there are two peaks. If we want to know to what frequency they correspond, just find:
order(Mod(x)[1:22050], decreasing=T)[1:10]
[1] 441 881 882 880 883 442 440 879 884 878
First two values are really near to the frequency I've used to create my sound:
real computed
Freq1: 440 | 441
Freq2: 880 | 881
So, now comes the problem: how to proceed, if I want to delete from my sound the frequencies in the range, say, (1, 500)
? And how to select (and save) only the range (1, 500)
?
What I attend, is that my new sound (with deleted frequencies) will be something near to simple Sine(freq=880, duration=1)
(I know, it cannot be exactly like so!).
Is that possible?
I'm pretty sure that fft(DATA, inverse = TRUE)
is what I need. But I'm not sure, and however I don't know how to proceed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你不想编程,你可以使用 Praat。
Praat 是一个免费的科学软件程序,用于语音学中的语音分析。
但您也可以使用它来编辑任何声音的频谱(删除频率等),然后将结果导出为新的声音文件。
If you don't want to programm it, you can use Praat.
Praat is a free scientific software program for the analysis of speech in phonetics.
But you can also use it to edit the spectrum of any sound (remove frequencies, ...) and then export the result as a new sound file.
也许我没有抓住重点,但你不是已经有了答案吗?从您的帖子中:
只需收集高于 500 的所有值:
要生成新信号,只需重复您为构建原始信号所做的操作:
将值保持在 500 以下:
Maybe I missed the point, but don't you already have your answer? From your post:
Simply collect all values above 500:
To generate the new signal simply repeat what you did to build the original signal:
To keep the values below 500:
查看 cran 上的“信号”包,其中应该执行的过滤功能之一
look at the 'signal' package on cran, one of the filter functions there should do