%  Program to take audio data and convert it into a sonogram
%  Version for Matlab 5
%
%  Version 5, 26/8-98, by JAJ

function res = sonogram (file_name)

%  Load the audio file

cmd=['load ',file_name];
eval(cmd)

audio=complex_data;
lines=max(size(audio));

%  Set parameters for calculating the sonogram

f0=3.2e6;                       %  Transducer center frequency
fprf=8e3;                       %  Audio sampling frequency
lines=5*fprf;                   %  Find spectrum for 4 seconds
dN=floor(lines/500+0.5);        %  Number of new samples for spectrum
Ndft=256;                       %  Samples in one spectrum
Nspec=floor((lines-Ndft)/dN);   %  Number of spectra

%  Initialize data structure

power_dft=zeros(Ndft,Nspec);
power_min=[];
power_max=[];

disp('Making spectrum...')
for i=1:Nspec
  sig=audio((i-1)*dN+(1:Ndft));
  sig=sig.*hanning(Ndft);
  power_dft(:,i)=fftshift(abs(fft(sig)).^2);
end

%  Scale power density

power_min=min(min(power_dft));
power_max=max(max(power_dft));
scale=128/(power_max-power_min);
power_dft=(power_dft-power_min)*scale;

power_dft=log(power_dft+0.1);
power_min=min(min(power_dft));
power_max=max(max(power_dft));
scale=128/(power_max-power_min);
power_dft=(power_dft-power_min)*scale;

%  Make frequency axis

f=fprf/Ndft*(-(Ndft/2):(Ndft/2-1));
tidsakse=(1:Nspec)*dN/fprf;

%  Make the figure

image(tidsakse,f/1000,power_dft(Ndft:-1:1,:));		% Sonogram-plot
map=hot(128);
colormap(map)
title('Sonogram');					
xlabel('Time [s]');
ylabel('Frequency [kHz]');
axis([0 max(tidsakse) -2.5 2.5])
drawnow;


