| 1 |
/* Conditional Median Filter - Better Version |
| 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 |
/* Remove the `dont' to get the gate instead of the normal output - useful |
| 11 |
for verifying properties. */ |
| 12 |
#define dontVIEW_INTERNALS |
| 13 |
|
| 14 |
/* Choose the highpass filter: */ |
| 15 |
#define noSECOND_ORDER |
| 16 |
#define FOURTH_ORDER |
| 17 |
#define noSIXTH_ORDER |
| 18 |
|
| 19 |
/* To use the (slow) fancy interpolation routine, define FANCY_FILL here */ |
| 20 |
#define noFANCY_FILL |
| 21 |
|
| 22 |
|
| 23 |
#include "signpr_cmf2.h" |
| 24 |
#include "signpr_general.h" |
| 25 |
#include "signpr_l1fit.h" |
| 26 |
#include "errorwindow.h" |
| 27 |
#include "stringinput.h" |
| 28 |
#include "buttons.h" |
| 29 |
#include "clrscr.h" |
| 30 |
#include "boxes.h" |
| 31 |
#include "helpline.h" |
| 32 |
#include "yesnowindow.h" |
| 33 |
#include <stdlib.h> |
| 34 |
#include <stdio.h> |
| 35 |
#include <math.h> |
| 36 |
#ifndef OLD_CURSES |
| 37 |
#include <ncurses.h> |
| 38 |
#else |
| 39 |
#include <curses.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
|
| 43 |
void |
| 44 |
cond_median2_param_defaults (parampointer_t parampointer) |
| 45 |
{ |
| 46 |
parampointer->postlength2 = 4; |
| 47 |
parampointer->prelength2 = 4; |
| 48 |
parampointer->postlength3 = 5; |
| 49 |
parampointer->prelength3 = 5; |
| 50 |
parampointer->postlength4 = 100; |
| 51 |
parampointer->prelength4 = 100; |
| 52 |
parampointer->int1 = 12; |
| 53 |
#if defined (SECOND_ORDER) |
| 54 |
parampointer->long1 = 1000; /* Threshold to detect precise tick length. */ |
| 55 |
parampointer->long2 = 2500; /* Must be above this to be a tick. */ |
| 56 |
#elif defined (FOURTH_ORDER) |
| 57 |
parampointer->long1 = 2000; |
| 58 |
parampointer->long2 = 8500; |
| 59 |
#elif defined (SIXTH_ORDER) |
| 60 |
parampointer->long1 = 1500; |
| 61 |
parampointer->long2 = 7500; |
| 62 |
#else |
| 63 |
#error A Highpass version must be defined (signpr_cmf2.c) |
| 64 |
#endif |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
#ifdef FOURTH_ORDER |
| 70 |
#undef SIGNPR_CMF2_PARAMSCR_HEADERTEXT |
| 71 |
#define SIGNPR_CMF2_PARAMSCR_HEADERTEXT "CMF II [FOURTH ORDER] - Parameters" |
| 72 |
#endif |
| 73 |
|
| 74 |
#ifdef SIXTH_ORDER |
| 75 |
#undef SIGNPR_CMF2_PARAMSCR_HEADERTEXT |
| 76 |
#define SIGNPR_CMF2_PARAMSCR_HEADERTEXT "CMF II [SIXTH ORDER] - Parameters" |
| 77 |
#endif |
| 78 |
|
| 79 |
void |
| 80 |
cond_median2_param_screen (parampointer_t parampointer) |
| 81 |
{ |
| 82 |
stringinput_t rmslengthstr, rmflengthstr, decimatestr, threshold1str, |
| 83 |
threshold2str; |
| 84 |
button_t ok_button, cancel_button, defaults_button; |
| 85 |
int dont_stop = TRUE; |
| 86 |
int focus = 0; |
| 87 |
int in_ch; |
| 88 |
int i; |
| 89 |
long helplong; |
| 90 |
|
| 91 |
char *helplines[8] = |
| 92 |
{ |
| 93 |
" ^: less ticks detected. v: not all of tick interpolated. ", |
| 94 |
" ^: bad following of dynamics. v: less ticks detected. ", |
| 95 |
" ^: bad following of dynamics. v: less ticks detected. ", |
| 96 |
" ^: detected tick length too short v: detected tick length longer. ", |
| 97 |
" ^: only strong ticks detected. v: music-ticks also filtered out. ", |
| 98 |
" Discard changes. ", |
| 99 |
" Reset default values. ", |
| 100 |
" Accept changes. "}; |
| 101 |
|
| 102 |
rmslengthstr.maxlen = 500; |
| 103 |
rmslengthstr.string = (char *) malloc (rmslengthstr.maxlen * |
| 104 |
sizeof (char)); |
| 105 |
sprintf (rmslengthstr.string, "%ld", parampointer->prelength2 + |
| 106 |
parampointer->postlength2 + 1); |
| 107 |
rmslengthstr.y = 6; |
| 108 |
rmslengthstr.x = 59; |
| 109 |
rmslengthstr.w = 19; |
| 110 |
rmslengthstr.cursorpos = strlen (rmslengthstr.string); |
| 111 |
rmslengthstr.firstcharonscreen = 0; |
| 112 |
|
| 113 |
rmflengthstr.maxlen = 500; |
| 114 |
rmflengthstr.string = (char *) malloc (rmflengthstr.maxlen * |
| 115 |
sizeof (char)); |
| 116 |
sprintf (rmflengthstr.string, "%ld", parampointer->prelength3 + |
| 117 |
parampointer->postlength3 + 1); |
| 118 |
rmflengthstr.y = 8; |
| 119 |
rmflengthstr.x = 59; |
| 120 |
rmflengthstr.w = 19; |
| 121 |
rmflengthstr.cursorpos = strlen (rmflengthstr.string); |
| 122 |
rmflengthstr.firstcharonscreen = 0; |
| 123 |
|
| 124 |
decimatestr.maxlen = 500; |
| 125 |
decimatestr.string = (char *) malloc (decimatestr.maxlen * |
| 126 |
sizeof (char)); |
| 127 |
sprintf (decimatestr.string, "%d", parampointer->int1); |
| 128 |
decimatestr.y = 10; |
| 129 |
decimatestr.x = 59; |
| 130 |
decimatestr.w = 19; |
| 131 |
decimatestr.cursorpos = strlen (decimatestr.string); |
| 132 |
decimatestr.firstcharonscreen = 0; |
| 133 |
|
| 134 |
threshold1str.maxlen = 500; |
| 135 |
threshold1str.string = (char *) malloc (threshold1str.maxlen * |
| 136 |
sizeof (char)); |
| 137 |
sprintf (threshold1str.string, "%ld", parampointer->long1); |
| 138 |
threshold1str.y = 12; |
| 139 |
threshold1str.x = 59; |
| 140 |
threshold1str.w = 19; |
| 141 |
threshold1str.cursorpos = strlen (threshold1str.string); |
| 142 |
threshold1str.firstcharonscreen = 0; |
| 143 |
|
| 144 |
threshold2str.maxlen = 500; |
| 145 |
threshold2str.string = (char *) malloc (threshold2str.maxlen * |
| 146 |
sizeof (char)); |
| 147 |
sprintf (threshold2str.string, "%ld", parampointer->long2); |
| 148 |
threshold2str.y = 14; |
| 149 |
threshold2str.x = 59; |
| 150 |
threshold2str.w = 19; |
| 151 |
threshold2str.cursorpos = strlen (threshold2str.string); |
| 152 |
threshold2str.firstcharonscreen = 0; |
| 153 |
|
| 154 |
ok_button.text = " OK "; |
| 155 |
ok_button.y = 20; |
| 156 |
ok_button.x = 71; |
| 157 |
ok_button.selected = FALSE; |
| 158 |
|
| 159 |
cancel_button.text = " Cancel "; |
| 160 |
cancel_button.y = 20; |
| 161 |
cancel_button.x = 5; |
| 162 |
cancel_button.selected = FALSE; |
| 163 |
|
| 164 |
defaults_button.text = " Defaults "; |
| 165 |
defaults_button.y = 20; |
| 166 |
defaults_button.x = 36; |
| 167 |
defaults_button.selected = FALSE; |
| 168 |
|
| 169 |
clearscreen (SIGNPR_CMF2_PARAMSCR_HEADERTEXT); |
| 170 |
|
| 171 |
do |
| 172 |
{ |
| 173 |
header (SIGNPR_CMF2_PARAMSCR_HEADERTEXT); |
| 174 |
|
| 175 |
if (focus == 5) |
| 176 |
cancel_button.selected = TRUE; |
| 177 |
else |
| 178 |
cancel_button.selected = FALSE; |
| 179 |
|
| 180 |
if (focus == 6) |
| 181 |
defaults_button.selected = TRUE; |
| 182 |
else |
| 183 |
defaults_button.selected = FALSE; |
| 184 |
|
| 185 |
if (focus == 7) |
| 186 |
ok_button.selected = TRUE; |
| 187 |
else |
| 188 |
ok_button.selected = FALSE; |
| 189 |
|
| 190 |
mvprintw (3, 2, |
| 191 |
"See also the Signproc.txt file for the meaning of the parameters."); |
| 192 |
|
| 193 |
stringinput_display (&rmslengthstr); |
| 194 |
mvprintw (rmslengthstr.y, 2, |
| 195 |
"Length of the RMS operation (samples):"); |
| 196 |
|
| 197 |
stringinput_display (&rmflengthstr); |
| 198 |
mvprintw (rmflengthstr.y, 2, |
| 199 |
"Length of the recursive median operation (samples):"); |
| 200 |
|
| 201 |
stringinput_display (&decimatestr); |
| 202 |
mvprintw (decimatestr.y, 2, |
| 203 |
"Decimation factor for the recursive median:"); |
| 204 |
|
| 205 |
stringinput_display (&threshold1str); |
| 206 |
mvprintw (threshold1str.y, 2, |
| 207 |
"Fine threshold for tick start/end (thousandths):"); |
| 208 |
|
| 209 |
stringinput_display (&threshold2str); |
| 210 |
mvprintw (threshold2str.y, 2, |
| 211 |
"Threshold for detection of tick presence (thousandths):"); |
| 212 |
|
| 213 |
button_display (&cancel_button); |
| 214 |
mybox (cancel_button.y - 1, cancel_button.x - 1, |
| 215 |
3, strlen (cancel_button.text) + 2); |
| 216 |
button_display (&defaults_button); |
| 217 |
mybox (defaults_button.y - 1, defaults_button.x - 1, |
| 218 |
3, strlen (defaults_button.text) + 2); |
| 219 |
button_display (&ok_button); |
| 220 |
mybox (ok_button.y - 1, ok_button.x - 1, |
| 221 |
3, strlen (ok_button.text) + 2); |
| 222 |
|
| 223 |
helpline (helplines[focus]); |
| 224 |
|
| 225 |
switch (focus) |
| 226 |
{ |
| 227 |
case 0: |
| 228 |
stringinput_display (&rmslengthstr); |
| 229 |
break; |
| 230 |
case 1: |
| 231 |
stringinput_display (&rmflengthstr); |
| 232 |
break; |
| 233 |
case 2: |
| 234 |
stringinput_display (&decimatestr); |
| 235 |
break; |
| 236 |
case 3: |
| 237 |
stringinput_display (&threshold1str); |
| 238 |
break; |
| 239 |
case 4: |
| 240 |
stringinput_display (&threshold2str); |
| 241 |
break; |
| 242 |
default: |
| 243 |
move (0, 79); |
| 244 |
} |
| 245 |
|
| 246 |
refresh (); |
| 247 |
|
| 248 |
in_ch = getch (); |
| 249 |
|
| 250 |
switch (focus) |
| 251 |
{ |
| 252 |
case 0: /* rmslengthstr */ |
| 253 |
stringinput_stdkeys (in_ch, &rmslengthstr); |
| 254 |
switch (in_ch) |
| 255 |
{ |
| 256 |
case KEY_ENTER: |
| 257 |
case 13: |
| 258 |
i = sscanf (rmslengthstr.string, "%li", &helplong); |
| 259 |
if (i < 1 || helplong < 1 || helplong % 2 == 0) |
| 260 |
error_window ("A whole, odd number, greater than 0, must \ |
| 261 |
be specified."); |
| 262 |
else |
| 263 |
focus++; |
| 264 |
break; |
| 265 |
|
| 266 |
case KEY_UP: |
| 267 |
focus--; |
| 268 |
break; |
| 269 |
case KEY_DOWN: |
| 270 |
focus++; |
| 271 |
break; |
| 272 |
} |
| 273 |
break; |
| 274 |
|
| 275 |
case 1: /* rmflengthstr */ |
| 276 |
stringinput_stdkeys (in_ch, &rmflengthstr); |
| 277 |
switch (in_ch) |
| 278 |
{ |
| 279 |
case KEY_ENTER: |
| 280 |
case 13: |
| 281 |
i = sscanf (rmflengthstr.string, "%li", &helplong); |
| 282 |
if (i < 1 || helplong < 1 || helplong % 2 == 0) |
| 283 |
error_window ("A whole, odd number, greater than 0, must \ |
| 284 |
be specified."); |
| 285 |
else |
| 286 |
focus++; |
| 287 |
break; |
| 288 |
|
| 289 |
case KEY_UP: |
| 290 |
focus--; |
| 291 |
break; |
| 292 |
case KEY_DOWN: |
| 293 |
focus++; |
| 294 |
break; |
| 295 |
} |
| 296 |
break; |
| 297 |
|
| 298 |
case 2: /* decimatestr */ |
| 299 |
stringinput_stdkeys (in_ch, &decimatestr); |
| 300 |
switch (in_ch) |
| 301 |
{ |
| 302 |
case KEY_ENTER: |
| 303 |
case 13: |
| 304 |
i = sscanf (decimatestr.string, "%li", &helplong); |
| 305 |
if (i < 1 || helplong < 1) |
| 306 |
error_window ("A whole number, greater than 0, must \ |
| 307 |
be specified."); |
| 308 |
else |
| 309 |
focus++; |
| 310 |
break; |
| 311 |
|
| 312 |
case KEY_UP: |
| 313 |
focus--; |
| 314 |
break; |
| 315 |
case KEY_DOWN: |
| 316 |
focus++; |
| 317 |
break; |
| 318 |
} |
| 319 |
break; |
| 320 |
|
| 321 |
case 3: /* threshold1str */ |
| 322 |
stringinput_stdkeys (in_ch, &threshold1str); |
| 323 |
switch (in_ch) |
| 324 |
{ |
| 325 |
case KEY_ENTER: |
| 326 |
case 13: |
| 327 |
i = sscanf (threshold1str.string, "%li", &helplong); |
| 328 |
if (i < 1 || helplong < 1) |
| 329 |
error_window ("A whole, positive number must be specified."); |
| 330 |
else |
| 331 |
focus++; |
| 332 |
break; |
| 333 |
|
| 334 |
case KEY_UP: |
| 335 |
focus--; |
| 336 |
break; |
| 337 |
case KEY_DOWN: |
| 338 |
focus++; |
| 339 |
break; |
| 340 |
} |
| 341 |
break; |
| 342 |
|
| 343 |
case 4: /* threshold2str */ |
| 344 |
stringinput_stdkeys (in_ch, &threshold2str); |
| 345 |
switch (in_ch) |
| 346 |
{ |
| 347 |
case KEY_ENTER: |
| 348 |
case 13: |
| 349 |
i = sscanf (threshold2str.string, "%li", &helplong); |
| 350 |
if (i < 1 || helplong < 1) |
| 351 |
error_window ("A whole, positive number must be specified."); |
| 352 |
else |
| 353 |
focus = 7; |
| 354 |
break; |
| 355 |
|
| 356 |
case KEY_UP: |
| 357 |
focus--; |
| 358 |
break; |
| 359 |
case KEY_DOWN: |
| 360 |
focus++; |
| 361 |
break; |
| 362 |
} |
| 363 |
break; |
| 364 |
|
| 365 |
case 5: /* Cancel */ |
| 366 |
switch (in_ch) |
| 367 |
{ |
| 368 |
case KEY_ENTER: |
| 369 |
case 13: |
| 370 |
dont_stop = FALSE; |
| 371 |
break; |
| 372 |
|
| 373 |
case KEY_LEFT: |
| 374 |
case KEY_UP: |
| 375 |
focus--; |
| 376 |
break; |
| 377 |
case KEY_RIGHT: |
| 378 |
case KEY_DOWN: |
| 379 |
focus++; |
| 380 |
break; |
| 381 |
} |
| 382 |
break; |
| 383 |
|
| 384 |
case 6: /* Defaults */ |
| 385 |
switch (in_ch) |
| 386 |
{ |
| 387 |
case KEY_ENTER: |
| 388 |
case 13: |
| 389 |
if (yesno_window ("Restore default parameters?", " Yes ", |
| 390 |
" No ", 0)) |
| 391 |
{ |
| 392 |
cond_median2_param_defaults (parampointer); |
| 393 |
dont_stop = FALSE; |
| 394 |
} |
| 395 |
break; |
| 396 |
|
| 397 |
case KEY_LEFT: |
| 398 |
case KEY_UP: |
| 399 |
focus--; |
| 400 |
break; |
| 401 |
case KEY_RIGHT: |
| 402 |
case KEY_DOWN: |
| 403 |
focus++; |
| 404 |
break; |
| 405 |
} |
| 406 |
break; |
| 407 |
|
| 408 |
case 7: /* OK */ |
| 409 |
switch (in_ch) |
| 410 |
{ |
| 411 |
case KEY_ENTER: |
| 412 |
case 13: |
| 413 |
|
| 414 |
i = sscanf (rmslengthstr.string, "%li", &helplong); |
| 415 |
if (i < 1 || helplong < 1 || helplong % 2 == 0) |
| 416 |
{ |
| 417 |
error_window ("A whole, odd number, greater than 0, must \ |
| 418 |
be specified as RMS length."); |
| 419 |
rmslengthstr.cursorpos = |
| 420 |
strlen (rmslengthstr.string); |
| 421 |
focus = 0; |
| 422 |
break; |
| 423 |
} |
| 424 |
|
| 425 |
parampointer->prelength2 = (helplong - 1) / 2; |
| 426 |
parampointer->postlength2 = (helplong - 1) / 2; |
| 427 |
|
| 428 |
i = sscanf (rmflengthstr.string, "%li", &helplong); |
| 429 |
if (i < 1 || helplong < 1 || helplong % 2 == 0) |
| 430 |
{ |
| 431 |
error_window ("A whole, odd number, greater than 0, must \ |
| 432 |
be specified as length of the recursive median."); |
| 433 |
rmflengthstr.cursorpos = |
| 434 |
strlen (rmflengthstr.string); |
| 435 |
focus = 1; |
| 436 |
break; |
| 437 |
} |
| 438 |
|
| 439 |
parampointer->prelength3 = (helplong - 1) / 2; |
| 440 |
parampointer->postlength3 = (helplong - 1) / 2; |
| 441 |
|
| 442 |
i = sscanf (decimatestr.string, "%li", &helplong); |
| 443 |
if (i < 1 || helplong < 1) |
| 444 |
{ |
| 445 |
error_window ("A whole number, greater than 0, must \ |
| 446 |
be specified as decimation factor."); |
| 447 |
decimatestr.cursorpos = |
| 448 |
strlen (decimatestr.string); |
| 449 |
focus = 2; |
| 450 |
break; |
| 451 |
} |
| 452 |
|
| 453 |
parampointer->int1 = helplong; |
| 454 |
|
| 455 |
i = sscanf (threshold1str.string, "%li", &helplong); |
| 456 |
if (i < 1 || helplong < 1) |
| 457 |
{ |
| 458 |
error_window ("A whole, positive number must be \ |
| 459 |
specified as threshold."); |
| 460 |
threshold1str.cursorpos = |
| 461 |
strlen (threshold1str.string); |
| 462 |
focus = 3; |
| 463 |
break; |
| 464 |
} |
| 465 |
|
| 466 |
parampointer->long1 = helplong; |
| 467 |
|
| 468 |
i = sscanf (threshold2str.string, "%li", &helplong); |
| 469 |
if (i < 1 || helplong < 1000) |
| 470 |
{ |
| 471 |
error_window ("A whole, positive number must be \ |
| 472 |
specified as threshold."); |
| 473 |
threshold2str.cursorpos = |
| 474 |
strlen (threshold2str.string); |
| 475 |
focus = 4; |
| 476 |
break; |
| 477 |
} |
| 478 |
|
| 479 |
parampointer->long2 = helplong; |
| 480 |
|
| 481 |
dont_stop = FALSE; |
| 482 |
break; |
| 483 |
|
| 484 |
case KEY_LEFT: |
| 485 |
case KEY_UP: |
| 486 |
focus--; |
| 487 |
break; |
| 488 |
case KEY_RIGHT: |
| 489 |
case KEY_DOWN: |
| 490 |
focus++; |
| 491 |
break; |
| 492 |
} |
| 493 |
break; |
| 494 |
} |
| 495 |
|
| 496 |
if (in_ch == 9) /* TAB */ |
| 497 |
focus++; |
| 498 |
|
| 499 |
if (in_ch == 27) |
| 500 |
dont_stop = FALSE; |
| 501 |
|
| 502 |
if (focus > 7) |
| 503 |
focus = 0; |
| 504 |
if (focus < 0) |
| 505 |
focus = 7; |
| 506 |
} |
| 507 |
while (dont_stop); |
| 508 |
|
| 509 |
free (rmslengthstr.string); |
| 510 |
free (rmflengthstr.string); |
| 511 |
free (decimatestr.string); |
| 512 |
free (threshold1str.string); |
| 513 |
free (threshold2str.string); |
| 514 |
} |
| 515 |
|
| 516 |
void |
| 517 |
init_cond_median2_filter (int filterno, parampointer_t parampointer) |
| 518 |
{ |
| 519 |
long total_post; |
| 520 |
long total_pre; |
| 521 |
long l; |
| 522 |
|
| 523 |
total_post = parampointer->postlength4 + parampointer->prelength4 + 1 + 4; |
| 524 |
/* +1+4=+5 : for highpass, max. 11th order */ |
| 525 |
|
| 526 |
total_pre = parampointer->postlength4 + parampointer->prelength4 + 1; |
| 527 |
l = parampointer->prelength4 + parampointer->prelength3 * |
| 528 |
parampointer->int1 + parampointer->prelength2 + 5; |
| 529 |
/* + 5 : for highpass, max. 11th order */ |
| 530 |
if (l > total_pre) |
| 531 |
total_pre = l; |
| 532 |
|
| 533 |
parampointer->buffer = init_buffer (total_post, total_pre); |
| 534 |
parampointer->buffer2 = init_buffer (parampointer->postlength2, |
| 535 |
parampointer->prelength2); |
| 536 |
parampointer->buffer3 = init_buffer (parampointer->postlength3, |
| 537 |
parampointer->prelength3 * parampointer->int1); |
| 538 |
parampointer->buffer4 = init_buffer (parampointer->postlength4, |
| 539 |
parampointer->prelength4); |
| 540 |
|
| 541 |
parampointer->filterno = filterno; |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
void |
| 546 |
delete_cond_median2_filter (parampointer_t parampointer) |
| 547 |
{ |
| 548 |
delete_buffer (¶mpointer->buffer); |
| 549 |
delete_buffer (¶mpointer->buffer2); |
| 550 |
delete_buffer (¶mpointer->buffer3); |
| 551 |
delete_buffer (¶mpointer->buffer4); |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
sample_t |
| 556 |
cond_median2_highpass (long offset, long offset_zero, |
| 557 |
parampointer_t parampointer) |
| 558 |
{ |
| 559 |
sample_t sample; |
| 560 |
longsample_t sum; |
| 561 |
|
| 562 |
offset += offset_zero; /* middle for highpass filter in |
| 563 |
'big buffer' */ |
| 564 |
sum.left = 0; |
| 565 |
sum.right = 0; |
| 566 |
|
| 567 |
#if defined (SECOND_ORDER) |
| 568 |
#define notTEST_DAVE_PLATT |
| 569 |
#ifndef TEST_DAVE_PLATT |
| 570 |
/* Original: */ |
| 571 |
sample = get_from_buffer (¶mpointer->buffer, offset - 1); |
| 572 |
sum.left += sample.left; |
| 573 |
sum.right += sample.right; |
| 574 |
sample = get_from_buffer (¶mpointer->buffer, offset); |
| 575 |
sum.left -= 2 * (long) sample.left; |
| 576 |
sum.right -= 2 * (long) sample.right; |
| 577 |
sample = get_from_buffer (¶mpointer->buffer, offset + 1); |
| 578 |
sum.left += sample.left; |
| 579 |
sum.right += sample.right; |
| 580 |
|
| 581 |
sum.left /= 4; |
| 582 |
sum.right /= 4; |
| 583 |
#else /* TEST_DAVE_PLATT */ |
| 584 |
/* Testing, suggested by Dave Platt. Invert phase of one channel, then |
| 585 |
do tick detection using the sum signal. This is because most ticks |
| 586 |
are out-of-phase signals. I've not really tested this - it might |
| 587 |
require other settings for thresholds etc. |
| 588 |
Note: implemented for second_order only! */ |
| 589 |
sample = get_from_buffer (¶mpointer->buffer, offset - 1); |
| 590 |
sum.left += sample.left; |
| 591 |
sum.left -= sample.right; |
| 592 |
sample = get_from_buffer (¶mpointer->buffer, offset); |
| 593 |
sum.left -= 2 * (long) sample.left; |
| 594 |
sum.left += 2 * (long) sample.right; |
| 595 |
sample = get_from_buffer (¶mpointer->buffer, offset + 1); |
| 596 |
sum.left += sample.left; |
| 597 |
sum.left -= sample.right; |
| 598 |
|
| 599 |
/* just in case L/R: 32000/-32000 -32000/32000 32000/-32000 : */ |
| 600 |
sum.left /= 8; |
| 601 |
sum.right = sum.left; |
| 602 |
#endif /* TEST_DAVE_PLATT */ |
| 603 |
|
| 604 |
#elif defined (FOURTH_ORDER) |
| 605 |
sample = get_from_buffer (¶mpointer->buffer, offset - 2); |
| 606 |
sum.left += sample.left; |
| 607 |
sum.right += sample.right; |
| 608 |
sample = get_from_buffer (¶mpointer->buffer, offset - 1); |
| 609 |
sum.left -= 4 * (long) sample.left; |
| 610 |
sum.right -= 4 * (long) sample.right; |
| 611 |
sample = get_from_buffer (¶mpointer->buffer, offset); |
| 612 |
sum.left += 6 * (long) sample.left; |
| 613 |
sum.right += 6 * (long) sample.right; |
| 614 |
sample = get_from_buffer (¶mpointer->buffer, offset + 1); |
| 615 |
sum.left -= 4 * (long) sample.left; |
| 616 |
sum.right -= 4 * (long) sample.right; |
| 617 |
sample = get_from_buffer (¶mpointer->buffer, offset + 2); |
| 618 |
sum.left += sample.left; |
| 619 |
sum.right += sample.right; |
| 620 |
|
| 621 |
sum.left /= 4; |
| 622 |
sum.right /= 4; |
| 623 |
|
| 624 |
#elif defined (SIXTH_ORDER) |
| 625 |
sample = get_from_buffer (¶mpointer->buffer, offset - 3); |
| 626 |
sum.left -= sample.left; |
| 627 |
sum.right -= sample.right; |
| 628 |
sample = get_from_buffer (¶mpointer->buffer, offset - 2); |
| 629 |
sum.left += 6 * (long) sample.left; |
| 630 |
sum.right += 6 * (long) sample.right; |
| 631 |
sample = get_from_buffer (¶mpointer->buffer, offset - 1); |
| 632 |
sum.left -= 15 * (long) sample.left; |
| 633 |
sum.right -= 15 * (long) sample.right; |
| 634 |
sample = get_from_buffer (¶mpointer->buffer, offset); |
| 635 |
sum.left += 20 * (long) sample.left; |
| 636 |
sum.right += 20 * (long) sample.right; |
| 637 |
sample = get_from_buffer (¶mpointer->buffer, offset + 1); |
| 638 |
sum.left -= 15 * (long) sample.left; |
| 639 |
sum.right -= 15 * (long) sample.right; |
| 640 |
sample = get_from_buffer (¶mpointer->buffer, offset + 2); |
| 641 |
sum.left += 6 * (long) sample.left; |
| 642 |
sum.right += 6 * (long) sample.right; |
| 643 |
sample = get_from_buffer (¶mpointer->buffer, offset + 3); |
| 644 |
sum.left -= sample.left; |
| 645 |
sum.right -= sample.right; |
| 646 |
|
| 647 |
/* Should be /64, but the signal is extremely soft, so divide by less to |
| 648 |
get more quantization levels (more accurate) */ |
| 649 |
sum.left /= 10; |
| 650 |
sum.right /= 10; |
| 651 |
#endif |
| 652 |
|
| 653 |
if (sum.left < 32767) |
| 654 |
sample.left = sum.left; |
| 655 |
else |
| 656 |
sample.left = 32767; |
| 657 |
|
| 658 |
if (sum.right < 32767) |
| 659 |
sample.right = sum.right; |
| 660 |
else |
| 661 |
sample.right = 32767; |
| 662 |
|
| 663 |
return sample; |
| 664 |
} |
| 665 |
|
| 666 |
fillfuncpointer_t cond_median2_highpass_pointer = cond_median2_highpass; |
| 667 |
|
| 668 |
sample_t |
| 669 |
cond_median2_rms (long offset, long offset_zero, |
| 670 |
parampointer_t parampointer) |
| 671 |
{ |
| 672 |
sample_t sample; |
| 673 |
doublesample_t doublesample; |
| 674 |
doublesample_t sum; |
| 675 |
long i; |
| 676 |
|
| 677 |
advance_current_pos_custom (¶mpointer->buffer2, |
| 678 |
cond_median2_highpass_pointer, |
| 679 |
offset + offset_zero, |
| 680 |
parampointer); |
| 681 |
|
| 682 |
sum.left = 0; |
| 683 |
sum.right = 0; |
| 684 |
|
| 685 |
for (i = -parampointer->postlength2; i <= parampointer->prelength2; |
| 686 |
i++) |
| 687 |
{ |
| 688 |
sample = get_from_buffer (¶mpointer->buffer2, i); |
| 689 |
doublesample.left = sample.left; |
| 690 |
doublesample.right = sample.right; |
| 691 |
sum.left += doublesample.left * doublesample.left; |
| 692 |
sum.right += doublesample.right * doublesample.right; |
| 693 |
} |
| 694 |
|
| 695 |
sum.left /= (parampointer->postlength2 + |
| 696 |
parampointer->prelength2 + 1); |
| 697 |
sum.right /= (parampointer->postlength2 + |
| 698 |
parampointer->prelength2 + 1); |
| 699 |
|
| 700 |
sample.left = sqrt (sum.left + 1); |
| 701 |
sample.right = sqrt (sum.right + 1); |
| 702 |
|
| 703 |
return sample; |
| 704 |
} |
| 705 |
|
| 706 |
fillfuncpointer_t cond_median2_rms_pointer = cond_median2_rms; |
| 707 |
|
| 708 |
sample_t |
| 709 |
cond_median2_gate (long offset, long offset_zero, |
| 710 |
parampointer_t parampointer) |
| 711 |
/* Well, not a `gate' any more - just (w[t]-b[t])/b[t], comparision to |
| 712 |
make the real gate is done later. */ |
| 713 |
{ |
| 714 |
sample_t sample; |
| 715 |
sample_t w_t; |
| 716 |
sample_t b_t; |
| 717 |
sample_t returnval; |
| 718 |
signed short list1[parampointer->postlength3 + |
| 719 |
parampointer->prelength3 * parampointer->int1 + 1]; |
| 720 |
signed short list2[parampointer->postlength3 + |
| 721 |
parampointer->prelength3 * parampointer->int1 + 1]; |
| 722 |
long i, j; |
| 723 |
|
| 724 |
advance_current_pos_custom (¶mpointer->buffer3, |
| 725 |
cond_median2_rms_pointer, |
| 726 |
offset + offset_zero, |
| 727 |
parampointer); |
| 728 |
|
| 729 |
w_t = get_from_buffer (¶mpointer->buffer3, 0); |
| 730 |
|
| 731 |
/* The RMF Filter */ |
| 732 |
|
| 733 |
for (i = 0; i < parampointer->postlength3; i++) |
| 734 |
{ |
| 735 |
sample = get_from_buffer (¶mpointer->buffer3, |
| 736 |
i - parampointer->postlength3); |
| 737 |
list1[i] = sample.left; |
| 738 |
list2[i] = sample.right; |
| 739 |
} |
| 740 |
|
| 741 |
j = i; |
| 742 |
|
| 743 |
for (; i <= parampointer->postlength3 + |
| 744 |
parampointer->prelength3 * parampointer->int1; |
| 745 |
i += parampointer->int1) |
| 746 |
{ |
| 747 |
sample = get_from_buffer (¶mpointer->buffer3, |
| 748 |
i - parampointer->postlength3); |
| 749 |
list1[j] = sample.left; |
| 750 |
list2[j] = sample.right; |
| 751 |
j++; |
| 752 |
} |
| 753 |
|
| 754 |
b_t.left = median (list1, j); |
| 755 |
b_t.right = median (list2, j); |
| 756 |
|
| 757 |
put_in_buffer (¶mpointer->buffer3, 0, b_t); |
| 758 |
|
| 759 |
|
| 760 |
i = (labs (w_t.left - b_t.left) * 1000) |
| 761 |
/ |
| 762 |
b_t.left; |
| 763 |
if (i > 32767) |
| 764 |
i = 32767; |
| 765 |
returnval.left = i; |
| 766 |
|
| 767 |
i = (labs (w_t.right - b_t.right) * 1000) |
| 768 |
/ |
| 769 |
b_t.right; |
| 770 |
if (i > 32767) |
| 771 |
i = 32767; |
| 772 |
returnval.right = i; |
| 773 |
|
| 774 |
return returnval; |
| 775 |
} |
| 776 |
|
| 777 |
fillfuncpointer_t cond_median2_gate_pointer = cond_median2_gate; |
| 778 |
|
| 779 |
sample_t |
| 780 |
cond_median2_filter (parampointer_t parampointer) |
| 781 |
{ |
| 782 |
sample_t sample, gate, returnval; |
| 783 |
/* max. length if toleft=postlength4 and toright=prelength4 */ |
| 784 |
signed short list3[2 * (parampointer->postlength4 + |
| 785 |
parampointer->prelength4 + 1) + 1]; |
| 786 |
#ifdef FANCY_FILL |
| 787 |
signed short list4[2 * (parampointer->postlength4 + |
| 788 |
parampointer->prelength4 + 1) + 1]; |
| 789 |
double a1, b1; |
| 790 |
#endif |
| 791 |
|
| 792 |
long i; |
| 793 |
int toleft, toright; |
| 794 |
signed short maxval; |
| 795 |
|
| 796 |
advance_current_pos (¶mpointer->buffer, parampointer->filterno); |
| 797 |
|
| 798 |
advance_current_pos_custom (¶mpointer->buffer4, |
| 799 |
cond_median2_gate_pointer, |
| 800 |
0, |
| 801 |
parampointer); |
| 802 |
|
| 803 |
gate = get_from_buffer (¶mpointer->buffer4, 0); |
| 804 |
|
| 805 |
#ifdef VIEW_INTERNALS |
| 806 |
returnval.left = 0; |
| 807 |
returnval.right = 0; |
| 808 |
#else |
| 809 |
/* 'Default' value - unchanged if there is no tick */ |
| 810 |
returnval = get_from_buffer (¶mpointer->buffer, 0); |
| 811 |
#endif |
| 812 |
|
| 813 |
if (gate.left > parampointer->long1) |
| 814 |
{ |
| 815 |
maxval = gate.left; |
| 816 |
|
| 817 |
toleft = -1; |
| 818 |
sample.left = 0; |
| 819 |
do |
| 820 |
{ |
| 821 |
toleft++; |
| 822 |
if (toleft < parampointer->postlength4) |
| 823 |
{ |
| 824 |
sample = get_from_buffer (¶mpointer->buffer4, -toleft - 1); |
| 825 |
if (sample.left > maxval) |
| 826 |
/* if so, the `while' will continue anyway, so maxval may |
| 827 |
be adjusted here already (if necessary) */ |
| 828 |
maxval = sample.left; |
| 829 |
} |
| 830 |
} |
| 831 |
while (toleft < parampointer->postlength4 && |
| 832 |
sample.left > parampointer->long1); |
| 833 |
|
| 834 |
toright = -1; |
| 835 |
sample.left = 0; |
| 836 |
do |
| 837 |
{ |
| 838 |
toright++; |
| 839 |
if (toright < parampointer->prelength4) |
| 840 |
{ |
| 841 |
sample = get_from_buffer (¶mpointer->buffer4, toright + 1); |
| 842 |
if (sample.left > maxval) |
| 843 |
/* if so, the `while' will continue anyway, so maxval may |
| 844 |
be adjusted here already (if necessary) */ |
| 845 |
maxval = sample.left; |
| 846 |
} |
| 847 |
} |
| 848 |
while (toright < parampointer->prelength4 && |
| 849 |
sample.left > parampointer->long1); |
| 850 |
|
| 851 |
/* total length of gate is toleft+toright+1, so a median with |
| 852 |
size 2*(toleft+toright+1)+1 is needed. */ |
| 853 |
|
| 854 |
/* only interpolate if there really is a tick */ |
| 855 |
if (maxval > parampointer->long2) |
| 856 |
{ |
| 857 |
|
| 858 |
#ifdef VIEW_INTERNALS |
| 859 |
returnval.left = (toright + toleft + 1) * 500; |
| 860 |
#else |
| 861 |
#ifdef FANCY_FILL |
| 862 |
for (i = 0; i <= 2 * (toleft + toright + 1); i++) |
| 863 |
{ |
| 864 |
list3[i] = get_from_buffer (¶mpointer->buffer, |
| 865 |
i - (toleft + toright + 1)).left; |
| 866 |
list4[i] = i - (toleft + toright + 1); |
| 867 |
} |
| 868 |
l1fit (list4, list3, 2 * (toleft + toright + 1), &a1, &b1); |
| 869 |
returnval.left = (signed short) rint (a1); |
| 870 |
#else |
| 871 |
for (i = 0; i <= 2 * (toleft + toright + 1); i++) |
| 872 |
list3[i] = get_from_buffer (¶mpointer->buffer, |
| 873 |
i - (toleft + toright + 1)).left; |
| 874 |
|
| 875 |
returnval.left = median (list3, 2 * (toleft + toright + 1) + 1); |
| 876 |
#endif |
| 877 |
#endif |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
if (gate.right > parampointer->long1) |
| 882 |
{ |
| 883 |
maxval = gate.right; |
| 884 |
|
| 885 |
toleft = -1; |
| 886 |
sample.right = 0; |
| 887 |
do |
| 888 |
{ |
| 889 |
toleft++; |
| 890 |
if (toleft < parampointer->postlength4) |
| 891 |
{ |
| 892 |
sample = get_from_buffer (¶mpointer->buffer4, -toleft - 1); |
| 893 |
if (sample.right > maxval) |
| 894 |
/* if so, the `while' will continue anyway, so maxval may |
| 895 |
be adjusted here already (if necessary) */ |
| 896 |
maxval = sample.right; |
| 897 |
} |
| 898 |
} |
| 899 |
while (toleft < parampointer->postlength4 && |
| 900 |
sample.right > parampointer->long1); |
| 901 |
|
| 902 |
toright = -1; |
| 903 |
sample.right = 0; |
| 904 |
do |
| 905 |
{ |
| 906 |
toright++; |
| 907 |
if (toright < parampointer->prelength4) |
| 908 |
{ |
| 909 |
sample = get_from_buffer (¶mpointer->buffer4, toright + 1); |
| 910 |
if (sample.right > maxval) |
| 911 |
/* if so, the `while' will continue anyway, so maxval may |
| 912 |
be adjusted here already (if necessary) */ |
| 913 |
maxval = sample.right; |
| 914 |
} |
| 915 |
} |
| 916 |
while (toright < parampointer->prelength4 && |
| 917 |
sample.right > parampointer->long1); |
| 918 |
|
| 919 |
/* total length of gate is toleft+toright+1, so a median with |
| 920 |
size 2*(toleft+toright+1)+1 is needed. */ |
| 921 |
|
| 922 |
/* only interpolate if there really is a tick */ |
| 923 |
if (maxval > parampointer->long2) |
| 924 |
{ |
| 925 |
|
| 926 |
#ifdef VIEW_INTERNALS |
| 927 |
returnval.right = (toright + toleft + 1) * 500; |
| 928 |
#else |
| 929 |
#ifdef FANCY_FILL |
| 930 |
for (i = 0; i <= 2 * (toleft + toright + 1); i++) |
| 931 |
{ |
| 932 |
list3[i] = get_from_buffer (¶mpointer->buffer, |
| 933 |
i - (toleft + toright + 1)).right; |
| 934 |
list4[i] = i - (toleft + toright + 1); |
| 935 |
} |
| 936 |
l1fit (list4, list3, 2 * (toleft + toright + 1), &a1, &b1); |
| 937 |
returnval.right = (signed short) rint (a1); |
| 938 |
#else |
| 939 |
for (i = 0; i <= 2 * (toleft + toright + 1); i++) |
| 940 |
list3[i] = get_from_buffer (¶mpointer->buffer, |
| 941 |
i - (toleft + toright + 1)).right; |
| 942 |
|
| 943 |
returnval.right = median (list3, 2 * (toleft + toright + 1) + 1); |
| 944 |
#endif |
| 945 |
#endif |
| 946 |
} |
| 947 |
} |
| 948 |
|
| 949 |
return returnval; |
| 950 |
} |