Permalink
Browse files

Fixes formant measures

Fix formant calculation issue where it couldn't compute a mean in cases when there was a praat error in measuring formants at a given glottal pulse.  All formants should produce a number now.
  • Loading branch information...
drfeinberg committed Feb 6, 2019
1 parent cfbe578 commit f749f13b768a008da7fd8a2170c2ff465b1961a9
Showing with 68 additions and 33 deletions.
  1. +68 −33 Measure Pitch, HNR, Jitter, Shimmer, and Formants.ipynb
@@ -16,7 +16,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
@@ -43,7 +43,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
@@ -55,7 +55,7 @@
" pitch = call(sound, \"To Pitch\", 0.0, f0min, f0max) #create a praat pitch object\n",
" meanF0 = call(pitch, \"Get mean\", 0, 0, unit) # get mean pitch\n",
" stdevF0 = call(pitch, \"Get standard deviation\", 0 ,0, unit) # get standard deviation\n",
" harmonicity = call(sound, \"To Harmonicity (cc)\", 0.01, 75, 0.1, 1.0)\n",
" harmonicity = call(sound, \"To Harmonicity (cc)\", 0.01, f0min, 0.1, 1.0)\n",
" hnr = call(harmonicity, \"Get mean\", 0, 0)\n",
" pointProcess = call(sound, \"To PointProcess (periodic, cc)\", f0min, f0max)\n",
" localJitter = call(pointProcess, \"Get jitter (local)\", 0, 0, 0.0001, 0.02, 1.3)\n",
@@ -86,7 +86,7 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
@@ -95,6 +95,7 @@
" sound = parselmouth.Sound(sound) # read the sound\n",
" pitch = call(sound, \"To Pitch (cc)\", 0, f0min, 15, 'no', 0.03, 0.45, 0.01, 0.35, 0.14, f0max)\n",
" pointProcess = call(sound, \"To PointProcess (periodic, cc)\", f0min, f0max)\n",
" \n",
" formants = call(sound, \"To Formant (burg)\", 0.0025, 5, 5000, 0.025, 50)\n",
" numPoints = call(pointProcess, \"Get number of points\")\n",
"\n",
@@ -116,6 +117,11 @@
" f3_list.append(f3)\n",
" f4_list.append(f4)\n",
" \n",
" f1_list = [f1 for f1 in f1_list if str(f1) != 'nan']\n",
" f2_list = [f2 for f2 in f2_list if str(f2) != 'nan']\n",
" f3_list = [f3 for f3 in f3_list if str(f3) != 'nan']\n",
" f4_list = [f4 for f4 in f4_list if str(f4) != 'nan']\n",
" \n",
" # calculate mean formants across pulses\n",
" f1_mean = statistics.mean(f1_list)\n",
" f2_mean = statistics.mean(f2_list)\n",
@@ -141,7 +147,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 75,
"metadata": {},
"outputs": [],
"source": [
@@ -168,7 +174,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": null,
"metadata": {
"scrolled": false
},
@@ -205,9 +211,9 @@
" sound = parselmouth.Sound(wave_file)\n",
" (duration, meanF0, stdevF0, hnr, localJitter, localabsoluteJitter, rapJitter, ppq5Jitter, ddpJitter, \n",
" localShimmer, localdbShimmer, apq3Shimmer, aqpq5Shimmer, apq11Shimmer, ddaShimmer) = measurePitch(\n",
" sound, 75, 500, \"Hertz\")\n",
" sound, 75, 300, \"Hertz\")\n",
" (f1_mean, f2_mean, f3_mean, f4_mean, f1_median, f2_median, f3_median, f4_median) = measureFormants(\n",
" sound, wave_file, 75, 500)\n",
" sound, wave_file, 75, 300)\n",
" file_list.append(wave_file) # make an ID list\n",
" duration_list.append(duration) # make duration list\n",
" mean_F0_list.append(meanF0) # make a mean F0 list\n",
@@ -247,18 +253,9 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/david/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:475: DataConversionWarning: Data with input dtype object was converted to float64 by StandardScaler.\n",
" warnings.warn(msg, DataConversionWarning)\n"
]
}
],
"outputs": [],
"source": [
"# Add the data to Pandas\n",
"df = pd.DataFrame(np.column_stack([file_list, duration_list, mean_F0_list, sd_F0_list, hnr_list, \n",
@@ -281,7 +278,8 @@
"df = pd.concat([df, pcaData], axis=1) # Add PCA data\n",
"# reload the data so it's all numbers\n",
"df.to_csv(\"processed_results.csv\", index=False)\n",
"df = pd.read_csv('processed_results.csv', header=0)"
"df = pd.read_csv('processed_results.csv', header=0)\n",
"df.sort_values('voiceID').head(20)"
]
},
{
@@ -296,15 +294,31 @@
"metadata": {},
"source": [
"### Formant position\n",
" Puts, D. A., Apicella, C. L., & Cárdenas, R. A. (2012). Masculine voices signal men's threat potential in forager and industrial societies. Proceedings of the Royal Society of London B: Biological Sciences, 279(1728), 601-609.\n",
" code adapted from: https://osf.io/k2bhs/ - DOI 10.17605/OSF.IO/K2BHS"
" Puts, D. A., Apicella, C. L., & Cárdenas, R. A. (2012). Masculine voices signal men's threat potential in forager and industrial societies. Proceedings of the Royal Society of London B: Biological Sciences, 279(1728), 601-609."
]
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 56,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/david/anaconda3/lib/python3.6/site-packages/scipy/stats/stats.py:2242: RuntimeWarning: Mean of empty slice.\n",
" mns = a.mean(axis=axis)\n",
"/home/david/anaconda3/lib/python3.6/site-packages/numpy/core/_methods.py:80: RuntimeWarning: invalid value encountered in double_scalars\n",
" ret = ret.dtype.type(ret / rcount)\n",
"/home/david/anaconda3/lib/python3.6/site-packages/numpy/core/_methods.py:135: RuntimeWarning: Degrees of freedom <= 0 for slice\n",
" keepdims=keepdims)\n",
"/home/david/anaconda3/lib/python3.6/site-packages/numpy/core/_methods.py:105: RuntimeWarning: invalid value encountered in true_divide\n",
" arrmean, rcount, out=arrmean, casting='unsafe', subok=False)\n",
"/home/david/anaconda3/lib/python3.6/site-packages/numpy/core/_methods.py:127: RuntimeWarning: invalid value encountered in double_scalars\n",
" ret = ret.dtype.type(ret / rcount)\n"
]
}
],
"source": [
"df['pF'] = (zscore(df.f1_median) + zscore(df.f2_median) + zscore(df.f3_median) + zscore(df.f4_median)) / 4"
]
@@ -319,7 +333,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
@@ -336,7 +350,7 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
@@ -353,7 +367,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 59,
"metadata": {},
"outputs": [],
"source": [
@@ -370,7 +384,7 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
@@ -394,7 +408,7 @@
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 61,
"metadata": {
"scrolled": true
},
@@ -415,7 +429,7 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
@@ -431,12 +445,12 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 63,
"metadata": {},
"outputs": [],
"source": [
"# Write out the final dataframe\n",
"df.to_csv(\"processed_results.csv\", index=False)"
"df.to_csv(\"processed_results2.csv\", index=False)"
]
},
{
@@ -448,7 +462,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 64,
"metadata": {
"scrolled": true
},
@@ -464,6 +478,27 @@
"source": [
"print(\"finished\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {

0 comments on commit f749f13

Please sign in to comment.