How to plot the histogram of an audio file

29 views (last 30 days)
Annie
Annie on 30 Jul 2016
Commented: Walter Roberson on 31 Jul 2016
I would like to plot the histogram of an audio .wav file. The histogram maintains the a sample value and its count of occurrences in the file.Please help me in this regard. I'm completely new to matlab

Answers (2)

Walter Roberson
Walter Roberson on 30 Jul 2016
data = audioread('TheAudioFile.wav', 'native');;
min_data = min(data(:));
max_data = max(data(:));
data_range = min_data : max_data;
hist(data(:), data_range)

Image Analyst
Image Analyst on 30 Jul 2016
Try this:
[y, Fs] = audioread('guitartune.wav');
histogram(y, 'FaceColor', 'blue');
grid on;
  5 Comments
Image Analyst
Image Analyst on 31 Jul 2016
I'm not sure what you mean by "each sample". Is that a measurement of an amplitude of the audio signal at some specific point in time, or is sample the whole audio waveform?
I think Walter's and my code are essentially the same. I'm just using the more modern histogram() function while Walter is using the older, and deprecated, hist() function. They both give amplitude along the "x" axis, and counts along the "y" axis.
They both give the histogram of the entire audio signal in the file. Taking the histogram of just one sample in time, like at time=4 seconds the amplitude was 0.87, doesn't make sense. You don't take the histogram of just 0.87 - a single number. But you do take the histogram of a bunch of samples, like from the whole file.
Walter Roberson
Walter Roberson on 31 Jul 2016
My interpretation is that the user wants to take the histogram for each possible different value that can be encountered, so a signal level of (say) -117/128 would be in a different bin than -118/128 and a different bin yet than -116/128 -- so if the range was -128/128 to +127/128 then the user wants individual bins for each of the (-128:127)/128 . As opposed to grouping to (e.g.) 10 different bins only.
My answer is different than yours in that I take the care to construct those bins based upon the dynamic range of data -- including the possibility of different data types. And to make "different samples" more meaningful and the histogram more accurate=, I take care to read the native encoding, rather than the normalized data that audioread would normally return.
I do see, though, that my code is faulty for the rather uncommon situation where the native file encoding is floating point: I do assume integer native encoding.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!