Can you attach the data in a .mat file? Also explain how you're getting the red line. For example sometimes you take the tops of the blue spikes and other times, it's in the middle of the spikes. So, why is the red line from 9 - 14 in the 180-182 region, and not in the 4-8 range for y? And from 188-192, why is the red line in the middle and not going along the tops of the blue spikes like in the left hand side of the plot? What rule(s) are you using?
Have you tried boundary()? Or sgolayfilt()?
For the data on the right, have you tried filtering and then tossing out data far away from the filtered result, and then splining the data to fill in missing values? (spline demo attached)