3
$\begingroup$

I'm trying to apply a butterworth frequency filter to a multichannel audio file recorded by a hydrophone array using the function "bwfilter" from the package "seewave".

Is there a way that "bwfilter" will work for multichannel files? If not, is there another way to filter my audio files? Thank you!

$\endgroup$
1
  • $\begingroup$ Can you share what you've already tried? From the seewave documentation, it seems that bwfilter allows for the specification of channel. Might be informative if you try passing a list of channels (e.g. channels = list(1,2) or similar) and share the results. Even if bwfilter can't handle multiple channels in a single call, this suggests at the very least that one could call bwfilter for each channel individually. $\endgroup$
    – Brian Miller
    Commented Oct 11 at 1:25

1 Answer 1

1
$\begingroup$

Here's a potential solution that loops over all channels of a multi-channel wav file. It's not that pretty or concise, but it works.

First, let's grab a multi-channel wave file as a test dataset

### Dataset Start ###

# Grab some open-access multi-channel wav data from a relatively short 3-channel 
# recording from a dodgy sonobuoy. 
# File is approximately 3MB. 
wavURL <- 'https://data.aad.gov.au/eds/api/dataset/41072dc7-996b-436e-b31a-776f28de07bf/object/download?prefix=AAS_4600_ENRICH_Sonobuoy_Data/audio/20190221/INV_20190221_013959_828.wav'

# Specify the file name to save the file on your computer
wavFile<-'INV_20190221_013959_828.wav'

# Call the download.file() function, passing in the URL and file name/location as arguments
download.file(wavURL, wavFile, mode = "w")

### Dataset End ##

Now we load the data and filter it in a for-loop. We plot the unfiltered data in the columns on the left, and the filtered data in the columns on the right. I've used seewave::oscillo to view the results here and confirm that the data look filtered, but one could remove the calls to 'oscillo' if you don't care to plot the results.

# Read the first 10 ms of the data from all three channels
wav <- tuneR::readWave(wavFile,header = F,units = "seconds",from = 0,to=0.01)

# Placeholder for filtered data -- same size as original data
wavFiltData <- [email protected]; 

op<-par(mfrow=c(3,2),mar=c(4.5,4,2,2))
for (i in 1:nchannel(wav)) {
  seewave::oscillo(wav,channel=i)
  wavFiltData[,i] <- seewave::bwfilter(wav,from=7400, to=7600,channel=i)
  # Edit: replaced wavFilt in line below with wavFiltData
  # to fix the error found by WMXZ
  seewave::oscillo(wavFiltData, [email protected], channel=i)
}

# Convert data back into a WaveMC format
wavFilt <- WaveMC(wavFiltData, [email protected], bit=wav@bit)

Plot showing original wav file on left, and filtered on right

$\endgroup$
2
  • $\begingroup$ Q: I'm not good in R, but the second oscillo accesses wavFilt, which is only assigned to after the loop. Is that correct? $\endgroup$
    – WMXZ
    Commented Oct 18 at 6:00
  • $\begingroup$ Yes, thank you this was indeed an error -- good catch. I have replaced wavFilt with wavFiltData, which I believe should now be correct. $\endgroup$
    – Brian Miller
    Commented Oct 21 at 0:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.