Convert sequence of 3D points into a BVH file [I reformulate the question to something more concrete]

I have 2 different .bvh files with 2 poses. From one pose I want to find the transform to get the second pose.
If I compute the rotation to align one bone to the other bone (see Figure  1)



I get that I should rotate Z axis 45º, and if I put this rotation in Pose mode I can align the 2 bones (see Figure 2)



However, if I write 45º in the .bvh file the alignment goes completely wrong (see Figure 3) 



I think probably rotation should be expressed in armature coordinates but I can't find how to do that in the .bvh files.
Retarget animation

of one rig to another which differs only by rest pose

Given your two example bvh files produced rigs, toy_example_source and toy_example_target It looks like you want to do same as this qanda

Since your two rigs only differ by rest pose can snap target to source using a global copy transforms constraint. The new transforms of the snapped bone can be grabbed from the pose bones matrix. The local coordinates of the new transform using Object.convert_space(...) 

Edit the name of source and target rigs to suit. 

import bpy
from bpy import context

remove_constraints = True
scene = context.scene
rig1 = scene.objects["toy_example_source"]

rig2 = scene.objects["toy_example_target"]
if not rig2.animation_data:
    rig2.animation_data_create()
rig2.animation_data.action = None

# add copy transform constraint to each bone
for pb in rig2.pose.bones:
    ct = pb.constraints.get(pb.name)    
    if ct is not None:
        ct.influence = 1
        continue
    ct = pb.constraints.new('COPY_TRANSFORMS')
    ct.name = pb.name
    ct.target = rig1
    ct.subtarget = pb.name

action = rig1.animation_data.action

f = action.frame_range.x
# add a keyframe to each frame of new rig
while f &lt; action.frame_range.y:
    scene.frame_set(f)
    for pb in rig2.pose.bones:
        #pb2 = rig1.pose.bones.get(pb.name)
        m = rig2.convert_space(pb, pb.matrix, to_space='LOCAL')
        if pb.rotation_mode == 'QUATERNION':
            pb.rotation_quaternion = m.to_quaternion()
            pb.keyframe_insert("rotation_quaternion", frame=f)
        else:

        # add rot mode checking 
            pb.rotation_euler = m.to_euler(pb.rotation_mode)
            pb.keyframe_insert("rotation_euler", frame=f)
        pb.location = m.to_translation()

        pb.keyframe_insert("location", frame=f)
    f += 1

# set constraints to zero or remove entirely.
for pb in rig2.pose.bones:
    ct = pb.constraints.get(pb.name)    
    if ct is not None:
        if remove_constraints:
            pb.constraints.remove(ct)
        else:
            ct.influence = 0