function [ S_scale ] = ScaleTransform(S, r)

% Function to interpolate CC-DST cell array by a factor r.

% S: array contains the CC-DST voices.
% r: scaling factor.
% S_scale: CC-DST scaled by a factor r.

if (r<0) || (r==1)
    error('scaling factor must be positive and not equal to 1.')
end

rows = length(S);
ff = 0;

for f=1:rows
    c = 0;
    St = S{f};
    cols = length(St);
    
    if cols/r >= 1 && numel(St)>1
        elem = 0;
        ph0 = 0;
        
        for j=0:r:cols-2
                idx = floor(j)+[1, 2];
                belem = St(idx);
                tf = j - floor(j);   % overflow
                bmag = (1-tf)*abs(belem(1)) + tf*(abs(belem(2)));
                dp = angle(belem(2)) - angle(belem(1));
                dp = dp - 2 * pi * round(dp/(2*pi)); % Reduce the phase to -pi:pi range
                ph0 = ph0 + dp;
                elem = elem +1;
                c(elem) = bmag * exp(1i*ph0);   
        end
        ff=ff+1;
        S_scale{ff} = c;
           
    else
        ff=ff+1;
        S_scale{ff} = S{ff};
        
    end
    
end


end

