123
APPENDIX B
pspectrum.m
% % FUNCTION pspectrum %% Display the sound power spectrum (in linear and logarithmic scales) % function r = pspectrum(s, fs) l = length(s); m = 100; n = 256; nbFrame = floor((l - n) / m ) + 1; %
% Create a matrix M containg all the frames % for i = 1:n for j = 1:nbFrame M(i, j) = s(((j - 1) * m) + i); end end %
% Matrix M created. Now apply HAMMING window and store in matrix N. Column vectors of N are
% the original frame vectors transformed by the Hamming window filter %
h = hamming(n); N = diag(h) * M;
%
% Now apply FFT and create a new matrix M2 where the column vectors are the FFTs of the
% column vectors of N. The elements of column matrix M2 contain the frames of the original
% signal, filtered by the Hamming window and transformed with the FFT. The elements of M2
% are complex numbers and symmetrical because FFT was used to transform the data.
%
% Each column in M2 is a power spectrum of the original signal %
for i = 1:nbFrame
M2(:,i) = fft(N(:, i)); end
124
tm = length(s) / fs; subplot(121);
imagesc([0 tm], [0 fs/2], abs(M2(1:t, :)).^2), axis xy; title('Power spectrum (Linear)');
xlabel('Time [s]');
ylabel('Frequency [Hz]'); colorbar;
subplot(122);
imagesc([0 tm], [0 fs/2], 20*log10(abs(M2(1:t, :)).^2)), axis xy; title('Power spectrum (Logarithmic)');
xlabel('Time [s]');
ylabel('Frequency [Hz]'); colorbar;
r=1;
%
% END OF FUNCTION pspectrum %