-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrepet.m
1322 lines (1036 loc) · 67.4 KB
/
repet.m
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
classdef repet
% repet This Matlab class a number of functions for the REpeating
% Repetition is a fundamental element in generating and perceiving
% structure. In audio, mixtures are often composed of structures
% where a repeating background signal is superimposed with a varying
% foreground signal (e.g., a singer overlaying varying vocals on a
% repeating accompaniment or a varying speech signal mixed up with a
% repeating background noise). On this basis, we present the
% REpeating Pattern Extraction Technique (REPET), a simple approach
% for separating the repeating background from the non-repeating
% foreground in an audio mixture. The basic idea is to find the
% repeating elements in the mixture, derive the underlying repeating
% models, and extract the repeating background by comparing the
% models to the mixture. Unlike other separation approaches, REPET
% does not depend on special parameterizations, does not rely on
% complex frameworks, and does not require external information.
% Because it is only based on repetition, it has the advantage of
% being simple, fast, blind, and therefore completely and easily
% automatable.
%
% repet Methods:
% original - Compute the original REPET.
% extended - Compute REPET extended.
% adaptive - Compute the adaptive REPET.
% sim - Compute REPET-SIM.
% simonline - Compute REPET-SIM.
%
% repet Other:
% specshow - Display an spectrogram in dB, seconds, and Hz.
%
% Author:
% Zafar Rafii
% zafarrafii@gmail.com
% http://zafarrafii.com
% https://github.com/zafarrafii
% https://www.linkedin.com/in/zafarrafii/
% 01/27/21
% Define the properties
properties (Access = private, Constant = true)
% Define the cutoff frequency in Hz for the dual high-pass filter
% of the foreground (vocals are rarely below 100 Hz)
cutoff_frequency = 100;
% Define the period range in seconds for the beat spectrum (for the
% original REPET, REPET extented, and the adaptive REPET)
period_range = [1,10];
% Define the segment length and step in seconds (for REPET extented
% and the adaptive REPET)
segment_length = 10;
segment_step = 5;
% Define the filter order for the median filter (for the adaptive
% REPET)
filter_order = 5;
% Define the minimal threshold for two similar frames in [0,1],
% minimal distance between two similar frames in seconds, and
% maximal number of similar frames for every frame (for REPET-SIM
% and the online REPET-SIM)
similarity_threshold = 0;
similarity_distance = 1;
similarity_number = 100;
% Define the buffer length in seconds (for the online REPET-SIM)
buffer_length = 10;
end
% Define the public methods
methods (Access = public, Static = true)
function background_signal = original(audio_signal,sampling_frequency)
% original Compute the original REPET.
% The original REPET aims at identifying and extracting the
% repeating patterns in an audio mixture, by estimating a
% period of the underlying repeating structure and modeling a
% segment of the periodically repeating background.
%
% background_signal = repet.original(audio_signal,sampling_frequency)
%
% Inputs:
% audio_signal: audio signal [number_samples,number_channels]
% sampling_frequency: sampling frequency in Hz
% Output:
% background_signal: background signal [number_samples,number_channels]
%
% Example: Estimate the background and foreground signals, and display their spectrograms.
% % Read the audio signal with its sampling frequency in Hz
% [audio_signal,sampling_frequency] = audioread('audio_file.wav');
%
% % Estimate the background signal, and the foreground signal
% background_signal = repet.original(audio_signal,sampling_frequency);
% foreground_signal = audio_signal-background_signal;
%
% % Write the background and foreground signals
% audiowrite('background_signal.wav',background_signal,sampling_frequency)
% audiowrite('foreground_signal.wav',foreground_signal,sampling_frequency)
%
% % Compute the mixture, background, and foreground spectrograms
% window_length = 2^nextpow2(0.04*sampling_frequency);
% window_function = hamming(window_length,'periodic');
% step_length = window_length/2;
% audio_spectrogram = abs(spectrogram(mean(audio_signal,2),window_length,window_length-step_length));
% background_spectrogram = abs(spectrogram(mean(background_signal,2),window_length,window_length-step_length));
% foreground_spectrogram = abs(spectrogram(mean(foreground_signal,2),window_length,window_length-step_length));
%
% % Display the mixture, background, and foreground spectrograms in dB, seconds, and Hz
% time_duration = length(audio_signal)/sampling_frequency;
% maximum_frequency = sampling_frequency/8;
% xtick_step = 1;
% ytick_step = 1000;
% figure
% subplot(3,1,1)
% repet.specshow(audio_spectrogram(1:window_length/8,:),time_duration,maximum_frequency,xtick_step,ytick_step)
% title('Audio spectrogram (dB)')
% subplot(3,1,2)
% repet.specshow(background_spectrogram(1:window_length/8,:),time_duration,maximum_frequency,xtick_step,ytick_step)
% title('Background spectrogram (dB)')
% subplot(3,1,3)
% repet.specshow(foreground_spectrogram(1:window_length/8,:),time_duration,maximum_frequency,xtick_step,ytick_step)
% title('Foreground spectrogram (dB)')
% Get the number of samples and channels in the audio signal
[number_samples,number_channels] = size(audio_signal);
% Set the parameters for the STFT
% (audio stationary around 40 ms, power of 2 for fast FFT and constant overlap-add (COLA),
% periodic Hamming window for COLA, and step equal to half the window length for COLA)
window_length = 2^nextpow2(0.04*sampling_frequency);
window_function = hamming(window_length,'periodic');
step_length = window_length/2;
% Derive the number of time frames
% (given the zero-padding at the start and the end of the signal)
number_times = ceil(((number_samples+2*floor(window_length/2))-window_length) ...
/step_length)+1;
% Initialize the STFT
audio_stft = zeros(window_length,number_times,number_channels);
% Loop over the channels
for i = 1:number_channels
% Compute the STFT of the current channel
audio_stft(:,:,i) = repet.stft(audio_signal(:,i),window_function,step_length);
end
% Derive the magnitude spectrogram
% (with the DC component and without the mirrored frequencies)
audio_spectrogram = abs(audio_stft(1:window_length/2+1,:,:));
% Compute the beat spectrum of the spectrograms averaged over the channels
% (take the square to emphasize peaks of periodicitiy)
beat_spectrum = repet.beatspectrum(mean(audio_spectrogram,3).^2);
% Get the period range in time frames for the beat spectrum
period_range2 = round(repet.period_range*sampling_frequency/step_length);
% Estimate the repeating period in time frames given the period range
repeating_period = repet.periods(beat_spectrum,period_range2);
% Get the cutoff frequency in frequency channels
% for the dual high-pass filtering of the foreground
cutoff_frequency2 = round(repet.cutoff_frequency*window_length/sampling_frequency);
% Initialize the background signal
background_signal = zeros(number_samples,number_channels);
% Loop over the channels
for channel_index = 1:number_channels
% Compute the repeating mask for the current channel given the repeating period
repeating_mask = repet.mask(audio_spectrogram(:,:,channel_index),repeating_period);
% Perform a high-pass filtering of the dual foreground
repeating_mask(2:cutoff_frequency2+1,:) = 1;
% Recover the mirrored frequencies
repeating_mask = cat(1,repeating_mask,repeating_mask(end-1:-1:2,:));
% Synthesize the repeating background for the current channel
background_signal1 ...
= repet.istft(repeating_mask.*audio_stft(:,:,channel_index),window_function,step_length);
% Truncate to the original number of samples
background_signal(:,channel_index) = background_signal1(1:number_samples);
end
end
function background_signal = extended(audio_signal,sampling_frequency)
% extended Compute REPET extended.
% The original REPET can be easily extended to handle varying
% repeating structures, by simply applying the method along
% time, on individual segments or via a sliding window.
%
% background_signal = repet.extended(audio_signal,sampling_frequency)
%
% Inputs:
% audio_signal: audio signal [number_samples,number_channels]
% sampling_frequency: sampling frequency in Hz
% Output:
% background_signal: background signal [number_samples,number_channels]
%
% Example: Estimate the background and foreground signals, and display their spectrograms.
% % Read the audio signal with its sampling frequency in Hz
% [audio_signal,sampling_frequency] = audioread('audio_file.wav');
%
% % Estimate the background signal, and the foreground signal
% background_signal = repet.extended(audio_signal,sampling_frequency);
% foreground_signal = audio_signal-background_signal;
%
% % Write the background and foreground signals
% audiowrite('background_signal.wav',background_signal,sampling_frequency)
% audiowrite('foreground_signal.wav',foreground_signal,sampling_frequency)
%
% % Compute the mixture, background, and foreground spectrograms
% window_length = 2^nextpow2(0.04*sampling_frequency);
% window_function = hamming(window_length,'periodic');
% step_length = window_length/2;
% audio_spectrogram = abs(spectrogram(mean(audio_signal,2),window_length,window_length-step_length));
% background_spectrogram = abs(spectrogram(mean(background_signal,2),window_length,window_length-step_length));
% foreground_spectrogram = abs(spectrogram(mean(foreground_signal,2),window_length,window_length-step_length));
%
% % Display the mixture, background, and foreground spectrograms in dB, seconds, and Hz
% time_duration = length(audio_signal)/sampling_frequency;
% maximum_frequency = sampling_frequency/8;
% xtick_step = 1;
% ytick_step = 1000;
% figure
% subplot(3,1,1)
% repet.specshow(audio_spectrogram(1:window_length/8,:),time_duration,maximum_frequency,xtick_step,ytick_step)
% title('Audio spectrogram (dB)')
% subplot(3,1,2)
% repet.specshow(background_spectrogram(1:window_length/8,:),time_duration,maximum_frequency,xtick_step,ytick_step)
% title('Background spectrogram (dB)')
% subplot(3,1,3)
% repet.specshow(foreground_spectrogram(1:window_length/8,:),time_duration,maximum_frequency,xtick_step,ytick_step)
% title('Foreground spectrogram (dB)')
% Get the number of samples and channels
[number_samples,number_channels] = size(audio_signal);
% Get the segment length, step, and overlap in samples
segment_length2 = round(repet.segment_length*sampling_frequency);
segment_step2 = round(repet.segment_step*sampling_frequency);
segment_overlap2 = segment_length2-segment_step2;
% Get the number of segments
if number_samples < segment_length2+segment_step2
% Use a single segment if the signal is too short
number_segments = 1;
else
% Use multiple segments if the signal is long enough
% (the last segment could be longer)
number_segments = 1+floor((number_samples-segment_length2)/segment_step2);
% Use a triangular window for the overlapping parts
segment_window = triang(2*segment_overlap2);
end
% Set the parameters for the STFT
% (audio stationary around 40 ms, power of 2 for fast FFT and constant overlap-add (COLA),
% periodic Hamming window for COLA, and step equal to half the window length for COLA)
window_length = 2^nextpow2(0.04*sampling_frequency);
window_function = hamming(window_length,'periodic');
step_length = window_length/2;
% Get the period range in time frames for the beat spectrum
period_range2 = round(repet.period_range*sampling_frequency/step_length);
% Get the cutoff frequency in frequency channels
% for the dual high-pass filter of the foreground
cutoff_frequency2 = round(repet.cutoff_frequency*window_length/sampling_frequency);
% Initialize the background signal
background_signal = zeros(number_samples,number_channels);
% Loop over the segments
k = 0;
for j = 1:number_segments
% Check if there is a single segment or multiple ones
if number_segments == 1
% Use the whole signal as the segment
audio_segment = audio_signal;
segment_length2 = number_samples;
else
% Check if it is one of the first segments (same length)
% or the last one (could be longer)
if j < number_segments
audio_segment = audio_signal(k+1:k+segment_length2,:);
elseif j == number_segments
audio_segment = audio_signal(k+1:number_samples,:);
segment_length2 = length(audio_segment);
end
end
% Get the number of time frames
number_times = ceil((window_length-step_length+segment_length2)/step_length);
% Initialize the STFT
audio_stft = zeros(window_length,number_times,number_channels);
% Loop over the channels
for i = 1:number_channels
% Compute the STFT of the current channel
audio_stft(:,:,i) = repet.stft(audio_segment(:,i),window_function,step_length);
end
% Derive the magnitude spectrogram
% (with the DC component and without the mirrored frequencies)
audio_spectrogram = abs(audio_stft(1:window_length/2+1,:,:));
% Compute the beat spectrum of the spectrograms averaged over the channels
% (take the square to emphasize peaks of periodicitiy)
beat_spectrum = repet.beatspectrum(mean(audio_spectrogram,3).^2);
% Repeating period in time frames given the period range
repeating_period = repet.periods(beat_spectrum,period_range2);
% Initialize the background segment
background_segment = zeros(segment_length2,number_channels);
% Loop over the channels
for i = 1:number_channels
% Compute the repeating mask for the current channel
repeating_mask = repet.mask(audio_spectrogram(:,:,i),repeating_period);
% Perform a high-pass filtering of the dual foreground
repeating_mask(2:cutoff_frequency2+1,:) = 1;
% Recover the mirrored frequencies
repeating_mask = cat(1,repeating_mask,repeating_mask(end-1:-1:2,:));
% Synthesize the repeating background for the current channel
background_segment1 ...
= repet.istft(repeating_mask.*audio_stft(:,:,i),window_function,step_length);
% Truncate to the original number of samples
background_segment(:,i) = background_segment1(1:segment_length2);
end
% Check again if there is a single segment or multiple ones
if number_segments == 1
% Use the segment as the whole signal
background_signal = background_segment;
else
% Check if it is the first segment or the following ones
if j == 1
% Add the segment to the signal
background_signal(1:segment_length2,:) ...
= background_signal(1:segment_length2,:) ...
+ background_segment;
elseif j <= number_segments
% Perform a half windowing of the overlap part
% of the background signal on the right
background_signal(k+1:k+segment_overlap2,:) ...
= background_signal(k+1:k+segment_overlap2,:) ...
.*segment_window(segment_overlap2+1:2*segment_overlap2);
% Perform a half windowing of the overlap part
% of the background segment on the left
background_segment(1:segment_overlap2,:) ...
= background_segment(1:segment_overlap2,:) ...
.*segment_window(1:segment_overlap2);
% Add the segment to the signal
background_signal(k+1:k+segment_length2,:) ...
= background_signal(k+1:k+segment_length2,:) ...
+ background_segment;
end
% Update the index
k = k + segment_step2;
end
end
end
function background_signal = adaptive(audio_signal,sampling_frequency)
% adaptive Compute the adaptive REPET.
% The original REPET works well when the repeating background
% is relatively stable (e.g., a verse or the chorus in a
% song); however, the repeating background can also vary over
% time (e.g., a verse followed by the chorus in the song).
% The adaptive REPET is an extension of the original REPET
% that can handle varying repeating structures, by estimating
% the time-varying repeating periods and extracting the
% repeating background locally, without the need for
% segmentation or windowing.
%
% background_signal = repet.adaptive(audio_signal,sampling_frequency)
%
% Inputs:
% audio_signal: audio signal [number_samples,number_channels]
% sampling_frequency: sampling frequency in Hz
% Output:
% background_signal: background signal [number_samples,number_channels]
%
% Example: Estimate the background and foreground signals, and display their spectrograms.
% % Read the audio signal with its sampling frequency in Hz
% [audio_signal,sampling_frequency] = audioread('audio_file.wav');
%
% % Estimate the background signal, and the foreground signal
% background_signal = repet.adaptive(audio_signal,sampling_frequency);
% foreground_signal = audio_signal-background_signal;
%
% % Write the background and foreground signals
% audiowrite('background_signal.wav',background_signal,sampling_frequency)
% audiowrite('foreground_signal.wav',foreground_signal,sampling_frequency)
%
% % Compute the mixture, background, and foreground spectrograms
% window_length = 2^nextpow2(0.04*sampling_frequency);
% window_function = hamming(window_length,'periodic');
% step_length = window_length/2;
% audio_spectrogram = abs(spectrogram(mean(audio_signal,2),window_length,window_length-step_length));
% background_spectrogram = abs(spectrogram(mean(background_signal,2),window_length,window_length-step_length));
% foreground_spectrogram = abs(spectrogram(mean(foreground_signal,2),window_length,window_length-step_length));
%
% % Display the mixture, background, and foreground spectrograms in dB, seconds, and Hz
% time_duration = length(audio_signal)/sampling_frequency;
% maximum_frequency = sampling_frequency/8;
% xtick_step = 1;
% ytick_step = 1000;
% figure
% subplot(3,1,1)
% repet.specshow(audio_spectrogram(1:window_length/8,:),time_duration,maximum_frequency,xtick_step,ytick_step)
% title('Audio spectrogram (dB)')
% subplot(3,1,2)
% repet.specshow(background_spectrogram(1:window_length/8,:),time_duration,maximum_frequency,xtick_step,ytick_step)
% title('Background spectrogram (dB)')
% subplot(3,1,3)
% repet.specshow(foreground_spectrogram(1:window_length/8,:),time_duration,maximum_frequency,xtick_step,ytick_step)
% title('Foreground spectrogram (dB)')
% Get the number of samples and channels
[number_samples,number_channels] = size(audio_signal);
% Set the parameters for the STFT
% (audio stationary around 40 ms, power of 2 for fast FFT and constant overlap-add (COLA),
% periodic Hamming window for COLA, and step equal to half the window length for COLA)
window_length = 2^nextpow2(0.04*sampling_frequency);
window_function = hamming(window_length,'periodic');
step_length = window_length/2;
% Derive the number of time frames
% (given the zero-padding at the start and the end of the signal)
number_times = ceil(((number_samples+2*floor(window_length/2))-window_length) ...
/step_length)+1;
% Initialize the STFT
audio_stft = zeros(window_length,number_times,number_channels);
% Loop over the channels
for i = 1:number_channels
% STFT of the current channel
audio_stft(:,:,i) = repet.stft(audio_signal(:,i),window_function,step_length);
end
% Derive the magnitude spectrogram
% (with the DC component and without the mirrored frequencies)
audio_spectrogram = abs(audio_stft(1:window_length/2+1,:,:));
% Get the segment length and step in time frames for the beat spectrogram
segment_length2 = round(repet.segment_length*sampling_frequency/step_length);
segment_step2 = round(repet.segment_step*sampling_frequency/step_length);
% Compute the beat spectrogram of the spectrograms averaged over the channels
% (take the square to emphasize peaks of periodicitiy)
beat_spectrogram = repet.beatspectrogram(mean(audio_spectrogram,3).^2,segment_length2,segment_step2);
% Get the period range in time frames
period_range2 = round(repet.period_range*sampling_frequency/step_length);
% Estimate the repeating periods in time frames given the period range
repeating_periods = repet.periods(beat_spectrogram,period_range2);
% Get the cutoff frequency in frequency channels
% for the dual high-pass filter of the foreground
cutoff_frequency2 = round(repet.cutoff_frequency*window_length/sampling_frequency);
% Initialize the background signal
background_signal = zeros(number_samples,number_channels);
% Loop over the channels
for i = 1:number_channels
% Compute the repeating mask for the current channel given the repeating periods
repeating_mask ...
= repet.adaptivemask(audio_spectrogram(:,:,i),repeating_periods,repet.filter_order);
% Perform a high-pass filtering of the dual foreground
repeating_mask(2:cutoff_frequency2+1,:) = 1;
% Recover the mirrored frequencies
repeating_mask = cat(1,repeating_mask,repeating_mask(end-1:-1:2,:));
% Synthesize the repeating background for the current channel
background_signal1 = repet.istft(repeating_mask.*audio_stft(:,:,i),window_function,step_length);
% Truncate to the original number of samples
background_signal(:,i) = background_signal1(1:number_samples);
end
end
function background_signal = sim(audio_signal,sampling_frequency)
% sim REPET-SIM
% The REPET methods work well when the repeating background
% has periodically repeating patterns (e.g., jackhammer
% noise); however, the repeating patterns can also happen
% intermittently or without a global or local periodicity
% (e.g., frogs by a pond). REPET-SIM is a generalization of
% REPET that can also handle non-periodically repeating
% structures, by using a similarity matrix to identify the
% repeating elements.
%
% background_signal = repet.sim(audio_signal,sampling_frequency)
%
% Inputs:
% audio_signal: audio signal [number_samples,number_channels]
% sampling_frequency: sampling frequency in Hz
% Output:
% background_signal: background signal [number_samples,number_channels]
%
% Example: Estimate the background and foreground signals, and display their spectrograms.
% % Read the audio signal with its sampling frequency in Hz
% [audio_signal,sampling_frequency] = audioread('audio_file.wav');
%
% % Estimate the background signal, and the foreground signal
% background_signal = repet.sim(audio_signal,sampling_frequency);
% foreground_signal = audio_signal-background_signal;
%
% % Write the background and foreground signals
% audiowrite('background_signal.wav',background_signal,sampling_frequency)
% audiowrite('foreground_signal.wav',foreground_signal,sampling_frequency)
%
% % Compute the mixture, background, and foreground spectrograms
% window_length = 2^nextpow2(0.04*sampling_frequency);
% window_function = hamming(window_length,'periodic');
% step_length = window_length/2;
% audio_spectrogram = abs(spectrogram(mean(audio_signal,2),window_length,window_length-step_length));
% background_spectrogram = abs(spectrogram(mean(background_signal,2),window_length,window_length-step_length));
% foreground_spectrogram = abs(spectrogram(mean(foreground_signal,2),window_length,window_length-step_length));
%
% % Display the mixture, background, and foreground spectrograms in dB, seconds, and Hz
% time_duration = length(audio_signal)/sampling_frequency;
% maximum_frequency = sampling_frequency/8;
% xtick_step = 1;
% ytick_step = 1000;
% figure
% subplot(3,1,1)
% repet.specshow(audio_spectrogram(1:window_length/8,:),time_duration,maximum_frequency,xtick_step,ytick_step)
% title('Audio spectrogram (dB)')
% subplot(3,1,2)
% repet.specshow(background_spectrogram(1:window_length/8,:),time_duration,maximum_frequency,xtick_step,ytick_step)
% title('Background spectrogram (dB)')
% subplot(3,1,3)
% repet.specshow(foreground_spectrogram(1:window_length/8,:),time_duration,maximum_frequency,xtick_step,ytick_step)
% title('Foreground spectrogram (dB)')
% Get the number of samples and channels
[number_samples,number_channels] = size(audio_signal);
% Set the parameters for the STFT
% (audio stationary around 40 ms, power of 2 for fast FFT and constant overlap-add (COLA),
% periodic Hamming window for COLA, and step equal to half the window length for COLA)
window_length = 2^nextpow2(0.04*sampling_frequency);
window_function = hamming(window_length,'periodic');
step_length = window_length/2;
% Derive the number of time frames
% (given the zero-padding at the start and the end of the signal)
number_times = ceil(((number_samples+2*floor(window_length/2))-window_length) ...
/step_length)+1;
% Initialize the STFT
audio_stft = zeros(window_length,number_times,number_channels);
% Loop over the channels
for i = 1:number_channels
% STFT of the current channel
audio_stft(:,:,i) = repet.stft(audio_signal(:,i),window_function,step_length);
end
% Derive the magnitude spectrogram
% (with the DC component and without the mirrored frequencies)
audio_spectrogram = abs(audio_stft(1:window_length/2+1,:,:));
% Compute the self-similarity matrix of the spectrograms averaged over the channels
similarity_matrix = repet.selfsimilaritymatrix(mean(audio_spectrogram,3));
% Get the similarity distance in time frames
similarity_distance2 = round(repet.similarity_distance*sampling_frequency/step_length);
% Estimate the similarity indices for all the frames
similarity_indices ...
= repet.indices(similarity_matrix,repet.similarity_threshold,similarity_distance2,repet.similarity_number);
% Get the cutoff frequency in frequency channels
% for the dual high-pass filter of the foreground
cutoff_frequency2 = ceil(repet.cutoff_frequency*(window_length-1)/sampling_frequency);
% Initialize the background signal
background_signal = zeros(number_samples,number_channels);
% Loop over the channels
for i = 1:number_channels
% Compute the repeating mask for the current channel given the similarity indices
repeating_mask = repet.simmask(audio_spectrogram(:,:,i),similarity_indices);
% Perform a high-pass filtering of the dual foreground
repeating_mask(2:cutoff_frequency2+1,:) = 1;
% Recover the mirrored frequencies
repeating_mask = cat(1,repeating_mask,repeating_mask(end-1:-1:2,:));
% Synthesize the repeating background for the current channel
background_signal1 = repet.istft(repeating_mask.*audio_stft(:,:,i),window_function,step_length);
% Truncate to the original number of samples
background_signal(:,i) = background_signal1(1:number_samples);
end
end
function background_signal = simonline(audio_signal,sampling_frequency)
% simonline Online REPET-SIM
% REPET-SIM can be easily implemented online to handle
% real-time computing, particularly for real-time speech
% enhancement. The online REPET-SIM simply processes the time
% frames of the mixture one after the other given a buffer
% that temporally stores past frames.
%
% background_signal = repet.simonline(audio_signal,sampling_frequency)
%
% Inputs:
% audio_signal: audio signal [number_samples,number_channels]
% sampling_frequency: sampling frequency in Hz
% Output:
% background_signal: background signal [number_samples,number_channels]
%
% Example: Estimate the background and foreground signals, and display their spectrograms.
% % Read the audio signal with its sampling frequency in Hz
% [audio_signal,sampling_frequency] = audioread('audio_file.wav');
%
% % Estimate the background signal, and the foreground signal
% background_signal = repet.simonline(audio_signal,sampling_frequency);
% foreground_signal = audio_signal-background_signal;
%
% % Write the background and foreground signals
% audiowrite('background_signal.wav',background_signal,sampling_frequency)
% audiowrite('foreground_signal.wav',foreground_signal,sampling_frequency)
%
% % Compute the mixture, background, and foreground spectrograms
% window_length = 2^nextpow2(0.04*sampling_frequency);
% window_function = hamming(window_length,'periodic');
% step_length = window_length/2;
% audio_spectrogram = abs(spectrogram(mean(audio_signal,2),window_length,window_length-step_length));
% background_spectrogram = abs(spectrogram(mean(background_signal,2),window_length,window_length-step_length));
% foreground_spectrogram = abs(spectrogram(mean(foreground_signal,2),window_length,window_length-step_length));
%
% % Display the mixture, background, and foreground spectrograms in dB, seconds, and Hz
% time_duration = length(audio_signal)/sampling_frequency;
% maximum_frequency = sampling_frequency/8;
% xtick_step = 1;
% ytick_step = 1000;
% figure
% subplot(3,1,1)
% repet.specshow(audio_spectrogram(1:window_length/8,:),time_duration,maximum_frequency,xtick_step,ytick_step)
% title('Audio spectrogram (dB)')
% subplot(3,1,2)
% repet.specshow(background_spectrogram(1:window_length/8,:),time_duration,maximum_frequency,xtick_step,ytick_step)
% title('Background spectrogram (dB)')
% subplot(3,1,3)
% repet.specshow(foreground_spectrogram(1:window_length/8,:),time_duration,maximum_frequency,xtick_step,ytick_step)
% title('Foreground spectrogram (dB)')
% Get the number of samples and channels
[number_samples,number_channels] = size(audio_signal);
% Set the parameters for the STFT
% (audio stationary around 40 ms, power of 2 for fast FFT and constant overlap-add (COLA),
% periodic Hamming window for COLA, and step equal to half the window length for COLA)
window_length = 2^nextpow2(0.04*sampling_frequency);
window_function = hamming(window_length,'periodic');
step_length = window_length/2;
% Derive the number of time frames
number_times = ceil((number_samples-window_length)/step_length+1);
% Derive the number of frequency channels
number_frequencies = window_length/2+1;
% Get the buffer length in time frames
buffer_length2 = round((repet.buffer_length*sampling_frequency)/step_length);
% Initialize the buffer spectrogram
buffer_spectrogram = zeros(number_frequencies,buffer_length2,number_channels);
% Loop over the time frames to compute the buffer spectrogram
% (the last frame will be the frame to be processed)
k = 0;
for j = 1:buffer_length2-1
% Loop over the channels
for i = 1:number_channels
% Compute the FT of the segment
buffer_ft = fft(audio_signal(k+1:k+window_length,i).*window_function);
% Derive the spectrum of the frame
buffer_spectrogram(:,j,i) = abs(buffer_ft(1:number_frequencies));
end
% Update the index
k = k+step_length;
end
% Zero-pad the audio signal at the end
audio_signal = [audio_signal;zeros((number_times-1)*step_length+window_length-number_samples,number_channels)];
% Get the similarity distance in time frames
similarity_distance2 = round(repet.similarity_distance*sampling_frequency/step_length);
% Get the cutoff frequency in frequency channels
% for the dual high-pass filter of the foreground
cutoff_frequency2 = ceil(repet.cutoff_frequency*window_length/sampling_frequency);
% Initialize the background signal
background_signal = zeros((number_times-1)*step_length+window_length,number_channels);
% Loop over the time frames to compute the background signal
for j = buffer_length2:number_times
% Get the time index of the current frame
j0 = mod(j-1,buffer_length2)+1;
% Initialize the FT of the current segment
current_ft = zeros(window_length,number_channels);
% Loop over the channels
for i = 1:number_channels
% Compute the FT of the current segment
current_ft(:,i) = fft(audio_signal(k+1:k+window_length,i).*window_function);
% Derive the magnitude spectrum and update the buffer spectrogram
buffer_spectrogram(:,j0,i) = abs(current_ft(1:number_frequencies,i));
end
% Compute the cosine similarity between the spectrum of the current frame
% and the past frames, for all the channels
similarity_vector ...
= repet.similaritymatrix(mean(buffer_spectrogram,3),mean(buffer_spectrogram(:,j0,:),3));
% Estimate the indices of the similar frames
[~,similarity_indices] ...
= repet.localmaxima(similarity_vector,repet.similarity_threshold,similarity_distance2,repet.similarity_number);
% Loop over the channels
for i = 1:number_channels
% Compute the repeating spectrum for the current frame
repeating_spectrum = median(buffer_spectrogram(:,similarity_indices,i),2);
% Refine the repeating spectrum
repeating_spectrum = min(repeating_spectrum,buffer_spectrogram(:,j0,i));
% Derive the repeating mask for the current frame
repeating_mask = (repeating_spectrum+eps)./(buffer_spectrogram(:,j0,i)+eps);
% Perform a high-pass filtering of the dual foreground
repeating_mask(2:cutoff_frequency2+1,:) = 1;
% Recover the mirrored frequencies
repeating_mask = cat(1,repeating_mask,repeating_mask(end-1:-1:2));
% Apply the mask to the FT of the current segment
background_ft = repeating_mask.*current_ft(:,i);
% Take the inverse FT of the current segment
background_signal(k+1:k+window_length,i) ...
= background_signal(k+1:k+window_length,i)+real(ifft(background_ft));
end
% Update the index
k = k+step_length;
end
% Truncate the signal to the original length
background_signal = background_signal(1:number_samples,:);
% Normalize the signal by the gain introduced by the COLA (if any)
background_signal = background_signal/sum(window_function(1:step_length:window_length));
end
function specshow(audio_spectrogram, number_samples, sampling_frequency, xtick_step, ytick_step)
% specshow Display a spectrogram in dB, seconds, and Hz.
% repet.specshow(audio_spectrogram, number_samples, sampling_frequency, xtick_step, ytick_step)
%
% Inputs:
% audio_spectrogram: audio spectrogram (without DC and mirrored frequencies) [number_frequencies, number_times]
% number_samples: number of samples from the original signal
% sampling_frequency: sampling frequency from the original signal in Hz
% xtick_step: step for the x-axis ticks in seconds (default: 1 second)
% ytick_step: step for the y-axis ticks in Hz (default: 1000 Hz)
% Set the default values for xtick_step and ytick_step
if nargin <= 3
xtick_step = 1;
ytick_step = 1000;
end
% Get the number of frequency channels and time frames
[number_frequencies,number_times] = size(audio_spectrogram);
% Derive the number of Hertz and seconds
number_hertz = sampling_frequency/2;
number_seconds = number_samples/sampling_frequency;
% Derive the number of time frames per second and the number of frequency channels per Hz
time_resolution = number_times/number_seconds;
frequency_resolution = number_frequencies/number_hertz;
% Prepare the tick locations and labels for the x-axis
xtick_locations = xtick_step*time_resolution:xtick_step*time_resolution:number_times;
xtick_labels = xtick_step:xtick_step:number_seconds;
% Prepare the tick locations and labels for the y-axis
ytick_locations = ytick_step*frequency_resolution:ytick_step*frequency_resolution:number_frequencies;
ytick_labels = ytick_step:ytick_step:number_hertz;
% Display the spectrogram in dB, seconds, and Hz
imagesc(db(audio_spectrogram))
axis xy
colormap(jet)
xticks(xtick_locations)
xticklabels(xtick_labels)
yticks(ytick_locations)
yticklabels(ytick_labels)
xlabel('Time (s)')
ylabel('Frequency (Hz)')
end
end
% Define the private methods
methods (Access = private, Static = true)
function audio_stft = stft(audio_signal,window_function,step_length)
% audio_stft = repet.stft(audio_signal,window_function,step_length)
%
% Inputs:
% audio_signal: audio signal [number_samples,1]
% window_function: window function [window_length,1]
% step_length: step length in samples
% Output:
% audio_stft: audio STFT [window_length,number_frames]
% Get the number of samples and the window length in samples
number_samples = length(audio_signal);
window_length = length(window_function);
% Derive the zero-padding length at the start and at the end of the signal to center the windows
padding_length = floor(window_length/2);
% Compute the number of time frames given the zero-padding at the start and at the end of the signal
number_times = ceil(((number_samples+2*padding_length)-window_length)/step_length)+1;
% Zero-pad the start and the end of the signal to center the windows
audio_signal = [zeros(padding_length,1);audio_signal; ...
zeros((number_times*step_length+(window_length-step_length)-padding_length)-number_samples,1)];
% Initialize the STFT
audio_stft = zeros(window_length,number_times);
% Loop over the time frames
i = 0;
for j = 1:number_times
% Window the signal
audio_stft(:,j) = audio_signal(i+1:i+window_length).*window_function;
i = i+step_length;
end
% Compute the Fourier transform of the frames using the FFT
audio_stft = fft(audio_stft);
end
function audio_signal = istft(audio_stft,window_function,step_length)
% istft Compute the inverse short-time Fourier transform (STFT).
% audio_signal = repet.istft(audio_stft,window_function,step_length)
%
% Inputs:
% audio_stft: audio STFT [window_length,number_frames]
% window_function: window function [window_length,1]
% step_length: step length in samples
% Output:
% audio_signal: audio signal [number_samples,1]
% Get the window length in samples and the number of time frames
[window_length,number_times] = size(audio_stft);
% Compute the number of samples for the signal
number_samples = number_times*step_length+(window_length-step_length);
% Initialize the signal
audio_signal = zeros(number_samples,1);
% Compute the inverse Fourier transform of the frames and real part to ensure real values
audio_stft = real(ifft(audio_stft));
% Loop over the time frames
i = 0;
for j = 1:number_times
% Perform a constant overlap-add (COLA) of the signal
% (with proper window function and step length)
audio_signal(i+1:i+window_length) ...
= audio_signal(i+1:i+window_length)+audio_stft(:,j);
i = i+step_length;
end
% Remove the zero-padding at the start and at the end of the signal
audio_signal = audio_signal(window_length-step_length+1:number_samples-(window_length-step_length));
% Normalize the signal by the gain introduced by the COLA (if any)
audio_signal = audio_signal/sum(window_function(1:step_length:window_length));
end
function autocorrelation_matrix = acorr(data_matrix)
% acorr Compute the autocorrelation of the columns in a matrix using the WienerKhinchin theorem.
% autocorrelation_matrix = repet.acorr(data_matrix)
%
% Input:
% data_matrix: data matrix [number_rows,number_columns]
% Output:
% autocorrelation_matrix: autocorrelation matrix [number_lags,number_columns]
% Get the number of rows in each column
number_rows = size(data_matrix,1);
% Compute the power spectral density (PSD) of the columns
% (with zero-padding for proper autocorrelation)
data_matrix = abs(fft(data_matrix,2*number_rows)).^2;
% Compute the autocorrelation using the WienerKhinchin theorem
% (the PSD equals the Fourier transform of the autocorrelation)
autocorrelation_matrix = real(ifft(data_matrix));
% Discard the symmetric part
autocorrelation_matrix = autocorrelation_matrix(1:number_rows,:);
% Derive the unbiased autocorrelation