How to resample multiple sampling frequencies within a large dataset?

Good day
I have a data set of 920 audio samples, 96 of these audio samples are recorded at a different sampling rate and are spreaded throughout the data set. I want to code a loop to single out the 96 audio samples and resample them from 4000 Hz to 41000 Hz. I have written code to do it for a single sample, but can not seem to implement over the whole data set.
%%Resampling code
[X, Fs]=audioread('104_1b1_Al_sc_Litt3200.wav');
[P,Q]=rat(41000./Fs);
Xn = resample(X,P,Q);
%%The following code is the loop that runs through my entire data set
myFolder = 'C:\Users\Ré van Zyl\Downloads\ICBHI_final_database\All';
if exist(myFolder, 'dir') ~= 7
Message = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(Message));
return;
end
filePattern = fullfile(myFolder, '*.wav');
wavFiles = dir(filePattern);
for k = 1:length(wavFiles)
baseFileName = wavFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
%%Insert code to resample sampling frequencies?
end
If anyone can help me in this regard it would be much appreciated. Or just let me know if it is possible to do something like this.
Kind regards Re

 Accepted Answer

I am not quite certain what you want.
Also, if you first want the information about your audio files in order to determine the ones you want to resample, the audioinfo (link) function may be more efficient than audioread, at least initially, in a separate loop. You can then use audioread (and your resampling code) for the files you have previously identified and want to process.
I have resampled signals and done other signal processing on them, although I have not done the sort of file search you intend, so my expertise in that regard is limited.

6 Comments

Thank you very much for your response. I will try to explain more clearly exactly what I need to do. I use the following code to run through all 920 audio files:
%%Start of dataloop
myFolder = 'C:\Users\Ré van Zyl\Downloads\ICBHI_final_database\All';
if exist(myFolder, 'dir') ~= 7
Message = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(Message));
return;
end
filePattern = fullfile(myFolder, '*.wav'); wavFiles = dir(filePattern);
for k = 1:length(wavFiles) baseFileName = wavFiles(k).name; fullFileName = fullfile(myFolder, baseFileName);
info= audioinfo(fullFileName)
[X,Fs] = audioread(fullFileName);
baseFileName = wavFiles(k).name
end
%%end of dataloop
So I do use audioinfo to see when the sampling Frequency is not 44100 Hz. But while the loop is running I want to extract those 96 audio files that does not have a sampling frequency of 44100 Hz and resample their frequencies from 4000 Hz to 44100 Hz.
I used the following code to change the sampling frequency of one audio file and want to implement it in the loop to change the extracted files frequency.
%%Resampling code
[X, Fs]=audioread('104_1b1_Al_sc_Litt3200.wav');
[P,Q]=rat(41000./Fs);
Xn = resample(X,P,Q);
%%end of resampling code
Then I want to save all the files to memory and they must have the same sampling frequency, because I need to use them for feature extraction.
I hope this is clearer. Regards Re
My pleasure.
I do not see where you are having a problem. I would simply use audiowrite to save the files.
Audiowrite worked, I did not understand the function but after some research I implemented it. Thank you.
My pleasure.
If my Answer helped you solve your problem, please Accept it!
I just want to make sure of something, this is probably a rhetorical question. If I implement it as follow:
audiowrite(filename,X,44100)
clear X Fs
My original audio data, X, stays the same and only my sampling frequency changes?
The sampling frequency saved with ‘X’ in your ‘filename’ file is what you tell audiowrite it is. That does not change the sampling frequency of ‘X’, which is what you defined it to be when you recorded or created it.
Specifying a different sampling frequency in your audiowrite call will simply mystify anyone who loads it and wants to use ‘X’, as to the reason it sounds strange or has an uncharacteristic spectrum.

Sign in to comment.

More Answers (0)

Categories

Find more on Audio Processing Algorithm Design in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!