How to transform using a math formula a signal in another one such that to keep the maximum of the first signal in the same position Let's say we have the following terms in a string (there is no formula for the next terms) 
$$\begin{array}{c|c}
\text{ms} & \text{value}  \\\hline
1&1 \\\hline
2&2 \\\hline
3&3 \\\hline
4&4 \\\hline
5&5 \\\hline
6&6 \\\hline
7&9 \\\hline
8&10 \\\hline
9&12 \\\hline
10&13 \\\hline
11&7 \\\hline
12&6 \\\hline
13&5 \\\hline
14&11 \\\hline
15&13 \\\hline
16&15 \\\hline
17&17 \\\hline
18&20 \\\hline
19&20 \\\hline
20&20 \\\hline
21&18 \\\hline
22&17 \\\hline
23&16 \\\hline
24&15 \\\hline
25&13 \\\hline
26&11 \\\hline
27&10 \\\hline
28&9 \\\hline
29&8 \\\hline
30&7 \\\hline
31&5 \\\hline
32&3 \\\hline
33&1 \\\hline
34&0 \\\hline
\end{array}$$
For this table we have the following input signal:

From the table and from the picture it can be observed that the maximum it is around $18[\text{ms}]$ when the graph have the biggest value equals with $20$.
What I want to try is to create a "filter" such that the signal to be smoother and also the maximum of the "filter" to be almost in the same position with the input signal. The ideal case is the red curve from the following picture:

I am aware that the ideal case is very hard to be obtained, so I would like to obtain a formula which helps me to transform the input signal in another one smoother and with the maximum almost in the same position. 
What I have tried was to sum all the time the first $8$ values and then to divide by 8, then to sum the elements starting from $2$[ms] to $9$[ms] and divided by 8, $\ldots$ and so on until starting from $27$[ms] to $34$[ms] and divided by 8, then starting from $28$[ms] to $34$[ms] and divided by 8, $\ldots$ $33$[ms] to $34$[ms] and divided by 8, $34$[ms] and divided by 8. 
So, I obtain the following picture which is smoother but the maximum is not in almost the same position.  

Later edit
I want to say that there is  a formula, but this formula is no so good because the maximum of the input is not in the same time with the maximum for the filter. 
So, 
$$\text{Filter}=\sum_{i=t-\text{buffer}}^{t}{\text{Input}_{t}},$$ where $\text{buffer}$ can be any value.  
 A: One approach you can try is to make a weighted linear least squares (Wikipedia) fit.
An unweighted linear least squares (MathWorld) can be expressed $${\bf c_o} = \min_{\bf c}\|\bf \Phi c - d\|_2$$
Where the basis functions you fit to are stored as column vectors in $\bf \Phi$. Now what you can do is to add a diagonal weight matrix $\bf W$:
$${\bf c_o} = \min_{\bf c}\bf \|W(\Phi c -d)\|_2$$
We can make $\bf W$ have diagonal element corresponding to the maximum data point really big, say 100 or 1000 times as large as the rest of the diagonal elements. That will force the maximum point to stay in the same spot, but it won't ensure that no other point becomes bigger, which they can for example if you choose an unsuitable set of functions in $\bf \Phi$ to make the fit for. How easy it will be to choose a set of basis functions will depend a lot on what your data looks like. However for the data in your plot, probably a polynomial of order 2 or 4 will do fine.
Here is what it looks like for one configuration of parameters and polynomial degree $4$. It might not always be that polynomials are the best candidate functions to work with here.

A: I suppose you would like a method that you can apply to a wide class of input signals with the minimum of taylored adjustment of the parameters.
For that, I would proceed as follows:  


*

*do a mild pre-filtering of the signal, enough to smooth
irregularities, without affecting too much the prominent maximum
(maxima), e.g. a moving average with 2 or 3 steps;   

*amplify in a non linear way the resulting signal so as to enhance the maximum (maxima), e.g. raising to power (2 - 4), exponentiating, ...;   

*filter the resulting signal with a method of your choice as for the best smoothing, e.g. centered moving average, with a number of steps you retain sufficiently smooth     

*de-amplify


For instance, for the example you give a
m. avg. pre-f. of 2 st, a raise to power 4 and centered m. avg.filtering with 5 st, would give the following result

Notes


*

*Step 1- "pre-filtering" is optional, in the sense that you apply directly on the input signal a low-pass filtering to remove high frequency noise,
if you expect the signal might come with some (mild= removing only highest non significant frequencies).  

*I indicated moving average  because of its simple implementation and because it is quite effective. However other type of filtering can be applied. 

*For what you need to achieve, centered moving average should be chosen in order to limit the shift in the resulting signal. That means that you take the average over a window with ca. $(n-1)/2$ points before and after the point in question, i.e. $$\overline x _k  = {1 \over n}\sum\limits_{\left\lfloor {(n - 1)/2} \right\rfloor \, \le \,j\, \le \,\left\lceil {(n - 1)/2} \right\rceil } {x_{k - j} } $$ . Then at the beginning and at the end, either you reduce the $n$, or pad the signal with $0$'s (or with a repetition of the signal if you want to make it periodic, etc.).

*From the structure of the formula above, you can recognize a convolution between the signal and a "box" (rectangular window) of width $n$ centered at the origin.
That to indicate that you have further choices for the window to use beyond the rectangular one: each with its special features and target applications. A summary list of them is given here. 

*Literature to refer to is quite vast. You may start from this wikipedia  at the point where it states : "For a number of applications, it is advantageous to avoid the shifting induced by using only 'past' data. Hence a central moving average can be computed, using data equally spaced on either side of the point in the series where the mean is calculated" and follow the links given there.
In particular the link to the Savitzky–Golay filter, one the cited application for which is "..Location of maxima and minima in experimental data curves. This was the application that first motivated Savitzky....".

*Final consideration is that, to orient yourself among the math/signal processing tools available, much (I would say all) depends on the physical considerations governing the input signal and intended application of the output.
