# polynomial is not fitting well

I have two signal spectrums. I represent them with polynomial coefficients and then do convolution.

fig 1 & 2 are the two signal spectrums and their corresponding polynomial representations. (I have used numpy.polynomial.polynomial.Polynomial.fit and convert().coef). As far as I know, the .fit() method uses least squares fit to data. fig 3 shows the convolution (using numpy.convolve) but it has oscillations at the edges and it also goes to negative.

My target is to not get those oscillations and at the same time keep as much of the edge as possible.

So in fig 4, I have taken reciprocal of fig 1 and then done its poly representation. (hoping to avoid the zero-crossings and oscillations at the edges). But as seen in fig 5, there are oscillations (that also goes to negative) in the middle part of the signal instead of the edges. And even if I trim the tail off (take only the values larger than 0.01 from fig 1), I still get oscillations in fig 7 (although small in magnitude). I have also taken reciprocal of fig 2 and its corresponding poly fit, but have not shown it here.

And because of that oscillation in fig 7, when I do the convolution of the two reciprocal signals (fig 8), I also get some oscillation in the final output in fig 9 (it is the inverse of fig 8; also zooming in fig 8 shows that oscillation, which I have not shown here)

So how can I get rid of the oscillation in fig 3, 5 and 9? (I want to fit the data from -25 to +25 along the x-axis and trim the rest according to the scale of fig 1)

• Are you sure you don't get the same oscillatory behavior in Figure 1? Dec 28, 2022 at 15:50
• The statement "when I inverse the signal" is unclear. I assume that you mean "when I use the reciprocal of the signal" -- but either way, could you edit your question for clarity, keeping in mind that there's a lot of different "inverses", but only one reciprocal (or whatever you did). Dec 28, 2022 at 15:58
• While you're at it -- I assume that what you're showing is the spectrum of a signal, not the signal's time domain representation. Please clarify that, as well. Dec 28, 2022 at 15:59
• And -- state the range over which you're fitting the signal (i.e., the entire x axis, -5 to 5, whatever). Dec 28, 2022 at 16:01
• The FFT is, itself, a curve fit. Why do you want to take the curve fit of a curve fit? If a polynomial fit isn't working for you, why aren't you trying to fit other curves? You may want to edit your question yet again to tell us what your end goal is -- then instead of rejecting good advice about the question as you stated it, repeatedly, we can try to give you good advice about what you actually want. Dec 28, 2022 at 17:52

It looks like your inverse is very poorly behaved. Your original signal $$x[n]$$ is very very small at the edges. That means that your inverse $$y[n] = 1/x[n]$$ is very very large. From the looks of it it's mostly amplified noise.

Your fit in in the flat region is poor since $$y[n]$$ is very small there. A least square fit will be pay more attention to larger values than to the smaller ones. If you want to adjust that, you can try a weighted fit.

You an also try fitting it in the log domain but than the result will not be a polynomial but an exponential of a polynomial.

• can you please suggest me regarding the weight values? I mean what values should the weights have?
– MRR
Dec 29, 2022 at 2:34

What I am seeing in your polynomial fits to an inverted signal is an astonishingly good fit.

A least-squares fit minimizes the sum of the squares of the differences between the sample and the curve fit. Polynomial fits tend to have that oscillatory behavior that you are seeing in your fit. Given that you appear to be using a polynomial on the order of 9, the peak-peak oscillation appears to be about 0.001 times the overall variation of the thing you're fitting too, I'd say you're doing pretty well.

You have to remember that polynomial fitting has the same properties as projecting your function that you're trying to fit on a cylinder (look at high order Chebyshev polynomials to see what I mean, they look like cosines on a cylinder) and doing a discrete cosine transform on that. And the problem with your function is that it contains fairly high frequency components so you need a rather high order polynomial to represent them or you'll get the same ripples as when you truncate a spectrum, which is what you're doing when turning 800 points into 100 polynomial degrees.

But the cylindrical aspect of polynomial fitting gives you an important advantage, you can change the range over which you fit without problems (not so with a DCT or DFT), and essentially the smaller the range the more accurate the fit becomes. So instead of a 100 degree polynomial over the whole range that isn't even accurate enough you can divide the signal to fit into small chunks (small as in spanning a few original samples) which would each be represented by its own polynomial, and it could be only 20-degree polynomials instead of one big 100 degree polynomial. So when it comes to evaluation you'd just have to find the chunk that represents your value of x then evaluate the polynomial for that chunk.

But if you want a smoothed fit then there's another thing you can do to remove the rippling. The rippling comes from the truncation of the spectrum, which is what happens when you abruptly end the polynomial fitting at the top degree, and just as rippling from convolving a signal by a sinc is dealt with by windowing the sinc or even replacing it with a bell curve you can analyse the signal into Chebyshev polynomial coefficients, multiply each coefficient by the Gaussian function (or other bell curve function like the Hann or Blackman window functions) of its degree multiplied by an arbitrary constant (the idea is that the top degree would have a multiplier close to zero to smooth out the transition to the unrepresented degrees that have a multiplier of zero due to not being there) and then turn your windowed Chebyshev coefficients into regular polynomials or even use the Clenshaw method to directly evaluate Chebyshev polynomials (it only costs an extra subtraction per degree compared with Horner's method). Or of course you could also smooth out the signal prior to fitting.