processing and saving wav files in a loop

1 view (last 30 days)
Hello. I'm attempting to change the sample rate of a directory of .wav files. This is what I've tried so far, and it's not working. I've tried similar code with a single file, but it is not working with my loop.
directory = dir('/data/*.wav');
numfiles = length (directory);
for i=1:numfiles
[yOld, FsOld] = audioread('directory(i)');
FsNew = 22050;
yNew = resample(yOld,FsNew, FsOld);
filename = sprintf('%s%d.wav',i)
audiowrite(filename, yNew, FsNew);
end
Thank you for the help in advanced.

Answers (2)

Image Analyst
Image Analyst on 18 Nov 2018
directory(i) is NOT the filename. directory(i).name is the base file name.
See the FAQ
You'll want something like
baseFileName = theFiles(k).name;
thisFolder = theFiles(k).folder;
fullFileName = fullfile(thisFolder, baseFileName);
Then do audioread() and audiowrite().
  9 Comments
Image Analyst
Image Analyst on 2 Mar 2023
@Marena You may have waveforms that are less than 69049200 elements long. Try this
numElements = 5 * Fs; % 5 seconds.
lastIndex = max([numElements, size(y_old, 1)]);
if lastIndex < numElements
% The sound was not 5 seconds long.
firstIndex = 1;
else
% Sound is at least 5 seconds long.
firstIndex = lastIndex - numElements + 1;
end
y_trim = y_old(firstIndex : lastIndex, :); % clip after first 5s
If youi have any more questions, please start your own discussionn thread (not here) and attach a file that worked and one that threw the error, and tell me what the error is.
Chali Dadi
Chali Dadi on 20 Apr 2023
I have also the same problem please help for the problem below. thank you in advance
I am using emperical wavelet transform for signal decomposition for feature extraction from audio signal. the problem is not decomposing signal using empirical wavelet transform, but it is calculating the energy of all the signal decomposed and finding the maximum energy.then extracting feature from signal with maximum energy.
so simply what I want in loop in one is
1.decompose signal using ewt
2.calculate the energy of the decomposed signal
3. find the signal with maximum energy from all the decomposed signal(mra).
4.finally extracting audio feature from the signal with maximum energy.

Sign in to comment.


Chali Dadi
Chali Dadi on 20 Apr 2023
I have also the same problem please help for the problem below. thank you in advance
I am using emperical wavelet transform for signal decomposition for feature extraction from audio signal. the problem is not decomposing signal using empirical wavelet transform, but it is calculating the energy of all the signal decomposed and finding the maximum energy.then extracting feature from signal with maximum energy.
so simply what I want in loop in one is
1.decompose signal using ewt
2.calculate the energy of the decomposed signal
3. find the signal with maximum energy from all the decomposed signal(mra).
4.finally extracting audio feature from the signal with maximum energy

Community Treasure Hunt

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

Start Hunting!