Back to Yuan Chai’s tutorial page
source("praat_waveform_spectrogram_pitch_functions.R")
spectrogram = "sample_spectrogram.Spectrogram"
waveform = "sample_waveform.Sound"
egg = "sample_EGG.Sound"
pitch = "sample_pitch.Pitch"
#ignore the Warning: NAs introduced by coercion
df.waveform = convert_waveform_to_df(waveform)
df.egg = convert_waveform_to_df(egg)
df.pitch = convert_pitch_to_df(pitch)
df.spectrogram = convert_spectrogram_to_df(spectrogram)%>%
pre_emphasize() %>% # creates Column `Level_preemp`
constrain_dynamic_range(column = "Level_preemp",
dynamic_range = 90) # you can change the dynamic_range for your preference. The larger the number, the darker the spectrogram gets.
fig.waveform = df.waveform %>%
ggplot(aes(x = time, y = value)) +
geom_line() +
theme_bw()+
scale_x_continuous(breaks = function(x) seq(min(x), max(x), length.out = 2), label = label_number(accuracy = 0.01),expand = c(0, 0))+
scale_y_continuous(expand = c(0, 0)) +
ylab("")+
xlab("")+
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
text = element_text(size = 10))
print(fig.waveform)
fig.egg = df.egg %>%
ggplot(aes(x = time, y = value)) +
geom_line() +
theme_bw()+
scale_x_continuous(breaks = function(x) seq(min(x), max(x), length.out = 2), label = label_number(accuracy = 0.01),expand = c(0, 0))+
scale_y_continuous(breaks = function(x) seq(min(x), max(x), length.out = 2), label = label_number(accuracy = 0.01),expand = c(0, 0)) +
ylab("")+
xlab("")+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
text = element_text(size = 10))
print(fig.egg)
fig.spectrogram <- ggplot()+
geom_tile(data = df.spectrogram, aes(x = Time, y=Frequency, fill=Level_preemp_dr))+
scale_fill_gradient(high="black", low="white", na.value = "white")+
geom_point(data = df.pitch, aes(x = time,y=(frequency-40)*31.25),color = "blue",size = 3)+
xlab("Time (s)")+
scale_x_continuous(breaks = function(x) seq(min(x), max(x), length.out = 2), label = label_number(accuracy = 0.01))+
scale_y_continuous(name = "Frequency (Hz)", limits = c(0,5000), breaks = seq(0,5000,1000),
sec.axis = sec_axis(~(./31.25 + 40), name = 'F0 (Hz)'),expand = c(0,0))+
theme_bw()+
theme(legend.position="none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
text = element_text(size = 10))
print(fig.spectrogram)