Small typo corrected in SGC_ToneProt/README.txt
[sgc.git] / SGC_ToneProt / CoGcalculation.praat
blob66306993f461f451a2091e9604337174f449674f
1 #! praat
3 # Calcualte the CoG and convert to Pitch object
4 # Silence the start (<-35 dB) and the end (<20 dB)
6 procedure CoGcalculation
7     Copy... PCM
9     select Sound PCM
10     To TextGrid (silences)... 100 0 -35 0.1 0.1 silent sounding
11     Rename... LowNoise
12     select Sound PCM
13     To TextGrid (silences)... 100 0 -20 0.1 0.1 silent sounding
14     Rename... HighNoise
16     select Sound PCM
17     noprogress To Spectrogram... 0.025 8000 0.005 10 Gaussian
18     select Spectrogram PCM
19     To Matrix
20     Rename... FE
21     select Spectrogram PCM
22     Remove
23     select Matrix FE
24     Copy... E
26     select Matrix FE
27     Formula... if(row>1) then self*y+self[row-1,col] else self*y fi
28     To Sound (slice)... -1
29     select Matrix FE
30     Remove
32     select Matrix E
33     Formula... if(row>1) then self+self[row-1,col] else self fi
34     To Sound (slice)... -1
35     select Matrix E
36     Remove
38     select Sound FE
39     Rename... CoG
40     # Formula... 12*log2(self/Sound_E[col])
41     Formula... (self/Sound_E[col])
42     select Sound E
43     Remove
45     select Sound CoG
46     Down to Matrix
47     To Pitch
48     
49     # Remove silent parts: Initial Low Noise
50     select TextGrid LowNoise
51     numberOfSoundIntervals = Get number of intervals... 1
52     for int from 1 to numberOfSoundIntervals
53         select TextGrid LowNoise
54         starttime = Get starting point... 1 'int'
55         endtime = Get end point... 1 'int'
56         soundValue$ = Get label of interval... 1 'int'
57         if soundValue$ = "silent"
58             select Pitch CoG
59             Formula... if x >= 'starttime' and x < 'endtime' then 0 else self endif
60         endif
61     endfor
62     
63     # Remove silent parts: Final High Noise
64     select Pitch CoG
65     durationCoG = Get total duration
66     endDurationCoG = durationCoG * 0.75
67     select TextGrid HighNoise
68     numberOfSoundIntervals = Get number of intervals... 1
69     for int from 1 to numberOfSoundIntervals
70         select TextGrid HighNoise
71         starttime = Get starting point... 1 'int'
72         endtime = Get end point... 1 'int'
73         soundValue$ = Get label of interval... 1 'int'
74         if soundValue$ = "silent" and endtime > endDurationCoG
75             select Pitch CoG
76             Formula... if x >= 'starttime' and x < 'endtime' then 0 else self endif
77         endif
78     endfor
79     
80     # Clean up
81     select Sound CoG
82     plus Sound PCM
83     plus Matrix CoG
84     plus TextGrid LowNoise
85     plus TextGrid HighNoise
86     Remove
87     
88     select Pitch CoG
89 endproc