MATLAB: Using WavWrite to Create a Single Audio File

In summary, you are trying to create a 'wav' file from audio samples. You are having trouble because you are creating each tone individually and there is not a way to combine them all into a single file.
  • #1
mushiman
10
0
I am a MATLAB newbie, and apparently I am having a terrible day because everything that I do in MATLAB has been resulting in an error and/or undesired behavior.

I am trying to write out a 'wav' file using a number of MATLAB created audio samples (simply by generating them using a sin function, and outputting them using sound();). The problem is that I need to create several sequentially, and create a single file that will have each in order.

As it stands, I am creating each tone individually, and therefore I cannot write them into a single 'wav' file with my low knowledge of MATLAB.

Right now, I am doing something like this:
Code:
f0 = 220;
fs = 16000;
x = (insert my function here);
sound(x,fs);
wavwrite(x,fs,bits,'audio.wav');

Of course, I have to create a new batch of code like this for every new value of f0 that I need (and there are quite a few), but for my purposes, I suppose that this can work.

I need to know how to write the value(s) of 'x' into a matrix or something to that effect so that I can have the final result of all of my audio clips combined into a single 'wav' file that plays them sequentially. Any help would be greatly appreciated -- I can provide more detail on this issue if necessary.
 
Physics news on Phys.org
  • #2
You could do this...

Code:
xbuff = [];
for k = 1:(the number of tones)
    x     = (insert your function here);
    xbuff = [xbuff; x(:)];
end

sound(xbuff,fs);
wavwrite(xbuff,fs,bits,'audio.wav');
 
  • #3
I will give that a try and post back. Thank you!

Edit: This worked like a charm. Thanks again.
 
Last edited:
  • #4
Quick follow-up question: Is there a more compact way that I can implement this function into my code? Right now I'm starting a new loop (k = 1:1) and redefining the frequency of the sinusoid for each tone. Is there a way to implement all of the frequencies into one large matrix and have it loop (ie, f = [220 440 880 1760 etc]? Right now, I'm looking at about 200 lines of code or so.

I'm sure there is an easy way to do it, but I'm not seeing it right off the top of my head at the moment... I know it's a pretty easy question, but I'm rather tired right now.
 
Last edited:

1. What is MATLAB?

MATLAB is a high-level programming language and interactive environment commonly used by scientists and engineers for data analysis, visualization, and numerical computation.

2. What is WavWrite?

WavWrite is a function in MATLAB used to create an audio file in the .wav format.

3. How do I use WavWrite to create a single audio file?

To use WavWrite, you first need to have an audio signal stored in a variable. Then, you can use the syntax "wavwrite(y, Fs, nbits, filename)" where "y" is the audio signal, "Fs" is the sampling frequency, "nbits" is the number of bits per sample, and "filename" is the name of the audio file you want to create.

4. What are the parameters needed for WavWrite?

The parameters needed for WavWrite are the audio signal, sampling frequency, number of bits per sample, and the filename.

5. Can I use WavWrite to create audio files of different formats?

No, WavWrite is specifically designed to create audio files in the .wav format. If you want to create audio files in other formats, you will need to use different functions or third-party software.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
993
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
20
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Back
Top