Roger Federer edged a thrilling battle with Jo-Wilfried Tsonga to reach the quarter-finals of the Halle Open with a 7-6, 4-6, 7-5 victory.