-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmpcf_weight.m
230 lines (218 loc) · 7.83 KB
/
nmpcf_weight.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
function [Xrr, Xss] = nmpcf_weight(Xabs,Yabs,L,div_segf, Kw, Kr, alpha ,lamR_0, lamR_1, lamW, C)
%
%
% Objetive: The purpose of this function is to add weight into the NMPCF
% decomposition. This allows to enhance the quality of the WS
% by removing the RS that implicitly appear in the human
% breathing process.
%
% Input:
% - Xabs: magnitude spectrogram X of the input mixture signal
% - Yabs: magnitude spectrogram Y of the respiratory training signal
% - L: number of segments
% - div_segf: vector that indicates the separation frames between segments
% - Kw: number of wheezing components
% - Kr: number of respiratory components
% - C: Indicator to distinguish between non-wheezing (C=0) and wheezing segments (C=1)
% Weighting factors:
% - alpha
% - lamR_0
% - lamR_1
% - lamW
%
% Output:
% - Xrr: estimated respiratory spectrograms
% - Xss: estimated wheezing spectrograms
%
%
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%% Vectors that contain the weighing factors
%--------------------------------------------------------------------------
lam = ones(1,L+1).*lamR_1;
lam(1)=alpha;
lam(find(C==0)+1)=lamR_0;
lamW = ones(1,L+1).*lamW;
lamW(find(C==1)+1)=0.05;
lamW(1)=eps;
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%% Normalization process
%--------------------------------------------------------------------------
norm = sum(sum(Xabs));
Xnorm = Xabs/norm;
[Fx,Tx] = size(Xnorm);
norm = sum(sum(Yabs));
Ynorm = Yabs/norm;
[Fy,Ty] = size(Ynorm);
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%% Segmentation of the input spectrogram X (L-segments)
%--------------------------------------------------------------------------
X = cell(1,L+1);
% Setting the respiratory training matrix
X{1}=Ynorm;
% L-segments
for seg=1:L
if seg<L
X{seg+1}=Xnorm(:,div_segf(seg):div_segf(seg+1)-1);
else
X{seg+1}=Xnorm(:,div_segf(seg):end);
end
end
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%% Initialization of the basis (U) and activation (V) matrices
%--------------------------------------------------------------------------
Ur = rand(Fx,Kr);
Vr = cell(1,L+1);
Us = cell(1,L+1);
Vs = cell(1,L+1);
for uv = 1:L+1
if uv==1
Vr{uv} = rand(Kr,Ty);
Us{uv} = zeros(Fx,Kw)+eps;
Vs{uv} = zeros(Kw,Ty)+eps;
else
tamu = size(X{uv},2);
Vr{uv} = rand(Kr,tamu);
Us{uv} = rand(Fx,Kw);
Vs{uv} = rand(Kw,tamu);
end
end
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%% Normalization process
%--------------------------------------------------------------------------
er=cell(1,6);
es=cell(1,6);
err=cell(1,6);
ess=cell(1,6);
v = sqrt(sum(Ur.^2));
Ur = bsxfun(@rdivide,Ur,v);
for nor = 1:L+1
er{nor} = ones(1,Kr)/(Kr);
es{nor} = ones(1,Kw)/(Kw);
err{nor} = ones(1,Kr)/(Kr);
ess{nor} = ones(1,Kw)/(Kw);
if nor==1
v = sqrt(sum(Vr{nor}.^2,2));
Vr{nor} = bsxfun(@rdivide,Vr{nor},v);
else
v = sqrt(sum(Vr{nor}.^2,2));
Vr{nor} = bsxfun(@rdivide,Vr{nor},v);
v = sqrt(sum(Us{nor}.^2));
Us{nor} = bsxfun(@rdivide,Us{nor},v);
v = sqrt(sum(Vs{nor}.^2,2));
Vs{nor} = bsxfun(@rdivide,Vs{nor},v);
end
end
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%% Updating process
%--------------------------------------------------------------------------
max_iter = 50;
B = 1;
% Initial reconstruction
Xn = cell(1,L+1);
for r=1:L+1
Xn{r} = [Ur*diag(er{r}) Us{r}*diag(es{r})]*[diag(err{r})*Vr{r}; diag(ess{r})*Vs{r}];
normaXn = (sum(sum(Xn{r}.^B)).^(1/B));
er{r} = er{r}/normaXn;
es{r} = es{r}/normaXn;
err{r} = err{r}/normaXn;
ess{r} = ess{r}/normaXn;
Xn{r} = [Ur*diag(er{r}) Us{r}*diag(es{r})]*[diag(err{r})*Vr{r}; diag(ess{r})*Vs{r}];
end
%--------------------------------------------------------------------------
for i = 1:max_iter
%----------------->Ur
for l=1:L+1
dnUr(:,:,l)=lam(l)*((Xn{l}.^(B-2)).*X{l}) * ((diag(err{l})*Vr{l})');
dpUr(:,:,l)=lam(l)*(Xn{l}.^(B-1)) * ((diag(err{l})*Vr{l})');
end
Ur = Ur.*(sum(dnUr,3)./(sum(dpUr,3)+2*1*(L+1)*Ur));
% Normalization
v = sqrt(sum(Ur.^2));
v(isinf(v)) = 1;
v(isnan(v)) = 1;
v((v==0)) = 1;
Ur = bsxfun(@rdivide,Ur,v);
Ur(isinf(Ur)) = 0;
Ur(isnan(Ur)) = 0;
for l=1:L+1
er{l} = er{l}.*v;
end
%----------------->Us
for l=1:L+1
dnUs = ((lamW(l))*((Xn{l}.^(B-2)).*X{l}) * ((diag(ess{l})*Vs{l})'));
dpUs = ((lamW(l))*(Xn{l}.^(B-1)) * ((diag(ess{l})*Vs{l})'));
Us{l} = Us{l}.*((dnUs)./((dpUs+2*1*Us{l})));clear dnUs dpUs
% Normalization
v = sqrt(sum(Us{l}.^2));
v(isinf(v)) = 1;
v(isnan(v)) = 1;
v((v==0)) = 1;
Us{l} = bsxfun(@rdivide,Us{l},v);
Us{l}(isinf(Us{l})) = 0;
Us{l}(isnan(Us{l})) = 0;
es{l} = es{l}.*v;
end
%--------------------------------------------------------------------------
for r=1:L+1
Xn{r} = [Ur*diag(er{r}) Us{r}*diag(es{r})]*[diag(err{r})*Vr{r}; diag(ess{r})*Vs{r}];
end
%--------------------------------------------------------------------------
%----------------->Vr
for l=1:L+1
dnVr = ((Ur*diag(er{l}))' * ((Xn{l}.^(B-2)).*X{l}));
dpVr = ((Ur*diag(er{l}))' * ((Xn{l}.^(B-1))));
Vr{l} = Vr{l}.*((dnVr)./(dpVr));clear dnVr dpVr;
% Normalization
v = sqrt(sum(Vr{l}.^2,2));
v(isinf(v)) = 1;
v(isnan(v)) = 1;
v((v==0)) = 1;
Vr{l} = bsxfun(@rdivide,Vr{l},v);
Vr{l}(isinf(Vr{l})) = 0;
Vr{l}(isnan(Vr{l})) = 0;
err{l} = err{l}.*v';
end
%----------------->Vs
for l=1:L+1
dnVs = ((Us{l}*diag(es{l}))' * ((Xn{l}.^(B-2)).*X{l}));
dpVs = ((Us{l}*diag(es{l}))' * ((Xn{l}.^(B-1))));
Vs{l} = Vs{l}.*((dnVs)./(dpVs));clear dnVs dpVs;
% Normalization
v = sqrt(sum(Vs{l}.^2,2));
v(isinf(v)) = 1;
v(isnan(v)) = 1;
v((v==0)) = 1;
Vs{l} = bsxfun(@rdivide,Vs{l},v);
Vs{l}(isinf(Vs{l})) = 0;
Vs{l}(isnan(Vs{l})) = 0;
ess{l} = ess{l}.*v';
end
%--------------------------------------------------------------------------
for r=1:L+1
Xn{r} = [Ur*diag(er{r}) Us{r}*diag(es{r})]*[diag(err{r})*Vr{r}; diag(ess{r})*Vs{r}];
end
%--------------------------------------------------------------------------
end
%% Final reconstruction
Xr=cell(1,L+1);
Xs=cell(1,L+1);
for s=2:L+1
Xr{s} = X{s}.*((Ur*diag(er{s})*diag(err{s})*Vr{s})./((Ur*diag(er{s})*diag(err{s})*Vr{s})+(Us{s}*diag(es{s})*diag(ess{s})*Vs{s})));
Xs{s} = X{s}.*((Us{s}*diag(es{s})*diag(ess{s})*Vs{s})./((Us{s}*diag(es{s})*diag(ess{s})*Vs{s})+(Ur*diag(er{s})*diag(err{s})*Vr{s})));
end
for s=2:L+1
if s==2
Xrr = Xr{s};
Xss = Xs{s};
else
Xrr = [Xrr Xr{s}];
Xss = [Xss Xs{s}];
end
end
%--------------------------------------------------------------------------