Mercurial > hg > emotion-detection-top-level
comparison Code/Descriptors/Matlab/MPEG7/FromWeb/VoiceSauce/func_GetA1A2A3.m @ 4:92ca03a8fa99 tip
Update to ICASSP 2013 benchmark
author | Dawn Black |
---|---|
date | Wed, 13 Feb 2013 11:02:39 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:e1cfa7765647 | 4:92ca03a8fa99 |
---|---|
1 function [A1, A2, A3] = func_GetA1A2A3(y, Fs, F0, F1, F2, F3, variables, textgridfile) | |
2 % [A1, A2, A3] = func_GetA1A2A3(y, Fs, F0, F1, F2, F3, variables, textgridfile) | |
3 % Input: y - input signal | |
4 % Fs - sampling frequency | |
5 % F0 - fundamental frequency (vector) | |
6 % F1-F3 - formant frequencies (vectors) | |
7 % variables - settings from VS | |
8 % textgridfile - textgrid filename if available (optional) | |
9 % Output: A1, A2, A3 - uncorrected amplitudes of the spectra at the formant | |
10 % frequencies. | |
11 % | |
12 % Author: Yen-Liang Shue, Speech Processing and Auditory Perception Laboratory, UCLA | |
13 % Copyright UCLA SPAPL 2009 | |
14 | |
15 N_periods = variables.Nperiods; | |
16 sampleshift = (Fs / 1000 * variables.frameshift); | |
17 | |
18 A1 = zeros(length(F0), 1) * NaN; | |
19 A2 = zeros(length(F0), 1) * NaN; | |
20 A3 = zeros(length(F0), 1) * NaN; | |
21 | |
22 if (nargin == 7) | |
23 | |
24 for k=1:length(F0) | |
25 ks = round(k * sampleshift); | |
26 | |
27 if (ks <= 0 || ks > length(y)) | |
28 continue; | |
29 end | |
30 | |
31 F0_curr = F0(k); | |
32 | |
33 if (isnan(F0_curr) || F0_curr == 0) | |
34 continue; | |
35 end | |
36 | |
37 N0_curr = 1 / F0_curr * Fs; | |
38 | |
39 ystart = round(ks - N_periods/2*N0_curr); | |
40 yend = round(ks + N_periods/2*N0_curr) - 1; | |
41 | |
42 if (ystart <= 0) | |
43 continue; | |
44 end; | |
45 | |
46 if (yend > length(y)) | |
47 continue; | |
48 end; | |
49 | |
50 yseg = y(ystart:yend); | |
51 | |
52 % proceed if no NaNs detected | |
53 if (~isnan(F1(k)) && ~isnan(F2(k)) && ~isnan(F3(k)) && ~isnan(N0_curr)) | |
54 [A1_, fmax] = ana_GetMagnitudeMax(yseg, F1(k), Fs, 8192); | |
55 [A2_, fmax] = ana_GetMagnitudeMax(yseg, F2(k), Fs, 8192); | |
56 [A3_, fmax] = ana_GetMagnitudeMax(yseg, F3(k), Fs, 8192); | |
57 | |
58 A1(k) = A1_; | |
59 A2(k) = A2_; | |
60 A3(k) = A3_; | |
61 end | |
62 end | |
63 else | |
64 % use textgrids - get ignore string list | |
65 tbuffer = variables.tbuffer; | |
66 ignorelabels = textscan(variables.TextgridIgnoreList, '%s', 'delimiter', ','); | |
67 ignorelabels = ignorelabels{1}; | |
68 | |
69 [labels, start, stop] = func_readTextgrid(textgridfile); | |
70 labels_tmp = []; | |
71 start_tmp = []; | |
72 stop_tmp = []; | |
73 | |
74 for k=1:length(variables.TextgridTierNumber) | |
75 inx = variables.TextgridTierNumber(k); | |
76 if (inx <= length(labels)) | |
77 labels_tmp = [labels_tmp; labels{inx}]; | |
78 start_tmp = [start_tmp; start{inx}]; | |
79 stop_tmp = [stop_tmp; stop{inx}]; | |
80 end | |
81 end | |
82 | |
83 labels = labels_tmp; | |
84 start = start_tmp; | |
85 stop = stop_tmp; | |
86 | |
87 | |
88 for m=1:length(start) | |
89 switch(labels{m}) | |
90 case ignorelabels | |
91 continue; % skip anything that is within the ignore list | |
92 end | |
93 | |
94 kstart = round((start(m) * 1000 - tbuffer) / variables.frameshift); | |
95 kstop = round((stop(m) * 1000 + tbuffer) / variables.frameshift); | |
96 kstart(kstart <= 0) = 1; | |
97 kstop(kstop > length(F0)) = length(F0); | |
98 | |
99 ystart = round(kstart * sampleshift); | |
100 ystop = round(kstop * sampleshift); | |
101 ystart(ystart <= 0) = 1; | |
102 ystop(ystop > length(y)) = length(y); | |
103 | |
104 ysegment = y(ystart:ystop); | |
105 F0seg = F0(kstart:kstop); | |
106 F1seg = F1(kstart:kstop); | |
107 F2seg = F2(kstart:kstop); | |
108 F3seg = F3(kstart:kstop); | |
109 | |
110 for k=1:length(F0seg) | |
111 ks = round(k*sampleshift); | |
112 | |
113 if (ks <= 0 || ks > length(ysegment)) | |
114 continue; | |
115 end | |
116 | |
117 F0_curr = F0seg(k); | |
118 if (isnan(F0_curr) || F0_curr == 0) | |
119 continue; | |
120 end | |
121 | |
122 N0_curr = Fs/F0_curr; | |
123 ysstart = round(ks - N_periods/2 * N0_curr); | |
124 ysend = round(ks + N_periods/2 * N0_curr) - 1; | |
125 | |
126 if (isnan(F1seg(k)) || isnan(F2seg(k)) || isnan(F3seg(k))) | |
127 continue; | |
128 end | |
129 | |
130 if (ysstart <= 0) | |
131 continue; | |
132 end; | |
133 if (ysend > length(ysegment)) | |
134 continue; | |
135 end; | |
136 | |
137 yseg = ysegment(ysstart:ysend); | |
138 | |
139 [A1_, fmax] = ana_GetMagnitudeMax(yseg, F1seg(k), Fs, 8192); | |
140 [A2_, fmax] = ana_GetMagnitudeMax(yseg, F2seg(k), Fs, 8192); | |
141 [A3_, fmax] = ana_GetMagnitudeMax(yseg, F3seg(k), Fs, 8192); | |
142 | |
143 inx = kstart + k - 1; | |
144 if (inx <= length(A1)) | |
145 A1(inx) = A1_; | |
146 A2(inx) = A2_; | |
147 A3(inx) = A3_; | |
148 end | |
149 | |
150 end | |
151 | |
152 end | |
153 end | |
154 | |
155 | |
156 %------------------------------------------------------------------------ | |
157 function [M,fmax] = ana_GetMagnitudeMax(x, Fx, Fs, fftlen) | |
158 % Get maximal spectral magnitude of signal x around position Fx Hz in dB | |
159 % Fx can be a vector of frequency points | |
160 % Note that the bigger fftlen the better the resolution | |
161 | |
162 if (isnan(Fx)) | |
163 M = NaN; | |
164 fmax = NaN; | |
165 else | |
166 | |
167 len = length(x); | |
168 hamlen = min(fftlen, len); | |
169 %X = fft(hamming(hamlen).*x(1:hamlen), fftlen); | |
170 factor = 1; %/length(x); % not exactly Kosher but compatible to dfs_magn() | |
171 X = fft(x,fftlen); | |
172 X(X==0) = 0.000000001; % guard against log(0) warnings | |
173 X = 20*log10(factor*abs(X(1:fftlen/2, :))); | |
174 fstep = Fs / fftlen; | |
175 lowf = Fx-0.1*Fx; | |
176 if (lowf<0) lowf=0; end | |
177 highf = Fx+0.1*Fx; | |
178 if (highf>Fs/2-fstep) highf=Fs/2-fstep; end | |
179 | |
180 for cnt = 1:length(Fx) | |
181 [M(cnt),pos] = max(X(1+round(lowf(cnt)/fstep):1+round(highf(cnt)/fstep), :)); | |
182 fmax(cnt) = (pos-1+round(lowf(cnt)/fstep))*fstep; | |
183 end | |
184 | |
185 end | |
186 | |
187 % f=0:fstep:(Fs/2-fstep); | |
188 % plot(f,X); hold; | |
189 % plot(pos, M, 'rx'); hold off; | |
190 % fprintf('End get_magnitude_max()\n'); |