function [ bvhchannels, skel ] = expmap2bvh_iva( skel, expchannels, bvhfile, fl)
%Converts an exp map representation to bvh file

 counter = 1;
 for j=2:size(skel.tree,2) 
         skel.tree(j).posInd = [];
        if ~isempty(skel.tree(j).rotInd)
           skel.tree(j).rotInd = skel.tree(j).rotInd - 3*(counter);
           skel.tree(j).expmapInd = skel.tree(j).expmapInd - 3*(counter);
           skel.tree(j).channels = [{'Zrotation'},{'Yrotation'},{'Xrotation'}];
           skel.tree(j).order = 'zyx';
           counter = counter + 1;
        end
 end

 M(:,1:6) = expchannels(:,1:6);
 counter = 1;
 for i=10:6:size(expchannels,2)
     ch = expchannels(:,i:i+2);
     M(:,counter+6:counter+8) = ch;
     counter=counter+3;
 end
 
bvhchannels = zeros(size(M));
bvhchannels(:,1:3) = M(:,1:3);


for i=1:size(M,1)
    %for j=4:3:(size(expchannels,2))
     for k=1:length(skel.tree)
         if ~isempty(skel.tree(k).rotInd)
             %expmap = expchannels(i,j:j+2);
             
             expmap = M(i,skel.tree(k).expmapInd);
             
             R = RotationMatrix(expmap,'exponentialMap');
             
             euler = R.GetEulerAngles();
             
             E = -rad2deg(euler(1,:));
             
             bvhchannels(i,skel.tree(k).rotInd) = E;
        end
    end
end


bvhWriteFileIVA(bvhfile,skel,bvhchannels,fl);
%bvhWriteFileXYZ(bvhfile,skel,bvhchannels,fl);
end

     

