Applying bandstop filter to multiple files

How can I write a loop code to filter out 20-150 Hz band from multiple audio files before taking the spectrograms?
The code is as follows:
% load and take specs of COPD
cd('C:\Users\User\Desktop\uni\II semester\DSP\RESPDATA\COPD');
files = dir;
count = 0;
for i = files'
if length(i.name) > 4 && i.name(end-3:end) == ".wav" % only read file if filename is greater then four and extension is '.wav'
count = count+1;
[f{count} fs{count}] = audioread(i.name);
count101{1,count} = (i.name(1:3));
end
end
for ii=1:numel(f)
spectrogram(f{1,ii},'yaxis');
colorbar('off')
set(gca,'Visible','off')
baseFileName = sprintf('Image # %d.png', ii);
fullFileName = fullfile('C:\Users\User\Desktop\uni\II semester\DSP\specs\A', baseFileName);
saveas(gcf, fullFileName);
end

 Accepted Answer

I am not certain if you want a bandpass (link) or bandstop (link) filter. (These functions are in R2018a and later releases.)
If you do not have those functions, an efficient elliptical filter is not difficult to code. More details would help.

4 Comments

Thanks for the response. I want a bandstop filter. But I can't seem to implement it in the loop to to filter out multiple files. a=bandstop(f,[20 150],fs); is the filter i used for filtering single files.
As always, my pleasure.
The bandstop function has two outputs, the second one being a digital filter object that you can use with the filtfilt (link) function to filter a vector or matrix of data. You can then design the filter once, and use it for all your files (providing they have the same sampling frequency).
You may need to subscript ‘f’ and ‘fs’ in each filter step:
a{count} = bandstop(f{count},[20 150],fs{count});
or if they all have the same sampling frequency, return the second output the first time for the first file, and then use that digital filter object output in all the other files. (So if ‘count’ is greater than 1, skip over the design step and just use filtfilt with it to do the filtering.)
It is difficult for me to follow your code with respect to the files. See the documentation section on: Import or Export a Sequence of Files (link) for guidance.
It worked, thank You so much!
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 28 May 2019

Edited:

on 28 May 2019

Community Treasure Hunt

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

Start Hunting!