Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

How would I use FFT to analyse an audio wave in R, Rstudio

I am trying to use R to find the Harmonics within a sound file, I would also like to plot these findings as a Frequency(Hz)(x) Strength(y) graph to show the harmonics found. I've found it hard so far to find a helpful, working example of FFT being used on an audio file in R as most of the tutorials work with a premade cosine or sine wave.

I have found an example on the https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/fft page under community code, but it did not work very well when I attempted to use it on the audio file.

#my addition (I've left the wave file space empty deliberately)
voice <- readWave("",from=0, to=Inf, units=c("seconds"), header=FALSE, toWaveMC=NULL) 

#the community code
x <- wavobj@left
fs <- [email protected]
nbits <- wavobj@bit      

x <- x[1:(fs*5)]   

y <- fft(x)

y.tmp <- Mod(y)   

y.tmp <- Mod(y)
y.ampspec <- y.tmp[1:(length(y)/2+1)]
y.ampspec[2:(length(y)/2)] <- y.ampspec[2:(length(y)/2)] * 2

f <- seq(from=0, to=fs/2, length=length(y)/2+1)

plot(f, y.ampspec, type="h", xlab="Frequency (Hz)", ylab="Amplitude Spectrum", xlim=c(0, 350))

Please send some help!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you're OK to use the seewave package, it has some helpful functions including meanspec which works out the mean frequency spectrum. Here's an example.

library(tuneR)
library(seewave)

data('sheep')
ms <- meanspec(sheep)

The ms object is a two dimensional array where the first column is the frequency and the second is the amplitude.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...