clear; close all;

N_type = 5; N_slice = 1; N_data = 100;
N_clip = N_type*N_slice*N_data;

oct_diff = [0 200 400 800 1600 3200 6400 11025];  % Freq Normalized interval
oct_alpha = 0.02;  % tunable

N_freq_intvl = (length(oct_diff)-1);
feature = zeros(N_clip, N_freq_intvl*2);
style = zeros(N_clip, 1);

%

load('feature.mat','feature');
load('style.mat','style');

%

for i = 1:1:N_type
    feature_klt((N_slice*N_data*(i-1)+1):(N_slice*N_data*i),:) = ...
        klt_audio(feature((N_slice*N_data*(i-1)+1):(N_slice*N_data*i),:));
end

%

alpha_spilt = 0.2;   % training & testing data split factor
num_train = N_clip*(1-alpha_spilt);
num_test = N_clip*alpha_spilt;
% x means feature; y means label
x_train = zeros(num_train, N_freq_intvl*2);
x_test = zeros(num_test, N_freq_intvl*2);
y_train = zeros(num_train, 1);
y_test = zeros(num_test, 1);
% Random and class-uniform distribution & 
for i = 1:1:N_type
    indices=randperm(N_slice*N_data);
    x_tmp = feature_klt((N_slice*N_data*(i-1)+1):(N_slice*N_data*i),:);
    y_tmp = style((N_slice*N_data*(i-1)+1):(N_slice*N_data*i),:);
    x_tmp = x_tmp(indices,:);
    y_tmp = y_tmp(indices,:);
    x_train((N_slice*N_data*(1-alpha_spilt)*(i-1)+1):(N_slice*N_data*(1-alpha_spilt)*i),:) = ...
        x_tmp(1:N_slice*N_data*(1-alpha_spilt),:);
    x_test((N_slice*N_data*alpha_spilt*(i-1)+1):(N_slice*N_data*alpha_spilt*i),:) = ...
        x_tmp(N_slice*N_data*(1-alpha_spilt)+1:N_slice*N_data,:);
    y_train((N_slice*N_data*(1-alpha_spilt)*(i-1)+1):(N_slice*N_data*(1-alpha_spilt)*i),:) = ...
        y_tmp(1:N_slice*N_data*(1-alpha_spilt),:);
    y_test((N_slice*N_data*alpha_spilt*(i-1)+1):(N_slice*N_data*alpha_spilt*i),:) = ...
        y_tmp(N_slice*N_data*(1-alpha_spilt)+1:N_slice*N_data,:);
end

%

% Data
% x_train, y_train => training feature/label
% x_test, y_test   => testing  feature/label

% train
[y_pred_train,model,llh] = mixGaussEm(feature_klt',style');
num_correct = sum(style==y_pred_train')
acc_train = num_correct/(N_type*N_slice*N_data)*100

% train
[y_pred_train,model,llh] = mixGaussEm(x_train',y_train');
num_correct = sum(y_train==y_pred_train')
acc_train = num_correct/(N_type*N_slice*N_data*(1-alpha_spilt))*100    % training performance

% predict
y_pred_test = mixGaussPred(x_test',model);
num_correct = sum(y_test==y_pred_test')
acc_test = num_correct/(N_type*N_slice*N_data*alpha_spilt)*100