-
Notifications
You must be signed in to change notification settings - Fork 0
/
formants.m
145 lines (128 loc) · 3.8 KB
/
formants.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
function [ F B ] = formants( signal, fs, n_formants, window_length, overlap, n_lpc, Freqs, window, Emph )
%FORMANTS Estimates the formants of @signal every @window_length samples
%with @overlap samples. It returns the first @n_formants in the interval
%@Freqs. It can do preprocessing by applying window @window and
%pre-emphasis with coefficients @Emph
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%REQUIRED ARGUMENTS:
% @signal - vector with the signal to be analysed
% @fs - sampling frequency
%DEFAULT PARAMETERS:
% @window_length
% integer, default 10 ms
% @overlap
% integer, default 40 samples
% @n_formants
% integer, default 2 formants
% @Freqs
% vector of length 2 integers (in Hz), interval in which the formants
% will be considered
% @window
% default no window, otherwise 'Hamming'
% @Emph
% vector of 2 decimals, coefficients for preemphasis
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
PAUSE = false;
DRAW_RESPONSE = 20;
if nargin < 3
display( 'REQUIRED: @signal, @fs, @window_length' );
return;
end
if nargin < 9
Emph = [1 0.63];
end
if nargin < 8
window = 'hamming';
end
if nargin < 7
Freqs = [ 90 fs/2 150 ];
end
if nargin < 6
n_lpc = n_formants * 2 + 2;
end
if nargin < 5
overlap = 40;
end
if nargin < 4
window_length = 10;
end
if nargin < 3
n_formants = round( fs / 1000 );
end
threshold = 200;
window_length = round( fs * window_length / 1000 );
overlap = round( window_length * 0.25 );
shift = window_length - overlap;
len = length( signal ) - mod( length(signal), shift ) - shift;
frames = len / shift;
F = zeros(frames, n_formants);
B = zeros(frames, n_formants);
frame = 1;
for begin = 1:shift:len
x = signal( begin:(begin + window_length - 1) );
if strcmp( window, 'hamming' )
x = x .* hamming( length(x) );
end
if length( Emph ) == 2 && Emph(1) > 0
x = filter(1, Emph, x);
end
x = mean_normalise( x );
a = lpc( x, n_lpc );
if ismember( 1, isnan(a) )
display('Found silent portion, skipping');
continue;
end
rts = roots( a );
rts = rts( imag(rts) > 0 );
angz = atan2( imag(rts), real(rts) );
[frqs, indices] = sort( angz .* ( fs / ( 2 * pi ) ) );
bw = -1/2*(fs/(2*pi))*log(abs(rts(indices)));
frqs(frqs < Freqs(1) | frqs > Freqs(2) | bw > Freqs(3)) = 0;
F(frame, :) = filter_formants( frqs, threshold, n_formants, bw );
if PAUSE && DRAW_RESPONSE == frame
[h,f]=freqz(1,a,512,fs);
figure;
plot(f,20*log10(abs(h)+eps));
legend('LP Filter');
xlabel('Frequency (Hz)');
ylabel('Gain (dB)');
end
% for i = 1:length(frqs)
% if i_formants > n_formants
% break;
% end
% if ( frqs(i) > Freqs(1) && frqs(i) < Freqs(2) && bw(i) > 0 && bw(i) < Freqs(3) )
% F(frame, i_formants) = frqs(i);
% B(frame, i_formants) = bw(i);
% i_formants = i_formants + 1;
%
% if DRAW_RESPONSE && PAUSE
% [h,f]=freqz(1,a,512,fs);
% figure;
% plot(f,20*log10(abs(h)+eps));
% legend('LP Filter');
% xlabel('Frequency (Hz)');
% ylabel('Gain (dB)');
% DRAW_RESPONSE = DRAW_RESPONSE - 1;
% end
%
% end
% end
%
frame = frame + 1;
end
if PAUSE
figure;
NFFT = 2 ^ nextpow2( window_length );
spectrogram(mean_normalise(signal), window_length, overlap, NFFT, fs, 'yaxis' );
colormap bone;
figure;
plot(F(:, 1), 'b.');
hold on;
plot(F(:, 2), 'r.');
plot(F(:, 3), 'g.');
figure;
plot(F(:, 1), F(:, 2), '.');
end
end