An optimal filtering method for stable analytic continuation