Create a spectrogram from .wav file

71 views (last 30 days)
Rachael Courts
Rachael Courts on 20 May 2019
Answered: Matthew Wyant on 10 Oct 2024
Hi,
I'm currently working with one .wav file (hoping to achieve for multiple .wav files at once), and would like to create a black and white spectrogram. With my current code I can produce the .wav file image, but then struggle to create the actual spectrogram. I also noticed the frequency levels were very zoomed out. Code as follows;
%% reads file into matlab This reads an array of audio samples into y,
%%%assuming the file is in the current folder
[y,Fs] = audioread('T0005_01_0.03.wav');
window=hamming(512); %%window with size of 512 points
noverlap=256; %%the number of points for repeating the window
nfft=1024; %%size of the fit
[S,F,T,P] = spectrogram(y,window,noverlap,nfft,Fs,'yaxis');
surf(T,F,10*log10(P),'edgecolor','none'); axis tight;view(0,90);
colormap(hot); %%for the indexed colors, check this in help for blck/white
set(gca,'clim',[-80 -30]); %%clim is the limits of the axis colours
xlabel('Time s');
ylabel('Frequency kHz')

Answers (1)

Matthew Wyant
Matthew Wyant on 10 Oct 2024
Hi,
The spectrogram function will display a convenience plot if you do not specify any output arguments. Your code could look like the following:
%% reads file into matlab This reads an array of audio samples into y,
%%%assuming the file is in the current folder
[y,Fs] = audioread('T0005_01_0.03.wav');
window=hamming(512); %%window with size of 512 points
noverlap=256; %%the number of points for repeating the window
nfft=1024; %%size of the fit
spectrogram(y,window,noverlap,1024,Fs,'yaxis'); % no outputs specified will create a convenience plot
colormap(gray) % for black and white

Community Treasure Hunt

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

Start Hunting!