1 | |
---|
2 | /* file catflf.c |
---|
3 | * ======== |
---|
4 | * |
---|
5 | * version 1, 14-May-2003 |
---|
6 | * |
---|
7 | * Multiplies / divides two filters and writes result to stdout |
---|
8 | * K. Stammler, 14-May-2003 |
---|
9 | */ |
---|
10 | |
---|
11 | |
---|
12 | /* |
---|
13 | * |
---|
14 | * SeismicHandler, seismic analysis software |
---|
15 | * Copyright (C) 1996, Klaus Stammler, Federal Institute for Geosciences |
---|
16 | * and Natural Resources (BGR), Germany |
---|
17 | * |
---|
18 | * This program is free software; you can redistribute it and/or modify |
---|
19 | * it under the terms of the GNU General Public License as published by |
---|
20 | * the Free Software Foundation; either version 2 of the License, or |
---|
21 | * (at your option) any later version. |
---|
22 | * |
---|
23 | * This program is distributed in the hope that it will be useful, |
---|
24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
26 | * GNU General Public License for more details. |
---|
27 | * |
---|
28 | * You should have received a copy of the GNU General Public License |
---|
29 | * along with this program; if not, write to the Free Software |
---|
30 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
---|
31 | * |
---|
32 | */ |
---|
33 | |
---|
34 | |
---|
35 | #include <stdio.h> |
---|
36 | #include <string.h> |
---|
37 | #include <math.h> |
---|
38 | #include "basecnst.h" |
---|
39 | #ifdef BC_INC_STDLIB |
---|
40 | #include BC_INC_STDLIB |
---|
41 | #endif |
---|
42 | #include "sysbase.h" |
---|
43 | #include "ffusrdef.h" |
---|
44 | #include "erusrdef.h" |
---|
45 | #include "globalparams.h" |
---|
46 | |
---|
47 | |
---|
48 | /* prototypes of local routines */ |
---|
49 | static void ctf_writeflf( FFT_RATFCT *fil ); |
---|
50 | |
---|
51 | |
---|
52 | /* global variables */ |
---|
53 | char shd_filter[BC_FILELTH+1]; |
---|
54 | char shd_scratch[BC_FILELTH+1]; |
---|
55 | |
---|
56 | |
---|
57 | int main( int argc, char *argv[] ) |
---|
58 | { |
---|
59 | /* local variables */ |
---|
60 | char oper[cBcLineLth+1]; /* operation */ |
---|
61 | char filnam1[cBcFileLth+1]; /* filter filename 1 */ |
---|
62 | char filnam2[cBcFileLth+1]; /* filter filename 2 */ |
---|
63 | char *eptr; /* pointer to environment */ |
---|
64 | TSyStatus status; /* return status */ |
---|
65 | FFT_RATFCT fil1; /* filter 1 */ |
---|
66 | FFT_RATFCT fil2; /* filter 2 */ |
---|
67 | FFT_RATFCT filr; /* resulting filter */ |
---|
68 | int i, j; /* counters */ |
---|
69 | |
---|
70 | /* executable code */ |
---|
71 | |
---|
72 | if (argc != 4) { |
---|
73 | fprintf( stderr, "Usage: %s <filter1> <operation> <filter2>\n", argv[0] ); |
---|
74 | return 1; |
---|
75 | } /*endif*/ |
---|
76 | |
---|
77 | GpReadParfile(); |
---|
78 | |
---|
79 | eptr = (char *)getenv( "SH_FILTER" ); |
---|
80 | if (eptr != NULL) { |
---|
81 | strcpy( shd_filter, eptr ); |
---|
82 | } else { |
---|
83 | *shd_filter = '\0'; |
---|
84 | } /*endif*/ |
---|
85 | |
---|
86 | /* get parameters */ |
---|
87 | strcpy( filnam1, argv[1] ); |
---|
88 | strcpy( oper, argv[2] ); |
---|
89 | strcpy( filnam2, argv[3] ); |
---|
90 | |
---|
91 | status = cBcNoError; |
---|
92 | ff_read_filter( filnam1, 1, &fil1, &status ); |
---|
93 | if (SySevere(&status)) err_writemsg( status, "", TRUE ); |
---|
94 | ff_read_filter( filnam2, 1, &fil2, &status ); |
---|
95 | if (SySevere(&status)) err_writemsg( status, "", TRUE ); |
---|
96 | |
---|
97 | if (strcmp(oper,"mul") == 0) { |
---|
98 | if (fil1.no_of_zeroes + fil2.no_of_zeroes > FFC_MAXDEGREE || |
---|
99 | fil1.no_of_poles + fil2.no_of_poles > FFC_MAXDEGREE) { |
---|
100 | fprintf( stderr, "%s: too many poles/zeros in output filter\n", |
---|
101 | argv[0] ); |
---|
102 | return 1; |
---|
103 | } /*endif*/ |
---|
104 | filr.norm = fil1.norm * fil2.norm; |
---|
105 | filr.no_of_zeroes = fil1.no_of_zeroes + fil2.no_of_zeroes; |
---|
106 | i = 0; |
---|
107 | for (j=0; j<fil1.no_of_zeroes; j++) filr.zero[i++] = fil1.zero[j]; |
---|
108 | for (j=0; j<fil2.no_of_zeroes; j++) filr.zero[i++] = fil2.zero[j]; |
---|
109 | filr.no_of_poles = fil1.no_of_poles + fil2.no_of_poles; |
---|
110 | i = 0; |
---|
111 | for (j=0; j<fil1.no_of_poles; j++) filr.pole[i++] = fil1.pole[j]; |
---|
112 | for (j=0; j<fil2.no_of_poles; j++) filr.pole[i++] = fil2.pole[j]; |
---|
113 | } else if (strcmp(oper,"div") == 0) { |
---|
114 | if (fil1.no_of_zeroes + fil2.no_of_poles > FFC_MAXDEGREE || |
---|
115 | fil1.no_of_poles + fil2.no_of_zeroes > FFC_MAXDEGREE) { |
---|
116 | fprintf( stderr, "%s: too many poles/zeros in output filter\n", |
---|
117 | argv[0] ); |
---|
118 | return 1; |
---|
119 | } /*endif*/ |
---|
120 | filr.norm = fil1.norm / fil2.norm; |
---|
121 | filr.no_of_zeroes = fil1.no_of_zeroes + fil2.no_of_poles; |
---|
122 | i = 0; |
---|
123 | for (j=0; j<fil1.no_of_zeroes; j++) filr.zero[i++] = fil1.zero[j]; |
---|
124 | for (j=0; j<fil2.no_of_poles; j++) filr.zero[i++] = fil2.pole[j]; |
---|
125 | filr.no_of_poles = fil1.no_of_poles + fil2.no_of_zeroes; |
---|
126 | i = 0; |
---|
127 | for (j=0; j<fil1.no_of_poles; j++) filr.pole[i++] = fil1.pole[j]; |
---|
128 | for (j=0; j<fil2.no_of_zeroes; j++) filr.pole[i++] = fil2.zero[j]; |
---|
129 | } else { |
---|
130 | fprintf( stderr, "%s: illegal operation %s\n", argv[0], oper ); |
---|
131 | return 1; |
---|
132 | } /*endif*/ |
---|
133 | |
---|
134 | ff_shorten_zeroes( &filr, 1 ); |
---|
135 | |
---|
136 | fprintf( stdout, "! filter produced by catflf\n" ); |
---|
137 | fprintf( stdout, "! %s %s %s\n", filnam1, oper, filnam2 ); |
---|
138 | ctf_writeflf( &filr ); |
---|
139 | |
---|
140 | return 0; |
---|
141 | |
---|
142 | } /* end of main */ |
---|
143 | |
---|
144 | |
---|
145 | /*----------------------------------------------------------------------------*/ |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | static void ctf_writeflf( FFT_RATFCT *fil ) |
---|
150 | |
---|
151 | /* writes filter to stdout |
---|
152 | * |
---|
153 | * parameters of routine |
---|
154 | * FFT_RATFCT *fil; input; filte to be written |
---|
155 | */ |
---|
156 | { |
---|
157 | /* local variables */ |
---|
158 | int i; /* counter */ |
---|
159 | |
---|
160 | /* executable code */ |
---|
161 | |
---|
162 | fprintf( stdout, "1357913578\n" ); |
---|
163 | fprintf( stdout, "1\n" ); |
---|
164 | fprintf( stdout, "%g\n", fil->norm ); |
---|
165 | fprintf( stdout, "%d\n", fil->no_of_zeroes ); |
---|
166 | for (i=0; i<(fil->no_of_zeroes); i++) |
---|
167 | fprintf( stdout, "(%g,%g)\n", fil->zero[i].re, fil->zero[i].im ); |
---|
168 | fprintf( stdout, "%d\n", fil->no_of_poles ); |
---|
169 | for (i=0; i<(fil->no_of_poles); i++) |
---|
170 | fprintf( stdout, "(%g,%g)\n", fil->pole[i].re, fil->pole[i].im ); |
---|
171 | |
---|
172 | } /* end of ctf_writeflf */ |
---|
173 | |
---|
174 | |
---|
175 | |
---|
176 | /*----------------------------------------------------------------------------*/ |
---|