1 /* Simple Median Filter
2 
3  * Copyright (C) 1998 J.A. Bezemer
4  *
5  * Licensed under the terms of the GNU General Public License.
6  * ABSOLUTELY NO WARRANTY.
7  * See the file `COPYING' in this directory.
8  */
9 
10 #include "signpr_median.h"
11 #include "signpr_general.h"
12 #ifndef SWIG
13 #include "errorwindow.h"
14 #include "stringinput.h"
15 #include "buttons.h"
16 #include "clrscr.h"
17 #include "boxes.h"
18 #include "helpline.h"
19 #endif
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #ifndef SWIG
24 #ifndef OLD_CURSES
25 #include <ncurses.h>
26 #else
27 #include <curses.h>
28 #endif
29 #endif
30 
31 
32 void
simple_median_param_defaults(parampointer_t parampointer)33 simple_median_param_defaults (parampointer_t parampointer)
34 {
35   parampointer->postlength1 = 1;
36   parampointer->prelength1 = 1;
37 }
38 
39 #ifndef SWIG
40 void
simple_median_param_screen(parampointer_t parampointer)41 simple_median_param_screen (parampointer_t parampointer)
42 {
43   stringinput_t medlengthstr;
44   button_t ok_button, cancel_button;
45   int dont_stop = TRUE;
46   int returnval = 0;
47   int focus = 0;
48   int in_ch;
49   int i;
50   long helplong;
51 
52   char *helplines[3] =
53   {
54     " ^: broader ticks removed.              v: less distortion.                    ",
55     " Discard changes.                                                              ",
56     " Accept changes.                                                               "};
57 
58   medlengthstr.maxlen = 500;
59   medlengthstr.string = (char *) malloc (medlengthstr.maxlen *
60 					 sizeof (char));
61   sprintf (medlengthstr.string, "%ld", parampointer->prelength1 +
62 	   parampointer->postlength1 + 1);
63   medlengthstr.y = 6;
64   medlengthstr.x = 44;
65   medlengthstr.w = 19;
66   medlengthstr.cursorpos = strlen (medlengthstr.string);
67   medlengthstr.firstcharonscreen = strlen (medlengthstr.string) -
68     medlengthstr.w + 2;
69   if (medlengthstr.firstcharonscreen < 0)
70     medlengthstr.firstcharonscreen = 0;
71 
72   ok_button.text = " OK ";
73   ok_button.y = 20;
74   ok_button.x = 71;
75   ok_button.selected = FALSE;
76 
77   cancel_button.text = " Cancel ";
78   cancel_button.y = 20;
79   cancel_button.x = 5;
80   cancel_button.selected = FALSE;
81 
82   clearscreen (SIGNPR_MEDIAN_PARAMSCR_HEADERTEXT);
83 
84   do
85     {
86       header (SIGNPR_MEDIAN_PARAMSCR_HEADERTEXT);
87 
88       if (focus == 1)
89 	cancel_button.selected = TRUE;
90       else
91 	cancel_button.selected = FALSE;
92 
93       if (focus == 2)
94 	ok_button.selected = TRUE;
95       else
96 	ok_button.selected = FALSE;
97 
98       mvprintw (3, 2,
99 	    "See the Signproc.txt file for the meaning of the parameters.");
100 
101       stringinput_display (&medlengthstr);
102       mvprintw (medlengthstr.y, 2,
103 		"Number of samples to take the median of:");
104 
105       button_display (&cancel_button);
106       mybox (cancel_button.y - 1, cancel_button.x - 1,
107 	     3, strlen (cancel_button.text) + 2);
108       button_display (&ok_button);
109       mybox (ok_button.y - 1, ok_button.x - 1,
110 	     3, strlen (ok_button.text) + 2);
111 
112       helpline (helplines[focus]);
113 
114       if (focus == 0)
115 	stringinput_display (&medlengthstr);
116       else
117 	move (0, 79);
118 
119       refresh ();
120 
121       in_ch = getch ();
122 
123       switch (focus)
124 	{
125 	case 0:		/* medlengthstr */
126 	  stringinput_stdkeys (in_ch, &medlengthstr);
127 	  if (in_ch == KEY_ENTER || in_ch == 13)
128 	    {
129 	      i = sscanf (medlengthstr.string, "%li", &helplong);
130 	      if (i < 1 || helplong < 1 || helplong % 2 == 0)
131 		error_window ("A whole, odd number, greater than 0, must \
132 be specified.");
133 	      else
134 		focus = 2;
135 	    }
136 	  else
137 	    switch (in_ch)
138 	      {
139 	      case KEY_UP:
140 		focus--;
141 		break;
142 	      case KEY_DOWN:
143 		focus++;
144 		break;
145 	      }
146 	  break;
147 
148 	case 1:		/* Cancel */
149 	  if (in_ch == KEY_ENTER || in_ch == 13)
150 	    {
151 	      returnval = 0;
152 	      dont_stop = FALSE;
153 	    }
154 	  else
155 	    switch (in_ch)
156 	      {
157 	      case KEY_LEFT:
158 	      case KEY_UP:
159 		focus--;
160 		break;
161 	      case KEY_RIGHT:
162 	      case KEY_DOWN:
163 		focus++;
164 		break;
165 	      }
166 	  break;
167 
168 	case 2:		/* OK */
169 	  if (in_ch == KEY_ENTER || in_ch == 13)
170 	    {
171 	      i = sscanf (medlengthstr.string, "%li", &helplong);
172 	      if (i < 1 || helplong < 1 || helplong % 2 == 0)
173 		{
174 		  error_window ("A whole, odd number, greater than 0, must \
175 be specified as median length.");
176 		  medlengthstr.cursorpos =
177 		    strlen (medlengthstr.string);
178 		  focus = 0;
179 		}
180 	      else
181 		{
182 		  parampointer->prelength1 = (helplong - 1) / 2;
183 		  parampointer->postlength1 = (helplong - 1) / 2;
184 		  dont_stop = FALSE;
185 		}
186 	    }
187 	  else
188 	    switch (in_ch)
189 	      {
190 	      case KEY_LEFT:
191 	      case KEY_UP:
192 		focus--;
193 		break;
194 	      case KEY_RIGHT:
195 	      case KEY_DOWN:
196 		focus++;
197 		break;
198 	      }
199 	  break;
200 	}
201 
202       if (in_ch == 9)		/* TAB */
203 	focus++;
204 
205       if (in_ch == 27)
206 	dont_stop = FALSE;
207 
208       if (focus > 2)
209 	focus -= 3;
210       if (focus < 0)
211 	focus += 3;
212     }
213   while (dont_stop);
214 
215   free (medlengthstr.string);
216 }
217 #endif
218 
219 void
init_simple_median_filter(int filterno,parampointer_t parampointer)220 init_simple_median_filter (int filterno, parampointer_t parampointer)
221 {
222   parampointer->buffer = init_buffer (parampointer->postlength1,
223 				      parampointer->prelength1);
224 
225   parampointer->filterno = filterno;
226 
227   parampointer->sslist1 = (signed short *) malloc
228     ((parampointer->postlength1 +
229       parampointer->prelength1 + 1)
230      * sizeof (signed short));
231 
232   parampointer->sslist2 = (signed short *) malloc
233     ((parampointer->postlength1 +
234       parampointer->prelength1 + 1)
235      * sizeof (signed short));
236 }
237 
238 void
delete_simple_median_filter(parampointer_t parampointer)239 delete_simple_median_filter (parampointer_t parampointer)
240 {
241   delete_buffer (&parampointer->buffer);
242   free (parampointer->sslist1);
243   free (parampointer->sslist2);
244 }
245 
246 
247 sample_t
248 #ifndef SWIG
simple_median_filter(parampointer_t parampointer)249 simple_median_filter (parampointer_t parampointer)
250 #else
251 simple_median_filter (parampointer_t parampointer, int *filter_type)
252 #endif
253 {
254   sample_t sample;
255   long i;
256 
257 #ifndef SWIG
258   advance_current_pos (&parampointer->buffer, parampointer->filterno);
259 #else
260   advance_current_pos (&parampointer->buffer, parampointer->filterno, filter_type);
261 #endif
262 
263   for (i = 0; i <= parampointer->postlength1 + parampointer->prelength1;
264        i++)
265     {
266       sample = get_from_buffer (&parampointer->buffer,
267 				i - parampointer->postlength1);
268       parampointer->sslist1[i] = sample.left;
269       parampointer->sslist2[i] = sample.right;
270     }
271 
272   sample.left = median (parampointer->sslist1,
273 			parampointer->postlength1 +
274 			parampointer->prelength1 + 1);
275   sample.right = median (parampointer->sslist2,
276 			 parampointer->postlength1 +
277 			 parampointer->prelength1 + 1);
278 
279   return sample;
280 }
281