# Filtering Background with resonance or Peaks

I have some data, the blue curve which I want to filter out the background to get a flat signal with dips(Idea is to divide signal (except the dips) by the background). The orange curve is my best fit so far. You can see that it agrees for most of the points, apart from the deviation at around 1000. Otherwise, the fitting was very good. and It almost returned a noiseless result by dividing the blue curve (raw data) by the fitting.

I expect something like below However, the problem was that my fitting also has dips (which i don't want to divide out my dip!),also since the background was changing constantly, using a higher order fitting function returns wiggles, so it makes it really hard to have no wiggles between the dips. Here is the bad example,again, the blue = raw data, orange = fitting, green = processed data.

And I want something like this, where the blue curve sorts of fill the gap]4 Any ideas how to do this? I tried using lowpass filter, doesn't work. What i'm using now is Peak detect/ find peak method. I also tried Fourier transform and remove higher k components, but it doesn't work! I appreciate for any idea ! And I can write the code myself.

• You could create an interpolating function on the data with the dips excluded, i.e. find peak positions, extract all data within +/-N of peak, interpolate and then sample your interpolation function at all data points and use this to divide out background. Low pass filtering, or moving average before fitting, etc. should also be ok. without access to the data it's going to be tough to help. Jun 2, 2016 at 17:42
• It would be easier to help if you could provide the data. Jun 2, 2016 at 20:07
• You can use Quantile Regression for this as explained here. Jun 3, 2016 at 3:50
• N.J.Evans. That's exactly How i did it. But it fitted the gaps between peaks with a dip which is something i don't want. Jun 3, 2016 at 10:08
• Quantile Regression failed Jun 3, 2016 at 10:33

I converted the blue line in your image to a vector of amplitudes trace. The orange line is the filtered output, the green line is the residual. Vary the second input parameter of CompoundMedianFilter to see other baseline approximations.
ListLinePlot[{trace, CompoundMedianFilter[trace, 15],