-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphase_alignment3.m
208 lines (165 loc) · 5.28 KB
/
phase_alignment3.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
%% Progetto 2016 - Elaborazione del Audio Digitale
% version 3.0
% Load audio files
[ref, Fs] = audioread('reference2.wav');
[test, FsT] = audioread('test2.wav');
% Time vectors
tr=0:1/Fs:(length(ref)-1)/Fs;
tt=0:1/Fs:(length(test)-1)/Fs;
%% track segmentation
fd=4; %frame duration
fs=fd*Fs; %frame size
ws=1; %window size parameter <--ws*fs--| fs |--ws*fs-->
% Segmentation
[refF, refN]=segment(ref,fs,fs);
[testF, testN]=segment(test,fs,fs);
% Set test & ref to the same length
diff=abs(refN-testN);
zp = {zeros(fs,1)}; %zero-padding unit
if refN~=testN
if refN<testN
for i=1:diff
refF = [refF zp];
refN = refN+1;
end
else
for i=1:diff
testF = [testF zp];
testN = testN+1;
end
end
end
%initialize vectors
lagVector=zeros(testN,1);
M=zeros(refN,1);
aligned=zeros(fs*testN,1);
xc=cell(1,testN);
lag=cell(1,testN);
%% XCorrelation calc
% Zero-padding at REF borders
%padding unit
zp = {zeros(fs,1)};
for i=1:ws
refF = [zp refF zp];
refN=refN+2;
end
coef=-ws:ws;
%init refw
refw=cell(1,2*ws+1);
searchwindow=cell(1,testN);
for i=1+ws:refN-ws
%search segment under the window construction
for j=1:2*ws+1
refw{j}=refF{i+coef(j)};
end
%build the search window
searchwindow{i-ws}={cat(1,refw{1:end})};
end
for i=1:testN
%convert cell in array
SW=cell2mat(searchwindow{i});
% Xcorrelation
[xc{i}, lag{i}]=xcorr(testF{i},SW);
[M(i),I]=max(abs(xc{i}));
lagVector(i)=lag{i}(I);
end
lagVector=optlags2(lagVector,40);
%lagVector(lagVector<0)=lagVector(lagVector<0)+2*ws*fs;
%lagVector(lagVector>0)=lagVector(lagVector>0)-2*ws*fs;
%lagVector=lagVector+2*ws*fs;
%% Alignment
for i=1:testN
%{
if i==1+ws
if sign(lagVector(i)) == 1
resT=padarray(testF{i}(lagVector(i)+1:end),[lagVector(i) 0],'post');
aligned(1:fs)=resT;
%Problem: Information loses at the beginin of the segment
elseif sign(lagVector(i)) == -1
resT=padarray(testF{i},[lagVector(i) 0],'pre');
aligned(1:fs+abs(lagVector(i)))=resT;
%Problem: Information loses at the end of the segment when Segment
%i=2 overlaps
else
aligned(1:fs)=testF{i};
end
else
%}
if M(i) > 3
start=((i-1)*fs)-lagVector(i);
stop=(start+fs)-1;
aligned(start:stop)=testF{i};
end
%{
%Super Plots
ta=0:1/Fs:(length(aligned)-1)/Fs;
FigHandle = figure('Position', [100, 100, 1049, 895]);
subplot(4,1,1), plot(refW{i}), ylabel('Ref')
string=sprintf('Segment number %d',i);
title(string)
subplot(4,1,2), plot(testF{i}), ylabel('Test')
subplot(4,1,3), plot(lag{i},abs(xc{i})), ylabel('XCorr')
subplot(4,1,4), plot(ta,aligned), ylabel('aligned(start-fs:stop+fs)')
subplot(5,1,5), plot(ta,aligned), ylabel('aligned')
%}
end
aligned=aligned(1+ws*fs:end);
% ####################### Plots ################################
%% Plot each segment with search window and Xcorrelation graphs
refFplot={cat(1,refF{1:end})};
refFplot=cell2mat(refFplot);
trf=0:1/Fs:(length(refFplot)-1)/Fs;
testFplot={cat(1,testF{1:end})};
testFplot=cell2mat(testFplot);
ttf=0:1/Fs:(length(testFplot)-1)/Fs;
testNplot=testN;
for i=1:testNplot
SWplot=cell2mat(searchwindow{i});
FigHandle = figure('Position', [100, 100, 949, 705]);
%subplot(4,1,1), plot(trf,refFplot)
subplot(3,1,1), plot(trf,refFplot),hold on,plot(trf(((i-1)*fs)+1 : ((i-1)*fs)+length(SWplot) ),SWplot,'r'), ylabel('Ref')
string=sprintf('Segment number %d',i);
title(string)
subplot(3,1,2), plot(ttf,testFplot,'Color',[0,0.5,0]), hold on, plot(ttf(((i-1)*fs)+1 : ((i-1)*fs)+length(testF{i}) ),testF{i},'Color',[1 .5 0]), ylabel('Test'),axis([-(ws*fs/Fs) 70-(ws*fs/Fs) -0.3 0.3])
subplot(3,1,3), plot(lag{i},abs(xc{i})), ylabel('XCorr')
end
%% Plot XCorrelation between entire tracks
[xcT, lagT]=xcorr(test,ref);
[xcA, lagA]=xcorr(aligned,ref);
figure
title('Cross correlation beteen reference and test (blue) and aligned (red) tracks)')
subplot(2,1,1), plot(lagT,xcT,'b'), ylabel('XCorr')
subplot(2,1,2), plot(lagA,xcA,'r'), ylabel('XCorr')
%% Plot Ref & Test Signals
figure
subplot(3,1,1), plot(tr,ref), ylabel('Ref')
string=sprintf('Reference & Test (.wav) Signals, with %d seconds segmentation',fd);
title(string)
subplot(3,1,2), plot(tt,test,'g','Color',[0,0.5,0]), ylabel('Test')
%Plot lags found in each segment
seg_lags=repelem(lagVector,fd);
seg_lags=seg_lags ./ Fs;
subplot(3,1,3), plot(1:length(seg_lags),seg_lags,'Color',[1,0,0]), xlabel('Time (s)'), ylabel('Lag time')
%% Plot xcorr max values for each segment
figure
string=sprintf('Values of xcorr peaks for track segments');
title(string)
x=1:testN;
stem(x,M,'filled','Color',[1,0.5,0],'LineWidth',2);
hold on
t=1:testN;
t(1:testN)=3;
plot(x,t,'g');
%% Plot refF & aligned signal
FigHandle = figure('Position', [100, 100, 949, 705]);
subplot(3,1,1), plot(tr,ref), ylabel('Ref')
grid minor
string=sprintf('Reference & Aligned (.wav) Signals, with %d seconds segmentation',fd);
title(string)
ta=0:1/Fs:(length(aligned)-1)/Fs;
subplot(3,1,2), plot(ta,aligned,'r'), ylabel('Aligned'), axis([0 60 -0.3 0.2])
grid minor
subplot(3,1,3), plot(tt,test,'Color',[0,0.5,0]), ylabel('Test'), axis([0 60 -0.3 0.2])
grid minor
%% Export final aligned track
audiowrite('alignedFINAL.wav',aligned,Fs);