My Spectrogram Function

 

This is how I made my spectrograms:


Narrowband:

    Starting at 0 s, I take 20 ms of a recording and multiply it by a hamming window.  Then I zero pad in front of and behind the windowed audio to prevent aliasing.  Next, I take the Fast Fourier Transform (fft) of the zero padded audio to obtain the frequencies present in it.  I can now store this data in the first column of my spectrogram matrix.

    Next, I shift down the original recording by 600 us and take a new 20 ms chunk, thus overlapping frequency analysis. From here, I repeat the process mentioned above   until I have analyzed a desired amount of the recording.



MATLAB code:


[y, fs]= wavread('voice.wav');


total_time= 1;


az= 0;

el= 90;


toffset= 0;

offset= toffset * fs;

wtime= 0.02;

win= round(wtime * fs);

h= hamming(win);

percent_overlap= 0.9;

hop= round(win * (1- percent_overlap));



for i= 0: floor((total_time * fs) / hop)

    s= y((i * hop)+1 + offset : round((i * hop)+win+offset), 2);

    s= s .* h;

    temp= zeros(size(s));

    s= cat(1, temp, s);

    s= cat(1, s, temp);

    S= 20.*log10(abs(fft(s)));

    if i == 0

        Z1= S(1:round(length(S)/2));

    else

        Z1= cat(2, Z1, S(1:round(length(S)/2)));

    end

end

%subplot 212

t= linspace(0+toffset, total_time+toffset, (total_time * fs) / hop +1);

f= linspace(0, fs/2, length(S)/2);

[X,Y]= meshgrid(t, f);

surf(X,Y,Z1)

axis tight

ylabel('Freq (Hz)')

xlabel('Time (sec)')

zlabel('Power')

lighting phong

shading interp

view(az,el)

xlabel('Time (sec)')

ylabel('Freq (Hz)')

title('Voice, Spectrogram')




Back