-
Notifications
You must be signed in to change notification settings - Fork 0
/
3) ProPer visualization (Periograms).Rmd
481 lines (435 loc) · 22.6 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# 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 <- .05 # {.001 -- .05}
#
#### anchor for relative measurements,
# relative to the entire data ('data'), each token ('token'), or speaker ('speaker'):
# preferably choose 'speaker' if applicable
relTo <- c("data", "speaker", "token")[3] # {[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
***base = 0.05***
*change to 0.01 (marked blue):*
file == "a026_a_m_interviewee_dir_ch_ap_rep_exc" |
file == "a041_a_m_hearer_dir_ov_noa_rep_exc" |
file == "a045_a_m_hearer_dir_ch_noa_rep_exc" |
file == "a050_a_m_interviewee_dir_ov_ap_rep_exc" |
file == "a053_a_f_presscon_dir_nc_noa_rep_nex" |
file == "a057_a_m_hearer_dir_ch_noa_rep_exc" |
file == "a058_a_m_hearer_dir_ch_noa_rep_exc" |
file == "a061_a_m_interviewee_dir_ch_noa_rep_exc" |
file == "a079_a_m_hearer_dir_ch_noa_rep_exc" |
file == "a090_a_m_hearer_dir_ch_ap_rep_exc" |
file == "b042_b_m_reporter_ind_nc_nr_x_x" |
file == "b046_b_f_speech_ind_nc_nr_x_x" |
file == "b050_b_m_other_x_nc_x_x_x" |
file == "b054_b_m_speech_ind_nc_nr_x_x" |
file == "b055_b_m_weather_ind_nc_nr_x_x" |
file == "b056_b_m_sport_ind_nc_nr_x_x" |
file == "b066_b_m_interviewer_dir_ch_de_x_x" |
file == "c004_c_m_interviewer_dir_ch_noa_x_x" |
file == "c006_c_m_interviewer_dir_ch_de_x_x" |
file == "c009_c_m_interviewer_dir_ch_ap_x_x" |
file == "c044_c_m_interviewer_dir_ch_noa_x_x" |
file == "c067_c_m_interviewer_dir_ch_noa_x_x" |
file == "c069_c_m_interviewer_dir_ch_noa_x_x" |
file == "c082_c_m_interviewer_dir_ch_noa_x_x" |
file == "c098_c_m_interviewer_dir_ch_de_x_x",
*change to 0.025 (marked purple):*
file == "a015_a_m_presscon_dir_ch_ap_rep_exc" |
file == "a081_a_f_interviewer_dir_ch_noa_rep_nex" |
file == "a082_a_f_interviewee_dir_ch_ap_rep_exc" |
file == "a089_a_f_interviewee_dir_ch_x_x_exc" |
file == "a097_a_m_hearing_dir_ch_noa_rep_exc" |
file == "a099_a_m_hearer_dir_ov_noa_rep_exc" |
file == "b012_b_m_speech_ind_nc_noa_x_x" |
file == "b019_b_m_news_dir_nc_noa_x_x" |
file == "b021_b_m_weather_ind_nc_nr_x_x" |
file == "b058_b_f_other_ind_nc_nr_x_x" |
file == "b068_b_m_speech_ind_nc_nr_x_x" |
file == "b096_b_m_news_dir_ov_noa_x_x" |
file == "b098_b_m_interviewee_dir_ch_noa_x_x" |
file == "b103_b_m_reporter_dir_nc_nr_x_x" |
file == "c013_c_m_interviewer_dir_ch_ap_x_x" |
file == "c021_c_m_interviewer_dir_ch_de_x_x" |
file == "c045_c_f_interviewer_dir_ch_de_x_x" |
file == "c046_c_f_interviewer_dir_ch_de_x_x" |
file == "c056_c_m_interviewer_dir_ch_ap_x_x" |
file == "c065_c_m_interviewer_dir_ch_noa_x_x" |
file == "c066_c_m_interviewer_dir_ch_ap_x_x" |
file == "c070_c_m_interviewer_dir_ch_de_x_x" |
file == "c072_c_m_interviewer_dir_ov_ap_x_x" |
file == "c075_c_m_interviewer_dir_ch_ap_x_x" |
file == "c076_c_m_interviewer_dir_ch_noa_x_x" |
file == "c088_c_m_interviewer_dir_ch_noa_x_x" |
file == "c089_c_f_interviewer_dir_ch_noa_x_x" |
file == "c096_c_m_interviewer_dir_ch_de_x_x",
```{r readjust_singles, warning=FALSE, echo=FALSE}
#### change the perFloor of specific tokens
main_df <- mutate(
group_by(main_df, file),
#
perFloorStatus = ifelse(
file == "a026_a_m_interviewee_dir_ch_ap_rep_exc" |
file == "a041_a_m_hearer_dir_ov_noa_rep_exc" |
file == "a045_a_m_hearer_dir_ch_noa_rep_exc" |
file == "a050_a_m_interviewee_dir_ov_ap_rep_exc" |
file == "a053_a_f_presscon_dir_nc_noa_rep_nex" |
file == "a057_a_m_hearer_dir_ch_noa_rep_exc" |
file == "a058_a_m_hearer_dir_ch_noa_rep_exc" |
file == "a061_a_m_interviewee_dir_ch_noa_rep_exc" |
file == "a079_a_m_hearer_dir_ch_noa_rep_exc" |
file == "a090_a_m_hearer_dir_ch_ap_rep_exc" |
file == "b042_b_m_reporter_ind_nc_nr_x_x" |
file == "b046_b_f_speech_ind_nc_nr_x_x" |
file == "b050_b_m_other_x_nc_x_x_x" |
file == "b054_b_m_speech_ind_nc_nr_x_x" |
file == "b055_b_m_weather_ind_nc_nr_x_x" |
file == "b056_b_m_sport_ind_nc_nr_x_x" |
file == "b066_b_m_interviewer_dir_ch_de_x_x" |
file == "c004_c_m_interviewer_dir_ch_noa_x_x" |
file == "c006_c_m_interviewer_dir_ch_de_x_x" |
file == "c009_c_m_interviewer_dir_ch_ap_x_x" |
file == "c044_c_m_interviewer_dir_ch_noa_x_x" |
file == "c067_c_m_interviewer_dir_ch_noa_x_x" |
file == "c069_c_m_interviewer_dir_ch_noa_x_x" |
file == "c082_c_m_interviewer_dir_ch_noa_x_x" |
file == "c098_c_m_interviewer_dir_ch_de_x_x", # choose token to change
0.01, # set the new perFloor value
perFloorStatus),
# # repeat previous 4 lines for each unique perFloor value if necessary
perFloorStatus = ifelse(
file == "a015_a_m_presscon_dir_ch_ap_rep_exc" |
file == "a081_a_f_interviewer_dir_ch_noa_rep_nex" |
file == "a082_a_f_interviewee_dir_ch_ap_rep_exc" |
file == "a089_a_f_interviewee_dir_ch_x_x_exc" |
file == "a097_a_m_hearing_dir_ch_noa_rep_exc" |
file == "a099_a_m_hearer_dir_ov_noa_rep_exc" |
file == "b012_b_m_speech_ind_nc_noa_x_x" |
file == "b019_b_m_news_dir_nc_noa_x_x" |
file == "b021_b_m_weather_ind_nc_nr_x_x" |
file == "b058_b_f_other_ind_nc_nr_x_x" |
file == "b068_b_m_speech_ind_nc_nr_x_x" |
file == "b096_b_m_news_dir_ov_noa_x_x" |
file == "b098_b_m_interviewee_dir_ch_noa_x_x" |
file == "b103_b_m_reporter_dir_nc_nr_x_x" |
file == "c013_c_m_interviewer_dir_ch_ap_x_x" |
file == "c021_c_m_interviewer_dir_ch_de_x_x" |
file == "c045_c_f_interviewer_dir_ch_de_x_x" |
file == "c046_c_f_interviewer_dir_ch_de_x_x" |
file == "c056_c_m_interviewer_dir_ch_ap_x_x" |
file == "c065_c_m_interviewer_dir_ch_noa_x_x" |
file == "c066_c_m_interviewer_dir_ch_ap_x_x" |
file == "c070_c_m_interviewer_dir_ch_de_x_x" |
file == "c072_c_m_interviewer_dir_ov_ap_x_x" |
file == "c075_c_m_interviewer_dir_ch_ap_x_x" |
file == "c076_c_m_interviewer_dir_ch_noa_x_x" |
file == "c088_c_m_interviewer_dir_ch_noa_x_x" |
file == "c089_c_f_interviewer_dir_ch_noa_x_x" |
file == "c096_c_m_interviewer_dir_ch_de_x_x",
0.025,
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)
```