How to make a audio file (.wav) from a colored noise object?

Hi,
i would like to make an audio (.wav) file for pink noise. the length of the file should be 8 mins. and i want fs=44100. using the Matlab documentation i could make a colored noise object. now i want to make an audio file from it. so far my code is this-
cn = dsp.ColoredNoise(1,44100,1,'Color','pink');
noiseOut= cn();
noiseOut creates only one sample or one frame of coloured noise data. now how do i make/write an audio file of length 8 mins and save it as a .wav file?

 Accepted Answer

Muhammad - I don't have this toolbox but looking at the examples from Generate colored noise signal and the fact that you are creating one frame of 44100 samples with
cn = dsp.ColoredNoise(1,44100,1,'Color','pink');
then that means you have one second's worth of data (I'm assuming that a frame is one second's worth of data because of the sampling rate of 44100 samples per second) . You could then just have a for loop that repeats the above 8*60 times
numberOfFrames = 8 * 60;
Fs = 44100;
y = zeros(numberOfFrames * Fs, 1);
for k = 1:numberOfFrames
cn = dsp.ColoredNoise(1,Fs,1,'Color','pink');
y((k - 1)*Fs + 1:k*Fs,1) = cn();
end
Then you can use audiowrite to save your samples to file
audiowrite('somefile.wav',y,Fs);
The above could work...or will hopefully provide a hint of what you can do.

3 Comments

hi,
thnx for your answer. i was wondering what was wrong with my code-
cn = dsp.ColoredNoise(1,44100,1,'Color','pink');
noiseOut= cn();
audiowrite('filename.wav',noiseOut,44100*60*8);
Matlab did not give me any error. but when i tried to run the file, my player (vlc etc.) could not run it saying that the player does not support the format of the file.
Muhammad - with your above code, your cn is only one frame (so maybe one second). You need to call this 8*60 times so that you have the full eight minutes of data (and build this into an array like I've shown above). Note also your code at
audiowrite('filename.wav',noiseOut,44100*60*8);
where the third parameter is the sampling frequency. This should remain as 44100 as this indicates the number of seconds per sample...not the total number of samples (that you seem to be showing).

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!