Quickest way to do a bandpass filter?

263 views (last 30 days)
I have a huge dataset of .wav files that I would like to bandpass filter.
Is the following the most time efficient way to do it..?
This filter takes about 2 minutes to run so it's quite time consuming with such a huge dataset.
[xbit, fs]=audioread(filename, [1*fs,120*fs]); %read in file from 1-120 secs
xbit=detrend(xbit);%removes DC offset
tic
xbit_filt=bandpass(xbit,[50 24000],fs);
toc

Accepted Answer

Mathieu NOE
Mathieu NOE on 9 Jan 2023
Hello Louise
welcome back and happy new year !
I made some speed comparison with your code and two other options using a butterworth digital filter , then using the function filter or filtfilt (filtering twice , forward and backward in time, for phase distorsion cancellation)
see my suggestion are much faster than using the bandpass function as in your code
I wonder why you put the low pass frequency at 24 kHz as this is already above the human hearing upper frequency limit...
at what sampling frequency are your wav files ?
with the my small audio file sampled at 11025 Hz , the differences are significant :
  • your code : Elapsed time is 1.574109 seconds.
  • my code with filter : Elapsed time is 0.016102 seconds.
  • my code with filtfilt : Elapsed time is 0.042606 seconds.
filename = 'test_voice_mono.wav' ;
% NB this file is sampled at 11025 Hz, so the bandpass filter cut off
% frequencies are modified (LPF : 24kHz => 2.4kHz)
[xbit, fs]=audioread(filename); %we don't know yet what if fs so I cannot define range = [1*fs,120*fs] as in your original code
samples = length(xbit);
start = 1*fs;
stop = min(120*fs,samples);
xbit=xbit(start:stop); % extract data for t = 1-120 secs
xbit=detrend(xbit);%removes DC offset
% your code
tic
xbit_filt=bandpass(xbit,[50 2400],fs);
toc
% good old butter bandpass filter (applied once)
tic
N = 2;
[B,A] = butter(N,[50 2400]/(fs/2));
xbit_filt = filter(B,A,xbit); % NB filter applies the filter once !
toc
% good old butter bandpass filter (applied twice)
tic
N = 2;
[B,A] = butter(N,[50 2400]/(fs/2));
xbit_filt = filtfilt(B,A,xbit); % NB filtfilt applies the filter twice !
toc
  6 Comments
Louise Wilson
Louise Wilson on 9 Jan 2023
Thanks Mathieu for your brilliant and detailed answer.
I am just using the filter to get the 5th and 95th percentiles, median, and rms level of the file, then store those in an output. Got the loop sorted :-)
Mathieu NOE
Mathieu NOE on 9 Jan 2023
ok
glad I could help !
all the best

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!