function visualization(code,filename)
%The first function to be called in this slide
%this function reflects the principle of maximum likelihood decoding
%Inputs defined by the user from the webdemo page:
%code: type of coding
rng('shuffle');
if(code==1)
    
    amountlineblocks=2;
    
    x=combinations(2);
    x = reshape(x',1,[]);
    validCodeWords=[0 0;1 1];
    vv = correction1(x,2,'hard');
    clf;
    axis  off
    hold on
    colorMap = containers.Map({'00', '11'}, {[255 0 0], [0,0,255]});
    
    for i=1:length(vv)
        
        if (vv(i) == 0)
            d = '00';
        else
            d = '11';
        end
        my_color = colorMap(d);
        
        my_color=my_color ./ 255;
        colNo = mod(i-1,amountlineblocks)+1;
        rowNo = ceil(i /amountlineblocks);
        startX = (colNo -1) * 4;
        startY = (amountlineblocks -rowNo) *3;
        rectangle('Position',[startX, startY,4,3],'FaceColor',my_color,'EdgeColor',[1 1 1],'LineWidth',3)
        text(startX + 2,startY + 1.5,dec2bin(i-1,2), 'FontSize', 15,'HorizontalAlignment','center' );
        
        
    end
    for i=1:length(validCodeWords)
        startX =  amountlineblocks * 4 + 0.5;
        startY = (amountlineblocks - i) *3 + 1;
        d = int2str(validCodeWords (i,:));
        d =  d(~(d == ' '));
        
        my_color = colorMap (d) ./ 255;
        rectangle('Position',[startX, startY ,1,1],'FaceColor',my_color,'EdgeColor',[1 1 1],'LineWidth',3)
        text(startX + 0.5 ,startY +0.5 ,d, 'FontSize', 15,'HorizontalAlignment','center' );
    end
 %   fh= figure(1);
 %   figure(1);
    print(filename,'-dpng','-r72');
    
end
if(code==2)
    amountlineblocks=4;
    
    M = combinations (2);
    x=combinations(4);
    
    validCodeWords=[M,xor(M(:,1), M(:,2)),M(:,1)];
    
    z= pdist2(validCodeWords,x,'L1');
    
    m =min(z);
    vv = zeros (length(x),4);
    for i = 1: length(x)
        col = z (:,i);
        idx = (col == m(i));
        pos = zeros(1,sum(idx));
        k = 1;
        for j = 1:length(validCodeWords)
            if (idx (j))
                pos (k) = j;
                k = k+1;
            end
        end
        
        choice = randi([1,sum(idx)]);
        vv(i,:) = validCodeWords(pos(choice),:);
    end
    
    clf;
    
    axis  off
    hold on
    colorMap = containers.Map({'0000', '0110','1011','1101'}, {[255 0 0], [0,255,0], [0 0 255], [255 255 0]});
    for i=1:length(vv)
        
        d = int2str(vv(i,:));
        d = d(~(d == ' '));
        my_color = colorMap(d);
        
        my_color=my_color ./ 255;
        colNo = mod(i-1,amountlineblocks)+1;
        rowNo = ceil(i /amountlineblocks);
        startX = (colNo -1) * 4;
        startY = (amountlineblocks -rowNo) *3;
        rectangle('Position',[startX, startY,4,3],'FaceColor',my_color,'EdgeColor',[1 1 1],'LineWidth',3)
        text(startX + 2,startY + 1.5,dec2bin(i-1,4), 'FontSize', 10,'HorizontalAlignment','center' );
        
        
    end
    
    for i=1:length(validCodeWords)
        startX =  amountlineblocks * 4 + 2;
        startY = (amountlineblocks - i) *3 + 1;
        d = int2str(validCodeWords (i,:));
        d =  d(~(d == ' '));
        my_color = colorMap (d) ./ 255;
        rectangle('Position',[startX, startY ,2,1],'FaceColor',my_color,'EdgeColor',[1 1 1],'LineWidth',3)
        text(startX + 1 ,startY +0.5 ,d, 'FontSize', 15,'HorizontalAlignment','center' );
    end
    
   % fh= figure(1);
   % figure(1);
    print(filename,'-dpng','-r72');
    
    
end


if(code==3)
    amountlineblocks=8;
    M = combinations (3);
    x=combinations(6);
    
    validCodeWords= [M,xor(M(:,1), M(:,2)),xor(M(:,1), M(:,3)),xor(M(:,2), M(:,3))]  ;
    
    z= pdist2(validCodeWords,x,'L1');
   
    
    
    m =min(z);
    vv = zeros (length(x),6);
    for i = 1: length(x)
        col = z (:,i);
        idx = (col == m(i));
        pos = zeros(1,sum(idx));
        k = 1;
        for j = 1:length(validCodeWords)
            if (idx (j))
                pos (k) = j;
                k = k+1;
            end
        end
        
        choice = randi([1,sum(idx)]);
        vv(i,:) = validCodeWords(pos(choice),:);
    end
    
    
    
    
    
    
    clf;
    axis  off
    hold on
    k =  {'000000','001011','010101','011110','100110','101101','110011','111000'};
    v = {[0 255 0],[0 0 255],[255 0 0],[233 243 73],[32 214 244],[204 204 204],[255 0 255],[231 116 50]};
    colorMap = containers.Map(k,v);
    
    
    
    for i=1:length(vv)
        
        d = int2str(vv(i,:))
        d = d(~(d == ' '))
        my_color = colorMap(d)
        
        
        my_color=my_color./255;
        colNo = mod(i-1,amountlineblocks)+1;
        rowNo = ceil(i /amountlineblocks);
        
        startX = (colNo -1) * 4;
        startY = (amountlineblocks -rowNo) *3;
        
        
        rectangle('Position',[startX, startY,4,3],'FaceColor',my_color,'EdgeColor',[1 1 1],'LineWidth',3)
        text(startX + 2,startY + 1.5,dec2bin(i-1,6), 'FontSize', 10,'HorizontalAlignment','center' );
        
    end
    
    for i=1:length(validCodeWords)
        startX =  amountlineblocks * 4 + 2;
        startY = (amountlineblocks - i) *3 + 1;
        d = int2str(validCodeWords (i,:));
        d =  d(~(d == ' '));
        my_color = colorMap (d) ./ 255;
        rectangle('Position',[startX, startY ,4,1],'FaceColor',my_color,'EdgeColor',[1 1 1],'LineWidth',3)
        text(startX + 2 ,startY +0.5 ,d, 'FontSize', 10,'HorizontalAlignment','center' );
    end
    
    %figure(1)
    print( filename, '-dpng', '-r72');
    
end

if(code==4)
    
    amountlineblocks=16;
    M = combinations (4);
    o=code4(M);
    validCodeWords=reshape(o',8,[])';
    x=combinations(8);
    
    
    z= pdist2(validCodeWords,x,'L1');
     m =min(z);
    vv = zeros (length(x),8);
    for i = 1: length(x)
        col = z (:,i);
        idx = (col == m(i));
        pos = zeros(1,sum(idx));
        k = 1;
        for j = 1:length(validCodeWords)
            if (idx (j))
                pos (k) = j;
                k = k+1;
            end
        end
        
        choice = randi([1,sum(idx)]);
        vv(i,:) = validCodeWords(pos(choice),:);
    end
    
    
    clf;
    
    axis  off
    hold on
    
    
    
    k = {'00000000','11010010','01010101','10000111','10011001','01001011',...
        '11001100','00011110','11100001','00110011','10110100','01100110',...
        '01111000','10101010','00101101','11111111'}
    
    
    v = {[0 255 0], [0 0 255], [255 0 0], [233 243 73], [248 35 72], [32 214 244 ], [235 64 15], ...
        [231 116 50], [100 20 55], [90 80 70], [50 20 10], [220 180 20], [0 60 180], [75 110 80], [188 224 60], ...
        [170 150 20]}
    colorMap = containers.Map(k,v);
    
    
    
    for i=1:length(vv)
        
        d = int2str(vv(i,:));
        d = d(~(d == ' ')) ;
        my_color = colorMap(d);
        
        
        
        
        my_color=my_color./255;
        colNo = mod(i-1,amountlineblocks)+1;
        rowNo = ceil(i /amountlineblocks);
        
        startX = (colNo -1) * 4;
        startY = (amountlineblocks -rowNo) *3;
        
     %   figure(1)
        rectangle('Position',[startX, startY,4,3],'FaceColor',my_color,'EdgeColor',[1 1 1],'LineWidth',3)
       
        
    end
    
    
    for i=1:length(validCodeWords)
        startX =  amountlineblocks * 4 + 2;
        startY = (amountlineblocks - i) *3 + 1;
        d = int2str(validCodeWords (i,:));
        d =  d(~(d == ' '));
        my_color = colorMap (d) ./ 255;
        rectangle('Position',[startX, startY ,10,2],'FaceColor',my_color,'EdgeColor',[1 1 1],'LineWidth',3)
        text(startX + 5 ,startY +1 ,d, 'FontSize', 12,'HorizontalAlignment','center' );
    end
    
    
    figure(1)
    print( filename, '-dpng', '-r72');
    
    
    
    
    
end


end
