Praat脚本开发:如何提取每个音节的基频最大值?
Let's walk through the issues in your script and fix it step by step.
First, the core problem with your original code: when you run Get maximum, Praat needs to explicitly target the Pitch object—your initial selection includes both the TextGrid and Pitch, which can lead to Praat running commands on the wrong item by default. We’ll also add safeguards to avoid crashes if a syllable interval has no detected pitch data.
Here’s the corrected script with clear explanations:
writeInfoLine: "" # Explicitly select the TextGrid first to access syllable intervals selectObject: "TextGrid example" # Syllable tier is set to 1 number = Get number of intervals: 1 # Select the Pitch object once (we’ll switch back to it in the loop) selectObject: "Pitch example" for i from 1 to number # Switch to TextGrid to fetch interval details selectObject: "TextGrid example" name$ = Get label of interval: 1, i start_time = Get start time of interval: 1, i end_time = Get end time of interval: 1, i # Switch back to Pitch object to calculate max pitch selectObject: "Pitch example" max_pitch = Get maximum: start_time, end_time, "Hertz", "Parabolic" # Handle cases where no pitch was detected (returns 'undefined') if max_pitch == undefined appendInfoLine: name$, " ", "No pitch data" else appendInfoLine: name$, " ", max_pitch endif endfor
Key Fixes & Improvements:
- Explicit object targeting: We switch between the TextGrid and Pitch object intentionally to eliminate ambiguity—Praat always knows which object to run commands on.
- Error handling for missing pitch data: The
ifcheck catches cases where no pitch is detected in an interval, preventing the script from crashing and making your output more informative. - Cleaner readability: Separating object selections makes the script easier to debug and adjust later, especially if you modify tier numbers or object names.
A quick sanity check: ensure your TextGrid and Pitch object names exactly match what’s in your Praat object list (names are case-sensitive!). If you’re working with imported files instead of named objects, you can use selectObject: 1/selectObject: 2 based on their position in the object window instead of hardcoding names.
内容的提问来源于stack exchange,提问作者dgr379

