[data,Fs] = audioread('myWAVaudiofile.wav');
channel = 1;
signal = data(:,channel);
samples = length(signal);
NFFT = 4096;
OVERLAP = 0.75;
spectrogram_dB_scale = 100;
decim = 2;
if decim>1
signal = decimate(signal,decim);
Fs = Fs/decim;
end
[sg,fsg,tsg] = myspecgram(signal,NFFT,Fs,OVERLAP);
sg_dBpeak = 20*log10(abs(sg))+20*log10(2/length(fsg));
min_disp_dB = round(max(max(sg_dBpeak))) - spectrogram_dB_scale;
sg_dBpeak(sg_dBpeak<min_disp_dB) = min_disp_dB;
figure(2);
imagesc(tsg,fsg,sg_dBpeak);colormap('jet');
axis('xy');colorbar('vert');grid
title([' Spectrogram / Fs = ' num2str(Fs) ' Hz / Delta f = ' num2str(fsg(2)-fsg(1)) ' Hz ']);
xlabel('Time (s)');ylabel('Frequency (Hz)');
function [sg,freq_vector,time] = myspecgram(signal,nfft,Fs,Overlap)
dt = 1/Fs;
signal = signal(:);
samples = length(signal);
if samples<nfft
s_tmp = zeros(nfft,1);
s_tmp((1:samples)) = signal;
signal = s_tmp;
samples = nfft;
end
window = hanning(nfft);
window = window(:);
offset = fix((1-Overlap)*nfft);
spectnum = 1+ fix((samples-nfft)/offset);
sg = [];
for i=1:spectnum
start = (i-1)*offset;
sw = signal((1+start):(start+nfft)).*window;
sg = [sg (abs(fft(sw))*4/nfft)];
time(i) = (start+nfft/2)*dt;
end
if rem(nfft,2)
select = (1:(nfft+1)/2)';
else
select = (1:nfft/2+1)';
end
sg = sg(select,:);
freq_vector = (select - 1)*Fs/nfft;
end
Best Answer