Oriol Baena Crespo
Oriol Baena Crespo

Reputation: 347

Selecting frequency range on audio files with fir {seewave}

A very very new user to audio R related stuff!

I have to process a bunch of files and extract a certain frequency range, let's say from 500 to 2000 Hz. Given a certain working directory I have:

myFiles <- list.files() 

for(i in seq_along(myFiles)){
  track <- readWave(myFiles[[i]])
  track <- fir(track, from=500, to=2000,output="Wave")
  track <- normalize(track, unit = as.character(track@bit))
  assign(paste0("pista",i),track)
}

I think fir from seewave is the right function to do so, but I have 2 additional doubts:

  1. How can I include here a line of code to create wav files into my working directory instead of R objects? I don't mind swapping to lapply if necessary.
  2. Something is wrong with my code, as I am not able to open the audio file afterwards in Raven (but I do can in Quicktime!). Any suggestion?

Thanks!

Upvotes: 0

Views: 117

Answers (1)

Andrew Chisholm
Andrew Chisholm

Reputation: 6567

Here's an example using lapply.

library(seewave)
# Make some files to test with
writeWave(noise(kind='pink'), filename = 'example1.wav')
writeWave(noise(kind='white'), filename = 'example2.wav')

myFiles <- list.files(pattern = 'example') 

myfilterandsave <- function(files, index) {
    track <- readWave(files[index])
    filtered <- fir(track, from=500, to=2000, output='Wave')
    normalized <- normalize(filtered, unit = as.character(filtered@bit))
    name <- paste0('filtered',index, files[index])
    writeWave(object = normalized, filename = name)
    cat(name, '\r\n')
}

lapply(seq_along(myFiles), function(i) myfilterandsave(myFiles, i))

Upvotes: 1

Related Questions