-
Notifications
You must be signed in to change notification settings - Fork 0
/
duet.cpp
1384 lines (1113 loc) · 41.2 KB
/
duet.cpp
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
#include "duet.h"
//#define OLD_MASK_BUILD
//#define OLD_PEAK_ASSIGN
const int MAX_PRECLUSTERS = 12;
const int MAX_MARGINAL_PEAKS = 16;
void RENDER_HIST(const std::string &filepath, const std::string &title, bool pause)
{
std::string cmd("gnuplot -e \"splot \\\"");
cmd += filepath;
cmd += "\\\" u 1:2:3 w pm3d title \\\"";
cmd += title;
cmd += "\\\", \\\"s.dat\\\" pt 7 ps .9 title \\\"Simulation clusters\\\", \\\"s_duet.dat\\\" pt 8 ps .8 title \\\"DUET clusters\\\"; set xlabel \\\"alpha\\\"; set ylabel \\\"delta (s)\\\";";
if (pause)
cmd += "pause -1";
cmd += "\"";
system(cmd.c_str());
}
const real _2Pi = 2*M_PI;
template <class T> void print(T o) { cout << o << endl; }
template <class T>
void swap (T &a, T &b)
{
T tmp = a;
a = b;
b = tmp;
}
real norm(real a, real b) { return std::sqrt(a*a + b*b); }
/// Returns the success state of the input and prints [DONE] or [FAIL] accordingly.
bool print_status (bool success)
{
if (success)
puts(GREEN "[DONE]" NOCOLOR);
else
puts(RED "[FAIL]" NOCOLOR);
return success;
}
/* READ!!: http://www.fftw.org/doc/The-Halfcomplex_002dformat-DFT.html */
/// Specialization for even sizes
void evenHC2magnitude(int samples, real *hc, real *magnitude)
{
magnitude[0] = hc[0];
idx I = samples/2;
for (idx i=1; i < I; ++i)
magnitude[i] = norm(hc[i], hc[samples-i]); // Not true for odd samples!!!
}
int valid_FFT_convolution(idx h_nonzero_size, idx FFT_N)
{
idx g_nonzero_size = FFT_N - h_nonzero_size + 1;
printf(
"FFT convolution with \n"
" FFT_N = %ld\n"
" g_size = %ld\n"
" h_size = %ld\n",
FFT_N, g_nonzero_size, h_nonzero_size);
if (g_nonzero_size < 1)
{
puts("Invalid configuration!");
return 0;
}
return 1;
}
inline void fillFFTblock(real *data, idx data_size, real *block, idx block_size)
{
idx i = 0;
for (; i < data_size; ++i)
block[i] = data[i];
for (; i < block_size; ++i)
block[i] = 0.0;
// memset((void*)wav_out, 0, sizeof(real) * (N_wav+h_size-1));
// memcpy()
}
/**
Z = Z1*Z2
@param[in] re1 - Re{Z1}
@param[in] im1 - Im{Z1}
@param[in] re2 - Re{Z2}
@param[in] im2 - Im{Z2}
@param[out] re - Re{Z}
@param[out] im - Im{Z}
*/
inline void complex_multiply(real re1, real im1, real re2, real im2, real *re, real *im)
{
*re = re1*re2 - im1*im2;
*im = re1*im2 + im1*re2;
}
// z = z1/z2
inline void complex_divide(real re1, real im1, real re2, real im2, real *re, real *im)
{
real denominator = re2*re2 + im2*im2;
*re = (re1*re2 + im1*im2) / denominator;
*im = (im1*re2 - re1*im2) / denominator;
}
/**
HalfComplex representation multiply
@param[in] z1 - Input HC array
@param[in] z2 - Input HC array
@param[out] z - Output HC array
@param[in] size - Size of the HC array
@warn: ONLY FOR EVEN TRANSFORMATIONS!!!
*/
void hc_multiply (real *z1, real *z2, real *z, idx size)
{
z[0] = z1[0]*z2[0];
const idx max_i = size/2;
for (idx i=1; i < max_i; ++i)
complex_multiply(z1[i], z1[size-i],
z2[i], z2[size-i],
&z[i], &z[size-i]);
}
template <class T> T blocks (T terms, T block_size)
{
return terms/block_size + ( terms % block_size ? 1:0 );
}
void heuristic_pre_filter_clusters (Histogram<real> &hist, RankList<real,real> &preclusters, real min_peak_fall)
{
static const size_t skip_bins = 0; // skip the next bin if this one is below the noise threshold for faster performance (for sure a peak will not arise in the next bins)
const size_t max_bin = hist.bins() - 1;
preclusters.clear();
// Exclude bins on the border (Borderless histogram and interior region = interest region)
for (size_t bin=1; bin < max_bin; ++bin)
{
static real min_peak_diff = 0; // (10) sets the minimum (-)derivative around the peak.
static real min_score = 50; // (50) Minimum threshold, below which is noise.
real score = hist.bin(bin);
if (score > min_score)
{
if (score - hist.bin(bin-1) >= min_peak_fall &&
score - hist.bin(bin+1) >= min_peak_fall)
preclusters.add(score, hist.get_bin_center(bin));
}
else
bin += skip_bins; // skip (faster but might not be safe - we don't want to skip over a peak)
}
}
void heuristic_pre_filter_clusters2D (Histogram2D<real> &hist, RankList<real, Point2D<real> > &preclusters, real min_peak_fall)
{
static const size_t skip_bins = 0; // skip the next bin if this one is below the noise threshold for faster performance (for sure a peak will not arise in the next bins)
const size_t max_alpha_bin = hist.xbins() - 1;
const size_t max_delta_bin = hist.ybins() - 1;
preclusters.clear();
// Bins on the histogram border are filtered out! We only want peaks in the histogram's interior region (since we're using a borderless histogram we filter out peaks from outside the chosen interest region this way)
for (size_t alphabin=1; alphabin < max_alpha_bin; ++alphabin)
{
for (size_t deltabin=1; deltabin < max_delta_bin; ++deltabin)
{
static real min_peak_diff = 0; // (10) sets the minimum (-)derivative around the peak.
static real min_score = 50; // (50) Minimum threshold, below which is noise.
real score = hist.bin(alphabin,deltabin);
if (score > min_score)
{
if (score - hist.bin(alphabin-1,deltabin ) >= min_peak_fall &&
score - hist.bin(alphabin+1,deltabin ) >= min_peak_fall &&
score - hist.bin(alphabin ,deltabin-1) >= min_peak_fall &&
score - hist.bin(alphabin ,deltabin+1) >= min_peak_fall)
preclusters.add(score, hist.get_bin_center(alphabin,deltabin));
}
else
deltabin += skip_bins; // skip the next one (faster) (if there's no points at this bin there isn't a peak in the next bin for sure)
}
}
}
/// Returns |a-b| for unsigned int type size_t
inline size_t subabs(size_t a, size_t b)
{
return ( a > b ? a-b : b-a );
}
// Checks if the distance between the 2 points is smaller than d_delta and d_alpha for each dimension respectively. Independent Box coordinates!
bool belongs (const Point2D<real> &a, const Point2D<real> &b, real max_distance_alpha, real max_distance_delta)
{
return (std::abs(b.x-a.x) <= max_distance_alpha && std::abs(b.y-a.y) <= max_distance_delta ? 1 : 0);
}
/// Aggregates clusters to the biggest cluster inside a certain radius (in box coordinates)
void heuristic_aggregate_preclusters (RankList<real,real> &preclusters, const DUETcfg &DUET, real min_peak_distance)
{
size_t size = preclusters.eff_size(DUET.noise_threshold);
real max_score = preclusters.scores[0];
for (size_t i=0; i < size; ++i)
if (preclusters.scores[i] * DUET.max_peak_scale_disparity < max_score)
{
preclusters.del(i,0.1);
--size;
--i; // just deleted, check again
}
if (DUET.aggregate_clusters) // if for test purposes: always ON at release.
{
for (size_t i=0; i < size; ++i)
{
for (size_t cluster=0; cluster < i; ++cluster)
{
if (std::abs(preclusters.values[i]-preclusters.values[cluster]) <= min_peak_distance)
{
preclusters.del(i,0.1);
--size; // Just deleted an element. No need to process extra 0-score entries.
--i; // The rest of the list was pushed up, process the next entry which is in the same position.
}
}
}
}
}
void heuristic_clustering(Histogram<real> &hist, RankList<real,real> &preclusters, const DUETcfg &DUET, real min_peak_distance)
{
heuristic_pre_filter_clusters(hist, preclusters, DUET.min_peak_fall);
// cout << preclusters;
heuristic_aggregate_preclusters(preclusters, DUET, min_peak_distance);
}
/// Aggregates clusters to the biggest cluster inside a certain radius (in box coordinates)
void heuristic_aggregate_preclusters2D (RankList<real,Point2D<real> > &preclusters, const DUETcfg &DUET)
{
size_t size = preclusters.eff_size(DUET.noise_threshold);
real max_score = preclusters.scores[0];
for (size_t i=0; i < size; ++i)
if (preclusters.scores[i] * DUET.max_peak_scale_disparity < max_score)
{
preclusters.del(i,0.1);
--size;
--i; // just deleted, check again
}
if (DUET.aggregate_clusters) // if for test purposes: always ON at release.
{
for (size_t i=0; i < size; ++i)
{
for (size_t cluster=0; cluster < i; ++cluster)
{
if (belongs(preclusters.values[i], preclusters.values[cluster], DUET.min_peak_dalpha, DUET.min_peak_ddelta))
{
preclusters.del(i,0.1);
--size; // Just deleted an element. No need to process extra 0-score entries.
--i; // The rest of the list was pushed up, process the next entry which is in the same position.
}
}
}
}
}
void heuristic_clustering2D(Histogram2D<real> &hist, RankList<real, Point2D<real> > &preclusters, const DUETcfg &DUET)
{
heuristic_pre_filter_clusters2D(hist, preclusters, DUET.min_peak_fall);
// cout << preclusters;
heuristic_aggregate_preclusters2D(preclusters, DUET);
}
// L2-norm for a vector with start and end point a, b
real distance(const Point2D<real> &a, const Point2D<real> &b)
{
return norm(b.x-a.x, b.y-a.y);
}
// Returns the index of the closest cluster in clusters.
size_t closest_cluster(const Point2D<real> &point, Buffer<Point2D<real> > &clusters)
{
const size_t size = clusters.size();
real dist, min_distance = FLT_MAX;
size_t min_i = 0;
// Find the closest cluster
for (size_t i=0; i < size; ++i)
{
dist = distance(point, clusters[i]);
if (dist < min_distance)
{
min_distance = dist;
min_i = i;
}
}
return min_i;
}
/// Returns the score for the DUET histogram based on the parameters p and q
real DUEThist_score(real x1re, real x1im, real x2re, real x2im, real omega, real p, real q)
{
real s_re, s_im, s_abs;
complex_multiply(x1re,x1im, x2re,x2im, &s_re,&s_im);
s_abs = norm(s_re,s_im);
return std::pow(s_abs,p)*std::pow(omega,q); // The tables of the powers of omega could be reused.
}
/** Taken from bessel.c, also distributed in this folder. Calculates the modified Bessel function I0. */
double bessi0( double x )
{
double ax,ans;
double y;
if ((ax=fabs(x)) < 3.75) {
y=x/3.75,y=y*y;
ans=1.0+y*(3.5156229+y*(3.0899424+y*(1.2067492
+y*(0.2659732+y*(0.360768e-1+y*0.45813e-2)))));
} else {
y=3.75/ax;
ans=(exp(ax)/sqrt(ax))*(0.39894228+y*(0.1328592e-1
+y*(0.225319e-2+y*(-0.157565e-2+y*(0.916281e-2
+y*(-0.2057706e-1+y*(0.2635537e-1+y*(-0.1647633e-1
+y*0.392377e-2))))))));
}
return ans;
}
/**
@param[in] K - Number of active sources: theta.size() >= K
@param[in] x - Must have fewer than RAND_MAX elements.
*/
void RANSAC (Buffer<real> &theta, Buffer<real> &x, int K, int RANSAC_samples_per_source)
{
const int N = x.size();
const int M = K * RANSAC_samples_per_source;
const int MAX_N = 5000;
const int MAX_M = 200; // Maximum number of RANSAC samples.
static Buffer<int> y(MAX_M);
Assert(MAX_N > N && MAX_M > M, "RANSAC compile-time constants too small.");
static Buffer<bool> I(MAX_N * MAX_M); // Since memory must be reused it will be dynamically addressed as if it was a linear 2D-array of dimensions (N,M).
I.clear();
for (int m=0; m < M; ++m)
{
y[m] = rand() % N;
// Fit(y[m])
}
}
void DUET_hist_add_score(Histogram2D<real> &hist, Histogram<real> &hist_alpha, Histogram<real> &hist_delta, real alpha, real delta, real X1_re, real X1_im, real X2_re, real X2_im, real omega, const DUETcfg &DUET)
{
if (std::isnan(alpha))
{
// This test can be taken out in the final system supposedly because it won't be padded with 0's, instead there will always be noise in the signal, and whatever the noise is it will produce frequencies. The thesis on the circular duet talks about this, that the window should have at least the size of the biggest consecutive chain of 0's possible to appear in the data ( so this doesn't happen ).
/*
static size_t alpha_isnan_count = 0;
++alpha_isnan_count;
printf("alpha=nan occurred %lu times.\n", alpha_isnan_count);
*/
//printf("nan value in alpha for t_block=%lu, f=%lu\n", time_block, f);
return;
}
real score = DUEThist_score(X1_re,X1_im, X2_re, X2_im, omega, DUET.p, DUET.q);
if (DUET.use_smoothing)
{
hist.smooth_add(score, alpha, delta, DUET.smoothing_Delta_alpha, DUET.smoothing_Delta_delta);
hist_alpha.smooth_add(score, alpha, DUET.smoothing_Delta_alpha);
hist_delta.smooth_add(score, delta, DUET.smoothing_Delta_delta);
}
else
{
hist(alpha, delta) += score;
// #error here
hist_alpha(alpha) += score;
hist_delta(delta) += score;
}
}
/// Calculates (alpha,delta) for a time block and adds to the histogram.
void calc_alpha_delta(idx time_block, idx pN, idx sample_rate_Hz,
Buffer<real> &X1, Buffer<real> &X2,
Matrix<real,MatrixAlloc::Rows> &alpha,
Matrix<real,MatrixAlloc::Rows> &delta,
Histogram2D<real> &hist,
Histogram<real> &hist_alpha, Histogram<real> &hist_delta,
const DUETcfg &DUET)
{
static real df = sample_rate_Hz/(real)pN;
/*
f = 0 Hz:
d_Re = X1(0) / X2(0);
d_Im = 0;
*/
real a = X2[0] / X1[0];
alpha(time_block, 0) = a - 1/a;
delta(time_block, 0) = 0.0;
idx fI; // imaginary part index
real _alpha, _delta; // aliases to avoid unneeded array re-access.
real omega;
if (DUET.FFT_p > 1) // Use the phase-aliasing correction extension.
{
for (idx f = 1; f < pN/2 - 1; ++f)
{
fI = pN-f; // imaginary part index
std::complex<real> F (std::complex<real>(X2[f ],X2[fI ])/std::complex<real>(X1[f ],X1[fI ]));
std::complex<real> Feps(std::complex<real>(X2[f+1],X2[fI-1])/std::complex<real>(X1[f+1],X1[fI-1]));
omega = _2Pi * f*df; // f_Hz = f*df
a = std::abs(F);
_alpha = alpha(time_block, f) = a - 1/a;
_delta = delta(time_block, f) = std::fmod(pN/_2Pi * (std::arg(F)-std::arg(Feps)), pN);
DUET_hist_add_score(hist, hist_alpha, hist_delta, _alpha, _delta, X1[f],X1[fI], X2[f],X2[fI], omega, DUET);
}
}
else // Standard DUET without phase-aliasing correction.
{
for (idx f = 1; f < pN/2; ++f)
{
idx fI = pN-f; // imaginary part index
std::complex<real> F(std::complex<real>(X2[f],X2[fI])/std::complex<real>(X1[f],X1[fI]));
omega = _2Pi * f*df; // f_Hz = f*df
a = std::abs(F);
_alpha = alpha(time_block, f) = a - 1/a;
_delta = delta(time_block, f) = - std::arg(F)/omega;
DUET_hist_add_score(hist, hist_alpha, hist_delta, _alpha, _delta, X1[f],X1[fI], X2[f],X2[fI], omega, DUET);
}
}
}
// Window functions
real Hann(idx n, idx N)
{
return 0.5 * (1.0 - std::cos(_2Pi*n/(N-1.0)));
}
real Hamming0(idx n, idx N)
{
return 0.53836 + 0.46164*std::cos(_2Pi*n/(N-1.0));
}
real Hamming(idx n, idx N)
{
// return 1 - (0.53836 + 0.46164*std::cos(_2Pi*n/(N-1.0)));
return 0.46164 - 0.46164*std::cos(_2Pi*n/(N-1.0));
}
real Rectangular(idx n, idx N)
{
return 1;
}
#define RELEASE(x) {}
/*
/// Copy one column from one matrix to another
void copycol(Matrix<real> &b, Matrix<real> &a, size_t to_col, size_t from_col)
{
Assert(b.cols() == a.cols() && b.size() == a.size(), "Different sizes!");
for (size_t row = 0; row < a.rows(); ++row)
b(row,to_col) = a(row,from_col);
}
*/
// After giving one buffer, at the end of the next (blocking) call, it can be released
// Cannot be reassigned to a different output because it stores the previous buffer and it would have conflicts, thus all the data must be passed
size_t write_data(Matrix<real> &o, Matrix<real> *new_buffer, const size_t FFT_N, const size_t FFT_slide)
{
static int buffers = 1; // how many buffers are in current use (state variable)
static Matrix<real> *a=new_buffer, *b=NULL;
static size_t i = 0, p = 0;
if (FFT_slide < FFT_N) // Up to 50% overlap
{
if (buffers == 2)
{
b = new_buffer;
while (i < FFT_N)
{
//o[p] = a[i] + b[i-FFT_slide];
for (uint row = 0; row < o.rows(); ++row)
o(row, p) = (*a)(row,i) + (*b)(row,i-FFT_slide);
++p;
++i;
}
RELEASE(a);
i = FFT_N-FFT_slide;
a = b;
buffers = 1;
}
// Buffers == 1
while (i < FFT_slide)
{
// o[p] = a[i]
for (uint row = 0; row < o.rows(); ++row)
o(row, p) = (*a)(row, i);
++p;
++i;
}
buffers = 2;
// Now wait for new call with new_buffer
}
else // No overlap
{
i = 0;
a = new_buffer;
while (i < FFT_slide) // == FFT_N
{
// o[p] = a[i]
for (uint row = 0; row < o.rows(); ++row)
o(row,p) = (*a)(row, i);
++p;
++i;
}
}
return p;
}
/// Transforms alpha back to a.
real alpha2a (real alpha)
{
return (alpha + std::sqrt(alpha*alpha + 4.0)) * 0.5;
}
/// Fills a buffer of size FFT_N/2 // To each bin will be assigned the number of the source. values < 0 indicate that the bin won't be assigned a source (noise or intentional algorithm rejection/discard).
/// Thus, a single buffer is required to hold all the masks
void build_masks(Buffer<int> &masks, real *alpha, real *delta, real *X1, real *X2, Buffer<Point2D<real> > &clusters, idx FFT_N, idx FFT_half_N, real FFT_df, Buffer<real> &calc_buffer)
{
Buffer<int> old_masks(masks);
idx masks_diffs = 0;
int K = clusters.size();
for (idx f = 0; f < FFT_half_N; ++f)
{
real omega = _2Pi * f * FFT_df;
idx f_im = FFT_N - f;
// Too simplistic: masks[f] = closest_cluster(Point2D<real>(alpha[f],delta[f]), clusters);
for (int k=0; k < K; ++k)
{
real a_k = alpha2a(clusters[k].x);
real delta_k = clusters[k].y;
calc_buffer[k] = std::norm(a_k*std::polar<real>(1,-delta_k*omega) * std::complex<real>(X1[f],X1[f_im]) - std::complex<real>(X2[f],X2[f_im])) / (1.0 + a_k*a_k);
}
masks[f] = array_ops::min_index(calc_buffer(), K);
old_masks[f] = closest_cluster(Point2D<real>(alpha[f],delta[f]), clusters);
if (masks[f]!=old_masks[f])
masks_diffs += 1;
}
#ifdef OLD_MASK_BUILD
masks = old_masks;
#endif // OLD_MASK_BUILD
// cout << RED << masks_diffs << NOCOLOR << endl;
}
void apply_masks(Matrix<real> &buffers, real *alpha, real *X1, real *X2, Buffer<int> &masks, Buffer<Point2D<real> > &clusters, uint active_sources, idx FFT_N, idx FFT_half_N, real FFT_df, fftw_plan &FFTi_plan, Buffer<real> &Xo)
{
/*
for (uint source = 0; source < active_sources; ++source)
{
Xo.clear();
if (masks[0] == source)
Xo[0] = X[0];
for (uint f = 1, f_max = FFT_N/2; f < f_max; ++f)
{
if (masks[f] == source)
{
uint f_im = FFT_N - f;
Xo[f ] = X[f ];
Xo[f_im] = X[f_im];
}
}
fftw_execute_r2r(FFTi_plan, Xo(), buffers(source));
}
buffers /= (real)FFT_N;
*/
// Rebuild one source per iteration to reuse the FFT plan (only 1 needed).
for (uint source = 0; source < active_sources; ++source)
{
Xo.clear();
if (masks[0] == source)
{
real a_k = alpha2a(clusters[source].x);
Xo[0] = a_k*X1[0]-X2[0];
Xo[0] *= Xo[0] / (1 + a_k*a_k);
}
for (uint f = 1, f_max = FFT_N/2; f < f_max; ++f)
{
if (masks[f] == source)
{
uint f_im = FFT_N - f;
real a_k = alpha2a(clusters[source].x);
real delta_k = clusters[source].y;
real omega = _2Pi * f * FFT_df;
std::complex<real> X(std::complex<real>(X1[f],X1[f_im])+std::polar<real>(a_k,delta_k*omega) * std::complex<real>(X2[f],X2[f_im]));
#ifdef OLD_PEAK_ASSIGN
Xo[f ] = X1[f ];
Xo[f_im] = X1[f_im];
#else
Xo[f ] = X.real();
Xo[f_im] = X.imag();
#endif // OLD_PEAK_ASSIGN
}
}
fftw_execute_r2r(FFTi_plan, Xo(), buffers(source));
}
buffers /= (real)FFT_N;
}
/** Dtotal metric to compare the original signal with the extracted one.
@param[in] e - Estimated signal
@param[in] o - Original signal
@param[in] samples - Number of samples
*/
real Dtotal(const real *e, const real *o, idx samples)
{
real O2 = array_ops::energy(o, samples);
real E2 = array_ops::energy(e, samples);
real eo = std::abs(array_ops::inner_product(e, o, samples));
real eo2 = eo*eo;
real D = (E2*O2 - eo2) / eo2; // Takes into account the modulus of both signals
return D;
}
real SNR(const real *e, const real *o, idx samples)
{
Buffer<real> e_normalized(e,samples), o_normalized(o,samples);
// This normalization guarantees that the energies of each signal is 1.
e_normalized /= std::sqrt(array_ops::energy(e_normalized(), samples));
o_normalized /= std::sqrt(array_ops::energy(o_normalized(), samples));
// 2 stands for square(d)
Buffer<real> x2(samples), x_diff2(samples);
for (size_t i=0; i < samples; ++i)
{
x2[i] = o_normalized[i]*o_normalized[i];
x_diff2[i] = e_normalized[i]-o_normalized[i];
x_diff2[i] *= x_diff2[i];
}
real sum_x2 = array_ops::sum(x2(),samples);
real sum_x_diff2 = array_ops::sum(x_diff2(), samples);
return 10 * std::log10(sum_x2/sum_x_diff2);
}
/** Relative Energy
@param[in] e - Estimated signal
@param[in] o - Original signal
@param[in] samples - Number of samples
*/
real Energy_ratio(const real *e, const real *o, idx samples)
{
real E_e = array_ops::energy(e, samples);
real E_o = array_ops::energy(o, samples);
return E_e/E_o;
}
bool in (int val, Buffer<int> &list)
{
size_t size = list.size();
for (size_t i=0; i < size; ++i)
if (val == list[i])
return true;
return false;
}
/** Provides the separation stats of the mixtures.
*/
void separation_stats(Matrix<real> &s, Matrix<real> &o, int N, idx samples)
{
real dtotal;
static Buffer<real> dtotals(N); // Store the results to find the minimum
static Buffer<int> matches(N); // Indices of the estimated mixtures that already have the minimum distortion measure (match found)
matches.clear();
for (int i = 0; i < N; ++i)
{
// Compare the score against all the signals and choose the smallest.
// Notice that the larger peak in the histogram should get the clearest reseults so we start with it since this is already sorted by score.
// Notice that two signals can't be compared against the same original.
for (int i_original = 0; i_original < N; ++i_original)
{/*
// UNCOMMENT THIS PART IF THE GUARANTEE IS REMOVED! (***)
if (in(i_original+1, matches)) // 0's can't be used thus +1 is applied
dtotals[i_original] = FLT_MAX;
else*/
// The original signal wasn't matched yet.
dtotals[i_original] = Dtotal(s(i), o(i_original), samples);
}
//cout << "dtotals:" << dtotals;
// Find the matching index and add it to the exlusion list for the next signals.
size_t match_index = array_ops::min_index(dtotals(), N);
// IF YOU REMOVE THIS GUARANTEE ADD THE REGION WITH THE COMMENT (***)
//Guarantee0(in(match_index+1, matches), "Dtotal was minimal for two sources: Probably signals are mixed!");
if (in(match_index+1, matches))
printf(RED "Dtotal was minimal for two sources (match=%lu): Probably signals weren't unmixed successfuly!\n" NOCOLOR, match_index);
matches[i] = match_index+1; // 0's cant be used
dtotal = dtotals[match_index];
// Other stats
real E_r = Energy_ratio(s(i), o(match_index), samples);
real snr = SNR(s(i), o(match_index), samples);
printf(BLUE "s%d : Dtotal=%g (%g/sample) SNR=%gdB E_r=%g\n" NOCOLOR, i, dtotal, dtotal/(real)samples, snr, E_r);
}
/*
for (int i = 0; i < N; ++i)
{
real dtotal = Dtotal(s(i),o(i),samples);
printf(BLUE "s%d : Dtotal = %f\n" NOCOLOR, i, dtotal);
}
*/
}
void build_window(Buffer<real> &W, real (*Wfunction)(idx n, idx N))
{
idx N = W.size();
for (idx n=0; n < N; ++n)
W[n] = Wfunction(n,N);
}
/**
Arguments: prgm [FFT_N] [x1_wav] [x2_wav]
*/
int main(int argc, char **argv)
{
/* Name convention throughout this file:
i - input
o - output
m - magnitude
and capital letters for the frequency domain
*/
/*
Histogram2D<real> hi(3,5, -2,2,-5,5, HistogramBounds::Boundless);
hi.bin(1,0) = 3;
print(hi);
return 1;
*/
/*
// Test output overlap
Matrix<real> a(1,1000), b(1,10000);
for (int i=0; i < 1000; ++i)
a(0,i) = Hann(i,1000);
for (int loop=0; loop<5;++loop)
write_data(b, &a, 1000, 990);
Gnuplot p;
p.plot_y(b(),10000,"Hann");
wait();
return 0;
*/
Options o("settings.cfg", Quit, 1);
DUETcfg _DUET; // Just to initialize, then a const DUET is initialized from this one.
int WAIT = o.i("wait");
fftw_plan xX1_plan, xX2_plan, Xxo_plan;
int FFT_flags;
const int N_max = o.i("N_max");
int N;
Buffer<real> tmp_real_buffer_N_max(N_max); // For calculations in sub-functions but we must allocate the space already
// Choose mic input files
/*
std::string x1_filepath = (argc == 4 ? argv[2] : o("x1_wav"));
std::string x2_filepath = (argc == 4 ? argv[3] : o("x2_wav"));
*/
std::string x1_filepath = o("x1_wav");//"sounds/x0.wav";
std::string x2_filepath = o("x2_wav");//"sounds/x1.wav";
// Estimated and simulation (true) centroids
real calpha[N_max], true_alpha[N_max];
real cdelta[N_max], true_delta[N_max];
// Read simulation parameters
std::ifstream sim;
sim.open("simulation.log");
Guarantee(sim.is_open(), "Couldn't open simulation log!");
sim >> N;
printf(YELLOW "N=%d" NOCOLOR, N);
for (uint i = 0; i < N; ++i)
sim >> true_alpha[i] >> true_delta[i];
// If N_max > N: Make the remaining true locations invisible by drawing over the same position of one of the active sources
for (uint i = N; i < N_max; ++i)
{
true_alpha[i] = true_alpha[0];
true_delta[i] = true_delta[0];
}
sim.close();
// Write data for gnuplot real source positions overlay.
std::ofstream sim_log;
sim_log.open("s.dat");
for (idx i=0; i < N; ++i)
// The last column is required for splot (0 height suffices for the 2D hist, not the 3D, which should have the height at that point).
sim_log << true_alpha[i] << " " << true_delta[i] << " 0\n\n";
sim_log.close();
SndfileHandle x1_file(x1_filepath), x2_file(x2_filepath);
if ( ! wav::ok(x1_file) || ! wav::ok(x2_file) )
return EXIT_FAILURE;
Guarantee(wav::mono(x1_file) && wav::mono(x2_file), "Input files must be mono.");
const uint sample_rate_Hz = x1_file.samplerate();
const idx samples = x1_file.frames();
Buffer<real> x1_wav(samples), x2_wav(samples);
x1_file.read(x1_wav(), samples);
x2_file.read(x2_wav(), samples);
// Only x1's are needed since that's the chosen channel for source separation
Matrix<real> original_waves_x1(N, samples);
for (int i = 0; i < N; ++i)
{
SndfileHandle wav_file("sounds/"+std::to_string(i)+"x0.wav");
if (! wav::ok (wav_file))
return EXIT_FAILURE;
wav_file.read(original_waves_x1(i), samples);
}
printf("\nProcessing input file with %lu frames @ %u Hz.\n\n",
samples, sample_rate_Hz);
printf("Max int: %d\n"
"Max idx: %ld\n", INT_MAX, LONG_MAX);
printf("Indexing usage: %.2f%%\n\n", 0.01*(float)x1_file.frames()/(float)LONG_MAX);
const idx FFT_N = (argc > 1 ? (idx)strtol(argv[1], NULL, 10) : o.i("FFT_N"));
_DUET.FFT_N = FFT_N;
Guarantee0(FFT_N % 2, "System implemented for FFTs with even size.");
_DUET.FFT_slide_percentage = o.i("FFT_slide_percentage", Warn);
if (! _DUET.FFT_slide_percentage)
_DUET.FFT_slide_percentage = 100;
_DUET.FFT_slide = FFT_N * (_DUET.FFT_slide_percentage/100.);
Guarantee(_DUET.FFT_slide <= _DUET.FFT_N, "FFT_slide(%ld) > FFT_N(%ld)", _DUET.FFT_slide, _DUET.FFT_N);
printf(YELLOW "FFT_N = %ld\n" "FFT_slide = %ld (%ld%%)\n" NOCOLOR, FFT_N, _DUET.FFT_slide, _DUET.FFT_slide_percentage);
const idx FFT_slide = _DUET.FFT_slide;
// This will require triple-buffering
Guarantee(FFT_slide >= FFT_N/2, "FFT_slide(%ld) > FFT_N/2(%ld)", FFT_slide, FFT_N/2);
_DUET.use_window = 1;
// Frequency oversampling
_DUET.FFT_p = o.i("FFT_oversampling_factor");
_DUET.FFT_pN = _DUET.FFT_p * _DUET.FFT_N;
const idx FFT_pN = _DUET.FFT_pN;
const uint time_blocks = 1 + blocks(samples, FFT_slide);
//// Storage allocation ///////
// Initialize the buffers all with the same characteristics and aligned for FFTW use.
Buffer<real> x1(FFT_pN, 0, fftw_malloc, fftw_free), x2(x1), X1(x1), X2(x1), xo(x1), Xo(x1);
// We're going to save at least one of the microphone transforms for all time blocks for the static heuristic reconstruction
Matrix<real> X1_history(time_blocks, FFT_pN), X2_history(time_blocks, FFT_pN);
// Organized as (time, frequency)
Matrix<real,MatrixAlloc::Rows>
alpha(time_blocks, FFT_pN/2),
delta(time_blocks, FFT_pN/2),
wav_out(N_max, time_blocks*FFT_slide);
const real FFT_df = sample_rate_Hz / (real) FFT_N;
FFT_flags = FFTW_ESTIMATE; // Use wisdom + FFTW_EXHAUSTIVE later!
cout << "Estimating FFT plan..." << endl;
cout << "The fast way!\n";
FFT_flags = FFTW_ESTIMATE;
xX1_plan = fftw_plan_r2r_1d(FFT_pN, x1(), X1(), FFTW_R2HC, FFT_flags);
xX2_plan = fftw_plan_r2r_1d(FFT_pN, x2(), X2(), FFTW_R2HC, FFT_flags);
Xxo_plan = fftw_plan_r2r_1d(FFT_pN, Xo(), xo(), FFTW_HC2R, FFT_flags);
cout << "DONE" << endl;
const HistogramBounds::Type hist_bound_type = HistogramBounds::Boundless;
Histogram2D<real> hist(o.d("hist.dalpha"), o.d("hist.ddelta"),
o.d("alpha.min"), o.d("alpha.max"),
o.d("delta.min"), o.d("delta.max"),
hist_bound_type);
if (hist.bins() > 1e6)
{
puts(RED "Exiting: Too many bins" NOCOLOR);
exit(1);
}
Histogram<real>
hist_alpha(o.d("hist.dalpha"), o.d("alpha.min"), o.d("alpha.max"), hist_bound_type),
hist_delta(o.d("hist.ddelta"), o.d("delta.min"), o.d("delta.max"), hist_bound_type);
Buffer<real> alpha_range(hist_alpha.bins()), delta_range(hist_delta.bins());
for (size_t i=0; i < alpha_range.size(); ++i)
{
real alpha_min = o.d("alpha.min");
real alpha_max = o.d("alpha.max");
real dalpha = (alpha_max-alpha_min)/(real)alpha_range.size();
alpha_range[i] = alpha_min + i*dalpha;
}
for (size_t i=0; i < delta_range.size(); ++i)
{
real delta_min = o.d("delta.min");
real delta_max = o.d("delta.max");
real ddelta = (delta_max-delta_min)/(real)delta_range.size();
delta_range[i] = delta_min + i*ddelta;
}
Histogram2D<real> cumulative_hist(hist), old_hist(hist);
hist.print_format();
/*
cumulative_hist.clear();