Code

Batch WAV file processing

July 24, 2011 Coded in Matlab

There are may times in DSP algorithm design where you need to run your new and shiny algorithm against a bunch of test vectors.  In audio processing, it is usually WAV files.  The only problem is that if you are constantly adding/removing files from your test database, you have to keep changing the filenames and path in your m-file.

But using the code below, it is now possible to point your m-file to a directory and say "go".  Files can be added/removed/renamed and each time you run this code it will scan the files that match the WAV extension and it will dump the outputs in a separate folder.  In essence, you have a way to batch process any number of files in a specific directory.

The code below shows a simple lowpass filter is applied to the entire directory of WAV files in the folder: "test_input_database".  This script can be tweaked to use any file type you want as long as you have the appropriate I/O parser.

Note: This code is not recursive.  All files must exist in one input folder.

%In this example we are going to apply a low-pass filter to all WAV files,
%but it could be a multitude of different "processing" methods.  This is
%only used to illustrate the batch processing example.
Fs = 44100; %Hz
Fc = 1000;  %Hz
[b,a] = butter(2, Fc/(Fs/2), 'low');

%These are the input and output directories relative to this M-file
input_directory = 'test_input_database\';
output_direcory = 'test_output\';

%What extensions are you looking for?  In our case, we want to batch
%process audio files that are store in the uncompressed *.WAV format
extension = '*.wav';

%Get the files
files=dir([input_directory '*.wav']);
N_files=numel(files);

%Loop through one file at a time
for i=1:N_files

    %This is a stereo file and for this example, 
    %I only care about channel number 1
    x = wavread([input_directory files(i).name ]);
    x = x(:,1);
    
    %Process the data by applying our filter
    y = filter(b,a,x);
    
    %Output the data as a unique filename based on input
    wavwrite(y, Fs, [output_directory 'processed' files(i).name]);
    disp(['Processed File: ' input_directory files(i).name 'as: ' ...
                             output_directory 'processed' files(i).name]);
    
end

Comments:

There are no comments yet!


Sorry, you need javascript enabled to post any comments.