From octave-sources-request at bevo dot che dot wisc dot edu Tue Sep 25 03:22:35 2001 Subject: another non-c version of conv2 and a different version of corr2. From: "Phillip Wang" To: Date: Tue, 25 Sep 2001 04:21:43 -0400 This is a multi-part message in MIME format. ------=_NextPart_000_0005_01C14579.94EFFE00 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Send me mail if you think there are errors in the code. I would like to know since I'm using it for my image processing class. Thanks p.w. ------=_NextPart_000_0005_01C14579.94EFFE00 Content-Type: application/octet-stream; name="conv2.m" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="conv2.m" % Copyright at 2001 by Phillip Wang % % 9-25-2001 written and released under GNU General Public License % This program is free software; you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation; either version 2 of the License, or % (at your option) any later version. % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % You should have received a copy of the GNU General Public License % along with this program; if not, write to the Free Software % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 = USA ## -*- texinfo -*- ## at deftypefn {Function File} {} conv2(@var{matrix1},@var{matrix2}) ## Return a 2D convolution of at var{matrix1} and @var{matrix2} ## at end deftypefn function returnMatrix =3D conv2(matrix1, matrix2) %to develop this, I started off with a 1D convolution example % and then generalized it for 2D. The examples in the documentation % that follows uses matrix1=3D[1x3] and matrix2=3D[1x4]. %Note that the values does not matter as much as figuring out the % correct index used to calculate the answer. Most of the work is in % determining the correct index to use. %declare and intialize some useful variables rMatrix1 =3D rows(matrix1); %row of matrix1 cMatrix1 =3D columns(matrix1); %column of matrix 1 rMatrix2 =3D rows(matrix2); %row of matrix2 cMatrix2 =3D columns(matrix2); %column of matrix2 mRow =3D min(rMatrix1, rMatrix2); %min row of either matrix mCol =3D min(cMatrix1, cMatrix2); %min column of either matrix % calculate the size of the final matrix retRow =3D rMatrix1 + rMatrix2 - 1; retCol =3D cMatrix1 + cMatrix2 - 1; %declare the final matrix returnMatrix =3D zeros(retRow, retCol); % big X, Y loop for each element in the final matrix for x =3D 1:retRow %rInv is the additive complement of X. def: x+rInv =3D retRow % I think of it as an additive inverse in a modulo way. rInv =3D rMatrix1 + rMatrix2 - x; for y =3D 1:retCol % disp('calculating.. ') % x % y %cInv is the additive complement of Y. def: y+cInv =3D retCol cInv =3D cMatrix1 + cMatrix2 - y; %initialize the temporary sum tmpSum =3D 0; %find the number of additions needed for returnMatrix(x,y) % this turns out to be something like [1 2 3 3 2 1] % with the max value (4 in the example) to be defined as below mmX =3D min(min(x, mRow), rInv); mmY =3D min(min(y, mCol), cInv); %mini loops to perform the multiple Multiply ACcumulate(MAC) % for each element in the finalMatrix for tmpx =3D 1:mmX for tmpy =3D 1:mmY %calculate the max index to be used % it looks something like [1 2 3 3 3 3] mR1X =3D min(rMatrix1, x); mR2X =3D min(rMatrix2, x); mC1Y =3D min(cMatrix1, y); mC2Y =3D min(cMatrix2, y); % [x mC1Y - mmY + tmpy] % [x mC2Y - tmpy + 1] %the index calculations %we want the index to look like this % Y 1 2 3 4 5 6 % mmY 1 2 3 3 2 1 % mC1Y 1 2 3 3 3 3 % mat1 1 12 123 123 23 3 % mat2 1 21 321 432 43 4 x1Ind =3D mR1X - mmX + tmpx; x2Ind =3D mR2X - tmpx + 1; y1Ind =3D mC1Y - mmY + tmpy; y2Ind =3D mC2Y - tmpy + 1; %finally the MAC tmpSum =3D tmpSum + (matrix1(x1Ind, y1Ind) * matrix2(x2Ind, = y2Ind)); end end returnMatrix(x, y) =3D tmpSum; end end ------=_NextPart_000_0005_01C14579.94EFFE00 Content-Type: application/octet-stream; name="corr2d.m" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="corr2d.m" % Copyright at 2001 by Phillip Wang % % 9-25-2001 written and released under GNU General Public License % This program is free software; you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation; either version 2 of the License, or % (at your option) any later version. % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % You should have received a copy of the GNU General Public License % along with this program; if not, write to the Free Software % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 = USA ## -*- texinfo -*- ## at deftypefn {Function File} {} corr2d(@var{matrix1},@var{matrix2}) ## Return a 2D correlation of at var{matrix1} and @var{matrix2} ## at end deftypefn function returnMatrix =3D corr2d(matrix1, matrix2) %declare and intialize some useful variables rMatrix1 =3D rows(matrix1); %row of matrix1 cMatrix1 =3D columns(matrix1); %column of matrix 1 rMatrix2 =3D rows(matrix2); %row of matrix2 cMatrix2 =3D columns(matrix2); %column of matrix2 mRow =3D min(rMatrix1, rMatrix2); %min row of either matrix mCol =3D min(cMatrix1, cMatrix2); %min column of either matrix % calculate the size of the final matrix retRow =3D rMatrix1 + rMatrix2 - 1; retCol =3D cMatrix1 + cMatrix2 - 1; % determine the complex conjugate of matrix1 matrix1 =3D conj(matrix1); %declare the final matrix returnMatrix =3D zeros(retRow, retCol); % big X, Y loop for each element in the final matrix for x =3D 1:retRow rInv =3D rMatrix1 + rMatrix2 - x; for y =3D 1:retCol % disp('calculating.. ') % x % y cInv =3D cMatrix1 + cMatrix2 - y; %initialize the temporary sum tmpSum =3D 0; mmX =3D min(min(x, mRow), rInv); mmY =3D min(min(y, mCol), cInv); %mini loops to perform the multiple Multiply ACcumulate(MAC) % for each element in the finalMatrix for tmpx =3D 1:mmX for tmpy =3D 1:mmY %calculate the max index to be used % it looks something like [1 2 3 3 3 3] mR1X =3D min(rMatrix1, x); mR2X =3D min(rMatrix2, x); mC1Y =3D min(cMatrix1, y); mC2Y =3D min(cMatrix2, y); %the index calculations %we want the index to look like this % Y 1 2 3 4 5 6 % mmY 1 2 3 3 2 1 % mC1Y 1 2 3 3 3 3 % mat1 1 12 123 123 23 3 % mat2 1 21 321 432 43 4 x1Ind =3D mR1X - mmX + tmpx; x2Ind =3D rMatrix2 - mR2X + tmpx; y1Ind =3D mC1Y - mmY + tmpy; y2Ind =3D cMatrix2 - mC2Y + tmpy; % [x1Ind y1Ind] % [x2Ind y2Ind] %finally the MAC tmpSum =3D tmpSum + (matrix1(x1Ind, y1Ind) * matrix2(x2Ind, = y2Ind)); end end returnMatrix(x, y) =3D tmpSum; end end ------=_NextPart_000_0005_01C14579.94EFFE00--