MatLab Solutions: "Introduction to Digital Signal Processing: A Computer Laboratory Textbook".
29 Oct 2012
25 Nov 2013)
These files are the MatLab solutions of exercises contained in the above DSP lab textbook.
function [K S] = my_spectrogram(x,Fs,N,M,win_type)
% This function calculates the spectrogram of the input signal x.
% Inputs: x is a row vector that contains the signal to be analyzed.
% Fs is the sampling frequency of x in Hz.
% N is the selected length of the window in samples.
% M is the selected ampount of overlap between successive windows in samples.
% win_type is a string containing one of the windows shown
% below. The default value of this variable corresponds to the rectangular window.
% Outputs: S is a matrix that contains the spectrogram of x. i.e. S = |STFT(x.*win)|^2.
% K is the number of length-N segments (windows) the whole signal has been divided to.
win = chebwin(N).';
win = blackman(N).';
win = hamming(N).';
win = hanning(N).';
beta = 1;
win = kaiser(N,beta).';
otherwise % otherwise use the rectangular window
win = ones(1,N);
L = length(x);
n = 0:N-1;
w = exp(-j*2*pi*n/N);
W = zeros(N,N);
W(:,k+1) = w.^k;
rows = N;
K = ceil((L-N)/(N-M))+1; % This is the number of segments the signal is divided to.
cols = K;
% zero pad the tail of x as necessary in order to have an integer number of segments with length N.
x = [x zeros(1,N-mod(L-N,N-M))];
X = zeros(rows,cols);
X(:,k) = x((k-1)*(N-M)+1:k*N - (k-1)*M).*win;
S = abs(W*X).^2;
if ~mod(N,2) % if N is even.
S = S(1:N/2+2,:);
else % if N is odd.
S = S(1:(N-1)/2+2,:);