function [path_out, acc_cost_mat_reg, path_reduced, acc_cost_mat] = ...
multiscale_dtw(ref_pitch, test_pitch, radius, time)
% Multiscale DTW implementation based on pyts for Python by Johann Faouzi
%
% Stepan Miklanek, 2020

% compute Chroma from pitch parameters
chroma1 = pitch_to_chroma(ref_pitch);
chroma2 = pitch_to_chroma(test_pitch);

% define parameter for CENS computation (CENS are going to by downscaled by
% a factor of 10)
parameter.winLenSmooth = 41;
parameter.downsampSmooth = 10;
parameter.midiMin = 50;
parameter.midiMax = 100;

% define downscale factor used in computation of the constraint region
resolution_level = 10; 

% compute CENS from pitch parameters
cens1 = pitch_to_CENS(ref_pitch,parameter);
cens2 = pitch_to_CENS(test_pitch,parameter);

% get length of the Chroma arrays
n_timestamps_1 = length(chroma1);
n_timestamps_2 = length(chroma2);

n_timestamps_reduced_1 = length(cens1);
n_timestamps_reduced_2 = length(cens2);

start_ = tic;
% compute coarse DTW on the whole cost matrix
cost_mat = cost_matrix_no_region(cens1,cens2);
acc_cost_mat = acc_cost_matrix_no_region(cost_mat);
path_reduced = return_path(acc_cost_mat);

% define constraint region for the next level of DTW
region = blurred_path_region(n_timestamps_1,n_timestamps_2,...
resolution_level, n_timestamps_reduced_1, n_timestamps_reduced_2,...
path_reduced, radius);

% compute next level of DTW on constrained region
cost_mat_reg = cost_matrix_region(chroma1,chroma2,region);
acc_cost_mat_reg = acc_cost_matrix_region(cost_mat_reg,region);
clearvars cost_mat_reg
path = return_path(acc_cost_mat_reg);
end_ = toc(start_);

% display time taken 
if time
    fprintf(['Alignment time: ' num2str(end_) ' seconds.\n'])
end

% return optimal path in seconds
path_out = (path * 5)/100;

end

