forked from finkelbert/ProPer_Projekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3) ProPer visualization (Periograms).Rmd
369 lines (328 loc) · 17.4 KB
/
3) ProPer visualization (Periograms).Rmd
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# ProPer visualization (III): *Periograms*
Adjust the periodic energy and FO curves and create their visual interaction, a.k.a. *Periogram*.
```{r clean_start}
rm(list = ls())
## Load required libraries
require(ggplot2)
require(dplyr)
require(seewave)
require(Cairo)
require(zoo)
raw_df <- read.csv("data_tables/raw_df.csv") %>% distinct(file, t, .keep_all = TRUE)
```
## Prepare periodic energy & F0 curves:
1. Adjust 'perFloor' and 'relTo' in first rows.
2. Use the following plots and codes to verify and change 'perFloor' values.
```{r prepare_main_df, warning=FALSE}
main_df <- droplevels(raw_df)
#
#####################################
############## Presets ##############
#####################################
#### set floor for log-transform (1.00 = 100%):
# adjust to find the most fitting value (change later to choose multiple values)
perFloor <- .01 # {.001 -- .05}
#
#### anchor for relative measurements,
# relative to the entire data ('data'), each tokeb ('token'), or speaker ('speaker'):
# preferably choose 'speaker' if applicable
relTo <- c("data", "speaker", "token")[2] # {[1] -- [3]}
#
#### set floor for periodic fraction (1.00 = 100%):
# keep at 0.25 unless you need this changed
strengThresh <- 0.25 # {0 -- .5}
#
#####################################
####### compute new variables #######
#####################################
## find values for entire data set
main_df <- mutate(
group_by(main_df),
## keep records of adjustable variables
perFloorStatus = perFloor,
relToStatus = relTo,
strengThreshStatus = strengThresh,
## find values for entire data set
max_data_per_power = max(periodic_power, na.rm=T),
max_data_strength = max(strength_rowmax, na.rm=T),
f0_data_min = round(min(f0_smooth, na.rm=T)),
f0_data_max = round(max(f0_smooth, na.rm=T)),
f0_data_range = round(f0_data_max - f0_data_min)
)
## find values for speaker-defined sets (if exist)
if(length(main_df$speaker)>0) main_df <- mutate(group_by(main_df, speaker),
max_speaker_per_power = max(periodic_power, na.rm=T),
max_speaker_strength = max(strength_rowmax, na.rm=T),
f0_speaker_min = round(min(f0_smooth, na.rm=T)),
f0_speaker_max = round(max(f0_smooth, na.rm=T)),
f0_speaker_median = round(median(f0_smooth, na.rm = T)),
f0_speaker_mean = round(mean(f0_smooth, na.rm = T)),
f0_speaker_range = round(f0_speaker_max - f0_speaker_min)
)
## find values for each token
main_df <- mutate(
group_by(main_df, file),
max_token_per_power = max(periodic_power, na.rm=T),
max_token_strength = max(strength_rowmax, na.rm=T),
f0_token_min = round(min(f0_smooth, na.rm=T)),
f0_token_max = round(max(f0_smooth, na.rm=T)),
f0_token_median = round(median(f0_smooth, na.rm = T)),
f0_token_mean = round(mean(f0_smooth, na.rm = T)),
f0_token_range = round(f0_token_max - f0_token_min),
# variables for plot normalization
plotFloorToken = round(f0_token_min - f0_token_range),
plotFloorSpeaker = round(f0_speaker_min - f0_speaker_range),
plotFloorData = round(f0_data_min - f0_data_range),
## conclude relative anchors and thresholds
perFloorStatus = perFloor, # keep records for potential fix
perFloor_indeed = ifelse(
relTo=="token", round(max_token_per_power * perFloor, 10), ifelse(
relTo=="data", round(max_data_per_power * perFloor, 10),
round(max_speaker_per_power * perFloor, 10))),
strengThresh_indeed = ifelse(
relTo=="token", round(max_token_strength * strengThresh, 8), ifelse(
relTo=="data", round(max_data_strength * strengThresh, 8),
round(max_speaker_strength * strengThresh, 8))),
## create new periodic power vector
periodic_fraction = ifelse(strength_rowmax < strengThresh_indeed, 0, strength_rowmax),
postPP = round(total_power * periodic_fraction, 9),
## log periodic power
logPP = 10*log10(postPP/perFloor_indeed),
logPP = ifelse(logPP<0 | is.na(logPP), 0, logPP),
## create relative scales (0--1)
intensityRel = ifelse(
intensity<0, 0, round(intensity / max(intensity, na.rm=TRUE), 5)),
total_powerRel = ifelse(
total_power<0, 0, round(total_power / max(total_power, na.rm=TRUE), 5)),
postPP_rel = ifelse(
postPP<0, 0, round(postPP / max(postPP, na.rm=TRUE), 5)),
logPP_rel = round(logPP / max(logPP, na.rm=TRUE), 5),
########## periodic energy smoothing (log+smooth = smog)
### 20Hz low-pass filter (50ms intervals): "segmental smooth"
smogPP_20Hz = bwfilter(wave = logPP, f = 1000, to = 20, n = 2),
smogPP_20Hz = ifelse(
smogPP_20Hz < 0, 0, round(smogPP_20Hz / max(smogPP_20Hz,na.rm=T), 5)),
### 12Hz low-pass filter (~83.3ms intervals): "seg-syll smooth"
smogPP_12Hz = bwfilter(wave = logPP, f = 1000, to = 12, n = 1),
smogPP_12Hz = ifelse(
smogPP_12Hz < 0, 0, round(smogPP_12Hz / max(smogPP_12Hz, na.rm=T), 5)),
### 8Hz low-pass filter (125ms intervals): "syll-seg smooth"
smogPP_8Hz = bwfilter(wave = logPP, f = 1000, to = 8, n = 1),
smogPP_8Hz = ifelse(
smogPP_8Hz < 0, 0, round(smogPP_8Hz / max(smogPP_8Hz, na.rm=T), 5)),
### 5Hz low-pass filter (200ms intervals): "syllabic smooth"
smogPP_5Hz = bwfilter(wave = logPP, f = 1000, to = 5, n = 1),
smogPP_5Hz = ifelse(
smogPP_5Hz < 0, 0, round(smogPP_5Hz / max(smogPP_5Hz, na.rm=T), 5)),
########## F0 interpolating and smooting
f0_interp = pracma::interp1(t, f0_smooth),
f0_interp_stretch = ifelse(
(is.na(f0_interp) & t<min(which(!is.na(f0_interp)))),
f0_interp[min(which(!is.na(f0_interp)))], ifelse(
(is.na(f0_interp) & t>=max(which(!is.na(f0_interp)))),
f0_interp[max(which(!is.na(f0_interp)))], f0_interp)),
### 6Hz low-pass filter (166.7ms intervals): "vibrato smooth"
f0_interp_stretch_smooth = round(bwfilter(wave = f0_interp_stretch, f = 1000, to = 6, n = 1), 2),
f0_interp_smooth = ifelse(
!is.na(f0_interp), f0_interp_stretch_smooth, NA),
### stretched F0 time-series with "real' local minima (used to compute CoG in script #4)
f0_realFloorStretch = ifelse(smogPP_20Hz > 0.1, f0_interp_stretch_smooth, NA),
f0_realFloorStretch = na.locf(f0_realFloorStretch, na.rm=F),
f0_realFloorStretch = ifelse(
(is.na(f0_realFloorStretch) & t<min(which(!is.na(f0_realFloorStretch)))),
f0_realFloorStretch[min(which(!is.na(f0_realFloorStretch)))], f0_realFloorStretch)
)
## get the filemames list
files <- main_df$file
files <- files[!duplicated(files)==TRUE]
```
## plot selected tokens (detailed review of the data)
Use the comment in/out (#) to toggle visualization of different data components.
```{r plot_singles, warning=FALSE, echo=FALSE}
### chosse the f0 scale for the y-axis in the plots
yScale1 <- c('tokenScale', 'speakerScale', 'dataScale')[1]
##################################
########### loop start ###########
plyr::ldply(files, function(f){
sel_file1 <- f
##################################
#####################################
###### manual singles, no-loop ######
# sel_file1 <- files[3] # or: "filename"
#####################################
# single_token1 <- dplyr::filter(main_df, file==sel_file1)
#
# plotUnits1 <- round(single_token1$f0_token_range[1]/30)
single_token1 <- dplyr::filter(main_df, file==sel_file1)
plotFloor1 <- ifelse(yScale1 == 'tokenScale', single_token1$plotFloorToken[1],
ifelse(yScale1 == 'speakerScale', single_token1$plotFloorSpeaker[1],
ifelse(yScale1 == 'dataScale', single_token1$plotFloorData[1], -275)))
plotUnits1 <- ifelse(yScale1 == 'tokenScale', round(single_token1$f0_token_range[1]/30),
ifelse(yScale1 == 'speakerScale', round(single_token1$f0_speaker_range[1]/30),
ifelse(yScale1 == 'dataScale', round(single_token1$f0_data_range[1]/30), 12)))
f0range1 <- ifelse(yScale1 == 'tokenScale', single_token1$f0_token_range[1],
ifelse(yScale1 == 'speakerScale', single_token1$f0_speaker_range[1],
ifelse(yScale1 == 'dataScale', single_token1$f0_data_range[1], 350)))
f0max1 <- ifelse(yScale1 == 'tokenScale', single_token1$f0_token_max[1],
ifelse(yScale1 == 'speakerScale', single_token1$f0_speaker_max[1],
ifelse(yScale1 == 'dataScale', single_token1$f0_data_max[1], 425)))
periogram_single1 <-
ggplot(single_token1, aes(x=t)) +
########## F0 curves
## pre-smoothed F0 (from pitch object)
# geom_point(aes(y=f0_row1),color="green", alpha=.5, size=.5) +
## smoothed F0 (from pitch tier)
# geom_point(aes(y=f0_smooth),color="blue3", alpha=.3, size=.3) +
## interpolated & stretched F0
# geom_point(aes(y=f0_interp_stretch),color="red", alpha=.3, size=.3) +
# geom_point(aes(y=f0_realFloorStretch),color="orange", alpha=.5, size=.5) +
## periogram (smogPP)
geom_line(aes(y=f0_interp_stretch_smooth),color="magenta2", alpha=single_token1$smogPP_20Hz, size=single_token1$smogPP_20Hz*5) +
########## Power/intensity
## intensity
# geom_line(aes(y=intensityRel*f0range1+plotFloor1),color="yellow", alpha=.6, size=.5) +
## power
# geom_line(aes(y=total_powerRel*f0range1+plotFloor1),color="cornflowerblue", alpha=.5, size=.5, linetype="dashed") +
########## Periodic fraction (strength/HNR)
## raw strength (before "strengThresh")
# geom_line(aes(y=strength_rowmax*f0range1+plotFloor1), color="green", alpha=.2, size=.75, linetype="twodash") +
## processed strength (after "strengThresh")
# geom_line(aes(y=periodic_fraction*f0range1+plotFloor1), color="tomato", alpha=.7, size=.5, linetype="dotted") +
########## Periodic power 'pp' (total power * periodic fraction)
geom_line(aes(y=postPP_rel*f0range1+plotFloor1),color="purple3", alpha=.5, size=.5, linetype="solid") +
########## Log periodic power 'logPP' (10*log10(PER/per_thresh))
# geom_line(aes(y=logPP_rel*f0range1+plotFloor1),color="seashell", alpha=.3, size=2, linetype="longdash") +
########## Smoothed logPP 'smogPP' (4 smoothing flavors: 5/ 8/ 12/ 20 Hz low-pass filter)
geom_line(aes(y=smogPP_20Hz*f0range1+plotFloor1),color="lightsteelblue", alpha=.5, size=.75) +
# geom_line(aes(y=smogPP_12Hz*f0range1+plotFloor1),color="lightyellow", alpha=.6, size=1) +
# geom_line(aes(y=smogPP_8Hz*f0range1+plotFloor1),color="moccasin", alpha=.4, size=1.5) +
geom_line(aes(y=smogPP_5Hz*f0range1+plotFloor1),color="rosybrown1", alpha=.3, size=2) +
########## TextGrids boundaries and annotations (comment out if not available)
## boundaries
{if(length(single_token1$syll_bounds)>0) geom_vline(aes(xintercept=single_token1$syll_bounds), linetype="dotted", color="white", size=.5, alpha=.5)} +
## annotations
{if(length(single_token1$syll_mid)>0) geom_text(aes(x=single_token1$syll_mid, y=f0max1+plotUnits1*2, label=as.character(syll_label), check_overlap=T), size=3, color="white", family= "Helvetica")} +
## plot stuff
ggtitle(paste0(sel_file1)) +
xlab("Time (ms)") + ylab("F0 (Hz)") +
ylim(plotFloor1,f0max1+plotUnits1*2) +
theme(plot.title = element_text(colour = "gray"), panel.background = element_blank(), plot.background = element_rect(fill = "black"), panel.grid = element_blank(), axis.title = element_text(colour = "gray"), axis.ticks = element_blank())
print(periogram_single1)
##--save?
ggsave(periogram_single1,file=paste0("plots/",sel_file1,"_perTest(",perFloor,")_",yScale1,".pdf"),device=cairo_pdf)
##################################
############ loop end ############
})
##################################
```
## re-adjust selected tokens: change the perFloor value of specific tokens
```{r readjust_singles, warning=FALSE, echo=FALSE}
#### change the perFloor of specific tokens
main_df <- mutate(
group_by(main_df, file),
#
perFloorStatus = ifelse(
file == files[1], # choose token to change
0.004, # set the new perFloor value
perFloorStatus),
# # repeat previous 4 lines for each unique perFloor value if necessary
perFloorStatus = ifelse(
file == files[3],
0.04,
perFloorStatus),
#
#### re-run
perFloor_indeed = ifelse(
relTo=="token", round(max_token_per_power * perFloorStatus, 10), ifelse(
relTo=="data", round(max_data_per_power * perFloorStatus, 10),
round(max_speaker_per_power * perFloorStatus, 10))),
## log periodic power
logPP = 10*log10(postPP/perFloor_indeed),
logPP = ifelse(logPP<0 | is.na(logPP), 0, logPP),
########## periodic energy smoothing (log+smooth = smog)
### 20Hz low-pass filter (50ms intervals): "segmental smooth"
smogPP_20Hz = bwfilter(wave = logPP, f = 1000, to = 20, n = 2),
smogPP_20Hz = ifelse(
smogPP_20Hz < 0, 0, round(smogPP_20Hz / max(smogPP_20Hz,na.rm=T), 5)),
### 12Hz low-pass filter (~83.3ms intervals): "seg-syll smooth"
smogPP_12Hz = bwfilter(wave = logPP, f = 1000, to = 12, n = 1),
smogPP_12Hz = ifelse(
smogPP_12Hz < 0, 0, round(smogPP_12Hz / max(smogPP_12Hz, na.rm=T), 5)),
### 8Hz low-pass filter (125ms intervals): "syll-seg smooth"
smogPP_8Hz = bwfilter(wave = logPP, f = 1000, to = 8, n = 1),
smogPP_8Hz = ifelse(
smogPP_8Hz < 0, 0, round(smogPP_8Hz / max(smogPP_8Hz, na.rm=T), 5)),
### 5Hz low-pass filter (200ms intervals): "syllabic smooth"
smogPP_5Hz = bwfilter(wave = logPP, f = 1000, to = 5, n = 1),
smogPP_5Hz = ifelse(
smogPP_5Hz < 0, 0, round(smogPP_5Hz / max(smogPP_5Hz, na.rm=T), 5))
)
```
## re-plot after changes (if relevant)
```{r re-plot, warning=FALSE, echo=FALSE}
### chosse the f0 scale for the y-axis in the plots
yScale2 <- c('tokenScale', 'speakerScale', 'dataScale')[1]
##################################
########### loop start ###########
plyr::ldply(files, function(f){
sel_file2 <- f
##################################
#####################################
###### manual singles, no-loop ######
# sel_file2 <- files[3] # or: "filename"
#####################################
single_token2 <- dplyr::filter(main_df, file==sel_file2)
plotFloor2 <- ifelse(yScale2 == 'tokenScale', single_token2$plotFloorToken[1],
ifelse(yScale2 == 'speakerScale', single_token2$plotFloorSpeaker[1],
ifelse(yScale2 == 'dataScale', single_token2$plotFloorData[1], -275)))
plotUnits2 <- ifelse(yScale2 == 'tokenScale', round(single_token2$f0_token_range[1]/30),
ifelse(yScale2 == 'speakerScale', round(single_token2$f0_speaker_range[1]/30),
ifelse(yScale2 == 'dataScale', round(single_token2$f0_data_range[1]/30), 12)))
f0range2 <- ifelse(yScale2 == 'tokenScale', single_token2$f0_token_range[1],
ifelse(yScale2 == 'speakerScale', single_token2$f0_speaker_range[1],
ifelse(yScale2 == 'dataScale', single_token2$f0_data_range[1], 350)))
f0max2 <- ifelse(yScale2 == 'tokenScale', single_token2$f0_token_max[1],
ifelse(yScale2 == 'speakerScale', single_token2$f0_speaker_max[1],
ifelse(yScale2 == 'dataScale', single_token2$f0_data_max[1], 425)))
periogram_single2 <-
ggplot(single_token2, aes(x=t)) +
########## F0 curves
## periogram (smogPP)
geom_line(aes(y=f0_interp_stretch_smooth),color="magenta2", alpha=single_token2$smogPP_20Hz, size=single_token2$smogPP_20Hz*5) +
########## Periodic power 'pp' (total power * periodic fraction)
geom_line(aes(y=postPP_rel*f0range2+plotFloor2),color="purple3", alpha=.5, size=.5, linetype="solid") +
########## Log periodic power 'logPP' (10*log10(PER/per_thresh))
# geom_line(aes(y=logPP_rel*f0range2+plotFloor2),color="seashell", alpha=.3, size=2, linetype="longdash") +
########## Smoothed logPP 'smogPP' (4 smoothing flavors: 5/ 8/ 12/ 20 Hz low-pass filter)
geom_line(aes(y=smogPP_20Hz*f0range2+plotFloor2),color="lightsteelblue", alpha=.5, size=.75) +
# geom_line(aes(y=smogPP_12Hz*f0range2+plotFloor2),color="lightyellow", alpha=.6, size=1) +
# geom_line(aes(y=smogPP_8Hz*f0range2+plotFloor2),color="moccasin", alpha=.4, size=1.5) +
# geom_line(aes(y=smogPP_5Hz*f0range2+plotFloor2),color="rosybrown1", alpha=.3, size=2) +
########## TextGrids boundaries and annotations (comment out if not available)
## boundaries
{if(length(single_token2$syll_bounds)>0) geom_vline(aes(xintercept=single_token2$syll_bounds), linetype="dotted", color="white", size=.5, alpha=.5)} +
## annotations
{if(length(single_token2$syll_mid)>0) geom_text(aes(x=single_token2$syll_mid, y=f0max2+plotUnits2*2, label=as.character(syll_label), check_overlap=T), size=3, color="white", family= "Helvetica")} +
## plot stuff
ggtitle(paste0(sel_file2)) +
xlab("Time (ms)") + ylab("F0 (Hz)") +
ylim(plotFloor2,f0max2+plotUnits2*2) +
theme(plot.title = element_text(colour = "gray"), panel.background = element_blank(), plot.background = element_rect(fill = "black"), panel.grid = element_blank(), axis.title = element_text(colour = "gray"), axis.ticks = element_blank())
print(periogram_single2)
##--save?
ggsave(periogram_single2,file=paste0("plots/",sel_file2,"_PERIOGRAM(",single_token2$perFloorStatus[1],")_",yScale2,".pdf"),device=cairo_pdf)
##################################
############ loop end ############
})
##################################
```
# Minimize main_df table
```{r minimize_main_df}
## get rid of some variables
mini_main_df <- droplevels(subset(main_df, select = -c(f0_smooth, intensity, strength_row1, f0_row1, strength_rowmax, total_power, periodic_power, max_data_per_power, max_data_strength, max_speaker_per_power, max_speaker_strength, max_token_per_power, max_token_strength, perFloor_indeed, strengThresh_indeed, periodic_fraction, postPP, logPP, f0_interp, f0_interp_stretch)))
```
# Write main_df table
```{r write_main_df}
## Write the main data file
write.csv(mini_main_df, "data_tables/main_df.csv", row.names=FALSE)
```