% Script which computes the Chroma and CENS features
% and visualizes them for some input signals.
clc
clear all

sampFreq = 44100;
frameLen = 2048;
frameShift = 64;
decimationFactor = 20;

currentFolder = pwd;
[signal,fs] = audioread([currentFolder '/McGill/oboe/ob_a4.wav']);
% [signal,fs] = audioread([currentFolder '/McGill/piano-ld/pil_a4.wav']);


% segment signal and compute FFT to obtain STFT
signalFrames = buffer(signal,frameLen,frameLen-frameShift,'nodelay');
stftSpecFull = fft(signalFrames);
stftSpecHalf = abs(stftSpecFull(1:frameLen/2+1,:));
upperFreq = sampFreq/4;

% define STFT frequency vectors
freqVec = [0:frameLen/2]'*sampFreq/frameLen;
% define chroma frequency vector starting at C0:
musicalNotesFreqs = 110.*2.^([3:14]'/12);
% initialize matrix for chromagram
chromaMat = zeros(12,size(stftSpecHalf,2));

% solution: begin
    % loop for computing chroma values

    % CENS features

    % temporal smoothing of chromagram

% solution: end

censMat = censMat./repmat(sqrt(sum(abs(censMat).^2)),12,1);

% time vectors for signal plots
timeVec = [0:size(stftSpecHalf,2)-1]'*frameShift/sampFreq;
timeVecSmoothed = [0:size(chromaMat,2)-1]'*decimationFactor*frameShift/sampFreq;

% plots (spectrogram and chromagram)
figure;
subplot 311
imagesc(timeVec,freqVec,db(stftSpecHalf));axis xy
ylabel('Frequency [Hz]')
title('Spectrogram')
colormap(hot)
caxis([-60 40])
colorbar
subplot 312
imagesc(timeVec,[],chromaMat);axis xy
colormap(hot)
colorbar
set(gca,'ytick',[1:12])
set(gca,'yticklabel',{'C','C#','D','D#','E','F','F#','G','G#','A','A#','B'})
title('Chromagram')
subplot 313
imagesc(timeVec,[],censMat);axis xy
colormap(hot)
colorbar
set(gca,'ytick',[1:12])
set(gca,'yticklabel',{'C','C#','D','D#','E','F','F#','G','G#','A','A#','B'})
xlabel('Time [s]')
title('CENS Chromagram')
