-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacousticLandmarks.m
214 lines (163 loc) · 6.29 KB
/
acousticLandmarks.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
clearvars
close all
clc
%% Acoustic Landmark Identification
% This script accompanies:
% Pushing the envelope: evaluating speech rhythm with different envelope
% extraction techniques, MacIntyre, A.D., Cai, C.Q., and Scott, S.K.
% (2021).
% Written by Alexis Deighton MacIntyre.
% Additional Contributions:
% Code for Envelope 3 is adapted from Oganian, Y., & Chang, E. F. (2019). A speech
% envelope landmark for syllable encoding in human superior temporal gyrus.
% Science advances, 5(11).
% Code for Envelope 4 is adapted from Tilsen, S., & Arvaniti, A.
% (2013). Speech rhythm analysis with decomposition of the amplitude
% envelope: characterizing rhythmic patterns within and across languages.
% The Journal of the Acoustical Society of America, 134(1), 628-639.
%% Parameters
% Acoustic Landmark
spEnvelope = 3; % 1. Moving max. 2. Hilbert transform. 3. Bark scale. 4. Vocalic energy.
sigEvent = 5; % 1. Lower crossings. 2. Mid-crossings. 3. Peaks. 4. Bases of peaks. 5. Peaks in the first derivative.
% Preprocessing and Windowing
cleanUp = 1; % Set to 1 to preprocess WAV (this will probably improve the windowing process)
winSp = 1; % Set to 1 to perform windowing
winLen = 3; % Length of window in seconds
rampLen = 50; % Length of ramping to apply to window edges in ms
% Signal Events
eventProm = 0.1; % Prominence parameter for peak finding
eventDist = 80; % Inter-peak distance parameter for peak finding (in sample Fs)
eventThresh = 0.005; % Threshold parameter for lower and mid-crossing finding
%% Load WAV
localPath = fileparts(matlab.desktop.editor.getActiveFilename);
[speechWav,Fs] = audioread(fullfile(localPath,'example.wav'));
%% Preprocess audio
if cleanUp == 1
CV = @(alpha) -sqrt(2) * erfcinv(2*alpha);
alpha = 0.9995;
zs = CV([(1-alpha)/2 1-(1-alpha)/2]);
speechWav = detrend(speechWav); % Detrend
speechWav = speechWav-mean(speechWav); % Remove offset
speechWav = speechWav./(max(abs(speechWav))); % Normalise
sigLim = mean(speechWav) + zs*std(speechWav); % Limit
speechWav(speechWav<sigLim(1)) = sigLim(1);
speechWav(speechWav>sigLim(2)) = sigLim(2);
speechWav = rescale(speechWav,-1,1); % Rescale
end
%% Window audio
if winSp == 1
speechOut = windowAudio(speechWav,Fs,'WindowLength',winLen,'RampLength',rampLen);
else
speechOut = {speechWav};
end
%% Extract envelope
FsOut = 1000; % Envelope sample rate
lowPass = 10; % 10 Hz
for i = 1:numel(speechOut)
switch spEnvelope
case 1
featureOut = env1(speechOut{i,1},Fs,FsOut,lowPass);
case 2
featureOut = env2(speechOut{i,1},Fs,FsOut,lowPass);
case 3
featureOut = env3(speechOut{i,1},Fs,FsOut); % Oganian and Chang, 2019
case 4
featureOut = env4(speechOut{i,1},Fs,FsOut); % Tilsen and Arvaniti, 2013
end
% Smooth noise in signal floor and remove spurious peaks
levels = statelevels(featureOut,round(FsOut/5),'mean');
levels(1) = levels(1)-(0.25*levels(1));
featureOut(featureOut<=levels(1)) = levels(1);
featureOut = rescale(featureOut,-1,1);
speechOut{i,2} = featureOut;
end
%% Identify Signal Events
for i = 1:size(speechOut,1)
featureIn = speechOut{i,2};
% For lower and mid-crossings, split into bi-level signal
levs = [quantile(featureIn,.49) quantile(featureIn,.51)];
if levs(1) == levs(2)
jj = 0.1;
while levs(1) == levs(2)
levs = [quantile(featureIn,.49) quantile(featureIn,.51+jj)];
jj = jj + 0.1;
end
end
switch sigEvent
case 1 % Lower Crossings
[eventsOut,~] = findSignalCrossings(featureIn,eventThresh);
case 2 % Mid-crossings
[~,eventsOut] = findSignalCrossings(featureIn,eventThresh);
case 3 % Peaks
peaks = islocalmax(featureIn,'MinProminence',eventProm,...
'MinSeparation',eventDist,'FlatSelection','first');
eventsOut = find(peaks);
case 4 % Bases of peaks
peaks = islocalmax(featureIn,'MinProminence',eventProm,...
'MinSeparation',eventDist,'FlatSelection','first');
peaks = find(peaks);
eventsOut = findSlopeBases(featureIn,peaks);
case 5 % Peaks in first derivative
chRt = diff(featureIn);
chRt(chRt<0) = 0;
chRt = rescale(chRt,-1,1);
peaks = islocalmax(chRt,'MinProminence',eventProm,...
'MinSeparation',eventDist,'FlatSelection','first');
peaks = find(peaks);
eventsOut = peaks;
end
speechOut{i,3} = eventsOut;
end
%% Plot Results
id = 1; % indices of window to plot
spSig = rescale(speechOut{id,1},-1,1);
feSig = speechOut{id,2};
evs = speechOut{id,3};
figure('Position',[300 300 1000 800],'Color','w');
clear p
ax1 = subplot(2,1,1);
p(1) = plot(spSig,'Color',[.88 .88 .88],'LineWidth',1);
ax2 = subplot(2,1,2);
p(2) = plot(feSig,'Color','k','LineWidth',1.25);
hold on
p(3) = plot(evs,feSig(round(evs)),'d',...
'Color','k','LineWidth',1,'MarkerSize',10);
[f,e] = makeLabels(spEnvelope,sigEvent);
l = legend([p(1) p(2) p(3)],...
'Speech Wave Form',f,e,...
'EdgeColor','none','FontSize',14,...
'Location','northoutside');
ts = 1:(numel(spSig)/Fs)/10:numel(spSig)/Fs;
ts = round(ts,2);
tix1 = 1:(numel(spSig)/numel(ts)):numel(spSig);
tix2 = 1:(numel(feSig)/numel(ts)):numel(feSig);
set(ax1,'XTick',tix1,'YLim',[-1.1 1.1],'XLim',[1 numel(spSig)])
set(ax2,'XTick',tix2,'YLim',[-1.1 1.1],'XLim',[1 numel(feSig)])
set(ax1,'YColor','none','YTick',[],'YGrid','off')
set(ax2,'YColor','none','YTick',[],'YGrid','off')
set(ax1,'XTickLabel',ts,'FontSize',12)
set(ax2,'XTickLabel',ts,'FontSize',12)
xlabel('Time (s)', 'FontSize',14)
%% Functions
function [f,e] = makeLabels(spEnvelope,signalEvent)
if spEnvelope == 1
f = 'Envelope (moving max)';
elseif spEnvelope == 2
f = 'Envelope (Hilbert transform)';
elseif spEnvelope == 3
f = 'Envelope (Bark scale)';
elseif spEnvelope == 4
f = 'Envelope (vocalic energy)';
end
if signalEvent == 1
e = 'Lower crossings';
elseif signalEvent == 2
e = 'Mid-crossings';
elseif signalEvent == 3
e = 'Peaks';
elseif signalEvent == 4
e = 'Bases of peaks';
elseif signalEvent == 5
e = 'Peaks in the first derivative';
end
end