%% Discrete Cosine Analysis
% Kurt Bryan and SIMIODE
% A Matlab Live script to load in an audio signal (stored in an Excel workbook) and perform a discrete cosine analysis of the frequency content.
% Load and Plot Audio Signal: Put file "gong.xlsx" where Matlab can find it. Here "n" is the number of data points.

clear;
adat = readmatrix('gong.xlsx');
n = length(adat)

% Compute the duration "T" of the signal, and set the sampling rate (16000 Hz).

samprate = 16000;
T = n/samprate

% Here's a plot of the entire signal versus time.

tt = (0:n-1)/samprate;
figure(1)
plot(tt,adat)
xlabel("Time (seconds)");
ylabel("Signal Intensity");

% For time t = 1 to t = 1.01 seconds plot from k = 16000 to k = 16161, roughly

figure(2);
plot(tt(16000:16161),adat(16000:16161));
xlabel("Time (seconds)");
ylabel("Signal Intensity");

% Compute the DCT of the Signal Vector and Plot: Use Matlab's dct command

C = dct(adat);

% Display the DCT magnitude versus frequency (C[k] is frequency (k-1)/(2*T)).

freqs = (0:n-1)/(2*T);
figure(2);
plot(freqs,abs(C));
xlabel("Frequency (hz)");
ylabel("Coefficient Magnitude");