title: Moving mean square error between 2 arrays, 'valid', where they fully overlap
body: <p>I have a noisy square signal which looks like this:</p>
  <p><a href="https://i.stack.imgur.com/6W0xD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6W0xD.png" alt="square signal" /></a></p>
  <p>The amplitude is known. To match the complete square, I can create a pattern of the square and apply <code>np.correlate</code> to find where the signal and the pattern maximally overlap. I wanted to apply a similar approach to find the edge, try to correlate with the 2 patterns below:</p>
  <p><a href="https://i.stack.imgur.com/lR6wF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lR6wF.png" alt="pattern" /></a></p>
  <p>As the correlation is nothing more than a convolution, this doesn't work. Half the pattern is equal to <code>0</code>, and the convolution of this half will return <code>0</code> no matter the position on the signal; while the other half is equal to <code>-X</code> with <code>X</code> the amplitude. This second half convoluted with the signal will be maximal when the signal amplitude is maximal. On the signal plot, you can observe that the square is not perfect and that the beginning has a slightly larger amplitude. Basically, both correlation leads to a match on the beginning of the square, where the convolution is maximal. The ramp up (end of the square) is not detected.</p>
  <p>To avoid this problem, I would like to use a different operation. As I do know the amplitude of the square signal, I can generate a pattern with the correct amplitude, in this case about <code>-0.3</code>. Thus, I would like to take the pattern and slide it across the signal. At each step, I would compute the mean square error and my pattern would match with the signal at the position where the mean square error is minimized. Moreover, I would like to use the same type of setting as for a convolution, 'valid', where the operation is performed only when the 2 arrays fully overlap.</p>
  <p><strong>Do you know of an other method; and/or which function, methods I should use?</strong> I couldn't find a all-in-one function line <code>np.convolve</code> or <code>np.correlate</code>.</p>
  <p>EDIT: Since I couldn't find a pre-coded function in a library, I've coded mine with a while loop. It's pretty inefficient... It's up <a href="https://codereview.stackexchange.com/questions/248184/moving-mean-square-error-between-2-arrays-valid-where-they-fully-overlap">here</a> on codereview for upgrades.</p>