What does [a,fs] = audioread(filename) do?

9 views (last 30 days)
Axel Blaze
Axel Blaze on 4 May 2022
Answered: Voss on 4 May 2022
I dont really understand the matlab explanation.
When i run it, in the command window after inputting a song that was around 3 min long, i got a mx2 matrix that had millions of rows and 2 columns and a fs value at the end.
What exactly was the matrix? What are its rows and columns exactly? Would love to get an explanation.
  1 Comment
dpb
dpb on 4 May 2022
What's not to understand, specifically?
The Output Arguments description in the doc seems pretty self-evident.
You get back the numerical values of the waveform for however many channels were in the file (in your case, 2), one sample every 1/fs seconds for the length of the audio -- the common CD rate is 44.1 kHz or 44,100 samples/per second. So, if the recording was 3 minutes, that would equate to 44,100 sps * 60 sec/min * 3 min --> 7,938,000 samples.

Sign in to comment.

Answers (1)

Voss
Voss on 4 May 2022
[a,fs] = audioread(filename)
reads the audio file filename and returns the audio data as a and the sampling rate as fs.
The number of columns of a is the number of channels in the recording, so 2 columns means it's a stereo recording, 1 column would be a mono recording.
The number of rows of a is the number of samples in the recording.
The sampling rate fs tells you how the samples correspond to actual time. For example, fs = 44100 means a 44.1kHz sampling rate, which means 44100 samples per second. This is necessary to know because generally you need to know how to map that matrix of samples (i.e., matrix of numbers) a to actual time if you want to play back the audio or do anything else with it. The matrix a itself doesn't tell you how far apart consecutive samples are in time; you need the sampling rate for that, so fs is returned separately.
sound(a,fs) % play the recording back
sound(a,fs/2) % play the recording back at half-speed (pitch is also necessarily halved doing this)
filename = 'Pink Floyd - Empty Spaces.wav';
[a,fs] = audioread(filename);
sound(a(end:-1:1,:),fs) % play the recording backwards to hear the secret message

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!