help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why aren't these convolution results equal to each other?


From: James Sherman Jr.
Subject: Re: Why aren't these convolution results equal to each other?
Date: Tue, 19 Apr 2016 12:17:26 -0400

On Tue, Apr 19, 2016 at 4:39 AM, Max Görner <address@hidden> wrote:
> Dear list,
>
> I struggle with some deviations of results derived on different ways.
> I would like to discuss these with you. I'm using Octave 4.0.0 on Ubuntu
> 15.10.
>
> The first problem concerns different ways to do 1D convolution. Please
> see the following listing:
>
>     kernel = normpdf (1:100, 50, 10);
>     M = randn(1, 199);
>     m1 = convn(M, kernel, 'valid');
>     m2 = conv(M, kernel, 'valid');
>     m3 = conv2(1, kernel, M, 'valid');
>     disp([isequal(m1, m2), isequal(m1, m3), isequal(m2, m3)]);
>
> Here m1 == m3 (i.e. convn == conv2), but m1 != m2 and m2 != m3. On
> Matlab R2015b it's only m1 == m2. I suspected that m1 == m2 == m3.
>
> The second problem concerns different ways to do 2D convolution. Please
> see the following listing:
>
>     kernel = normpdf(1:100, 50.5, 10);
>     M = randn(199);
>     m1 = conv2(abs(kernel), 1, conv2(1, kernel, M, 'valid'), 'valid');
>     m2 = conv2(1, kernel, conv2(abs(kernel), 1, M, 'valid'), 'valid');
>     m3 = conv2(abs(kernel), kernel, M, 'valid');
>     disp([isequal(m1, m2), isequal(m1, m3), isequal(m2, m3)]);
>
> Here m1 != m2 and m1 != m3 and m2 != m3. On Matlab R2015b it is m2 ==
> m3. I suspected, that exactly one of m1 or m2 equals m3, since either
> the vertical or the horizontal convolution should take place first.
>
> So now I'm a bit irritated, since I suspected bit equality in some of
> the results above but got small deviations. I hesitate to blame floating
> point arithmetics for thoose deviations.
>
> Could someone explain why these results are different and why this has
> to be that way or why it is acceptable?
>

Why do you hesitate to blame floating point operations?  Just look at
the differences between the two convolutions (I replaced your first
line with a random vector since I don't have the normpdf command):  In
Matlab 2013a:
---------
kernel = randn(1, 100);
M = randn(1, 199);
m1 = convn(M, kernel, 'valid');
m2 = conv(M, kernel, 'valid');
m3 = conv2(1, kernel, M, 'valid');
disp([isequal(m1, m2), isequal(m1, m3), isequal(m2, m3)]);
     1     0     0
disp([max(abs(m1-m2)), max(abs(m1-m3)), max(abs(m2-m3))]);
   1.0e-13 *

         0    0.1776    0.1776
---------
In Octave 4.0.0:
---------
>> kernel = randn(1, 100);
>> M = randn(1, 199);
>> m1 = convn(M, kernel, 'valid');
>> m2 = conv(M, kernel, 'valid');
>> m3 = conv2(1, kernel, M, 'valid');
>> disp([isequal(m1, m2), isequal(m1, m3), isequal(m2, m3)]);
   0   1   0
>> disp([max(abs(m1-m2)), max(abs(m1-m3)), max(abs(m2-m3))]);
  2.4869e-014  0.0000e+000  2.4869e-014
---------

That the differences are on the order of 10^(-14) shows me that they
are just flop round off errors.  As a rule of thumb, you should never
use "isequal" to compare floating point numbers.  Try to compare the
absolute value of the difference to some small number.

Hope this helps,

James Sherman



reply via email to

[Prev in Thread] Current Thread [Next in Thread]