Mercurial > hg > emotion-detection-top-level
comparison Code/Descriptors/Matlab/MPEG7/FromWeb/VoiceSauce/func_PraatFormants.m @ 4:92ca03a8fa99 tip
Update to ICASSP 2013 benchmark
| author | Dawn Black |
|---|---|
| date | Wed, 13 Feb 2013 11:02:39 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 3:e1cfa7765647 | 4:92ca03a8fa99 |
|---|---|
| 1 function [F1, F2, F3, F4, B1, B2, B3, B4, err] = func_PraatFormants(wavfile, ... | |
| 2 windowlength, frameshift, frameprecision, datalen) | |
| 3 % [F1, F2, F3, F4, B1, B2, B3, B4, err] = func_PraatFormants(wavfile, windowlength, frameshift, frameprecision, datalen) | |
| 4 % KY Time-stamp: <2010-10-16 21:47:24 amoebe> | |
| 5 % Input: wavfile - input wav file | |
| 6 % windowlength - windowlength in seconds | |
| 7 % frameshift - in seconds | |
| 8 % frameprecision - unitless | |
| 9 % datalen - output data length | |
| 10 % Output: Fx, Bx - formant and bandwidth vectors | |
| 11 % err - error flag | |
| 12 % Notes: This currently only works on a PC. | |
| 13 % | |
| 14 % Author: Yen-Liang Shue, Speech Processing and Auditory Perception Laboratory, UCLA | |
| 15 % Modified by Kristine Yu, 2010-10-16 to allow for variable precision in | |
| 16 % matching up time alignment between data vectors. | |
| 17 % | |
| 18 % Copyright UCLA SPAPL 2010 | |
| 19 | |
| 20 % settings | |
| 21 maxFormant = 6000; | |
| 22 iwantfilecleanup = 1; %delete pfmt files when done | |
| 23 | |
| 24 % check if we need to put double quotes around wavfile | |
| 25 if (wavfile(1) ~= '"') | |
| 26 pwavfile = ['"' wavfile '"']; | |
| 27 else | |
| 28 pwavfile = wavfile; | |
| 29 end | |
| 30 | |
| 31 if (ispc) % pc can run praatcon.exe | |
| 32 cmd = sprintf('Windows\\praatcon.exe Windows\\praatformants.praat %s %.3f %.3f %d', pwavfile, frameshift, windowlength, maxFormant); | |
| 33 | |
| 34 elseif (ismac) % mac osx can run Praat using terminal, call Praat from | |
| 35 % Nix/ folder | |
| 36 curr_dir = pwd; | |
| 37 curr_wav = [curr_dir wavfile(2:end)]; | |
| 38 | |
| 39 cmd = sprintf(['MacOS/Praat Windows/praatformants.praat ' ... | |
| 40 '%s %.3f %.3f %d'], curr_wav, frameshift, windowlength, maxFormant); | |
| 41 | |
| 42 else | |
| 43 F1 = NaN; F2 = NaN; F3 = NaN; F4 = NaN; | |
| 44 B1 = NaN; B2 = NaN; B3 = NaN; B4 = NaN; | |
| 45 err = -1; | |
| 46 return; | |
| 47 end | |
| 48 | |
| 49 % Set name of formant track file | |
| 50 fmtfile = [wavfile '.pfmt']; | |
| 51 | |
| 52 %call up praat | |
| 53 | |
| 54 % for pc | |
| 55 if (ispc) | |
| 56 err = system(cmd); | |
| 57 | |
| 58 if (err ~= 0) % oops, error, exit now | |
| 59 F1 = NaN; F2 = NaN; F3 = NaN; F4 = NaN; | |
| 60 B1 = NaN; B2 = NaN; B3 = NaN; B4 = NaN; | |
| 61 if (iwantfilecleanup) | |
| 62 if (exist(fmtfile, 'file') ~= 0) | |
| 63 delete(fmtfile); | |
| 64 end | |
| 65 end | |
| 66 return; | |
| 67 end | |
| 68 end | |
| 69 | |
| 70 | |
| 71 % for mac | |
| 72 if (ismac) | |
| 73 err = unix(cmd); | |
| 74 | |
| 75 if (err ~= 0) % oops, error, exit now | |
| 76 F1 = NaN; F2 = NaN; F3 = NaN; F4 = NaN; | |
| 77 B1 = NaN; B2 = NaN; B3 = NaN; B4 = NaN; | |
| 78 if (iwantfilecleanup) | |
| 79 if (exist(fmtfile, 'file') ~= 0) | |
| 80 delete(fmtfile); | |
| 81 end | |
| 82 end | |
| 83 return; | |
| 84 end | |
| 85 end | |
| 86 | |
| 87 % praat call was successful, return fmt values | |
| 88 F1 = zeros(datalen, 1) * NaN; B1 = zeros(datalen, 1) * NaN; | |
| 89 F2 = zeros(datalen, 1) * NaN; B2 = zeros(datalen, 1) * NaN; | |
| 90 F3 = zeros(datalen, 1) * NaN; B3 = zeros(datalen, 1) * NaN; | |
| 91 F4 = zeros(datalen, 1) * NaN; B4 = zeros(datalen, 1) * NaN; | |
| 92 | |
| 93 % Get formant file | |
| 94 | |
| 95 fid = fopen(fmtfile, 'rt'); | |
| 96 | |
| 97 % read and discard the header | |
| 98 C = textscan(fid, '%s', 1, 'delimiter', '\n'); | |
| 99 | |
| 100 % read the rest | |
| 101 C = textscan(fid, '%f %f %f %f %f %f %f %f %f %f', 'delimiter', '\n', 'TreatAsEmpty', '--undefined--'); | |
| 102 fclose(fid); | |
| 103 t = round(C{1} * 1000); % time locations | |
| 104 | |
| 105 start = 0; % KY changed since Praat doesn't necessarily have data start | |
| 106 % at time = 0 | |
| 107 finish = t(end); | |
| 108 increment = frameshift * 1000; | |
| 109 | |
| 110 for k=start:increment:finish | |
| 111 [val, inx] = min(abs(t - k)); % try to find the closest value | |
| 112 if (abs(t(inx) - k) > frameprecision * frameshift * 1000) % no valid value found | |
| 113 continue; | |
| 114 end | |
| 115 | |
| 116 n = round(k / (frameshift * 1000)) + 1; % KY I added 1 since Matlab | |
| 117 % index starts at 1, not 0 | |
| 118 if (n < 1 || n > datalen) | |
| 119 continue; | |
| 120 end | |
| 121 | |
| 122 F1(n) = C{3}(inx); | |
| 123 B1(n) = C{4}(inx); | |
| 124 F2(n) = C{5}(inx); | |
| 125 B2(n) = C{6}(inx); | |
| 126 F3(n) = C{7}(inx); | |
| 127 B3(n) = C{8}(inx); | |
| 128 F4(n) = C{9}(inx); | |
| 129 B4(n) = C{10}(inx); | |
| 130 end | |
| 131 | |
| 132 if (iwantfilecleanup) | |
| 133 if (exist(fmtfile, 'file') ~= 0) | |
| 134 delete(fmtfile); | |
| 135 end | |
| 136 end |
