How to Combine WAV Files Into One File in Matlab
Matlab uses the “wavread” and “wavwrite” functions to add and play .wav sound files in Matlab projects. Although you most often play .wav files individually, there are times when combining – or merging - two or more files are necessary to achieve a special effect. Merging .wav files is a relatively simple process that starts by loading each file individually and then combining the files in a single “wavwrite statement.
Instructions
-
-
1
Read each individual .wav file into Matlab. The syntax for reading files is [y1,fs,nbits] = wavread('filename'); where y1 identifies the file number, Fs identifies the sampling rate in Hertz and nbits identifies the file length in bits:
[y1,Fs,nbits] = wavread('chirp.wav');
[y2, Fs,nbits] = wavread('bell.wav'); -
2
Combine the files to achieve the effect you desire. For example, combine the two files so the resulting .wav file – y3 - plays the first half of y1, all of y2 and then ends with the second half of y1:
y3 = [y1(1:500,:); y2; y1(501:end)];
-
-
3
Listen to the playback and if necessary, make adjustments before committing the new .wav file in Matlab:
sound (y3, Fs)
-
4
Write the file to Matlab giving the new .wav file its own file name:
wavwrite(y3,Fs,nbits,'combofile.wav');
-
1
Tips & Warnings
Use the Matlab Import Wizard to add individual .wav files into your file collection if they are not already present. Access the Import Wizard from the Matlab main menu.
References
- Photo Credit Jupiterimages/Photos.com/Getty Images