DROP TABLE normalizedphonemevalues; 

SELECT 
phonemesphoneme.speaker, 
phonemesphoneme.style,
phonemesphoneme.id,
phonemesphoneme.value, 
(phonemesphoneme.duration - meanphonemevalues.meanduration)
/ meanphonemevalues.sdduration AS normduration,
0.0 AS normcog,
0.0 AS normintensity,
0.0 AS normf12dist,
phonemesphoneme.syllables,
phonemesphoneme.cgn,
phonemesphoneme.words,
phonemesphoneme.stress,
phonemesphoneme.author

INTO TABLE normalizedphonemevalues 
FROM phonemesphoneme, meanphonemevalues
WHERE 
phonemesphoneme.durationvalid = 'V' AND
phonemesphoneme.speaker = meanphonemevalues.speaker AND
phonemesphoneme.style = meanphonemevalues.style AND
phonemesphoneme.value = meanphonemevalues.value;
      
GRANT SELECT ON normalizedphonemevalues TO PUBLIC;
CREATE INDEX normalizedphonemevalues_index ON normalizedphonemevalues (id);


UPDATE  normalizedphonemevalues SET
normcog = (cog_fm.time2 - meanphonemevalues.meancog)
/ meanphonemevalues.sdcog
FROM cog_fm, meanphonemevalues
WHERE 
cog_fm.id = normalizedphonemevalues.id AND
normalizedphonemevalues.id = cog_fm.id AND
normalizedphonemevalues.speaker = meanphonemevalues.speaker AND
normalizedphonemevalues.style = meanphonemevalues.style AND
normalizedphonemevalues.value = meanphonemevalues.value;

UPDATE  normalizedphonemevalues SET
normalizedphonemevalues.normintensity = 
(intensity_hm.time2 - meanphonemevalues.meanintensity)
/ meanphonemevalues.sdintensity
FROM intensity_hm, meanphonemevalues
WHERE 
intensity_hm.id = normalizedphonemevalues.id AND
normalizedphonemevalues.id = intensity_hm.id AND
normalizedphonemevalues.speaker = meanphonemevalues.speaker AND
normalizedphonemevalues.style = meanphonemevalues.style AND
normalizedphonemevalues.value = meanphonemevalues.value;

UPDATE  normalizedphonemevalues SET
normf12dist =
(formantsemitones_fm.f12dist - meanphonemevalues.meanf12dist)
/ meanphonemevalues.sdf12dist
FROM formantsemitones_fm, meanphonemevalues
WHERE 
formantsemitones_fm.id = normalizedphonemevalues.id AND
normalizedphonemevalues.id = formantsemitones_fm.id AND
normalizedphonemevalues.speaker = meanphonemevalues.speaker AND
normalizedphonemevalues.style = meanphonemevalues.style AND
normalizedphonemevalues.value = meanphonemevalues.value;

