performance – Parallel 3D Discrete Cosine Transformation Implementation in Matlab

I am trying to implement 3D Discrete Cosine Transformation calculation in Matlab with parallel computing parfor. The formula of 3D Discrete Cosine Transformation is as follows.

The experimental implementation

The experimental implementation of 3D Discrete Cosine Transformation function is DCT3D.

function X=DCT3D(x)

N1=size(x,1);
N2=size(x,2);
N3=size(x,3);
X=zeros(N1,N2,N3);

for k1=0:N1-1
    for k2=0:N2-1
        for k3=0:N3-1
            sumResult=0;            
            parfor n1=0:N1-1
                for n2=0:N2-1
                    for n3=0:N3-1
                        sumResult=sumResult+...
                            x(n1+1,n2+1,n3+1)*...
                            cos(pi/(2*N1)*(2*n1+1)*k1)*...
                            cos(pi/(2*N2)*(2*n2+1)*k2)*...
                            cos(pi/(2*N3)*(2*n3+1)*k3);
                    end
                end
            end
            X(k1+1,k2+1,k3+1)=8*sumResult*CalculateK(k1)*CalculateK(k2)*CalculateK(k3)/(N1*N2*N3);            
        end
    end
end

Moreover, the used function CalculateK:

function output = CalculateK(x)
  output = ones(size(x));
  output(x==0) = 1 / sqrt(2);

Test cases

%% Create test cells
testCellsSize = 10;

test = zeros(testCellsSize, testCellsSize, testCellsSize);
for x = 1:size(test, 1)
    for y = 1:size(test, 2)
        for z = 1:size(test, 3)
            test(x, y, z) = x * 100 + y * 10 + z;
        end
    end
end

%% Perform test
result = DCT3D(test);

%% Print output
for z = 1:size(result, 3)
    fprintf('3D DCT result: %d planen' , z);
    for x = 1:size(result, 1)
        for y = 1:size(result, 2)
            fprintf('%ft' , result(x, y, z));
        end
        fprintf('n');
    end
    fprintf('n');
end

The output of the test code above:

3D DCT result: 1 plane
1726.754760 -80.720722  -0.000000   -8.646042   -0.000000   -2.828427   0.000000    -1.143708   -0.000000   -0.320717   
-807.207224 0.000000    0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   
-86.460422  -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   0.000000    -0.000000   
-0.000000   -0.000000   0.000000    0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   -0.000000   
-28.284271  -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    
0.000000    0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   0.000000    0.000000    
-11.437076  -0.000000   0.000000    0.000000    -0.000000   0.000000    -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    
-3.207174   -0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    

3D DCT result: 2 plane
-8.072072   0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   
-0.000000   -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    
-0.000000   -0.000000   -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   
-0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   
-0.000000   0.000000    -0.000000   0.000000    0.000000    0.000000    0.000000    -0.000000   -0.000000   -0.000000   
0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   0.000000    -0.000000   -0.000000   0.000000    
-0.000000   0.000000    0.000000    -0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   -0.000000   
0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    
0.000000    -0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    -0.000000   0.000000    

3D DCT result: 3 plane
-0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    
0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    
-0.000000   -0.000000   0.000000    -0.000000   0.000000    0.000000    0.000000    0.000000    -0.000000   0.000000    
0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    0.000000    0.000000    -0.000000   0.000000    
0.000000    0.000000    0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   
-0.000000   0.000000    0.000000    -0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   -0.000000   
-0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    
0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    0.000000    
-0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    
0.000000    0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    0.000000    

3D DCT result: 4 plane
-0.864604   -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   
-0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    -0.000000   0.000000    
-0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   
-0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   0.000000    0.000000    -0.000000   0.000000    
-0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   
-0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    
0.000000    -0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    
-0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    
0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   

3D DCT result: 5 plane
-0.000000   0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    
0.000000    -0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    0.000000    
-0.000000   0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    
0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   0.000000    
0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    -0.000000   
0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    
0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    -0.000000   
-0.000000   -0.000000   0.000000    0.000000    -0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    
0.000000    0.000000    -0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   
-0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   0.000000    -0.000000   

3D DCT result: 6 plane
-0.282843   -0.000000   -0.000000   -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    
-0.000000   0.000000    0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   -0.000000   0.000000    -0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    
-0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    
-0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    
0.000000    -0.000000   -0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    
0.000000    -0.000000   -0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   
0.000000    -0.000000   0.000000    0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    
0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   

3D DCT result: 7 plane
0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    0.000000    -0.000000   0.000000    0.000000    
0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   
-0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   
0.000000    0.000000    -0.000000   0.000000    0.000000    0.000000    0.000000    0.000000    -0.000000   -0.000000   
-0.000000   0.000000    0.000000    -0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   -0.000000   
-0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    -0.000000   0.000000    -0.000000   -0.000000   
0.000000    -0.000000   0.000000    0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    
0.000000    -0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    -0.000000   0.000000    -0.000000   
-0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    0.000000    

3D DCT result: 8 plane
-0.114371   -0.000000   -0.000000   0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   
-0.000000   -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   
-0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    
-0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    
0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   -0.000000   
-0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    
-0.000000   0.000000    0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    
-0.000000   0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   -0.000000   
-0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    
0.000000    -0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    0.000000    

3D DCT result: 9 plane
-0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   
0.000000    0.000000    0.000000    0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    0.000000    
0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   -0.000000   
0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   0.000000    -0.000000   
0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    
-0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   0.000000    -0.000000   
0.000000    0.000000    0.000000    0.000000    0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   
-0.000000   -0.000000   0.000000    0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   -0.000000   
0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    

3D DCT result: 10 plane
-0.032072   -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    
-0.000000   -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    
0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    0.000000    
-0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    -0.000000   0.000000    0.000000    
-0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   -0.000000   0.000000    
-0.000000   -0.000000   0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   
0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   -0.000000   
-0.000000   0.000000    -0.000000   -0.000000   0.000000    -0.000000   0.000000    -0.000000   -0.000000   0.000000    
0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    
-0.000000   0.000000    -0.000000   0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    

If there is any possible improvement, please let me know.