Read and Write Audio Files - MATLAB & Simulin... [Link]
Read and Write Audio Files
This example shows how to write data to an audio file, get information about the file, and then read data
from the audio file.
Write to Audio File
Get Information About Audio File
Read Audio File
Plot Audio Data
Write to Audio File
Load sample data from the file, [Link]
load [Link]
The workspace now contains a matrix of audio data, y, and a sample rate, Fs.
Use the audiowrite function to write the data to a WAVE file named [Link] in the current folder.
audiowrite('[Link]',y,Fs)
clear y Fs
The audiowrite function also can write to other audio file formats such as OGG, FLAC, and MPEG-4
AAC.
Get Information About Audio File
Use the audioinfo function to get information about the WAVE file, [Link].
info = audioinfo('[Link]')
info =
Filename: 'pwd\[Link]'
CompressionMethod: 'Uncompressed'
NumChannels: 1
SampleRate: 8192
TotalSamples: 73113
Duration: 8.9249
Title: []
Comment: []
Artist: []
BitsPerSample: 16
audioinfo returns a 1-by-1 structure array. The SampleRate field indicates the sample rate of the audio
data, in hertz. The Duration field indicates the duration of the file, in seconds.
Read Audio File
Use the audioread function to read the file, [Link]. The audioread function can support WAVE,
OGG, FLAC, AU, MP3, and MPEG-4 AAC files.
1 of 3 Monday 23 October 2017 10:53 PM
Read and Write Audio Files - MATLAB & Simulin... [Link]
[y,Fs] = audioread('[Link]');
Play the audio.
sound(y,Fs)
You also can read WAV, AU, or SND files interactively. Select Import Data or double-click the file name
in the Current Folder browser.
Plot Audio Data
Create a vector t the same length as y, that represents elapsed time.
t = 0:seconds(1/Fs):seconds([Link]);
t = t(1:end-1);
Plot the audio data as a function of time.
plot(t,y)
xlabel('Time')
ylabel('Audio Signal')
See Also
audioinfo | audioread | audiowrite
Related Topics
Import Images, Audio, and Video Interactively
2 of 3 Monday 23 October 2017 10:53 PM
Read and Write Audio Files - MATLAB & Simulin... [Link]
3 of 3 Monday 23 October 2017 10:53 PM