Skip to content

Instantly share code, notes, and snippets.

@Yiling-Huo
Yiling-Huo / remove-trackloss.r
Created August 6, 2024 13:19
A function that removes trials from visual world eye-tracking data where no eye gaze can be detected on any of the images in the visual display for an extensive proportion of the time.
remove_trackloss <- function(data='', subject_col='', trial_col='', trackloss_col='', threshold=0.5){
# print start
print(paste('Removing trackloss trials from...', deparse(substitute(data)), sep = ' '))
# create an empty data frame to store track loss trials
dat_exclude <- data.frame(matrix(ncol=3,nrow=0))
c <- c(subject_col, trial_col, 'exclude')
colnames(dat_exclude) <- c
# get the number of the columns of interest
@Yiling-Huo
Yiling-Huo / remove-outliers.r
Last active August 6, 2024 13:19
A simple function that removes outliers from a data frame based on the 3 s.d. rule.
remove_outliers <- function(x, na.rm = TRUE, ...) {
mean = mean(x)
std = sd(x)
Tmin = mean-(3*std)
Tmax = mean+(3*std)
y <- x
y[which(x < Tmin)] <- NA
y[which(x > Tmax)] <- NA
print(paste('Removed:', sum(is.na(y)), sep=' '))
y
@Yiling-Huo
Yiling-Huo / noise-reduction.py
Last active March 15, 2025 16:27
A Python script that reduces breathing and room noise from recordings.
import os
from pydub import AudioSegment, effects
import numpy as np
import noisereduce as nr
##########
# set working directory to the python file's directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))
@Yiling-Huo
Yiling-Huo / present-pictures.py
Last active August 6, 2024 13:12
A Python (pygame) program that presents participants with a series of pictures and their labels.
import pygame, csv, os, sys, random
from datetime import datetime
##########
# Appearances
##########
white = '#FFFFFF'
black = '#000000'
blue = '#0000FF'
@Yiling-Huo
Yiling-Huo / convert-audio.py
Last active August 6, 2024 13:12
A Python script that converts audio file formats using pydub and ffmpeg.
import os
from pydub import AudioSegment
# set working directory to the python file's directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# force pydub to find ffmpeg
AudioSegment.converter = "C:\\ffmpeg\\bin\\ffmpeg.exe"
AudioSegment.ffmpeg = "C:\\ffmpeg\\bin\\ffmpeg.exe"
AudioSegment.ffprobe ="C:\\ffmpeg\\bin\\ffprobe.exe"
@Yiling-Huo
Yiling-Huo / compare-images.py
Last active August 6, 2024 13:13
A Python script that compares images from two folders to search for duplicates.
# This script compares images from two folders, write down duplicate images, and optionally copy all unique images from the two folders into a new folder.
# Created by Yiling Huo, 30 March 2023
import os, csv, shutil, ntpath
from PIL import Image
import imagehash
# Set working directory to the location of this .py file
os.chdir(os.path.dirname(os.path.abspath(__file__)))
@Yiling-Huo
Yiling-Huo / get-formants.praat
Last active August 6, 2024 13:45
A Praat script that extracts by-interval mean formants based on textgrids.
form Get by-interval average formants
word Sound_file_extension .wav
sentence Output_file_name formants.csv
integer Tier_number 1
comment Press OK to choose a directory.
endform
procedure getFiles: .dir$, .ext$
.obj = Create Strings as file list: "files", .dir$ + "/*" + .ext$
.length = Get number of strings
@Yiling-Huo
Yiling-Huo / extract-pitch-contour.praat
Created August 6, 2024 13:02
A Praat script that extracts pitch contour information based on .wav sounds and .Textgrid files.
# Created by Yiling Huo 30/09/2022
# File selection procedure from Scott Seyfarth annotation.Praat (https://gist.github.com/scjs/ffbbba71cc8b3ff9d0476c82b2df9d0f)
# What this script does:
# 1. Takes sound files and textgrid files with the same name,
# 2. extract all intervals with a name in the selected tier,
# 3. divide each interval into a pre-determined amount of equal-length mini-intervals,
# 4. extract pitch at each boundary of the mini-intervals,
# 5. and save all the pitch information in a csv file.
# What this script is good for:
@Yiling-Huo
Yiling-Huo / get-duration.praat
Created August 6, 2024 13:00
A Praat script that gets audio file durations from a directory containing audio files. The script outputs a .csv file.
form Get audio file duration
word Sound_file_extension .mp3
sentence Output_file_name output.csv
comment Press OK to choose a directory.
endform
procedure getFiles: .dir$, .ext$
.obj = Create Strings as file list: "files", .dir$ + "/*" + .ext$
.length = Get number of strings
@Yiling-Huo
Yiling-Huo / auditory-SP-stimuli-from-textgrid.praat
Created August 6, 2024 12:58
A Praat script that, given the stimulus-onset asynchrony (SOA), makes auditory serial presentation stimuli from continuous speech recordings and their textgrids.
# Praat script Create Speech Stimulus Onset Asynchrony (SOA) Stimuli using Textgrids
# inspired by script by Daniel Hirst and Zhenghan Qi
# created by Yiling Huo
# 12 Aug 2022
# What this script does:
# load sound file
# load textgrid file
# extract every non-empty interval as separate sound object
# calculate how much silence is needed and create silent sound object