Recently, the method of choice to exploit robot dynamics for efficient walking is numerical optimization (NO). The main drawback in NO is the computational complexity, which strongly affects the time demand of the solution. Several strategies can be used to make the optimization more treatable and to efficiently describe the solution set. In this work, we present an algorithm to encode effective walking references, generated off-line via numerical optimization, extracting a limited number of principal components and using them as a basis of optimal motions. By combining these components, a good approximation of the optimal gaits can be generated at run time. The advantages of the presented approach are discussed, and an extensive experimental validation is carried out on a planar legged robot with elastic joints. The biped thus controlled is able to start and stop walking on a treadmill, and to control its speed dynamically as the treadmill speed changes.