forked from AlisterH/gwc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
declick.c.new
1548 lines (1233 loc) · 41.6 KB
/
declick.c.new
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* Gnome Wave Cleaner Version 0.19
* Copyright (C) 2001 Jeffrey J. Welty
*
* 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.
*******************************************************************************/
/* declick.c */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <float.h>
#include "gwc.h"
#include "stat.h"
#undef warning
#define MESCHACH
#ifndef MESCHACH
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_permutation.h>
#include <gsl/gsl_linalg.h>
#endif
void fit_cubic(fftw_real data[], int n, fftw_real estimated[]) ;
#define FFT_MAX 8192
double high_pass_filter(fftw_real x[], int N)
{
int i ;
double sum2 = 0.0 ;
double d2x ;
for(i = 1 ; i < N-1 ; i++) {
d2x = x[i-1] - 2.0 * x[i] + x[i+1] ;
sum2 += d2x*d2x ;
}
return sqrt(sum2/( (double)N - 2) ) ;
}
void stats(double x[], int n, double *pMean, double *pStderr, double *pVar, double *pCv, double *pStddev)
{
double sum_wgt = 0.0 ;
double sum = 0.0 ;
double sum2 = 0.0 ;
double wgt ;
int i ;
for(i = 0 ; i < n ; i++) {
wgt = 1./((double)(n-i)) ;
wgt = 1. ;
sum += x[i]*wgt ;
sum2 += x[i]*x[i]*wgt ;
sum_wgt += wgt ;
}
if(sum_wgt > -DBL_MIN && sum_wgt < DBL_MIN) sum_wgt = 10.0*DBL_MIN ;
*pMean = sum / sum_wgt ;
if(n > 1) {
*pVar = (sum2 - 2.0*(*pMean*sum) + *pMean**pMean*sum_wgt) / ((double)n - 1.0) ;
*pStddev = sqrt(*pVar) ;
*pCv = 100.0 * *pStddev / (*pMean+1.e-100) ;
*pStderr = sqrt(*pVar / sum_wgt) ;
} else {
*pVar = 0.0 ;
*pStddev = 0.0 ;
*pCv = 0.0 ;
*pStderr = 0.0 ;
}
} ;
#ifdef UNUSED_FUNCTION_IN_DECLICK
void get_windowed_ps(fftw_real ps[], fftw_real in[], double window_coef[], int FFT_SIZE, rfftw_plan pFor)
{
fftw_real out[FFT_MAX], windowed[FFT_MAX] ;
int k ;
for(k = 0 ; k < FFT_SIZE ; k++) {
windowed[k] = window_coef[k] * in[k] ;
}
rfftw_one(pFor, windowed, out);
for (k = 1; k <= FFT_SIZE/2 ; ++k)
ps[k] = k < FFT_SIZE/2 ? out[k]*out[k] + out[FFT_SIZE-k]*out[FFT_SIZE-k] : out[k]*out[k] ;
}
#endif
void fit_trig_basis(fftw_real data[], int n, fftw_real estimated[], int click_start, int click_end)
{
int leftmin = 0 ;
int leftmax = click_start ;
int rightmax = n-1 ;
int rightmin = click_end+1 ;
#define ORDER 3
#define NPARAMS (ORDER*2+1)
int o ;
double B[ORDER*2+1+1] ;
int i ;
double x[ORDER*2+1] ;
init_reg(ORDER*2+1) ;
for(i = leftmin ; i <= leftmax ; i++) {
double v = (double)(i-leftmin)/(double)n ;
for(o = 0 ; o < ORDER ; o++) {
x[o*2] = cos((o+1)*M_PI*v) ;
x[o*2+1] = sin((o+1)*M_PI*v) ;
}
x[NPARAMS-1] = v ;
sum_reg(x, data[i]) ;
}
for(i = rightmin ; i <= rightmax ; i++) {
double v = (double)(i-leftmin)/(double)n ;
for(o = 0 ; o < ORDER ; o++) {
x[o*2] = cos((o+1)*M_PI*v) ;
x[o*2+1] = sin((o+1)*M_PI*v) ;
}
x[NPARAMS-1] = v ;
sum_reg(x, data[i]) ;
}
estimate_reg(B) ;
for(i = 0 ; i < ORDER ; i++)
printf("B[%d,%d]=%10lg %10lg\n", i*2+1, i*2+1+1, B[i*2+1], B[i*2+1+1]) ;
printf("B[%d,%d]=%10lg %10lg\n", 0, NPARAMS, B[0], B[NPARAMS]) ;
for(i = leftmin ; i <= rightmax ; i++) {
double v = (double)(i-leftmin)/(double)n ;
estimated[i] = B[0] + B[NPARAMS]*v ;
for(o = 0 ; o < ORDER ; o++) {
estimated[i] += B[o*2+1] * cos((o+1)*M_PI*v) ;
estimated[i] += B[o*2+1+1] * sin((o+1)*M_PI*v) ;
}
}
}
#ifndef MESCHACH
gsl_matrix *gsl_transp(gsl_matrix *m)
{
int i,j,rows,cols ;
gsl_matrix *t ;
rows = m->size1 ;
cols = m->size2 ;
t = gsl_matrix_alloc(cols,rows) ;
for(i = 0 ; i < rows ; i++)
for(j = 0 ; j < cols ; j++)
gsl_matrix_set(t, j, i, gsl_matrix_get(m, i, j)) ;
return t ;
}
gsl_vector *gsl_mv_mlt(gsl_matrix *m, gsl_vector *v)
{
gsl_vector *r ;
int i,j,rows,cols ;
rows = m->size1 ;
cols = m->size2 ;
r = gsl_vector_alloc(cols) ;
for(j = 0 ; j < cols ; j++) {
double x = gsl_matrix_get(m, 0, j)*gsl_vector_get(v,0) ;
for(i = 1 ; i < rows ; i++)
x += gsl_matrix_get(m, i, j)*gsl_vector_get(v,i) ;
gsl_vector_set(r, i, x) ;
}
return r ;
}
gsl_matrix *gsl_m_mlt(gsl_matrix *m1, gsl_matrix *m2)
{
gsl_matrix *r ;
int i,j,k,rows,cols,out_cols ;
rows = m1->size1 ;
cols = m1->size2 ;
out_cols = m2->size2 ;
r = gsl_matrix_alloc(rows,out_cols) ;
for(i = 0 ; i < rows ; i++) {
for(k = 0 ; k < out_cols ; k++) {
double x = gsl_matrix_get(m1, i, 0)*gsl_matrix_get(m2,0,k) ;
for(j = 1 ; j < cols ; j++)
x += gsl_matrix_get(m1, i, j)*gsl_matrix_get(m2,j,k) ;
gsl_matrix_set(r, i, k, x) ;
}
}
return r ;
}
gsl_matrix * gsl_m_inverse(gsl_matrix *m)
{
gsl_matrix *inverse,*ludecomp ;
gsl_permutation *perm ;
int s ;
inverse = gsl_matrix_alloc(m->size1,m->size2) ;
ludecomp = gsl_matrix_alloc(m->size1,m->size2) ;
perm = gsl_permutation_alloc(m->size1) ;
gsl_matrix_memcpy(ludecomp,m) ;
gsl_linalg_LU_decomp(ludecomp,perm,&s) ;
gsl_linalg_LU_invert(ludecomp,perm,inverse) ;
gsl_matrix_free(ludecomp) ;
gsl_permutation_free(perm) ;
return inverse ;
}
#endif
int lsar_sample_restore(fftw_real data[], int firstbad, int lastbad, int siglen)
{
#ifdef MESCHACH
int n_bad = lastbad - firstbad + 1 ;
int autolen = 60 ;
int i, j, rows, cols ;
int rcode ;
gboolean clipped ;
double x[100], auto_coefs[101] ;
static MAT *A=MNULL, *Au=MNULL, *Aut=MNULL, *AutmAu=MNULL, *iAutmAu=MNULL, *final=MNULL ;
/* static MAT *A, *Au, *Aut, *AutmAu, *iAutmAu, *final ; */
static VEC *rhs, *sig, *sig_final ;
//estimate_region(data, firstbad, lastbad, siglen) ;
//return REPAIR_SUCCESS ;
autolen = (siglen-n_bad)/4 ;
//autolen *= 2 ;
if(autolen < 0) {
d_print("Autolen < 0!\n") ;
return REPAIR_FAILURE;
}
if(autolen > 3*n_bad) autolen = 3*n_bad ;
if(autolen > 100) autolen = 100 ;
//g_print("siglen:%d n_bad:%d Autolen:%d\n",siglen,n_bad,autolen) ;
sig = v_get(siglen) ;
A = m_resize(A,siglen-autolen, siglen) ;
Au = m_resize(Au, siglen-autolen, n_bad) ;
for(i = 0 ; i < siglen ; i++)
sig->ve[i] = data[i] ;
init_reg(autolen) ;
for(i = autolen ; i < firstbad ; i++) {
for(j = 0 ; j < autolen ; j++)
x[j] = data[i - autolen + j] ;
sum_reg(x, data[i]) ;
}
for(i = lastbad+autolen+1 ; i < siglen ; i++) {
for(j = 0 ; j < autolen ; j++)
x[j] = data[i - autolen + j] ;
sum_reg(x, data[i]) ;
}
if(estimate_reg(auto_coefs) == 1) {
rcode = SINGULAR_MATRIX ;
} else {
for(i = firstbad ; i <= lastbad ; i++) sig->ve[i] = 0.0 ;
rows = siglen - autolen ;
cols = siglen ;
for(i = 0 ; i < rows ; i++) {
for(j = 0 ; j < autolen ; j++)
A->me[i][i+j]= -auto_coefs[j] ;
A->me[i][i+autolen] = 1. ;
for(j = firstbad ; j <= lastbad ; j++)
Au->me[i][j-firstbad] = A->me[i][j] ;
}
for(j = firstbad ; j <= lastbad ; j++)
sig->ve[j] = 0.0 ;
Aut = m_transp(Au, Aut) ;
rhs = mv_mlt(A,sig,rhs) ;
AutmAu = m_mlt(Aut,Au, AutmAu) ;
iAutmAu = m_inverse(AutmAu, iAutmAu) ;
final = m_mlt(iAutmAu,Aut, final) ;
sig_final = mv_mlt(final,rhs, sig_final) ;
clipped = FALSE ;
for(j = firstbad ; j <= lastbad ; j++) {
double tmp = -sig_final->ve[j-firstbad] ;
if(tmp > 1.0) clipped = TRUE ;
if(tmp < -1.0) clipped = TRUE ;
}
if(clipped == FALSE) {
for(j = firstbad ; j <= lastbad ; j++) {
data[j] = -sig_final->ve[j-firstbad] ;
if(data[j] > 1.0) data[j] = 1.0 ;
if(data[j] < -1.0) data[j] = -1.0 ;
}
}
if(clipped == FALSE)
rcode = REPAIR_SUCCESS ;
else
rcode = REPAIR_CLIPPED ;
}
M_FREE(A) ;
M_FREE(Au) ;
M_FREE(Aut) ;
M_FREE(AutmAu) ;
M_FREE(iAutmAu) ;
M_FREE(final) ;
V_FREE(sig) ;
V_FREE(sig_final) ;
V_FREE(rhs) ;
return rcode ;
#else
int n_bad = lastbad - firstbad + 1 ;
int autolen = 60 ;
int i, j, rows, cols ;
int rcode ;
gboolean clipped ;
double x[100], auto_coefs[101] ;
static gsl_matrix *A, *Au, *Aut, *AutmAu, *iAutmAu, *final ;
static gsl_vector *rhs, *sig, *sig_final ;
//estimate_region(data, firstbad, lastbad, siglen) ;
//return REPAIR_SUCCESS ;
autolen = (siglen-n_bad)/4 ;
//autolen *= 2 ;
if(autolen < 0) {
d_print("Autolen < 0!\n") ;
return REPAIR_FAILURE;
}
if(autolen > 3*n_bad) autolen = 3*n_bad ;
if(autolen > 100) autolen = 100 ;
//g_print("siglen:%d n_bad:%d Autolen:%d\n",siglen,n_bad,autolen) ;
sig = gsl_vector_alloc(siglen) ;
A = gsl_matrix_alloc(siglen-autolen, siglen) ;
Au = gsl_matrix_alloc(siglen-autolen, n_bad) ;
for(i = 0 ; i < siglen ; i++)
gsl_vector_set(sig,i, data[i]) ;
init_reg(autolen) ;
for(i = autolen ; i < firstbad ; i++) {
for(j = 0 ; j < autolen ; j++)
x[j] = data[i - autolen + j] ;
sum_reg(x, data[i]) ;
}
for(i = lastbad+autolen+1 ; i < siglen ; i++) {
for(j = 0 ; j < autolen ; j++)
x[j] = data[i - autolen + j] ;
sum_reg(x, data[i]) ;
}
if(estimate_reg(auto_coefs) == 1) {
rcode = SINGULAR_MATRIX ;
} else {
for(i = firstbad ; i <= lastbad ; i++) gsl_vector_set(sig,i,0.0) ;
rows = siglen - autolen ;
cols = siglen ;
for(i = 0 ; i < rows ; i++) {
for(j = 0 ; j < autolen ; j++)
gsl_matrix_set(A,i,i+j,-auto_coefs[j]) ;
gsl_matrix_set(A,i,i+autolen, 1.) ;
for(j = firstbad ; j <= lastbad ; j++)
gsl_matrix_set(A,i,j-firstbad, gsl_matrix_get(A,i,j)) ;
}
for(j = firstbad ; j <= lastbad ; j++)
gsl_vector_set(sig,j, 0.0) ;
Aut = gsl_transp(Au) ;
rhs = gsl_mv_mlt(A,sig) ;
AutmAu = gsl_m_mlt(Aut,Au) ;
iAutmAu = gsl_m_inverse(AutmAu) ;
final = gsl_m_mlt(iAutmAu,Aut) ;
sig_final = gsl_mv_mlt(final,rhs) ;
clipped = FALSE ;
for(j = firstbad ; j <= lastbad ; j++) {
double tmp = -gsl_vector_get(sig_final,j-firstbad) ;
if(tmp > 1.0) clipped = TRUE ;
if(tmp < -1.0) clipped = TRUE ;
}
if(clipped == FALSE) {
for(j = firstbad ; j <= lastbad ; j++) {
double tmp = -gsl_vector_get(sig_final,j-firstbad) ;
if(data[j] > 1.0) data[j] = 1.0 ;
if(data[j] < -1.0) data[j] = -1.0 ;
}
}
if(clipped == FALSE)
rcode = REPAIR_SUCCESS ;
else
rcode = REPAIR_CLIPPED ;
}
gsl_matrix_free(A) ;
gsl_matrix_free(Au) ;
gsl_matrix_free(Aut) ;
gsl_matrix_free(AutmAu) ;
gsl_matrix_free(iAutmAu) ;
gsl_matrix_free(final) ;
gsl_vector_free(sig) ;
gsl_vector_free(sig_final) ;
gsl_vector_free(rhs) ;
return rcode ;
#endif
}
#define DECLICK_CUBIC 0x01
#define DECLICK_LSAR 0x02
int declick_a_click(struct sound_prefs *p, long first_sample, long last_sample, int channel_mask)
{
long n_samples = last_sample - first_sample + 1;
long first ;
int ch, k, last ;
int click_start, click_end ;
int repair_method ;
int FFT_SIZE ;
int result = REPAIR_FAILURE ;
fftw_real estimated[FFT_MAX*3], window_coef[FFT_MAX] ;
fftw_real data[2][FFT_MAX] ;
/* choose a repair strategy based on the length of the click */
if(n_samples < 1) {
d_print("Whoa there, trying to declick %d samples!\n", n_samples) ;
return 1 ;
} else if(n_samples < 6) {
/* cubic function -- interpolation */
first = first_sample-4 ;
if(first < 0) first = 0 ;
last = last_sample+4 ;
if(last > p->n_samples-1) last = p->n_samples-1 ;
repair_method = DECLICK_CUBIC ;
} else {
/* LSAR */
first = first_sample-200;
if(first < 0) first = 0 ;
last = last_sample+200;
if(last > p->n_samples-1) last = p->n_samples-1 ;
repair_method = DECLICK_LSAR ;
}
repair_method = DECLICK_LSAR ;
FFT_SIZE = last-first+1 ;
read_fft_real_wavefile_data(data[0], data[1], first, last) ;
save_undo_data( first, last, p, FALSE) ;
/* compute click starting and ending positions in the buffer data_all */
click_start = first_sample-first ;
click_end = last_sample-first ;
for(k = 0 ; k < FFT_SIZE ; k++) {
window_coef[k] = blackman(k, FFT_SIZE) ;
window_coef[k] = 1.0 ;
}
for(ch = 0 ; ch < 2 ; ch++) {
if(channel_mask & (ch+1)) {
if(repair_method == DECLICK_CUBIC) {
fit_cubic(data[ch], FFT_SIZE, estimated) ;
/* fit_trig_basis(data_all[ch], FFT_SIZE*3, windowed, click_start, click_end) ; */
/* merge results back into sample data based on window function */
for(k = 0 ; k < FFT_SIZE ; k++) {
double w = window_coef[k] ;
w = blackman(k, FFT_SIZE) ;
w = blackman_hybrid(k, click_end-click_start+2, FFT_SIZE) ;
data[ch][k] = (1.0-w) * data[ch][k] + w*estimated[k+FFT_SIZE] ;
}
} else
result = lsar_sample_restore(data[ch], click_start, click_end, FFT_SIZE) ;
}
}
write_fft_real_wavefile_data(data[0], data[1], first, last) ;
return result ;
}
/* bj 10/2002
* WINDOW_SIZE = number of data points to read at a time
* WINDOW_OVERLAP = number of data points to overlap between windows
* (set this to maximum click size you think reasonable)
* HPF_AVE_WING_BASE = number of points about current point (each side)
* to use as baseline rms average
* HPF_AVE_WING_LOCAL = number of points about current point (each side)
* to average to get rms value for current point
* HPF_DATA_WING = number of points about current point (each side)
* required to get rms value for current point
* (only change this if you change the hpf from 2nd
* derivative to something else)
* HPF_DELTA_WIDTH = number of previous points used as base to compare
* current change in hpf. used to detect trailing
* edge of a click
*/
#define WINDOW_SIZE 30000
#define HPF_AVE_WING_BASE 500
#define HPF_AVE_WING_LOCAL 8
#define HPF_DATA_WING 1
#define HPF_DELTA_WIDTH 50
#define WINDOW_OVERLAP 300
#define HPF_AVE_WIDTH_BASE (HPF_AVE_WING_BASE * 2 + 1)
#define HPF_AVE_WIDTH_LOCAL (HPF_AVE_WING_LOCAL * 2 + 1)
#define EXTRA_DATA_WING (HPF_AVE_WING_BASE + HPF_AVE_WING_LOCAL + HPF_DATA_WING)
#define MAX_WINDOW_SIZE (WINDOW_SIZE + EXTRA_DATA_WING * 2)
#define HPF2_AVE_WING_BASE 500
#define HPF2_AVE_WING_LOCAL 4
#define HPF2_DATA_WING 1
#define HPF2_DELTA_WIDTH 50
#define WINDOW_OVERLAP2 300
#define HPF2_AVE_WIDTH_BASE (HPF2_AVE_WING_BASE * 2 + 1)
#define HPF2_AVE_WIDTH_LOCAL (HPF2_AVE_WING_LOCAL * 2 + 1)
#define INC_POS(a,b,c) ( ((a)+(b)+(c)) % (c) )
/* maintain running sum of 2nd derivative rms so that we avoid excessive
* calculation time.
* 1) local rms value across small number of data points (HPF_AVE_WING_LOCAL
* about current point)
* 2) compare local rms value with HPF_AVE_WING_BASE local rms values about
* current point
*
* Also used to get change in hpf near a point (pass in sample=-N as flag)
*
* do_declick runs backward thru data points, so this does too.
* To get first real datapoint, have to run through (HPF_AVE_WIDTH_BASE
* + HPF_AVE_WING_LOCAL) * 2 calculations first in order to fill up
* the hpfl & hpfb arrays with correct rms values
*/
void get_hpf (long sample, fftw_real channel_data[], double *hpf, double *hpf_ave, double *hpf_dev, double *hpf2, double *hpf2_ave, double *hpf2_dev)
{
static double hpfl[HPF_AVE_WIDTH_LOCAL];
static double suml;
static int posl;
static double hpfb[HPF_AVE_WIDTH_BASE];
static double sumb;
static int posb;
long sample2 = sample/2 ;
static double hpfl2[HPF_AVE_WIDTH_LOCAL];
static double suml2;
static int posl2;
static double hpfb2[HPF_AVE_WIDTH_BASE];
static double sumb2;
static int posb2;
/* if real sample, get next hpf value into array */
if (sample >= 0)
{
{
fftw_real *data = &channel_data[sample - (HPF_AVE_WING_BASE+HPF_AVE_WING_LOCAL)];
posl = INC_POS(posl,-1,HPF_AVE_WIDTH_LOCAL);
posb = INC_POS(posb,-1,HPF_AVE_WIDTH_BASE);
/* get updated sum of rms values of actual data points
* and get updated sum of local average of rms values */
suml -= hpfl[posl];
hpfl[posl] = data[-1] - 2. * data[0] + data[1];
hpfl[posl] *= hpfl[posl];
suml += hpfl[posl];
/* bugfix -- thanks Paul Sanders 1/12/2007 */
*hpf = (suml > 0.0) ? sqrt(suml/(HPF_AVE_WIDTH_LOCAL-2)) : 0.0 ;
sumb -= hpfb[posb];
hpfb[posb] = *hpf;
sumb += *hpf;
*hpf_ave = sumb / HPF_AVE_WIDTH_BASE;
/* hpf of current point was read in a while back;
* retrieve that value now */
*hpf = hpfb[INC_POS(posb,HPF_AVE_WING_BASE,HPF_AVE_WIDTH_BASE)];
}
{
fftw_real *data = &channel_data[sample - (HPF2_AVE_WING_BASE+HPF2_AVE_WING_LOCAL)];
posl2 = INC_POS(posl2,-1,HPF2_AVE_WIDTH_LOCAL);
posb2 = INC_POS(posb2,-1,HPF2_AVE_WIDTH_BASE);
/* get updated sum of rms values of actual data points
* and get updated sum of local average of rms values */
suml2 -= hpfl2[posl2];
hpfl2[posl2] = data[-1] - 2. * data[0] + data[1];
hpfl2[posl2] *= hpfl2[posl2];
suml2 += hpfl2[posl2];
/* bugfix -- thanks Paul Sanders 1/12/2007 */
*hpf2 = (suml2 > 0.0) ? sqrt(suml2/(HPF2_AVE_WIDTH_LOCAL-2)) : 0.0 ;
sumb2 -= hpfb2[posb2];
hpfb2[posb2] = *hpf2;
sumb2 += *hpf2;
*hpf2_ave = sumb2 / HPF2_AVE_WIDTH_BASE;
/* hpf of current point was read in a while back;
* retrieve that value now */
*hpf2 = hpfb2[INC_POS(posb2,HPF2_AVE_WING_BASE,HPF2_AVE_WIDTH_BASE)];
}
}
/* negative sample; get change in hpf */
else
{
{
int posF, pos0, n = -sample;
double x = 0, sumx = 0, sumx2 = 0;
pos0 = INC_POS(posb,HPF_AVE_WING_BASE+n,HPF_AVE_WIDTH_BASE);
while (sample < 0)
{
posF = INC_POS(pos0,-2,HPF_AVE_WIDTH_BASE);
x = hpfb[posF] - hpfb[pos0];
sumx += x;
sumx2 += x*x;
pos0 = INC_POS(pos0,-1,HPF_AVE_WIDTH_BASE);
sample++;
}
*hpf = x; /* last value in x is value at "current" position */
*hpf_ave = sumx / n;
*hpf_dev = sqrt((sumx2 - sumx*sumx/n)/(n-1));
}
{
int posF, pos0, n = -sample2;
double x = 0, sumx = 0, sumx2 = 0;
pos0 = INC_POS(posb2,HPF2_AVE_WING_BASE+n,HPF2_AVE_WIDTH_BASE);
while (sample < 0)
{
posF = INC_POS(pos0,-2,HPF2_AVE_WIDTH_BASE);
x = hpfb2[posF] - hpfb2[pos0];
sumx += x;
sumx2 += x*x;
pos0 = INC_POS(pos0,-1,HPF2_AVE_WIDTH_BASE);
sample++;
}
*hpf2 = x; /* last value in x is value at "current" position */
*hpf2_ave = sumx / n;
*hpf2_dev = sqrt((sumx2 - sumx*sumx/n)/(n-1));
}
}
}
/* bj 10/2002 end, but several changes in do_declick also */
char *do_declick(struct sound_prefs *p, long first_sample, long last_sample, int channel_mask, double sensitivity, int repair,
struct click_data *clicks, int iterate_flag, int leave_click_marks)
{
extern int declick_detector_type ;
if(declick_detector_type == FFT_DETECT)
return do_declick_fft(p,first_sample,last_sample,channel_mask,sensitivity,repair,clicks,iterate_flag,leave_click_marks) ;
else
return do_declick_hpf(p,first_sample,last_sample,channel_mask,sensitivity,repair,clicks,iterate_flag,leave_click_marks) ;
}
#define DECLICK_MAX_FFT 128
char *do_declick_fft(struct sound_prefs *p, long first_sample, long last_sample, int channel_mask, double sensitivity, int repair,
struct click_data *clicks, int iterate_flag, int leave_click_marks)
{
static char results_buf[200] ;
long window_first ;
long i,k ;
int FFT_SIZE = 64 ;
int n_repaired[2] , n_this_pass ;
int n_not_repaired[2] ;
char max_exceeded_notice = 0 ;
#define FFT_WINDOW 1000
char level[2][2*FFT_WINDOW+1][DECLICK_MAX_FFT] ;
#ifdef HAVE_FFTW3
FFTW(plan) pLeft, pRight ;
#else /* HAVE_FFTW3 */
rfftw_plan fftw_p ;
#endif /* HAVE_FFTW3 */
fftw_real data[2][2*FFT_WINDOW+1] ;
fftw_real out[2*DECLICK_MAX_FFT] ;
fftw_real window[2*DECLICK_MAX_FFT] ;
fftw_real power_spectrum[2*DECLICK_MAX_FFT] ;
int in_click ;
int window_size = FFT_WINDOW * MIN((p->rate/44100.0),2) ;
int window_step ;
int channel ;
int done = 0 ;
if(last_sample > p->n_samples-20) last_sample = p->n_samples-20 ;
if(first_sample < 20) first_sample = 20 ;
if(first_sample >= last_sample) {
return "Region to small to declick." ;
}
start_timer();
if(repair == FALSE || leave_click_marks == FALSE)
clicks->n_clicks = 0 ;
n_repaired[0] = n_repaired[1] = 0 ;
n_not_repaired[0] = n_not_repaired[1] = 0 ;
window_step = 700 ;
window_size = 801 ;
g_print("Declick_fft first_sample:%ld last_sample:%ld window_size:%d FFT_SIZE:%d\n", first_sample, last_sample, window_size, FFT_SIZE) ;
update_progress_bar(0.0,PROGRESS_UPDATE_INTERVAL,TRUE) ;
#ifdef HAVE_FFTW3
pLeft = FFTW(plan_r2r_1d)(FFT_SIZE, data[0], out, FFTW_R2HC, FFTW_ESTIMATE);
pRight = FFTW(plan_r2r_1d)(FFT_SIZE, data[1], out, FFTW_R2HC, FFTW_ESTIMATE);
#else /* HAVE_FFTW3 */
fftw_p = rfftw_create_plan(FFT_SIZE, FFTW_REAL_TO_COMPLEX, FFTW_ESTIMATE);
#endif /* HAVE_FFTW3 */
for(k = 0 ; k < FFT_SIZE ; k++) {
window[k] = blackman(k,FFT_SIZE) ;
}
for(window_first = first_sample ; !done && window_first < last_sample ; window_first += window_step ) {
int clicks_repaired = 1 ;
int min_sample,max_sample ;
if(window_first + window_size > last_sample) {
window_first = last_sample - window_size ;
done = 1 ;
}
double percentage = (double)(window_first-first_sample)/(double)(last_sample-first_sample) ;
update_progress_bar(percentage,PROGRESS_UPDATE_INTERVAL,FALSE) ;
int istart ;
if(window_first == first_sample) {
istart = 0 ;
} else {
int n_shift = window_size-window_step ;
istart = n_shift ;
for(i=0 ; i < n_shift ; i++) {
int shift_i = i+window_step ;
for(k = 0 ; k < FFT_SIZE/2 ; k++) {
for (channel = 0; channel < 2; channel++) {
level[channel][i][k] = level[channel][shift_i][k] ;
}
}
}
}
for(i=istart ; i < window_size ; i++) {
min_sample = i+window_first-FFT_SIZE/2 ;
min_sample = MAX(0, min_sample) ;
max_sample = min_sample+FFT_SIZE-1 ;
if(max_sample > p->n_samples-1)
max_sample = p->n_samples-1 ;
read_fft_real_wavefile_data(data[0], data[1], min_sample, max_sample) ;
for(k = 0 ; k < FFT_SIZE ; k++) {
data[0][k] *= window[k] ;
data[1][k] *= window[k] ;
}
for (channel = 0; channel < 2; channel++) {
double min_p = 1.e30, max_p = -1.e30 ;
if(! ((channel+1) & channel_mask) ) continue ;
#ifdef HAVE_FFTW3
if (channel == 0)
FFTW(execute)(pLeft);
else
FFTW(execute)(pRight);
#else /* HAVE_FFTW3 */
if (channel == 0)
rfftw_one(fftw_p, data[0], out);
else
rfftw_one(fftw_p, data[1], out);
#endif /* HAVE_FFTW3 */
power_spectrum[0] = out[0]*out[0]; /* DC component */
for (k = 1; k < (FFT_SIZE+1)/2; ++k) /* (k < FFT_SIZE/2 rounded up) */
power_spectrum[k] = out[k]*out[k] + out[FFT_SIZE-k]*out[FFT_SIZE-k];
if (FFT_SIZE % 2 == 0) /* N is even */
power_spectrum[FFT_SIZE/2] = (out[FFT_SIZE/2]*out[FFT_SIZE/2]); /* Nyquist freq. */
for(k = 1 ; k <= FFT_SIZE/2 ; k++) {
double p = 10.0*log10(power_spectrum[k]) ;
if(p < -127.0) p = -127.0 ;
if(p > 127.0) p = 127.0 ;
if(p > max_p) max_p = p ;
if(p < min_p) min_p = p ;
level[channel][i][k-1] = (char)p ;
}
}
}
for(channel = 0 ; channel < 2 ; channel++) {
double mean_level[FFT_SIZE] ;
double mean_level_r[FFT_SIZE] ;
double mean_level_p[FFT_SIZE] ;
double offset[FFT_WINDOW*2+1] ;
double hgt_sum = 0 ;
double mean, std_err, var, cv, stddev ;
long click_start = 0 ;
int use_new_detector=1 ;
if(! ((channel+1) & channel_mask) ) continue ;
if(use_new_detector) {
double peak_sum_prev = -1 ;
double peak_width_prev = -1.0 ;
double sum_l_prev=0 ;
double sum_r_prev=0 ;
double sum_p_prev=0 ;
double delta_l_prev=0 ;
double delta_r_prev=0 ;
double delta_p_prev=0 ;
double balance_factor_prev=0 ;
for(i = 60 ; i < window_size ; i++) {
double peak_sum=0.0 ;
double peak_width = 0 ;
int j ;
int n_k = 0 ;
int k_start=4 ; /* lowest frequencies don't have good data for detecting clicks */
for(k = k_start ; k < FFT_SIZE/2 ; k++) {
double peak_sum_k = level[channel][i][k] ;
for(j = 1 ; j < 50 ; j++) {
if(j > 2 && level[channel][i-j][k] > level[channel][i-j+2][k]) {
j-- ;
break ;
}
if(j > 2 && level[channel][i+j][k] > level[channel][i+j-2][k]) {
j-- ;
break ;
}
peak_sum_k += level[channel][i-j][k] ;
peak_sum_k += level[channel][i+j][k] ;
}
int width = j*2 ;
mean_level_p[k] = peak_sum_k/((double)width+1.0) ;
int level_width = width ;
if(level_width < 50) level_width = 50 ;
mean_level[k] = 0.0 ;
mean_level_r[k] = 0.0 ;
int n=0 ;
for(; j < level_width ; j++) {
mean_level[k] += level[channel][i-j][k] ;
mean_level_r[k] += level[channel][i+j][k] ;
n += 1 ;
}
mean_level[k] /= (double)n ;
mean_level_r[k] /= (double)n ;
double ml = (mean_level[k]+mean_level_r[k]) / 2.0 ;
/* peak_sum -= mean_level[k]*(width+1) ; */
double tmp = peak_sum_k-(ml+3)*(width+1) ;
if(tmp < 0.0) tmp=0.0 ;
peak_sum += tmp ;
peak_width += width+1 ;
n_k++ ;
}
/* an analysis of mean_levels on left and right sides of peak */
double balance_factor = 1.0 ;
double delta_l=0.0, delta_p=0.0, delta_r=0.0, sum_l=0.0, sum_p=0.0, sum_r=0.0 ;
for(k = k_start ; k < FFT_SIZE/2 ; k++) {
sum_l += mean_level[k] ;
sum_r += mean_level_r[k] ;
sum_p += mean_level_p[k] ;
if(k < FFT_SIZE/2-1) {
delta_l += mean_level[k+1]-mean_level[k] ;
delta_r += mean_level_r[k+1]-mean_level_r[k] ;
delta_p += mean_level_p[k+1]-mean_level_p[k] ;
}
}
/* compute mean peak width */
peak_width /= (double)n_k ;
sum_l /= (double)n_k ;
sum_r /= (double)n_k ;
sum_p /= (double)n_k ;
/* note we are comparing negative values here for the ratio to use */
if(sum_l > sum_r) {
balance_factor=sqrt((sum_l-sum_r+.01)+1) ;
} else {
balance_factor=sqrt((sum_r-sum_l+.01)+1) ;
}
balance_factor = 1./balance_factor ;
peak_sum *= balance_factor ;
if(0 && channel == 0) {