Moving average algorithm that preserves the integral and number of points I need to smooth a noisy signal. I think that the moving average algorithm would be fine. However, I want the following conditions to be satisfied:

The total integral (the sum of all values) before and after the moving average filtering is kept unchanged
The number of points before and after the moving average filtering is kept unchanged

Implementation
The implementation is based on the answer to this post. The main question here is how to process the edges of a signal. The idea is to &quot;reflect&quot; the signal at the edges:
$$a_{-i} = a_{i-1},\, i &gt; 0\quad \text{at the beginning of a signal},$$
$$a_{L+i} = a_{L-i-1},\, i\geq 0\quad \text{at the end of a signal},$$
where L denotes the length of the signal.
Note
To produce more or less appropriate results this algorithm should be used with the signals which start and end with relatively flat waveform (the length of &quot;flatness&quot; depends on the size of the window used).
Algorithm
#include &lt;vector&gt;
#include &lt;iostream&gt;

//Input :
//       in : the original signal
//       w  : the size of the window of the moving average
//Output :
//       out : the filtered signal
void MovingAverage( const std::vector&lt;float&gt;&amp; in, std::vector&lt;float&gt;&amp; out, int w )
{
    //****** PREPARATION ******/
    out.clear();

    //Make the window size odd
    w += (w % 2) ? 0 : 1;

    if( (w &lt; 1)  or (in.size() &lt; w) )
    {
        std::cerr &lt;&lt; &quot;Bad input!&quot; &lt;&lt; std::endl;
        return;
    }

    //****** ALGORITHM ******/
    int i = 0;  //the current position

    //Firstly, average the head
    for( ; i &lt; w/2; i++ )
    {
        float averagedValue = 0.;
        for( int j = -w/2; j &lt; (w/2 + 1); j++ )
        {
            averagedValue += (i + j &gt;= 0) ? in[i + j] :
                                            in[-(i + j) - 1];//(i + j) is negative here
        }
        out.push_back( averagedValue / w );
    }

    //The middle is averaged normally
    for( ; i + w/2 &lt; in.size(); i++ )
    {
        float averagedValue = 0.;
        for( int j = -w/2; j &lt; (w/2 + 1); j++ )
        {
            averagedValue += in[i + j];
        }
        out.push_back( averagedValue / w );
    }

    //Finally, average the tail
    for( ; i &lt; in.size(); i++ )
    {
        float averagedValue = 0.;
        for( int j = -w/2; j &lt; (w/2 + 1); j++ )
        {
            //(i + j) is not negative here
            averagedValue += (i + j &lt; in.size()) ? in[i + j] :
                                                   in[in.size() - (i + j - in.size()) - 1];//(i + j - in.size()) is not negative here
        }
        out.push_back( averagedValue / w );
    }
}

Test
Where you make the window length odd: w += (w % 2) ? 0 : 1; I would prefer the conditional part to be something like ((w % 2) != 0). Others will no doubt disagree with me, but I personally prefer conditionals to be boolean.
But even better, you can get rid of the conditional entirely: w |= 0x1;. Maybe this makes some kind of not-guaranteed-to-be-fully-portable assumptions? But I'd be comfortable using it.

Using the same index i in the multiple consecutive loops makes me think a little. On the one hand, it's really good how it doesn't duplicate code that calculates where the loops start and end. On the other hand, one doesn't normally expect a loop index, particularly one named i, to be used after the end of a loop. What if some future maintainer doesn't realize this, and inserts some other loop in here that uses i as its index? Possibly you could rename i to something else, but that might make your subsequent calculations harder to follow. In the end, I'd probably at least put a comment by the declaration of i warning that it is re-used through multiple loops.

I don't like something that is purely DSP to write to std::cerr. Either return a status value, or throw an exception when you encounter invalid parameters. I think a status return value is preferable, but an exception may be OK if this really is an exceptional case.

You use float to hold the intermediate averagedValue. In most cases this is fine, but depending on the nature of your data and your window size, it is possible that you'd benefit from using double for the intermediate variable.