|
| 1 | +/* Convection filter. */ |
| 2 | +/* |
| 3 | + Copyright (C) 2004 University of Texas at Austin |
| 4 | + |
| 5 | + This program is free software; you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU General Public License as published by |
| 7 | + the Free Software Foundation; either version 2 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | + |
| 10 | + This program is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | + |
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with this program; if not, write to the Free Software |
| 17 | + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | +*/ |
| 19 | +#include <math.h> |
| 20 | + |
| 21 | +#include <rsf.h> |
| 22 | + |
| 23 | +int main (int argc, char* argv[]) |
| 24 | +{ |
| 25 | + bool verb; |
| 26 | + int niter, n1, n2, n12, i1, i2, rect[2], m[2]; |
| 27 | + float **data, **rhs, ***conv, ***lhs, dif, mean; |
| 28 | + sf_file in, flt; |
| 29 | + |
| 30 | + sf_init(argc, argv); |
| 31 | + in = sf_input("in"); |
| 32 | + flt = sf_input("out"); |
| 33 | + |
| 34 | + if (SF_FLOAT != sf_gettype(in)) sf_error("Need float input"); |
| 35 | + if (!sf_histint(in,"n1",&n1)) sf_error("Need n1= in input"); |
| 36 | + if (!sf_histint(in,"n2",&n2)) sf_error("Need n2= in input"); |
| 37 | + |
| 38 | + sf_putint(flt, "n3", 2); |
| 39 | + |
| 40 | + if (!sf_getint("rect1",&rect[0])) rect[0]=1; |
| 41 | + if (!sf_getint("rect2",&rect[1])) rect[1]=1; |
| 42 | + /* smoothing */ |
| 43 | + |
| 44 | + if (!sf_getint("niter",&niter)) niter=100; |
| 45 | + /* number of iterations */ |
| 46 | + |
| 47 | + if (!sf_getbool("verb",&verb)) verb=true; |
| 48 | + /* verbosity flag */ |
| 49 | + |
| 50 | + data = sf_floatalloc2(n1,n2); |
| 51 | + rhs = sf_floatalloc2(n1,n2); |
| 52 | + |
| 53 | + conv = sf_floatalloc3(n1,n2,2); |
| 54 | + lhs = sf_floatalloc3(n1,n2,2); |
| 55 | + |
| 56 | + n12 = n1*n2; |
| 57 | + m[0] = n1; |
| 58 | + m[1] = n2; |
| 59 | + |
| 60 | + sf_multidivn_init(2, 2, n12, m, rect, **lhs, NULL, verb); |
| 61 | + |
| 62 | + sf_floatread(data[0],n12,in); |
| 63 | + |
| 64 | + mean = 0.; |
| 65 | + for (i2=0; i2 < n2-1; i2++) { |
| 66 | + rhs[i2][0] = 0.0f; |
| 67 | + lhs[0][i2][0] = 0.0f; |
| 68 | + lhs[1][i2][0] = 0.0f; |
| 69 | + for (i1=1; i1 < n1-1; i1++) { |
| 70 | + dif = data[i2+1][i1] - data[i2][i1]; |
| 71 | + rhs[i2][i1] = dif; |
| 72 | + lhs[0][i2][i1] = dif - data[i2+1][i1+1] + data[i2][i1-1]; |
| 73 | + lhs[1][i2][i1] = dif - data[i2+1][i1-1] + data[i2][i1+1]; |
| 74 | + mean += lhs[0][i2][i1]*lhs[0][i2][i1] + lhs[1][i2][i1]*lhs[1][i2][i1]; |
| 75 | + } |
| 76 | + rhs[i2][n1-1] = 0.0f; |
| 77 | + lhs[0][i2][n1-1] = 0.0f; |
| 78 | + lhs[1][i2][n1-1] = 0.0f; |
| 79 | + } |
| 80 | + for (i1=0; i1 < n1; i1++) { |
| 81 | + rhs[n2-1][i1] = 0.0f; |
| 82 | + lhs[0][n2-1][i1] = 0.0f; |
| 83 | + lhs[1][n2-1][i1] = 0.0f; |
| 84 | + } |
| 85 | + mean = sqrtf (mean/(n12*2)); |
| 86 | + for (i2=0; i2 < n2-1; i2++) { |
| 87 | + for (i1=1; i1 < n1-1; i1++) { |
| 88 | + rhs[i2][i1] /= mean; |
| 89 | + lhs[0][i2][i1] /= mean; |
| 90 | + lhs[1][i2][i1] /= mean; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + sf_multidivn (*rhs,**conv,niter); |
| 95 | + sf_floatwrite(**conv,n12*2,flt); |
| 96 | + |
| 97 | + exit(0); |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | + |
0 commit comments