DataModeler.cpp 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171
  1. /* DataModeler.cpp
  2. *
  3. * Copyright (C) 2014-2016 David Weenink, 2017 Paul Boersma
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * ainteger with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. djmw 20140217
  20. */
  21. #include "DataModeler.h"
  22. #include "NUM2.h"
  23. #include "NUMmachar.h"
  24. #include "SVD.h"
  25. #include "Strings_extensions.h"
  26. #include "Sound_and_LPC_robust.h"
  27. #include "Table_extensions.h"
  28. #include "oo_DESTROY.h"
  29. #include "DataModeler_def.h"
  30. #include "oo_COPY.h"
  31. #include "DataModeler_def.h"
  32. #include "oo_EQUAL.h"
  33. #include "DataModeler_def.h"
  34. #include "oo_CAN_WRITE_AS_ENCODING.h"
  35. #include "DataModeler_def.h"
  36. #include "oo_WRITE_TEXT.h"
  37. #include "DataModeler_def.h"
  38. #include "oo_WRITE_BINARY.h"
  39. #include "DataModeler_def.h"
  40. #include "oo_READ_TEXT.h"
  41. #include "DataModeler_def.h"
  42. #include "oo_READ_BINARY.h"
  43. #include "DataModeler_def.h"
  44. #include "oo_DESCRIPTION.h"
  45. #include "DataModeler_def.h"
  46. extern machar_Table NUMfpp;
  47. Thing_implement (DataModeler, Function, 0);
  48. void structDataModeler :: v_info () {
  49. MelderInfo_writeLine (U" Time domain:");
  50. MelderInfo_writeLine (U" Start time: ", xmin, U" seconds");
  51. MelderInfo_writeLine (U" End time: ", xmax, U" seconds");
  52. MelderInfo_writeLine (U" Total duration: ", xmax - xmin, U" seconds");
  53. double ndf, rSquared = DataModeler_getCoefficientOfDetermination (this, nullptr, nullptr);
  54. double probability, chisq = DataModeler_getChiSquaredQ (this, useSigmaY, &probability, &ndf);
  55. MelderInfo_writeLine (U" Fit:");
  56. MelderInfo_writeLine (U" Number of data points: ", numberOfDataPoints);
  57. MelderInfo_writeLine (U" Number of parameters: ", numberOfParameters);
  58. MelderInfo_writeLine (U" Each data point has ", useSigmaY == DataModeler_DATA_WEIGH_EQUAL ? U" the same weight (estimated)." :
  59. useSigmaY == DataModeler_DATA_WEIGH_SIGMA ? U"a different weight (sigmaY)." :
  60. useSigmaY == DataModeler_DATA_WEIGH_RELATIVE ? U"a different relative weight (Y_value/sigmaY)." :
  61. U"a different weight (SQRT(sigmaY)).");
  62. MelderInfo_writeLine (U" Chi squared: ", chisq);
  63. MelderInfo_writeLine (U" Number of degrees of freedom: ", ndf);
  64. MelderInfo_writeLine (U" Probability: ", probability);
  65. MelderInfo_writeLine (U" R-squared: ", rSquared);
  66. for (integer ipar = 1; ipar <= numberOfParameters; ipar ++) {
  67. double sigma = parameterStatus [ipar] == DataModeler_PARAMETER_FIXED ? 0 : sqrt (parameterCovariances -> data [ipar] [ipar]);
  68. MelderInfo_writeLine (U" p [", ipar, U"] = ", parameter [ipar], U"; sigma = ", sigma);
  69. }
  70. }
  71. static double polynomial_evaluate (DataModeler me, double xin, double p [])
  72. {
  73. double xpi = 1.0, result = p [1];
  74. // From domain [xmin, xmax] to domain [-(xmax -xmin)/2, (xmax-xmin)/2]
  75. double x = (2.0 * xin - my xmin - my xmax) / 2.0;
  76. for (integer i = 2; i <= my numberOfParameters; i ++) {
  77. xpi *= x;
  78. result += p [i] * xpi;
  79. }
  80. return result;
  81. }
  82. static void polynomial_evaluateBasisFunctions (DataModeler me, double xin, double term []) {
  83. term [1] = 1.0;
  84. // From domain [xmin, xmax] to domain [-(xmax -xmin)/2, (xmax-xmin)/2]
  85. double x = (2.0 * xin - my xmin - my xmax) / 2.0;
  86. for (integer i = 2; i <= my numberOfParameters; i ++)
  87. term [i] = term [i-1] * x;
  88. }
  89. static double legendre_evaluate (DataModeler me, double xin, double p []) {
  90. // From domain [xmin, xmax] to domain [-1, 1]
  91. double x = (2.0 * xin - my xmin - my xmax) / (my xmax - my xmin);
  92. double pti, ptim1, ptim2 = 1.0, result = p [1];
  93. if (my numberOfParameters > 1) {
  94. double twox = 2.0 * x, f2 = x, d = 1.0;
  95. result += p [2] * (ptim1 = x);
  96. for (integer i = 3; i <= my numberOfParameters; i ++) {
  97. double f1 = d ++;
  98. f2 += twox;
  99. result += p [i] * (pti = (f2 * ptim1 - f1 * ptim2) / d);
  100. ptim2 = ptim1;
  101. ptim1 = pti;
  102. }
  103. }
  104. return result;
  105. }
  106. static void legendre_evaluateBasisFunctions (DataModeler me, double xin, double term []) {
  107. term [1] = 1.0;
  108. /* transform x from domain [xmin, xmax] to domain [-1, 1] */
  109. double x = (2.0 * xin - my xmin - my xmax) / (my xmax - my xmin);
  110. if (my numberOfParameters > 1) {
  111. double twox = 2.0 * x, f2 = term [2] = x, d = 1.0;
  112. for (integer i = 3; i <= my numberOfParameters; i ++) {
  113. double f1 = d ++;
  114. f2 += twox;
  115. term [i] = (f2 * term [i-1] - f1 * term [i-2]) / d;
  116. }
  117. }
  118. }
  119. static void chisqFromZScores (double *zscores, integer numberOfZScores, double *out_chisq, integer *out_numberOfValidZScores) {
  120. integer numberOfValidZScores = numberOfZScores;
  121. double chisq = 0.0;
  122. for (integer i = 1; i <= numberOfZScores; i ++) {
  123. if (isdefined (zscores [i])) {
  124. chisq += zscores [i] * zscores [i];
  125. } else {
  126. numberOfValidZScores --;
  127. }
  128. }
  129. if (out_chisq)
  130. *out_chisq = chisq;
  131. if (out_numberOfValidZScores)
  132. *out_numberOfValidZScores = numberOfValidZScores;
  133. }
  134. static double DataModeler_getDataPointInverseWeight (DataModeler me, integer iPoint, int useSigmaY ) {
  135. double iweight = 1.0;
  136. if (iPoint > 0 && iPoint <= my numberOfDataPoints && my dataPointStatus [iPoint] != DataModeler_DATA_INVALID) {
  137. if (useSigmaY == DataModeler_DATA_WEIGH_SIGMA) {
  138. iweight = my sigmaY [iPoint];
  139. } else if (useSigmaY == DataModeler_DATA_WEIGH_RELATIVE) {
  140. double q = my y [iPoint] / my sigmaY [iPoint];
  141. iweight = 500.0 / q; //
  142. } else if (useSigmaY == DataModeler_DATA_WEIGH_SQRT) {
  143. iweight = 7.071 * sqrt (my sigmaY [iPoint]); // .bw = 50 gives 50
  144. }
  145. }
  146. return iweight;
  147. }
  148. double DataModeler_getModelValueAtX (DataModeler me, double x) {
  149. double f = undefined;
  150. if (x >= my xmin && x <= my xmax)
  151. f = my f_evaluate (me, x, my parameter);
  152. return f;
  153. }
  154. double DataModeler_getModelValueAtIndex (DataModeler me, integer index) {
  155. double f = undefined;
  156. if (index > 0 && index <= my numberOfDataPoints)
  157. f = my f_evaluate (me, my x [index], my parameter);
  158. return f;
  159. }
  160. void DataModeler_getExtremaY (DataModeler me, double *out_ymin, double *out_ymax) {
  161. double ymin = 1e308, ymax = -ymin;
  162. for (integer i = 1; i <= my numberOfDataPoints; i++) {
  163. if (my dataPointStatus [i] != DataModeler_DATA_INVALID) {
  164. if (my y [i] < ymin)
  165. ymin = my y [i];
  166. if (my y [i] > ymax)
  167. ymax = my y [i];
  168. }
  169. }
  170. if (out_ymin)
  171. *out_ymin = ymin;
  172. if (out_ymax)
  173. *out_ymax = ymax;
  174. }
  175. double DataModeler_getDataPointYValue (DataModeler me, integer index) {
  176. double value = undefined;
  177. if (index > 0 && index <= my numberOfDataPoints && my dataPointStatus [index] != DataModeler_DATA_INVALID)
  178. value = my y [index];
  179. return value;
  180. }
  181. double DataModeler_getDataPointXValue (DataModeler me, integer index) {
  182. double value = undefined;
  183. if (index > 0 && index <= my numberOfDataPoints && my dataPointStatus [index] != DataModeler_DATA_INVALID)
  184. value = my x [index];
  185. return value;
  186. }
  187. void DataModeler_setDataPointYValue (DataModeler me, integer index, double value) {
  188. if (index > 0 && index <= my numberOfDataPoints)
  189. my y [index] = value;
  190. }
  191. void DataModeler_setDataPointXValue (DataModeler me, integer index, double value) {
  192. if (index > 0 && index <= my numberOfDataPoints)
  193. my x [index] = value;
  194. }
  195. void DataModeler_setDataPointValues (DataModeler me, integer index, double xvalue, double yvalue) {
  196. if (index > 0 && index <= my numberOfDataPoints) {
  197. my x [index] = xvalue;
  198. my y [index] = yvalue;
  199. }
  200. }
  201. void DataModeler_setDataPointYSigma (DataModeler me, integer index, double sigma) {
  202. if (index > 0 && index <= my numberOfDataPoints)
  203. my sigmaY [index] = sigma;
  204. }
  205. double DataModeler_getDataPointYSigma (DataModeler me, integer index) {
  206. double sigma = undefined;
  207. if (index > 0 && index <= my numberOfDataPoints)
  208. sigma = my sigmaY [index];
  209. return sigma;
  210. }
  211. int DataModeler_getDataPointStatus (DataModeler me, integer index) {
  212. int value = DataModeler_DATA_INVALID;
  213. if (index > 0 && index <= my numberOfDataPoints)
  214. value = my dataPointStatus [index];
  215. return value;
  216. }
  217. void DataModeler_setDataPointStatus (DataModeler me, integer index, int status) {
  218. if (index > 0 && index <= my numberOfDataPoints) {
  219. if (status == DataModeler_DATA_VALID && isundef (my y [index]))
  220. Melder_throw (U"Your data value is undefined. First set the value and then its status.");
  221. my dataPointStatus [index] = status;
  222. }
  223. }
  224. static void DataModeler_setDataPointValueAndStatus (DataModeler me, integer index, double value, int dataStatus) {
  225. if (index > 0 && index <= my numberOfDataPoints) {
  226. my y [index] = value;
  227. my dataPointStatus [index] = dataStatus;
  228. }
  229. }
  230. void DataModeler_setParameterValue (DataModeler me, integer index, double value, int status) {
  231. if (index > 0 && index <= my numberOfParameters) {
  232. my parameter [index] = value;
  233. my parameterStatus [index] = status;
  234. }
  235. }
  236. void DataModeler_setParameterValueFixed (DataModeler me, integer index, double value) {
  237. DataModeler_setParameterValue (me, index, value, DataModeler_PARAMETER_FIXED);
  238. }
  239. double DataModeler_getParameterValue (DataModeler me, integer index) {
  240. double value = undefined;
  241. if (index > 0 && index <= my numberOfParameters)
  242. value = my parameter [index];
  243. return value;
  244. }
  245. int DataModeler_getParameterStatus (DataModeler me, integer index) {
  246. int status = DataModeler_PARAMETER_UNDEFINED;
  247. if (index > 0 && index <= my numberOfParameters)
  248. status = my parameterStatus [index];
  249. return status;
  250. }
  251. double DataModeler_getParameterStandardDeviation (DataModeler me, integer index) {
  252. double stdev = undefined;
  253. if (index > 0 && index <= my numberOfParameters)
  254. stdev = sqrt (my parameterCovariances -> data [index] [index]);
  255. return stdev;
  256. }
  257. double DataModeler_getVarianceOfParameters (DataModeler me, integer fromIndex, integer toIndex, integer *p_numberOfFreeParameters) {
  258. double variance = undefined;
  259. if (toIndex < fromIndex || (toIndex == 0 && fromIndex == 0))
  260. fromIndex = 1, toIndex = my numberOfParameters;
  261. integer numberOfFreeParameters = 0;
  262. if (fromIndex <= toIndex && fromIndex > 0 && toIndex <= my numberOfParameters) {
  263. variance = 0;
  264. for (integer index = fromIndex; index <= toIndex; index ++) {
  265. if (my parameterStatus [index] != DataModeler_PARAMETER_FIXED) {
  266. variance += my parameterCovariances -> data [index] [index];
  267. numberOfFreeParameters ++;
  268. }
  269. }
  270. }
  271. if (p_numberOfFreeParameters) {
  272. *p_numberOfFreeParameters = numberOfFreeParameters;
  273. }
  274. return variance;
  275. }
  276. void DataModeler_setParametersFree (DataModeler me, integer fromIndex, integer toIndex) {
  277. if (toIndex < fromIndex || (toIndex == 0 && fromIndex == 0))
  278. fromIndex = 1, toIndex = my numberOfParameters;
  279. if (fromIndex <= toIndex && fromIndex > 0 && toIndex <= my numberOfParameters) {
  280. for (integer index = fromIndex; index <= toIndex; index ++)
  281. my parameterStatus [index] = DataModeler_PARAMETER_FREE;
  282. }
  283. }
  284. void DataModeler_setParameterValuesToZero (DataModeler me, double numberOfSigmas) {
  285. integer numberOfChangedParameters = 0;
  286. for (integer i = my numberOfParameters; i > 0; i --) {
  287. if (my parameterStatus [i] != DataModeler_PARAMETER_FIXED) {
  288. double value = my parameter [i];
  289. double sigmas = numberOfSigmas * DataModeler_getParameterStandardDeviation (me, i);
  290. if ((value - sigmas) * (value + sigmas) < 0) {
  291. DataModeler_setParameterValueFixed (me, i, 0.0);
  292. numberOfChangedParameters ++;
  293. }
  294. }
  295. }
  296. }
  297. static integer DataModeler_getNumberOfFreeParameters (DataModeler me) {
  298. integer numberOfFreeParameters = 0;
  299. for (integer i = 1; i <= my numberOfParameters; i ++) {
  300. if (my parameterStatus [i] == DataModeler_PARAMETER_FREE)
  301. numberOfFreeParameters ++;
  302. }
  303. return numberOfFreeParameters;
  304. }
  305. integer DataModeler_getNumberOfFixedParameters (DataModeler me) {
  306. return my numberOfParameters - DataModeler_getNumberOfFreeParameters (me);
  307. }
  308. static integer DataModeler_getNumberOfValidDataPoints (DataModeler me) {
  309. integer numberOfValidDataPoints = 0;
  310. for (integer i = 1; i <= my numberOfDataPoints; i ++) {
  311. if (my dataPointStatus [i] != DataModeler_DATA_INVALID)
  312. numberOfValidDataPoints ++;
  313. }
  314. return numberOfValidDataPoints;
  315. }
  316. integer DataModeler_getNumberOfInvalidDataPoints (DataModeler me) {
  317. return my numberOfDataPoints - DataModeler_getNumberOfValidDataPoints (me);
  318. }
  319. void DataModeler_setTolerance (DataModeler me, double tolerance) {
  320. my tolerance = tolerance > 0.0 ? tolerance : my numberOfDataPoints * NUMfpp -> eps;
  321. }
  322. double DataModeler_getDegreesOfFreedom (DataModeler me) {
  323. integer numberOfDataPoints = 0;
  324. for (integer i = 1; i <= my numberOfDataPoints; i ++) {
  325. if (my dataPointStatus [i] != DataModeler_DATA_INVALID)
  326. numberOfDataPoints ++;
  327. }
  328. double ndf = numberOfDataPoints - DataModeler_getNumberOfFreeParameters (me);
  329. return ndf;
  330. }
  331. void DataModeler_getZScores (DataModeler me, int useSigmaY, double zscores []) {
  332. try {
  333. Melder_assert (zscores != nullptr);
  334. double estimatedSigmaY;
  335. if (useSigmaY == DataModeler_DATA_WEIGH_EQUAL) {
  336. integer numberOfValidDataPoints;
  337. double rss = DataModeler_getResidualSumOfSquares (me, & numberOfValidDataPoints);
  338. Melder_require (numberOfValidDataPoints > 1, U"Not enough data points to calculate sigma.");
  339. estimatedSigmaY = rss / (numberOfValidDataPoints - 1);
  340. }
  341. for (integer i = 1; i <= my numberOfDataPoints; i ++) {
  342. double value = undefined;
  343. if (my dataPointStatus [i] != DataModeler_DATA_INVALID) {
  344. double estimate = my f_evaluate (me, my x [i], my parameter);
  345. double sigma = useSigmaY == DataModeler_DATA_WEIGH_EQUAL ? estimatedSigmaY : DataModeler_getDataPointInverseWeight (me, i, useSigmaY);
  346. value = (my y [i] - estimate) / sigma;
  347. }
  348. zscores [i] = value;
  349. }
  350. } catch (MelderError) {
  351. Melder_throw (U"No z-scores calculated.");
  352. }
  353. }
  354. // chisq and zscores may be the same arrays!
  355. static void DataModeler_getChisqScoresFromZScores (DataModeler me, double *zscores, bool substituteAverage, double *chisq) {
  356. Melder_assert (zscores != nullptr && chisq != nullptr);
  357. integer numberOfDefined = my numberOfDataPoints;
  358. double sumchisq = 0.0;
  359. for (integer i = 1; i <= my numberOfDataPoints; i ++) {
  360. if (isdefined (zscores [i])) {
  361. chisq [i] = zscores [i] * zscores [i];
  362. sumchisq += chisq [i];
  363. } else {
  364. numberOfDefined --;
  365. chisq [i] = undefined;
  366. }
  367. }
  368. if (substituteAverage && numberOfDefined != my numberOfDataPoints && numberOfDefined > 0) {
  369. for (integer i = 1; i <= my numberOfDataPoints; i ++) {
  370. if (isundef (chisq [i]))
  371. chisq [i] = sumchisq / numberOfDefined;
  372. }
  373. }
  374. }
  375. double DataModeler_getChiSquaredQ (DataModeler me, int useSigmaY, double *out_prob, double *out_df) {
  376. double chisq;
  377. integer numberOfValidZScores;
  378. autoNUMvector<double> zscores (1, my numberOfDataPoints);
  379. DataModeler_getZScores (me, useSigmaY, zscores.peek());
  380. chisqFromZScores (zscores.peek(), my numberOfDataPoints, & chisq, & numberOfValidZScores);
  381. double df = useSigmaY == DataModeler_DATA_WEIGH_EQUAL ? numberOfValidZScores - 1.0 : numberOfValidZScores; // we lose one df if sigma is estimated from the data
  382. if (out_prob)
  383. *out_prob = NUMchiSquareQ (chisq, df);
  384. if (out_df)
  385. *out_df = df;
  386. return chisq;
  387. }
  388. double DataModeler_getWeightedMean (DataModeler me) {
  389. double ysum = 0.0, wsum = 0.0;
  390. for (integer i = 1; i <= my numberOfDataPoints; i ++) {
  391. if (my dataPointStatus [i] != DataModeler_DATA_INVALID) {
  392. double s = DataModeler_getDataPointInverseWeight (me, i, my useSigmaY);
  393. double weight = 1.0 / (s * s);
  394. ysum += my y [i] * weight;
  395. wsum += weight;
  396. }
  397. }
  398. return ysum / wsum;
  399. }
  400. double DataModeler_getCoefficientOfDetermination (DataModeler me, double *out_ssreg, double *out_sstot) {
  401. /* We cannot use the standard expressions for ss_tot, and ss_reg because our data are weighted by 1 / sigma [i].
  402. * We need the weighted mean and we need to weigh all sums-of-squares accordingly;
  403. * if all sigma [i] terms are equal, the formulas reduce to the standard ones.
  404. * Ref: A. Buse (1973): Goodness of Fit in Generalized Least Squares Estimation, The American Statician, vol 27, 106-108
  405. */
  406. double ymean = DataModeler_getWeightedMean (me);
  407. double sstot = 0.0, ssreg = 0.0;
  408. for (integer i = 1; i <= my numberOfDataPoints; i ++) {
  409. if (my dataPointStatus [i] != DataModeler_DATA_INVALID) {
  410. double s = DataModeler_getDataPointInverseWeight (me, i, my useSigmaY);
  411. double diff = (my y [i] - ymean) / s;
  412. sstot += diff * diff; // total sum of squares
  413. double estimate = my f_evaluate (me, my x [i], my parameter);
  414. diff = (estimate - my y [i]) / s;
  415. ssreg += diff * diff; // regression sum of squares
  416. }
  417. }
  418. double rSquared = ( sstot > 0.0 ? 1.0 - ssreg / sstot : 1.0 );
  419. if (out_ssreg)
  420. *out_ssreg = sstot - ssreg;
  421. if (out_sstot)
  422. *out_sstot = sstot;
  423. return rSquared;
  424. }
  425. static void DataModeler_drawBasisFunction_inside (DataModeler me, Graphics g, double xmin, double xmax, double ymin, double ymax,
  426. integer iterm, bool scale, integer numberOfPoints)
  427. {
  428. if (xmax <= xmin)
  429. xmin = my xmin, xmax = my xmax;
  430. autoNUMvector<double> x (1, numberOfPoints);
  431. autoNUMvector<double> y (1, numberOfPoints);
  432. autoNUMvector<double> term (1, my numberOfParameters);
  433. for (integer i = 1; i <= numberOfPoints; i ++) {
  434. x [i] = xmin + (i - 0.5) * (xmax - xmin) / numberOfPoints;
  435. my f_evaluateBasisFunctions (me, x [i], term.peek());
  436. y [i] = term [iterm];
  437. y [i] = scale ? y [i] * my parameter [iterm] : y [i];
  438. }
  439. if (ymax <= ymin) {
  440. ymin = 1e308;
  441. ymax = -ymin;
  442. for (integer i = 1; i <= numberOfPoints; i ++) {
  443. ymax = y [i] > ymax ? y [i] : ymax;
  444. ymin = y [i] < ymin ? y [i] : ymin;
  445. }
  446. }
  447. Graphics_setWindow (g, xmin, xmax, ymin, ymax);
  448. for (integer i = 2; i <= numberOfPoints; i ++)
  449. Graphics_line (g, x [i-1], y [i-1], x [i], y [i]);
  450. }
  451. static integer DataModeler_drawingSpecifiers_x (DataModeler me, double *xmin, double *xmax, integer *ixmin, integer *ixmax) {
  452. if (*xmax <= *xmin)
  453. *xmin = my xmin; *xmax = my xmax;
  454. *ixmin = 2;
  455. while (my x [*ixmin] < *xmin && *ixmin < my numberOfDataPoints)
  456. (*ixmin) ++;
  457. (*ixmin) --;
  458. *ixmax = my numberOfDataPoints - 1;
  459. while (my x [*ixmax] > *xmax && *ixmax > 1)
  460. (*ixmax) --;
  461. (*ixmax) ++;
  462. return *ixmax - *ixmin + 1;
  463. }
  464. void DataModeler_drawOutliersMarked_inside (DataModeler me, Graphics g, double xmin, double xmax, double ymin, double ymax,
  465. double numberOfSigmas, int useSigmaY, conststring32 mark, int marksFontSize, double horizontalOffset_mm)
  466. {
  467. integer ixmin, ixmax;
  468. if (DataModeler_drawingSpecifiers_x (me, & xmin, & xmax, & ixmin, & ixmax) < 1) return;
  469. autoNUMvector<double> zscores (1, my numberOfDataPoints);
  470. DataModeler_getZScores (me, useSigmaY, zscores.peek());
  471. double horizontalOffset_wc = Graphics_dxMMtoWC (g, horizontalOffset_mm);
  472. Graphics_setWindow (g, xmin, xmax, ymin, ymax);
  473. Graphics_setFontSize (g, marksFontSize);
  474. Graphics_setTextAlignment (g, Graphics_CENTRE, Graphics_HALF);
  475. int currentFontSize = Graphics_inqFontSize (g);
  476. for (integer idata = 1; idata <= my numberOfDataPoints; idata ++) {
  477. if (my dataPointStatus [idata] != DataModeler_DATA_INVALID) {
  478. double x = my x [idata], y = my y [idata];
  479. if (x >= xmin && x <= xmax && y >= ymin && y <= ymax) {
  480. if (fabs (zscores [idata]) > numberOfSigmas)
  481. Graphics_text (g, x + horizontalOffset_wc, y, mark);
  482. }
  483. }
  484. }
  485. Graphics_setFontSize (g, currentFontSize);
  486. }
  487. void DataModeler_draw_inside (DataModeler me, Graphics g, double xmin, double xmax, double ymin, double ymax,
  488. int estimated, integer numberOfParameters, int errorbars, int connectPoints, double barWidth_mm, double horizontalOffset_mm, int drawDots)
  489. {
  490. if (xmax <= xmin)
  491. xmin = my xmin, xmax = my xmax;
  492. integer ixmin = 2;
  493. while (my x [ixmin] < xmin && ixmin < my numberOfDataPoints)
  494. ixmin ++;
  495. ixmin --;
  496. integer ixmax = my numberOfDataPoints - 1;
  497. while (my x [ixmax] > xmax && ixmax > 1)
  498. ixmax --;
  499. ixmax ++;
  500. if (ixmin >= ixmax)
  501. return; // nothing to draw
  502. numberOfParameters = ( numberOfParameters > my numberOfParameters ? my numberOfParameters : numberOfParameters );
  503. autoNUMvector<double> parameter (1, my numberOfParameters);
  504. NUMvector_copyElements (my parameter, parameter.peek(), 1, numberOfParameters);
  505. Graphics_setWindow (g, xmin, xmax, ymin, ymax);
  506. double horizontalOffset_wc = Graphics_dxMMtoWC (g, horizontalOffset_mm);
  507. double barWidth_wc = barWidth_mm <= 0.0 ? 0.0 : Graphics_dxMMtoWC (g, barWidth_mm);
  508. double x1, y1, x2, y2;
  509. bool x1defined = false, x2defined = false;
  510. for (integer idata = ixmin; idata <= ixmax; idata ++) {
  511. if (my dataPointStatus [idata] != DataModeler_DATA_INVALID) {
  512. double x = my x [idata], y = my y [idata];
  513. if (! x1defined) {
  514. x1 = x;
  515. y1 = estimated ? my f_evaluate (me, x, parameter.peek()) : y;
  516. x1defined = true;
  517. } else {
  518. x2 = x;
  519. y2 = estimated ? my f_evaluate (me, x, parameter.peek()) : y;
  520. x2defined = true;
  521. }
  522. if (x1defined && drawDots) {
  523. if (y >= ymin && y <= ymax)
  524. Graphics_speckle (g, x + horizontalOffset_wc, y);
  525. }
  526. if (x2defined) { // if (x1defined && x2defined)
  527. if (connectPoints) {
  528. double xo1, yo1, xo2, yo2;
  529. if (NUMclipLineWithinRectangle (x1 + horizontalOffset_wc, y1, x2 + horizontalOffset_wc, y2,
  530. xmin, ymin, xmax, ymax, & xo1, & yo1, & xo2, & yo2)) {
  531. Graphics_line (g, xo1, yo1, xo2, yo2);
  532. }
  533. // Graphics_line (g, x1 + horizontalOffset_wc, y1, x2 + horizontalOffset_wc, y2);
  534. }
  535. x1 = x;
  536. y1 = y2;
  537. }
  538. if (x1defined && errorbars != 0) {
  539. double sigma = my sigmaY [idata]; // DataModeler_getDataPointInverseWeight ?
  540. double ym = y1;
  541. double yt = ym + 0.5 * sigma, yb = ym - 0.5 * sigma;
  542. if (estimated) {
  543. yt = (y - y1) > 0.0 ? y : y1;
  544. yb = (y - y1) > 0.0 ? y1 : y;
  545. }
  546. bool topOutside = yt > ymax, bottomOutside = yb < ymin;
  547. yt = topOutside ? ymax : yt;
  548. yb = bottomOutside ? ymin : yb;
  549. Graphics_line (g, x1 + horizontalOffset_wc, yb, x1 + horizontalOffset_wc, yt);
  550. if (barWidth_wc > 0.0 && ! estimated) {
  551. double xl = x1 - 0.5 * barWidth_wc + horizontalOffset_wc;
  552. double xr = xl + barWidth_wc;
  553. if (! topOutside)
  554. Graphics_line (g, xl, yt, xr, yt);
  555. if (! bottomOutside)
  556. Graphics_line (g, xl, yb, xr, yb);
  557. }
  558. }
  559. }
  560. }
  561. }
  562. void DataModeler_drawTrack_inside (DataModeler me, Graphics g, double xmin, double xmax, double ymin, double ymax, int estimated, integer numberOfParameters, double horizontalOffset_mm)
  563. {
  564. int errorbars = 0, connectPoints = 1; double barWidth_mm = 0;
  565. DataModeler_draw_inside (me, g, xmin, xmax, ymin, ymax, estimated, numberOfParameters, errorbars, connectPoints, barWidth_mm, horizontalOffset_mm, 0);
  566. }
  567. void DataModeler_drawTrack (DataModeler me, Graphics g, double xmin, double xmax, double ymin, double ymax,
  568. int estimated, integer numberOfParameters, double horizontalOffset_mm, bool garnish)
  569. {
  570. if (ymax <= ymin)
  571. DataModeler_getExtremaY (me, &ymin, &ymax);
  572. Graphics_setInner (g);
  573. DataModeler_drawTrack_inside (me, g, xmin, xmax, ymin, ymax, estimated, numberOfParameters, horizontalOffset_mm);
  574. Graphics_unsetInner (g);
  575. if (garnish) {
  576. Graphics_drawInnerBox (g);
  577. Graphics_marksBottom (g, 2, true, true, false);
  578. Graphics_marksLeft (g, 2, true, true, false);
  579. }
  580. }
  581. void DataModeler_speckle_inside (DataModeler me, Graphics g, double xmin, double xmax, double ymin, double ymax,
  582. int estimated, integer numberOfParameters, int errorbars, double barWidth_mm, double horizontalOffset_mm)
  583. {
  584. int connectPoints = 0;
  585. DataModeler_draw_inside (me, g, xmin, xmax, ymin, ymax, estimated, numberOfParameters, errorbars, connectPoints, barWidth_mm, horizontalOffset_mm, 1);
  586. }
  587. void DataModeler_speckle (DataModeler me, Graphics g, double xmin, double xmax, double ymin, double ymax,
  588. int estimated, integer numberOfParameters, int errorbars, double barWidth_mm, double horizontalOffset_mm, bool garnish)
  589. {
  590. if (ymax <= ymin)
  591. DataModeler_getExtremaY (me, &ymin, &ymax);
  592. Graphics_setInner (g);
  593. DataModeler_speckle_inside (me, g, xmin, xmax, ymin, ymax,
  594. estimated, numberOfParameters, errorbars, barWidth_mm, horizontalOffset_mm);
  595. Graphics_unsetInner (g);
  596. if (garnish) {
  597. Graphics_drawInnerBox (g);
  598. Graphics_marksBottom (g, 2, true, true, false);
  599. Graphics_marksLeft (g, 2, true, true, false);
  600. }
  601. }
  602. autoTable DataModeler_to_Table_zscores (DataModeler me, int useSigmaY) {
  603. try {
  604. autoTable ztable = Table_createWithColumnNames (my numberOfDataPoints, U"x z");
  605. autoNUMvector<double> zscores (1, my numberOfDataPoints);
  606. DataModeler_getZScores (me, useSigmaY, zscores.peek());
  607. for (integer i = 1; i <= my numberOfDataPoints; i ++) {
  608. Table_setNumericValue (ztable.get(), i, 1, my x [i]);
  609. Table_setNumericValue (ztable.get(), i, 2, zscores [i]);
  610. }
  611. return ztable;
  612. } catch (MelderError) {
  613. Melder_throw (U"Table not created.");
  614. }
  615. }
  616. static void DataModeler_normalProbabilityPlot (DataModeler me, Graphics g,
  617. int useSigmaY, integer numberOfQuantiles, double numberOfSigmas, int labelSize, conststring32 label, bool garnish)
  618. {
  619. try {
  620. autoTable thee = DataModeler_to_Table_zscores (me, useSigmaY);
  621. Table_normalProbabilityPlot (thee.get(), g, 2, numberOfQuantiles, numberOfSigmas, labelSize, label, garnish);
  622. } catch (MelderError) {
  623. Melder_clearError ();
  624. }
  625. }
  626. void DataModeler_setBasisFunctions (DataModeler me, int type) {
  627. if (type == DataModeler_TYPE_LEGENDRE) {
  628. my f_evaluate = legendre_evaluate;
  629. my f_evaluateBasisFunctions = legendre_evaluateBasisFunctions;
  630. } else {
  631. my f_evaluate = polynomial_evaluate;
  632. my f_evaluateBasisFunctions = polynomial_evaluateBasisFunctions;
  633. }
  634. my type = type;
  635. }
  636. void DataModeler_init (DataModeler me, double xmin, double xmax, integer numberOfDataPoints, integer numberOfParameters, int type) {
  637. my xmin = xmin;
  638. my xmax = xmax;
  639. DataModeler_setBasisFunctions (me, type);
  640. my numberOfDataPoints = numberOfDataPoints;
  641. my x = NUMvector<double> (1, numberOfDataPoints);
  642. my y = NUMvector<double> (1, numberOfDataPoints);
  643. my sigmaY = NUMvector<double> (1, numberOfDataPoints);
  644. my dataPointStatus = NUMvector<int> (1, numberOfDataPoints);
  645. my numberOfParameters = numberOfParameters;
  646. Melder_require (numberOfParameters > 0,
  647. U"The number of parameters should be greater than zero.");
  648. Melder_require (numberOfParameters <= numberOfDataPoints,
  649. U"The number of parameters should not exceed the number of data points");
  650. my parameter = NUMvector<double> (1, numberOfParameters);
  651. my parameterStatus = NUMvector<int> (1, numberOfParameters);
  652. my parameterNames = Strings_createFixedLength (numberOfParameters);
  653. my parameterCovariances = Covariance_create (numberOfParameters);
  654. }
  655. autoDataModeler DataModeler_create (double xmin, double xmax, integer numberOfDataPoints, integer numberOfParameters, int type) {
  656. try {
  657. autoDataModeler me = Thing_new (DataModeler);
  658. DataModeler_init (me.get(), xmin, xmax, numberOfDataPoints, numberOfParameters, type);
  659. my xmin = xmin;
  660. my xmax = xmax;
  661. return me;
  662. } catch (MelderError) {
  663. Melder_throw (U"DataModeler not created.");
  664. }
  665. }
  666. autoDataModeler DataModeler_createSimple (double xmin, double xmax,
  667. integer numberOfDataPoints, conststring32 parameters, double gaussianNoiseStd, int type)
  668. {
  669. try {
  670. autoVEC parameter = VEC_createFromString (parameters);
  671. Melder_require (xmin < xmax,
  672. U"The domain should be defined properly.");
  673. autoDataModeler me = DataModeler_create (xmin, xmax, numberOfDataPoints, parameter.size, type);
  674. for (integer i = 1; i <= parameter.size; i ++)
  675. my parameter [i] = parameter [i]; // parameter status ok
  676. // generate the data that beinteger to the parameter values
  677. for (integer i = 1; i <= numberOfDataPoints; i ++) {
  678. my x [i] = xmin + (i - 0.5) * (xmax - xmin) / numberOfDataPoints;
  679. double modelY = my f_evaluate (me.get(), my x [i], my parameter);
  680. my y [i] = modelY + NUMrandomGauss (0.0, gaussianNoiseStd);
  681. my sigmaY [i] = undefined;
  682. }
  683. my useSigmaY = DataModeler_DATA_WEIGH_EQUAL;
  684. return me;
  685. } catch (MelderError) {
  686. Melder_throw (U"No simple DataModeler created.");
  687. }
  688. }
  689. void DataModeler_fit (DataModeler me) {
  690. try {
  691. // Count the number of parameters to be fitted
  692. integer numberOfParameters = DataModeler_getNumberOfFreeParameters (me);
  693. if (numberOfParameters == 0) return;
  694. integer numberOfDataPoints = DataModeler_getNumberOfValidDataPoints (me);
  695. autoVEC b = VECzero (numberOfDataPoints);
  696. autoVEC term = VECzero (my numberOfParameters);
  697. autoVEC parameter = VECzero (my numberOfParameters);
  698. autoMAT design = MATzero (numberOfDataPoints, numberOfParameters);
  699. // For function evaluation with only the FIXED parameters
  700. for (integer ipar = 1; ipar <= my numberOfParameters; ipar ++) {
  701. parameter [ipar] = my parameterStatus [ipar] == DataModeler_PARAMETER_FIXED ? my parameter [ipar] : 0.0;
  702. }
  703. // estimate sigma if we weigh all datapoint equally.
  704. // This is necessary to get the parameter covariances right
  705. double sigmaY = ( my useSigmaY == DataModeler_DATA_WEIGH_EQUAL ? DataModeler_estimateSigmaY (me) : undefined );
  706. integer idata = 0;
  707. // Accumulate coefficients of the design matrix
  708. for (integer i = 1; i <= my numberOfDataPoints; i ++) {
  709. if (my dataPointStatus [i] != DataModeler_DATA_INVALID) {
  710. ++ idata;
  711. // function evaluation with only the FIXED parameters
  712. double xi = my x [i], yi = my y [i];
  713. double yFixed = my f_evaluate (me, xi, parameter.at);
  714. double si = ( my useSigmaY != DataModeler_DATA_WEIGH_EQUAL ? DataModeler_getDataPointInverseWeight (me, i, my useSigmaY) : sigmaY );
  715. // individual terms of the function
  716. my f_evaluateBasisFunctions (me, xi, term.at);
  717. integer ipar = 0;
  718. for (integer j = 1; j <= my numberOfParameters; j ++) {
  719. if (my parameterStatus [j] != DataModeler_PARAMETER_FIXED)
  720. design [idata] [++ ipar] = term [j] / si;
  721. }
  722. // only 'residual variance' must be explained by the model
  723. b [idata] = (yi - yFixed) / si;
  724. }
  725. }
  726. // Singular value decomposition and evaluation of the singular values
  727. autoSVD thee = SVD_createFromGeneralMatrix (design.get());
  728. if (! NUMfpp)
  729. NUMmachar ();
  730. SVD_zeroSmallSingularValues (thee.get(), my tolerance > 0.0 ? my tolerance : numberOfDataPoints * NUMfpp -> eps);
  731. autoVEC result = SVD_solve (thee.get(), b.get());
  732. // Put the calculated parameters at the correct position in 'my p'
  733. Covariance cov = my parameterCovariances.get();
  734. integer ipar = 0;
  735. for (integer j = 1; j <= my numberOfParameters; j ++) {
  736. if (my parameterStatus [j] != DataModeler_PARAMETER_FIXED)
  737. my parameter [j] = parameter [++ ipar];
  738. cov -> centroid [j] = my parameter [j];
  739. }
  740. cov -> numberOfObservations = numberOfDataPoints;
  741. // estimate covariances between parameters
  742. if (numberOfParameters < my numberOfParameters) {
  743. autoNUMmatrix<double> covtmp (1, numberOfParameters, 1, numberOfParameters);
  744. SVD_getSquared (thee.get(), covtmp.peek(), true);
  745. // Set fixed parameters variances and covariances to zero.
  746. for (integer i = 1; i <= my numberOfParameters; i ++) {
  747. for (integer j = i; j <= my numberOfParameters; j ++)
  748. cov -> data [i] [j] = cov -> data [j] [i] = 0.0;
  749. }
  750. ipar = 0;
  751. for (integer i = 1; i <= my numberOfParameters; i ++) {
  752. if (my parameterStatus [i] != DataModeler_PARAMETER_FIXED) {
  753. ++ ipar;
  754. integer jpar = 0;
  755. for (integer j = 1; j <= my numberOfParameters; j ++) {
  756. if (my parameterStatus [j] != DataModeler_PARAMETER_FIXED)
  757. cov -> data [i] [j] = covtmp [ipar] [++ jpar];
  758. }
  759. }
  760. }
  761. } else {
  762. SVD_getSquared (thee.get(), cov -> data.at, true);
  763. }
  764. } catch (MelderError) {
  765. Melder_throw (U"DataModeler no fit.");
  766. }
  767. }
  768. void DataModeler_setDataWeighing (DataModeler me, int useSigmaY) {
  769. if (my useSigmaY != useSigmaY) {
  770. my useSigmaY = useSigmaY;
  771. DataModeler_fit (me); // because sigma has changed!
  772. }
  773. }
  774. autoCovariance DataModeler_to_Covariance_parameters (DataModeler me) {
  775. try {
  776. autoCovariance cov = Data_copy (my parameterCovariances.get());
  777. return cov;
  778. } catch (MelderError) {
  779. Melder_throw (U"Covariance not created.");
  780. }
  781. }
  782. autoDataModeler Table_to_DataModeler (Table me, double xmin, double xmax, integer xcolumn, integer ycolumn, integer scolumn,
  783. integer numberOfParameters, int type)
  784. {
  785. try {
  786. Table_checkSpecifiedColumnNumberWithinRange (me, xcolumn);
  787. Table_checkSpecifiedColumnNumberWithinRange (me, ycolumn);
  788. const bool useSigmaY = ( scolumn > 0 );
  789. if (useSigmaY)
  790. Table_checkSpecifiedColumnNumberWithinRange (me, scolumn);
  791. integer numberOfRows = my rows.size, numberOfData = 0;
  792. autoNUMvector<double> x (1, numberOfRows), y (1, numberOfRows), sy (1, numberOfRows);
  793. for (integer i = 1; i <= numberOfRows; i ++) {
  794. double val = Table_getNumericValue_Assert (me, i, xcolumn);
  795. if (isdefined (val)) {
  796. x [++ numberOfData] = val;
  797. if (numberOfData > 1) {
  798. if (val < x [numberOfData - 1]) {
  799. Melder_throw (U"Data with x-values should be sorted.");
  800. } else if (val == x [numberOfData - 1]) {
  801. Melder_throw (U"All x-values should be different.");
  802. }
  803. }
  804. y [numberOfData] = Table_getNumericValue_Assert (me, i, ycolumn);
  805. sy [numberOfData] = useSigmaY ? Table_getNumericValue_Assert (me, i, scolumn) : 1;
  806. }
  807. }
  808. if (xmax <= xmin)
  809. NUMvector_extrema<double> (x.peek(), 1, numberOfData, & xmin, & xmax);
  810. Melder_require (xmin < xmax, U"The range of the x-values is too small.");
  811. integer numberOfDataPoints = 0, validData = 0;
  812. for (integer i = 1; i <= numberOfData; i ++) {
  813. if (x [i] >= xmin && x [i] <= xmax)
  814. numberOfDataPoints ++;
  815. }
  816. autoDataModeler thee = DataModeler_create (xmin, xmax, numberOfDataPoints, numberOfParameters, type);
  817. numberOfDataPoints = 0;
  818. for (integer i = 1; i <= numberOfData; i ++) {
  819. if (x [i] >= xmin && x [i] <= xmax) {
  820. thy x [++ numberOfDataPoints] = x [i];
  821. thy dataPointStatus [numberOfDataPoints] = DataModeler_DATA_INVALID;
  822. if (isdefined (y [i])) {
  823. thy y [numberOfDataPoints] = y [i];
  824. thy sigmaY [numberOfDataPoints] = sy [i];
  825. thy dataPointStatus [numberOfDataPoints] = DataModeler_DATA_VALID;
  826. validData ++;
  827. }
  828. }
  829. }
  830. thy useSigmaY = useSigmaY;
  831. thy numberOfDataPoints = numberOfDataPoints;
  832. thy tolerance = 1e-5;
  833. Melder_require (validData >= numberOfParameters, U"The number of parameters should not exceed the number of data points.");
  834. DataModeler_setDataWeighing (thee.get(), DataModeler_DATA_WEIGH_SIGMA);
  835. DataModeler_fit (thee.get());
  836. return thee;
  837. } catch (MelderError) {
  838. Melder_throw (U"Datamodeler not created from Table.");
  839. }
  840. }
  841. Thing_implement (FormantModeler, Function, 0);
  842. void structFormantModeler :: v_info () {
  843. MelderInfo_writeLine (U"Time domain:");
  844. MelderInfo_writeLine (U" Start time: ", xmin, U" seconds");
  845. MelderInfo_writeLine (U" End time: ", xmax, U" seconds");
  846. MelderInfo_writeLine (U" Total duration: ", xmax - xmin, U" seconds");
  847. for (integer iformant = 1; iformant <= trackmodelers.size; iformant ++) {
  848. DataModeler ffi = trackmodelers.at [iformant];
  849. MelderInfo_writeLine (U"Formant ", iformant);
  850. ffi -> v_info();
  851. }
  852. }
  853. double DataModeler_getResidualSumOfSquares (DataModeler me, integer *numberOfDataPoints) {
  854. integer n = 0;
  855. longdouble rss = 0.0;
  856. for (integer i = 1; i <= my numberOfDataPoints; i ++) {
  857. if (my dataPointStatus [i] != DataModeler_DATA_INVALID) {
  858. ++ n;
  859. double dif = my y [i] - my f_evaluate (me, my x [i], my parameter);
  860. rss += dif * dif;
  861. }
  862. }
  863. if (numberOfDataPoints)
  864. *numberOfDataPoints = n;
  865. return ( n > 0 ? (double) rss : undefined );
  866. }
  867. void DataModeler_reportChiSquared (DataModeler me, int weighDataType) {
  868. MelderInfo_writeLine (U"Chi squared test:");
  869. MelderInfo_writeLine (weighDataType == DataModeler_DATA_WEIGH_EQUAL ? U"Standard deviation is estimated from the data." :
  870. weighDataType == DataModeler_DATA_WEIGH_SIGMA ? U"Sigmas are used as estimate for local standard deviations." :
  871. weighDataType == DataModeler_DATA_WEIGH_RELATIVE ? U"1/Q's are used as estimate for local standard deviations." :
  872. U"Sqrt sigmas are used as estimate for local standard deviations.");
  873. double ndf, probability, chisq = DataModeler_getChiSquaredQ (me, weighDataType, &probability, &ndf);
  874. MelderInfo_writeLine (U"Chi squared = ", chisq);
  875. MelderInfo_writeLine (U"Probability = ", probability);
  876. MelderInfo_writeLine (U"Number of degrees of freedom = ", ndf);
  877. }
  878. double DataModeler_estimateSigmaY (DataModeler me) {
  879. try {
  880. integer numberOfDataPoints = 0;
  881. autoVEC y (my numberOfDataPoints, kTensorInitializationType::RAW);
  882. for (integer i = 1; i <= my numberOfDataPoints; i ++) {
  883. if (my dataPointStatus [i] != DataModeler_DATA_INVALID)
  884. y [++ numberOfDataPoints] = my y [i];
  885. }
  886. y.size = numberOfDataPoints; // fake shrink
  887. return NUMstdev (y.get());
  888. } catch (MelderError) {
  889. Melder_throw (U"Cannot estimate sigma.");
  890. }
  891. }
  892. double FormantModeler_getStandardDeviation (FormantModeler me, integer iformant) {
  893. double sigma = undefined;
  894. if (iformant > 0 && iformant <= my trackmodelers.size) {
  895. DataModeler ff = my trackmodelers.at [iformant];
  896. sigma = DataModeler_estimateSigmaY (ff);
  897. }
  898. return sigma;
  899. }
  900. double FormantModeler_getDataPointValue (FormantModeler me, integer iformant, integer index) {
  901. double value = undefined;
  902. if (iformant > 0 && iformant <= my trackmodelers.size) {
  903. DataModeler ff = my trackmodelers.at [iformant];
  904. value = DataModeler_getDataPointYValue (ff, index);
  905. }
  906. return value;
  907. }
  908. void FormantModeler_setDataPointValue (FormantModeler me, integer iformant, integer index, double value) {
  909. if (iformant > 0 && iformant <= my trackmodelers.size) {
  910. DataModeler ff = my trackmodelers.at [iformant];
  911. DataModeler_setDataPointYValue (ff, index, value);
  912. }
  913. }
  914. double FormantModeler_getDataPointSigma (FormantModeler me, integer iformant, integer index) {
  915. double sigma = undefined;
  916. if (iformant > 0 && iformant <= my trackmodelers.size) {
  917. DataModeler ff = (DataModeler) my trackmodelers.at [iformant];
  918. sigma = DataModeler_getDataPointYSigma (ff, index);
  919. }
  920. return sigma;
  921. }
  922. void FormantModeler_setDataPointSigma (FormantModeler me, integer iformant, integer index, double sigma) {
  923. if (iformant > 0 && iformant <= my trackmodelers.size) {
  924. DataModeler ff = my trackmodelers.at [iformant];
  925. DataModeler_setDataPointYSigma (ff, index, sigma);
  926. }
  927. }
  928. int FormantModeler_getDataPointStatus (FormantModeler me, integer iformant, integer index) {
  929. int value = DataModeler_DATA_INVALID;
  930. if (iformant > 0 && iformant <= my trackmodelers.size) {
  931. DataModeler ff = my trackmodelers.at [iformant];
  932. value = DataModeler_getDataPointStatus (ff, index);
  933. }
  934. return value;
  935. }
  936. void FormantModeler_setDataPointStatus (FormantModeler me, integer iformant, integer index, int status) {
  937. if (iformant > 0 && iformant <= my trackmodelers.size) {
  938. DataModeler ff = my trackmodelers.at [iformant];
  939. DataModeler_setDataPointStatus (ff, index, status);
  940. }
  941. }
  942. static void FormantModeler_setDataPointValueAndStatus (FormantModeler me, integer iformant, integer index, double value, int dataStatus) {
  943. if (iformant > 0 && iformant <= my trackmodelers.size) {
  944. DataModeler ff = my trackmodelers.at [iformant];
  945. DataModeler_setDataPointValueAndStatus (ff, index, value, dataStatus);
  946. }
  947. }
  948. void FormantModeler_setParameterValueFixed (FormantModeler me, integer iformant, integer index, double value) {
  949. if (iformant > 0 && iformant <= my trackmodelers.size) {
  950. DataModeler ffi = my trackmodelers.at [iformant];
  951. DataModeler_setParameterValueFixed (ffi, index, value);
  952. }
  953. }
  954. void FormantModeler_setParametersFree (FormantModeler me, integer fromFormant, integer toFormant, integer fromIndex, integer toIndex) {
  955. integer numberOfFormants = my trackmodelers.size;
  956. if (toFormant < fromFormant || (fromFormant == toFormant && fromFormant == 0)) {
  957. fromFormant = 1;
  958. toFormant = numberOfFormants;
  959. }
  960. Melder_require (toFormant > 0 && toFormant <= numberOfFormants && fromFormant > 0 && fromFormant <= numberOfFormants && fromFormant <= toFormant,
  961. U"Formant number(s) should be in the interval [1, ", numberOfFormants, U"].");
  962. for (integer iformant = fromFormant; iformant <= toFormant; iformant ++) {
  963. DataModeler ffi = my trackmodelers.at [iformant];
  964. DataModeler_setParametersFree (ffi, fromIndex, toIndex);
  965. }
  966. }
  967. void FormantModeler_setDataWeighing (FormantModeler me, integer fromFormant, integer toFormant, int useSigmaY) {
  968. integer numberOfFormants = my trackmodelers.size;
  969. if (toFormant < fromFormant || (fromFormant == toFormant && fromFormant == 0)) {
  970. fromFormant = 1;
  971. toFormant= numberOfFormants;
  972. }
  973. Melder_require (toFormant > 0 && toFormant <= numberOfFormants && fromFormant > 0 && fromFormant <= numberOfFormants && fromFormant <= toFormant,
  974. U"Formant number(s) should be in the interval [1, ", numberOfFormants, U"].");
  975. for (integer iformant = fromFormant; iformant <= toFormant; iformant ++) {
  976. DataModeler ffi = my trackmodelers.at [iformant];
  977. DataModeler_setDataWeighing (ffi, useSigmaY);
  978. }
  979. }
  980. void FormantModeler_fit (FormantModeler me) {
  981. for (integer iformant = 1; iformant <= my trackmodelers.size; iformant ++) {
  982. DataModeler ffi = my trackmodelers.at [iformant];
  983. DataModeler_fit (ffi);
  984. }
  985. }
  986. void FormantModeler_drawBasisFunction (FormantModeler me, Graphics g, double tmin, double tmax, double fmin, double fmax,
  987. integer iformant, integer iterm, bool scaled, integer numberOfPoints, bool garnish)
  988. {
  989. if (tmax <= tmin)
  990. tmin = my xmin, tmax = my xmax;
  991. if (iformant < 1 || iformant > my trackmodelers.size)
  992. return;
  993. Graphics_setInner (g);
  994. DataModeler ffi = my trackmodelers.at [iformant];
  995. DataModeler_drawBasisFunction_inside (ffi, g, tmin, tmax, fmin, fmax, iterm, scaled, numberOfPoints);
  996. Graphics_unsetInner (g);
  997. if (garnish) {
  998. Graphics_inqWindow (g, &tmin, &tmax, &fmin, &fmax);
  999. Graphics_drawInnerBox (g);
  1000. Graphics_textBottom (g, true, U"Time (s)");
  1001. Graphics_textLeft (g, true, (scaled ? U"Frequency (Hz)" : U"Amplitude"));
  1002. Graphics_marksBottom (g, 2, true, true, false);
  1003. Graphics_markLeft (g, fmin, true, true, false, U"");
  1004. Graphics_markLeft (g, fmax, true, true, false, U"");
  1005. }
  1006. }
  1007. static integer FormantModeler_drawingSpecifiers_x (FormantModeler me, double *xmin, double *xmax, integer *ixmin, integer *ixmax) {
  1008. Melder_assert (my trackmodelers.size > 0);
  1009. DataModeler fm = my trackmodelers.at [1];
  1010. return DataModeler_drawingSpecifiers_x (fm, xmin, xmax, ixmin, ixmax);
  1011. }
  1012. static void FormantModeler_getCumulativeChiScores (FormantModeler me, int useSigmaY, double chisq []) {
  1013. try {
  1014. integer numberOfDataPoints = FormantModeler_getNumberOfDataPoints (me);
  1015. integer numberOfFormants = my trackmodelers.size;
  1016. autoNUMvector<double> zscores (1, numberOfDataPoints);
  1017. for (integer iformant = 1; iformant <= numberOfFormants; iformant ++) {
  1018. DataModeler fm = my trackmodelers.at [iformant];
  1019. DataModeler_getZScores (fm, useSigmaY, zscores.peek());
  1020. DataModeler_getChisqScoresFromZScores (fm, zscores.peek(), true, zscores.peek()); // undefined -> average
  1021. for (integer i = 1; i <= numberOfDataPoints; i ++)
  1022. chisq [i] += zscores [i];
  1023. }
  1024. } catch (MelderError) {
  1025. Melder_throw (me, U"cannot determine cumulative chi squares.");
  1026. }
  1027. }
  1028. static void FormantModeler_getVariancesBetweenTrackAndEstimatedTrack (FormantModeler me, integer iformant, integer estimatedFormant, double var []) {
  1029. integer numberOfDataPoints = FormantModeler_getNumberOfDataPoints (me);
  1030. integer numberOfFormants = my trackmodelers.size;
  1031. if (iformant < 1 || iformant > numberOfFormants || estimatedFormant < 1 || estimatedFormant > numberOfFormants)
  1032. return;
  1033. DataModeler fi = my trackmodelers.at [iformant];
  1034. DataModeler fe = my trackmodelers.at [estimatedFormant];
  1035. for (integer i = 1; i <= numberOfDataPoints; i ++) {
  1036. var [i] = undefined;
  1037. if (fi -> dataPointStatus [i] != DataModeler_DATA_INVALID) {
  1038. double ye = fe -> f_evaluate (fe, fe -> x [i], fe -> parameter);
  1039. double diff = ye - fi -> y [i];
  1040. var [i] = diff * diff;
  1041. }
  1042. }
  1043. }
  1044. static void FormantModeler_getSumOfVariancesBetweenShiftedAndEstimatedTracks (FormantModeler me,
  1045. int shiftDirection, integer *fromFormant, integer *toFormant, double var [])
  1046. {
  1047. try {
  1048. integer numberOfFormants = my trackmodelers.size;
  1049. if (*fromFormant < 1 || *fromFormant > numberOfFormants || *toFormant < 1 || *toFormant > numberOfFormants || *toFormant < *fromFormant) {
  1050. *toFormant = 1;
  1051. *fromFormant = numberOfFormants;
  1052. }
  1053. integer formantTrack = *fromFormant, estimatedFormantTrack = *fromFormant; // FormantModeler_NOSHIFT_TRACKS
  1054. if (shiftDirection == FormantModeler_DOWNSHIFT_TRACKS) {
  1055. estimatedFormantTrack = *fromFormant;
  1056. formantTrack = *fromFormant + 1;
  1057. *fromFormant = *fromFormant == 1 ? 2 : *fromFormant;
  1058. } else if (shiftDirection == FormantModeler_UPSHIFT_TRACKS) {
  1059. formantTrack = *fromFormant;
  1060. estimatedFormantTrack = *fromFormant + 1;
  1061. *toFormant = *toFormant == numberOfFormants ? numberOfFormants - 1 : *toFormant;
  1062. }
  1063. integer numberOfDataPoints = FormantModeler_getNumberOfDataPoints (me);
  1064. autoNUMvector<double> vari (1, numberOfDataPoints);
  1065. for (integer iformant = *fromFormant; iformant <= *toFormant; iformant ++) {
  1066. FormantModeler_getVariancesBetweenTrackAndEstimatedTrack (me, formantTrack, estimatedFormantTrack, vari.peek());
  1067. for (integer i = 1; i <= numberOfDataPoints; i ++) {
  1068. if (isdefined (vari [i]))
  1069. var [i] += vari [i];
  1070. }
  1071. formantTrack ++;
  1072. estimatedFormantTrack ++;
  1073. }
  1074. } catch (MelderError) {
  1075. Melder_throw (me, U" cannot get variances.");
  1076. }
  1077. }
  1078. void FormantModeler_drawVariancesOfShiftedTracks (FormantModeler me, Graphics g, double xmin, double xmax,
  1079. double ymin, double ymax, int shiftDirection, integer fromFormant, integer toFormant, bool garnish)
  1080. {
  1081. try {
  1082. integer ixmin, ixmax;
  1083. Melder_require (FormantModeler_drawingSpecifiers_x (me, & xmin, & xmax, & ixmin, & ixmax) > 0,
  1084. U"The are not enough data points in the drawing range.");
  1085. integer numberOfDataPoints = FormantModeler_getNumberOfDataPoints (me);
  1086. autoNUMvector<double> var (1, numberOfDataPoints);
  1087. autoNUMvector<double> varShifted (1, numberOfDataPoints);
  1088. FormantModeler_getSumOfVariancesBetweenShiftedAndEstimatedTracks (me, shiftDirection, & fromFormant, & toFormant, varShifted.peek());
  1089. FormantModeler_getSumOfVariancesBetweenShiftedAndEstimatedTracks (me, 0, & fromFormant, & toFormant, var.peek());
  1090. for (integer i = ixmin + 1; i <= ixmax; i ++) {
  1091. if (isdefined (varShifted [i]) && isdefined (var [i]))
  1092. var [i] -= varShifted [i];
  1093. }
  1094. if (ymax <= ymin)
  1095. NUMvector_extrema<double> (var.peek(), ixmin, ixmax, & ymin, & ymax);
  1096. Graphics_setInner (g);
  1097. Graphics_setWindow (g, xmin, xmax, ymin, ymax);
  1098. DataModeler thee = my trackmodelers.at [1];
  1099. while (isundef (var [ixmin]) && ixmin <= ixmax)
  1100. ixmin ++;
  1101. double xp = thy x [ixmin], yp = var [ixmin];
  1102. for (integer i = ixmin + 1; i <= ixmax; i ++) {
  1103. if (isdefined (var [i])) {
  1104. Graphics_line (g, xp, yp, thy x [i], var [i]);
  1105. xp = thy x [i];
  1106. yp = var [i];
  1107. }
  1108. }
  1109. Graphics_unsetInner (g);
  1110. if (garnish) {
  1111. Graphics_drawInnerBox (g);
  1112. Graphics_marksBottom (g, 2, true, true, false);
  1113. Graphics_marksLeft (g, 2, true, true, false);
  1114. }
  1115. } catch (MelderError) {
  1116. Melder_clearError ();
  1117. }
  1118. }
  1119. void FormantModeler_drawCumulativeChiScores (FormantModeler me, Graphics g, double xmin, double xmax, double ymin, double ymax,
  1120. int useSigmaY, bool garnish)
  1121. {
  1122. try {
  1123. integer ixmin, ixmax;
  1124. Melder_require (FormantModeler_drawingSpecifiers_x (me, & xmin, & xmax, & ixmin, & ixmax) > 0,
  1125. U"Not enough data points in drawing range.");
  1126. integer numberOfDataPoints = FormantModeler_getNumberOfDataPoints (me);
  1127. autoNUMvector<double> chisq (1, numberOfDataPoints);
  1128. FormantModeler_getCumulativeChiScores (me, useSigmaY, chisq.peek());
  1129. if (ymax <= ymin)
  1130. NUMvector_extrema<double> (chisq.peek(), ixmin, ixmax, & ymin, & ymax);
  1131. Graphics_setInner (g);
  1132. Graphics_setWindow (g, xmin, xmax, ymin, ymax);
  1133. DataModeler thee = my trackmodelers.at [1];
  1134. for (integer i = ixmin + 1; i <= ixmax; i ++)
  1135. Graphics_line (g, thy x [i - 1], chisq [i - 1], thy x [i], chisq [i]);
  1136. Graphics_unsetInner (g);
  1137. if (garnish) {
  1138. Graphics_drawInnerBox (g);
  1139. Graphics_marksBottom (g, 2, true, true, false);
  1140. Graphics_marksLeft (g, 2, true, true, false);
  1141. }
  1142. } catch (MelderError) {
  1143. Melder_clearError ();
  1144. }
  1145. }
  1146. void FormantModeler_drawOutliersMarked (FormantModeler me, Graphics g, double tmin, double tmax, double fmax, integer fromTrack, integer toTrack,
  1147. double numberOfSigmas, int useSigmaY, conststring32 mark, int marksFontSize, double horizontalOffset_mm, bool garnish)
  1148. {
  1149. if (tmax <= tmin)
  1150. tmin = my xmin, tmax = my xmax;
  1151. integer maxTrack = my trackmodelers.size;
  1152. if (toTrack == 0 && fromTrack == 0)
  1153. fromTrack = 1, toTrack = maxTrack;
  1154. if (fromTrack > maxTrack) return;
  1155. if (toTrack > maxTrack)
  1156. toTrack = maxTrack;
  1157. Graphics_setInner (g);
  1158. int currectFontSize = Graphics_inqFontSize (g);
  1159. for (integer iformant = fromTrack; iformant <= toTrack; iformant ++) {
  1160. DataModeler ffi = my trackmodelers.at [iformant];
  1161. double xOffset_mm = ( iformant % 2 == 1 ? horizontalOffset_mm : -horizontalOffset_mm );
  1162. DataModeler_drawOutliersMarked_inside (ffi, g, tmin, tmax, 0, fmax, numberOfSigmas, useSigmaY, mark, marksFontSize, xOffset_mm);
  1163. }
  1164. Graphics_setFontSize (g, currectFontSize);
  1165. Graphics_unsetInner (g);
  1166. if (garnish) {
  1167. Graphics_drawInnerBox (g);
  1168. Graphics_textBottom (g, true, U"Time (s)");
  1169. Graphics_textLeft (g, true, U"Formant frequency (Hz)");
  1170. Graphics_marksBottom (g, 2, true, true, false);
  1171. Graphics_marksLeftEvery (g, 1.0, 1000.0, true, true, true);
  1172. }
  1173. }
  1174. void FormantModeler_normalProbabilityPlot (FormantModeler me, Graphics g, integer iformant, int useSigmaY, integer numberOfQuantiles, double numberOfSigmas, int labelSize, conststring32 label, bool garnish) {
  1175. if (iformant > 0 || iformant <= my trackmodelers.size) {
  1176. DataModeler ff = my trackmodelers.at [iformant];
  1177. DataModeler_normalProbabilityPlot (ff, g, useSigmaY, numberOfQuantiles, numberOfSigmas, labelSize, label, garnish);
  1178. }
  1179. }
  1180. static void FormantModeler_drawTracks_inside (FormantModeler me, Graphics g, double xmin, double xmax, double fmax,
  1181. integer fromTrack, integer toTrack, int estimated, integer numberOfParameters, double horizontalOffset_mm) {
  1182. for (integer iformant = fromTrack; iformant <= toTrack; iformant ++) {
  1183. DataModeler ffi = my trackmodelers.at [iformant];
  1184. double xOffset_mm = ( iformant % 2 == 1 ? horizontalOffset_mm : -horizontalOffset_mm );
  1185. DataModeler_drawTrack_inside (ffi, g, xmin, xmax, 0, fmax, estimated, numberOfParameters, xOffset_mm);
  1186. }
  1187. }
  1188. void FormantModeler_drawTracks (FormantModeler me, Graphics g, double tmin, double tmax, double fmax,
  1189. integer fromTrack, integer toTrack, int estimated, integer numberOfParameters, double horizontalOffset_mm, bool garnish) {
  1190. if (tmax <= tmin)
  1191. tmin = my xmin, tmax = my xmax;
  1192. integer maxTrack = my trackmodelers.size;
  1193. if (toTrack == 0 && fromTrack == 0)
  1194. fromTrack = 1, toTrack = maxTrack;
  1195. if (fromTrack > maxTrack) return;
  1196. if (toTrack > maxTrack)
  1197. toTrack = maxTrack;
  1198. Graphics_setInner (g);
  1199. FormantModeler_drawTracks_inside (me, g, tmin, tmax, fmax, fromTrack, toTrack, estimated, numberOfParameters, horizontalOffset_mm);
  1200. Graphics_unsetInner (g);
  1201. if (garnish) {
  1202. Graphics_drawInnerBox (g);
  1203. Graphics_textBottom (g, true, U"Time (s)");
  1204. Graphics_textLeft (g, true, U"Formant frequency (Hz)");
  1205. Graphics_marksBottom (g, 2, true, true, false);
  1206. Graphics_marksLeftEvery (g, 1.0, 1000.0, true, true, true);
  1207. }
  1208. }
  1209. static void FormantModeler_speckle_inside (FormantModeler me, Graphics g, double xmin, double xmax, double fmax,
  1210. integer fromTrack, integer toTrack, int estimated, integer numberOfParameters, int errorBars, double barWidth_mm, double horizontalOffset_mm)
  1211. {
  1212. for (integer iformant = fromTrack; iformant <= toTrack; iformant ++) {
  1213. DataModeler ffi = my trackmodelers.at [iformant];
  1214. double xOffset_mm = ( iformant % 2 == 1 ? horizontalOffset_mm : -horizontalOffset_mm );
  1215. DataModeler_speckle_inside (ffi, g, xmin, xmax, 0, fmax, estimated, numberOfParameters, errorBars, barWidth_mm, xOffset_mm);
  1216. }
  1217. }
  1218. void FormantModeler_speckle (FormantModeler me, Graphics g, double tmin, double tmax, double fmax,
  1219. integer fromTrack, integer toTrack, int estimated, integer numberOfParameters,
  1220. int errorBars, double barWidth_mm, double horizontalOffset_mm, bool garnish)
  1221. {
  1222. if (tmax <= tmin)
  1223. tmin = my xmin, tmax = my xmax;
  1224. integer maxTrack = my trackmodelers.size;
  1225. if (toTrack == 0 && fromTrack == 0)
  1226. fromTrack = 1, toTrack = maxTrack;
  1227. if (fromTrack > maxTrack) return;
  1228. if (toTrack > maxTrack)
  1229. toTrack = maxTrack;
  1230. Graphics_setInner (g);
  1231. FormantModeler_speckle_inside (me, g, tmin, tmax, fmax, fromTrack, toTrack, estimated, numberOfParameters, errorBars, barWidth_mm, horizontalOffset_mm);
  1232. Graphics_unsetInner (g);
  1233. if (garnish) {
  1234. Graphics_drawInnerBox (g);
  1235. Graphics_textBottom (g, true, U"Time (s)");
  1236. Graphics_textLeft (g, true, U"Formant frequency (Hz)");
  1237. Graphics_marksBottom (g, 2, true, true, false);
  1238. Graphics_marksLeftEvery (g, 1.0, 1000.0, true, true, true);
  1239. }
  1240. }
  1241. autoFormantModeler FormantModeler_create (double tmin, double tmax, integer numberOfFormants, integer numberOfDataPoints, integer numberOfParameters) {
  1242. try {
  1243. autoFormantModeler me = Thing_new (FormantModeler);
  1244. my xmin = tmin; my xmax = tmax;
  1245. for (integer itrack = 1; itrack <= numberOfFormants; itrack ++) {
  1246. autoDataModeler ff = DataModeler_create (tmin, tmax, numberOfDataPoints, numberOfParameters, DataModeler_TYPE_LEGENDRE);
  1247. my trackmodelers. addItem_move (ff.move());
  1248. }
  1249. return me;
  1250. } catch (MelderError) {
  1251. Melder_throw (U"FormantModeler not created.");
  1252. }
  1253. }
  1254. double FormantModeler_getModelValueAtTime (FormantModeler me, integer iformant, double time) {
  1255. double f = undefined;
  1256. if (iformant >= 1 && iformant <= my trackmodelers.size) {
  1257. DataModeler thee = my trackmodelers.at [iformant];
  1258. f = DataModeler_getModelValueAtX (thee, time);
  1259. }
  1260. return f;
  1261. }
  1262. double FormantModeler_getModelValueAtIndex (FormantModeler me, integer iformant, integer index) {
  1263. double f = undefined;
  1264. if (iformant >= 1 && iformant <= my trackmodelers.size) {
  1265. DataModeler thee = my trackmodelers.at [iformant];
  1266. f = DataModeler_getModelValueAtIndex (thee, index);
  1267. }
  1268. return f;
  1269. }
  1270. double FormantModeler_getWeightedMean (FormantModeler me, integer iformant) {
  1271. double f = undefined;
  1272. if (iformant >= 1 && iformant <= my trackmodelers.size) {
  1273. DataModeler thee = my trackmodelers.at [iformant];
  1274. f = DataModeler_getWeightedMean (thee);
  1275. }
  1276. return f;
  1277. }
  1278. integer FormantModeler_getNumberOfTracks (FormantModeler me) {
  1279. return my trackmodelers.size;
  1280. }
  1281. integer FormantModeler_getNumberOfParameters (FormantModeler me, integer iformant) {
  1282. integer numberOfParameters = 0;
  1283. if (iformant > 0 && iformant <= my trackmodelers.size) {
  1284. DataModeler ff = my trackmodelers.at [iformant];
  1285. numberOfParameters = ff -> numberOfParameters;
  1286. }
  1287. return numberOfParameters;
  1288. }
  1289. integer FormantModeler_getNumberOfFixedParameters (FormantModeler me, integer iformant) {
  1290. integer numberOfParameters = 0;
  1291. if (iformant > 0 && iformant <= my trackmodelers.size) {
  1292. DataModeler ff = my trackmodelers.at [iformant];
  1293. numberOfParameters = ff -> numberOfParameters;
  1294. numberOfParameters -= DataModeler_getNumberOfFreeParameters (ff);
  1295. }
  1296. return numberOfParameters;
  1297. }
  1298. integer FormantModeler_getNumberOfInvalidDataPoints (FormantModeler me, integer iformant) {
  1299. integer numberOfInvalidDataPoints = 0;
  1300. if (iformant > 0 && iformant <= my trackmodelers.size) {
  1301. DataModeler ff = my trackmodelers.at [iformant];
  1302. numberOfInvalidDataPoints = DataModeler_getNumberOfInvalidDataPoints (ff);
  1303. }
  1304. return numberOfInvalidDataPoints;
  1305. }
  1306. double FormantModeler_getParameterValue (FormantModeler me, integer iformant, integer iparameter) {
  1307. double value = undefined;
  1308. if (iformant > 0 && iformant <= my trackmodelers.size) {
  1309. DataModeler ff = my trackmodelers.at [iformant];
  1310. value = DataModeler_getParameterValue (ff, iparameter);
  1311. }
  1312. return value;
  1313. }
  1314. int FormantModeler_getParameterStatus (FormantModeler me, integer iformant, integer index) {
  1315. int status = DataModeler_PARAMETER_UNDEFINED;
  1316. if (iformant > 0 && iformant <= my trackmodelers.size) {
  1317. DataModeler ff = my trackmodelers.at [iformant];
  1318. status = DataModeler_getParameterStatus (ff, index);
  1319. }
  1320. return status;
  1321. }
  1322. double FormantModeler_getParameterStandardDeviation ( FormantModeler me, integer iformant, integer index) {
  1323. double stdev = undefined;
  1324. if (iformant > 0 && iformant <= my trackmodelers.size) {
  1325. DataModeler ff = my trackmodelers.at [iformant];
  1326. stdev = DataModeler_getParameterStandardDeviation (ff, index);
  1327. }
  1328. return stdev;
  1329. }
  1330. double FormantModeler_getDegreesOfFreedom (FormantModeler me, integer iformant) {
  1331. double dof = 0.0;
  1332. if (iformant > 0 && iformant <= my trackmodelers.size) {
  1333. DataModeler ff = my trackmodelers.at [iformant];
  1334. dof = DataModeler_getDegreesOfFreedom (ff);
  1335. }
  1336. return dof;
  1337. }
  1338. double FormantModeler_getVarianceOfParameters (FormantModeler me, integer fromFormant, integer toFormant, integer fromIndex, integer toIndex, integer *numberOfFreeParameters) {
  1339. double variance = undefined;
  1340. integer numberOfFormants = my trackmodelers.size, numberOfParameters = 0, nofp;
  1341. if (toFormant < fromFormant || (toFormant == 0 && fromFormant == 0))
  1342. fromFormant = 1, toFormant = numberOfFormants;
  1343. if (fromFormant <= toFormant && fromFormant > 0 && toFormant <= numberOfFormants) {
  1344. variance = 0.0;
  1345. for (integer iformant = fromFormant; iformant <= toFormant; iformant ++) {
  1346. DataModeler ff = my trackmodelers.at [iformant];
  1347. variance += DataModeler_getVarianceOfParameters (ff, fromIndex, toIndex, &nofp);
  1348. numberOfParameters += nofp;
  1349. }
  1350. }
  1351. if (numberOfFreeParameters)
  1352. *numberOfFreeParameters = numberOfParameters;
  1353. return variance;
  1354. }
  1355. integer FormantModeler_getNumberOfDataPoints (FormantModeler me) {
  1356. Melder_assert (my trackmodelers.size > 0);
  1357. DataModeler thee = my trackmodelers.at [1];
  1358. // all tracks have the same number of data points
  1359. return thy numberOfDataPoints;
  1360. }
  1361. autoTable FormantModeler_to_Table_zscores (FormantModeler me, int useSigmaY) {
  1362. try {
  1363. integer icolt = 1, numberOfFormants = my trackmodelers.size;
  1364. integer numberOfDataPoints = FormantModeler_getNumberOfDataPoints (me);
  1365. autoNUMvector<double> zscores (1, numberOfDataPoints);
  1366. autoTable ztable = Table_createWithoutColumnNames (numberOfDataPoints, numberOfFormants + 1);
  1367. Table_setColumnLabel (ztable.get(), icolt, U"time");
  1368. for (integer iformant = 1; iformant <= numberOfFormants; iformant ++) {
  1369. integer icolz = iformant + 1;
  1370. Table_setColumnLabel (ztable.get(), icolz, Melder_cat (U"z", iformant));
  1371. DataModeler ffi = my trackmodelers.at [iformant];
  1372. if (iformant == 1) {
  1373. for (integer i = 1; i <= numberOfDataPoints; i ++) // only once all tracks have same x-values
  1374. Table_setNumericValue (ztable.get(), i, icolt, ffi -> x [i]);
  1375. }
  1376. DataModeler_getZScores (ffi, useSigmaY, zscores.peek());
  1377. for (integer i = 1; i <= numberOfDataPoints; i ++)
  1378. Table_setNumericValue (ztable.get(), i, icolz, zscores [i]);
  1379. }
  1380. return ztable;
  1381. } catch (MelderError) {
  1382. Melder_throw (U"Table not created.");
  1383. }
  1384. }
  1385. autoDataModeler FormantModeler_extractDataModeler (FormantModeler me, integer iformant) {
  1386. try {
  1387. Melder_require (iformant > 0 && iformant<= my trackmodelers.size,
  1388. U"The formant should be greater than zero and smaller than or equal to ", my trackmodelers.size);
  1389. DataModeler ff = my trackmodelers.at [iformant];
  1390. autoDataModeler thee = Data_copy (ff);
  1391. return thee;
  1392. } catch (MelderError) {
  1393. Melder_throw (U"DataModeler not created.");
  1394. }
  1395. }
  1396. autoCovariance FormantModeler_to_Covariance_parameters (FormantModeler me, integer iformant) {
  1397. try {
  1398. Melder_require (iformant > 0 && iformant<= my trackmodelers.size,
  1399. U"The formant should be greater than zero and smaller than or equal to ", my trackmodelers.size);
  1400. DataModeler thee = my trackmodelers.at [iformant];
  1401. autoCovariance cov = Data_copy (thy parameterCovariances.get());
  1402. return cov;
  1403. } catch (MelderError) {
  1404. Melder_throw (U"Covariance not created.");
  1405. }
  1406. }
  1407. void FormantModeler_setTolerance (FormantModeler me, double tolerance) {
  1408. for (integer iformant = 1; iformant <= my trackmodelers.size; iformant ++) {
  1409. DataModeler ffi = my trackmodelers.at [iformant];
  1410. DataModeler_setTolerance (ffi, tolerance);
  1411. }
  1412. }
  1413. double FormantModeler_indexToTime (FormantModeler me, integer index) {
  1414. Melder_assert (my trackmodelers.size > 0);
  1415. DataModeler thee = my trackmodelers.at [1];
  1416. return ( index > 0 && index <= thy numberOfDataPoints ? thy x [index] : undefined );
  1417. }
  1418. autoFormantModeler Formant_to_FormantModeler (Formant me, double tmin, double tmax,
  1419. integer numberOfFormants, integer numberOfParametersPerTrack, int bandwidthEstimatesSigma)
  1420. {
  1421. try {
  1422. integer ifmin, ifmax, posInCollection = 0;
  1423. if (tmax <= tmin)
  1424. tmin = my xmin, tmax = my xmax;
  1425. integer numberOfDataPoints = Sampled_getWindowSamples (me, tmin, tmax, & ifmin, & ifmax);
  1426. Melder_require (numberOfDataPoints >= numberOfParametersPerTrack, U"There are not enought data points, please extend the selection.");
  1427. autoFormantModeler thee = FormantModeler_create (tmin, tmax, numberOfFormants, numberOfDataPoints, numberOfParametersPerTrack);
  1428. for (integer iformant = 1; iformant <= numberOfFormants; iformant ++) {
  1429. posInCollection ++;
  1430. DataModeler ffi = thy trackmodelers.at [posInCollection];
  1431. integer idata = 0, validData = 0;
  1432. for (integer iframe = ifmin; iframe <= ifmax; iframe ++) {
  1433. Formant_Frame curFrame = & my d_frames [iframe];
  1434. ffi -> x [++ idata] = Sampled_indexToX (me, iframe);
  1435. ffi -> dataPointStatus [idata] = DataModeler_DATA_INVALID;
  1436. if (iformant <= curFrame -> nFormants) {
  1437. double frequency = curFrame -> formant [iformant]. frequency;
  1438. if (isdefined (frequency)) {
  1439. double bw = curFrame -> formant [iformant]. bandwidth;
  1440. ffi -> y [idata] = curFrame -> formant [iformant]. frequency;
  1441. ffi -> sigmaY [idata] = bw;
  1442. ffi -> dataPointStatus [idata] = DataModeler_DATA_VALID;
  1443. validData ++;
  1444. }
  1445. }
  1446. }
  1447. ffi -> useSigmaY = bandwidthEstimatesSigma;
  1448. ffi -> numberOfDataPoints = idata;
  1449. ffi -> tolerance = 1e-5;
  1450. if (validData < numberOfParametersPerTrack) { // remove don't throw exception
  1451. thy trackmodelers. removeItem (posInCollection);
  1452. posInCollection --;
  1453. }
  1454. }
  1455. if (posInCollection == 0)
  1456. Melder_throw (U"Not enough data points in all the formants.");
  1457. FormantModeler_fit (thee.get());
  1458. return thee;
  1459. } catch (MelderError) {
  1460. Melder_throw (U"FormantModeler not created.");
  1461. }
  1462. }
  1463. autoFormant FormantModeler_to_Formant (FormantModeler me, int useEstimates, int estimateUndefineds) {
  1464. try {
  1465. integer numberOfFormants = my trackmodelers.size;
  1466. DataModeler ff = my trackmodelers.at [1];
  1467. integer numberOfFrames = ff -> numberOfDataPoints;
  1468. double t1 = ff -> x [1], dt = ff -> x [2] - t1;
  1469. autoFormant thee = Formant_create (my xmin, my xmax, numberOfFrames, dt, t1, numberOfFormants);
  1470. autoNUMvector<double> sigma (1, numberOfFormants);
  1471. if (useEstimates || estimateUndefineds) {
  1472. for (integer iformant = 1; iformant <= numberOfFormants; iformant ++)
  1473. sigma [iformant] = FormantModeler_getStandardDeviation (me, iformant);
  1474. }
  1475. for (integer iframe = 1; iframe <= numberOfFrames; iframe ++) {
  1476. Formant_Frame thyFrame = & thy d_frames [iframe];
  1477. thyFrame -> intensity = 1.0; //???
  1478. thyFrame -> formant = NUMvector <structFormant_Formant> (1, numberOfFormants);
  1479. for (integer iformant = 1; iformant <= numberOfFormants; iformant ++) {
  1480. DataModeler ffi = my trackmodelers.at [iformant];
  1481. double f = undefined, b = f;
  1482. if (ffi -> dataPointStatus [iframe] != DataModeler_DATA_INVALID) {
  1483. f = ( useEstimates ? DataModeler_getModelValueAtX (ffi, ffi -> x [iframe]) : ffi -> y [iframe]);
  1484. b = ff -> sigmaY [iframe]; // copy original value
  1485. } else {
  1486. if (estimateUndefineds) {
  1487. f = FormantModeler_getModelValueAtTime (me, iformant, ffi -> x [iframe]);
  1488. b = sigma [iformant];
  1489. }
  1490. }
  1491. thyFrame -> formant [iformant]. frequency = f;
  1492. thyFrame -> formant [iformant]. bandwidth = b;
  1493. }
  1494. }
  1495. return thee;
  1496. } catch (MelderError) {
  1497. Melder_throw (U"Cannot create Formant from FormantModeler.");
  1498. }
  1499. }
  1500. double FormantModeler_getChiSquaredQ (FormantModeler me, integer fromFormant, integer toFormant, int useSigmaY,
  1501. double *out_probability, double *out_ndf)
  1502. {
  1503. double chisq = undefined, ndfTotal = 0.0;
  1504. if (toFormant < fromFormant || (fromFormant == 0 && toFormant == 0))
  1505. fromFormant = 1, toFormant = my trackmodelers.size;
  1506. if (fromFormant >= 1 && toFormant <= my trackmodelers.size) {
  1507. chisq = 0.0;
  1508. integer numberOfDefined = 0;
  1509. for (integer iformant= fromFormant; iformant <= toFormant; iformant ++) {
  1510. DataModeler ffi = my trackmodelers.at [iformant];
  1511. double p, df, chisqi = DataModeler_getChiSquaredQ (ffi, useSigmaY, &p, &df);
  1512. if (isdefined (chisqi)) {
  1513. chisq += df * chisqi;
  1514. ndfTotal += df;
  1515. numberOfDefined ++;
  1516. }
  1517. }
  1518. if (numberOfDefined == toFormant - fromFormant + 1) { // chisq of all tracks defined
  1519. chisq /= ndfTotal;
  1520. if (out_ndf)
  1521. *out_ndf = ndfTotal;
  1522. if (out_probability)
  1523. *out_probability = NUMchiSquareQ (chisq, ndfTotal);
  1524. }
  1525. }
  1526. return chisq;
  1527. }
  1528. double FormantModeler_getCoefficientOfDetermination (FormantModeler me, integer fromFormant, integer toFormant) {
  1529. double rSquared = undefined;
  1530. if (fromFormant == 0 && toFormant == 0) {
  1531. fromFormant = 1;
  1532. toFormant = my trackmodelers.size;
  1533. }
  1534. if (fromFormant >= 1 && toFormant <= my trackmodelers.size) {
  1535. double ssreg = 0.0, sstot = 0.0;
  1536. for (integer iformant= fromFormant; iformant <= toFormant; iformant ++) {
  1537. DataModeler ffi = my trackmodelers.at [iformant];
  1538. double ssregi, sstoti;
  1539. DataModeler_getCoefficientOfDetermination (ffi, & ssregi, & sstoti);
  1540. sstot += sstoti;
  1541. ssreg += ssregi;
  1542. }
  1543. rSquared = ( sstot > 0.0 ? ssreg / sstot : 1.0 );
  1544. }
  1545. return rSquared;
  1546. }
  1547. double FormantModeler_getResidualSumOfSquares (FormantModeler me, integer iformant, integer *out_numberOfDataPoints) {
  1548. double rss = undefined;
  1549. integer numberOfDataPoints = -1;
  1550. if (iformant > 0 && iformant <= my trackmodelers.size) {
  1551. DataModeler ff = my trackmodelers.at [iformant];
  1552. rss = DataModeler_getResidualSumOfSquares (ff, & numberOfDataPoints);
  1553. }
  1554. if (out_numberOfDataPoints)
  1555. *out_numberOfDataPoints = numberOfDataPoints;
  1556. return rss;
  1557. }
  1558. void FormantModeler_setParameterValuesToZero (FormantModeler me, integer fromFormant, integer toFormant, double numberOfSigmas) {
  1559. if (fromFormant == 0 && toFormant == 0) {
  1560. fromFormant = 1;
  1561. toFormant = my trackmodelers.size;
  1562. }
  1563. if (fromFormant >= 1 && toFormant <= my trackmodelers.size) {
  1564. for (integer iformant= fromFormant; iformant <= toFormant; iformant ++) {
  1565. DataModeler ffi = my trackmodelers.at [iformant];
  1566. DataModeler_setParameterValuesToZero (ffi, numberOfSigmas);
  1567. }
  1568. }
  1569. }
  1570. autoFormantModeler FormantModeler_processOutliers (FormantModeler me, double numberOfSigmas, int useSigmaY) {
  1571. try {
  1572. integer numberOfFormants = my trackmodelers.size;
  1573. Melder_require (numberOfFormants > 2, U"We need at least three formants to process outliers.");
  1574. integer numberOfDataPoints = FormantModeler_getNumberOfDataPoints (me);
  1575. autoNUMvector<double> x (1, numberOfDataPoints); // also store x-values
  1576. autoNUMmatrix<double> z (1, numberOfFormants, 1, numberOfDataPoints);
  1577. // maybe some of the formants had NUMundefind's.
  1578. // 1. calculate z-scores for each formant and sort them in descending order
  1579. DataModeler ff = my trackmodelers.at [1];
  1580. NUMvector_copyElements<double> (ff -> x, x.peek(), 1, numberOfDataPoints);
  1581. for (integer iformant = 1; iformant <= numberOfFormants; iformant ++) {
  1582. DataModeler ffi = my trackmodelers.at [iformant];
  1583. DataModeler_getZScores (ffi, useSigmaY, z [iformant]);
  1584. }
  1585. // 2. Do the manipulation in a copy
  1586. autoFormantModeler thee = Data_copy (me);
  1587. for (integer i = 1; i <= numberOfDataPoints; i ++) {
  1588. // First the easy one: first formant missing: F1' = F2; F2' = F3
  1589. if (isdefined (z [1] [i]) && isdefined (z [1] [i]) && isdefined (z [3] [i])) {
  1590. if (z [1] [i] > numberOfSigmas && z [2] [i] > numberOfSigmas && z [3] [i] > numberOfSigmas) {
  1591. // all deviations have the same sign:
  1592. // probably F1 is missing
  1593. // try if f2 <- F1 and f3 <- F2 reduces chisq
  1594. double f2 = FormantModeler_getDataPointValue (me, 1, i); // F1
  1595. double f3 = FormantModeler_getDataPointValue (me, 2, i); // F2
  1596. FormantModeler_setDataPointStatus (thee.get(), 1, i, DataModeler_DATA_INVALID);
  1597. FormantModeler_setDataPointValueAndStatus (thee.get(), 2, i, f2, FormantModeler_UPSHIFT_TRACKS);
  1598. FormantModeler_setDataPointValueAndStatus (thee.get(), 3, i, f3, FormantModeler_UPSHIFT_TRACKS);
  1599. }
  1600. }
  1601. }
  1602. FormantModeler_fit (thee.get());
  1603. return thee;
  1604. } catch (MelderError) {
  1605. Melder_throw (U"Cannot calculate track discontinuities");
  1606. }
  1607. }
  1608. double FormantModeler_getSmoothnessValue (FormantModeler me, integer fromFormant, integer toFormant, integer numberOfParametersPerTrack, double power) {
  1609. double smoothness = undefined;
  1610. if (toFormant < fromFormant || (toFormant == 0 && fromFormant == 0)) {
  1611. fromFormant = 1;
  1612. toFormant = my trackmodelers.size;
  1613. }
  1614. if (fromFormant > 0 && fromFormant <= toFormant && toFormant <= my trackmodelers.size) {
  1615. integer nofp;
  1616. double ndof, var = FormantModeler_getVarianceOfParameters (me, fromFormant, toFormant, 1, numberOfParametersPerTrack, &nofp);
  1617. double chisq = FormantModeler_getChiSquaredQ (me, fromFormant, toFormant, true, nullptr, &ndof);
  1618. if (isdefined (var) && isdefined (chisq) && nofp > 0)
  1619. smoothness = log10 (pow (var / nofp, power) * (chisq / ndof));
  1620. }
  1621. return smoothness;
  1622. }
  1623. double FormantModeler_getAverageDistanceBetweenTracks (FormantModeler me, integer track1, integer track2, int type) {
  1624. double diff = undefined;
  1625. if (track1 == track2)
  1626. return 0.0;
  1627. if (track1 <= my trackmodelers.size && track2 <= my trackmodelers.size) {
  1628. DataModeler fi = my trackmodelers.at [track1];
  1629. DataModeler fj = my trackmodelers.at [track2];
  1630. // fi and fj have equal number of data points
  1631. integer numberOfDataPoints = 0;
  1632. diff = 0.0;
  1633. for (integer i = 1; i <= fi -> numberOfDataPoints; i ++) {
  1634. if (type != 0) {
  1635. double fie = fi -> f_evaluate (fi, fi -> x [i], fi -> parameter);
  1636. double fje = fj -> f_evaluate (fj, fj -> x [i], fj -> parameter);
  1637. diff += fabs (fie - fje);
  1638. numberOfDataPoints ++;
  1639. } else if (fi -> dataPointStatus [i] != DataModeler_DATA_INVALID && fj -> dataPointStatus [i] != DataModeler_DATA_INVALID) {
  1640. diff += fabs (fi -> y [i] - fj -> y [i]);
  1641. numberOfDataPoints ++;
  1642. }
  1643. }
  1644. diff /= numberOfDataPoints;
  1645. }
  1646. return diff;
  1647. }
  1648. double FormantModeler_getFormantsConstraintsFactor (FormantModeler me, double minF1, double maxF1, double minF2, double maxF2, double minF3) {
  1649. double f1 = FormantModeler_getParameterValue (me, 1, 1); // trackmodelers -> item [1] -> parameter [1]
  1650. double minF1Factor = f1 > minF1 ? 1 : sqrt (minF1 - f1 + 1.0);
  1651. double maxF1Factor = f1 < maxF1 ? 1 : sqrt (f1 - maxF1 + 1.0);
  1652. double f2 = FormantModeler_getParameterValue (me, 2, 1); // trackmodelers -> item [2] -> parameter [1]
  1653. double minF2Factor = f2 > minF2 ? 1 : sqrt (minF2 - f2 + 1.0);
  1654. double maxF2Factor = f2 < maxF2 ? 1 : sqrt (f2 - maxF2 + 1.0);
  1655. double f3 = FormantModeler_getParameterValue (me, 3, 1); // trackmodelers -> item [3] -> parameter [1]
  1656. double minF3Factor = f3 > minF3 ? 1 : sqrt (minF3 - f3 + 1.0);
  1657. return minF1Factor * maxF1Factor * minF2Factor * maxF2Factor * minF3Factor;
  1658. }
  1659. void FormantModeler_reportChiSquared (FormantModeler me, int weighDataType) {
  1660. integer numberOfFormants = my trackmodelers.size;
  1661. double chisq = 0, ndf = 0, probability;
  1662. MelderInfo_writeLine (U"Chi squared tests for individual models of each of ", numberOfFormants, U" formant track:");
  1663. MelderInfo_writeLine (weighDataType == DataModeler_DATA_WEIGH_EQUAL ? U"Standard deviation is estimated from the data." :
  1664. weighDataType == DataModeler_DATA_WEIGH_SIGMA ? U"\tBandwidths are used as estimate for local standard deviations." :
  1665. weighDataType == DataModeler_DATA_WEIGH_RELATIVE ? U"\t1/Q's are used as estimate for local standard deviations." :
  1666. U"\tSqrt bandwidths are used as estimate for local standard deviations.");
  1667. for (integer iformant = 1; iformant <= numberOfFormants; iformant ++) {
  1668. chisq = FormantModeler_getChiSquaredQ (me, iformant, iformant, weighDataType, & probability, & ndf);
  1669. MelderInfo_writeLine (U"Formant track ", iformant, U":");
  1670. MelderInfo_writeLine (U"\tChi squared (F", iformant, U") = ", chisq);
  1671. MelderInfo_writeLine (U"\tProbability (F", iformant, U") = ", probability);
  1672. MelderInfo_writeLine (U"\tNumber of degrees of freedom (F", iformant, U") = ", ndf);
  1673. }
  1674. chisq = FormantModeler_getChiSquaredQ (me, 1, numberOfFormants, weighDataType, & probability, & ndf);
  1675. MelderInfo_writeLine (U"Chi squared test for the complete model with ", numberOfFormants, U" formants:");
  1676. MelderInfo_writeLine (U"\tChi squared = ", chisq);
  1677. MelderInfo_writeLine (U"\tProbability = ", probability);
  1678. MelderInfo_writeLine (U"\tNumber of degrees of freedom = ", ndf);
  1679. }
  1680. integer Formants_getSmoothestInInterval (CollectionOf<structFormant>* me, double tmin, double tmax,
  1681. integer numberOfFormantTracks, integer numberOfParametersPerTrack,
  1682. int useBandWidthsForTrackEstimation, int useConstraints, double numberOfSigmas, double power,
  1683. double minF1, double maxF1, double minF2, double maxF2, double minF3)
  1684. {
  1685. try {
  1686. integer numberOfFormantObjects = my size, minNumberOfFormants = 1000000;
  1687. if (numberOfFormantObjects == 1)
  1688. return 1;
  1689. autoNUMvector<integer> numberOfFormants (1, numberOfFormantObjects);
  1690. autoNUMvector<int> invalid (1, numberOfFormantObjects);
  1691. double tminf = 0.0, tmaxf = 0.0;
  1692. for (integer iobject = 1; iobject <= numberOfFormantObjects; iobject ++) {
  1693. // Check that all Formants have the same domain
  1694. Formant fi = my at [iobject];
  1695. if (tminf == tmaxf) {
  1696. tminf = fi -> xmin; tmaxf = fi -> xmax;
  1697. } else if (fi -> xmin != tminf || fi -> xmax != tmaxf) {
  1698. Melder_throw (U"All Formant objects must have the same starting and finishing times.");
  1699. }
  1700. // Find the one that has least formant tracks
  1701. numberOfFormants [iobject] = Formant_getMaxNumFormants (fi);
  1702. if (numberOfFormants [iobject] < minNumberOfFormants) {
  1703. minNumberOfFormants = numberOfFormants [iobject];
  1704. }
  1705. }
  1706. if (numberOfFormantTracks == 0) { // default
  1707. numberOfFormantTracks = minNumberOfFormants;
  1708. }
  1709. if (numberOfFormantTracks > minNumberOfFormants) {
  1710. // make formants with not enough tracks invalid for the competition
  1711. integer numberOfInvalids = 0;
  1712. for (integer iobject = 1; iobject <= numberOfFormantObjects; iobject ++) {
  1713. if (numberOfFormants [iobject] < numberOfFormantTracks) {
  1714. invalid [iobject] = 1;
  1715. numberOfInvalids ++;
  1716. }
  1717. }
  1718. Melder_require (numberOfInvalids < numberOfFormantObjects, U"None of the Formants has enough formant tracks. Please, lower your upper formant number.");
  1719. }
  1720. if (tmax <= tmin)
  1721. tmin = tminf, tmax = tmaxf;
  1722. Melder_require (tmin >= tminf && tmax <= tmaxf, U"The selected interval should be within the Formant object's domain.");
  1723. /* The chisq is not meaningfull as a the only test whether one model is better than the other because
  1724. if we have two models 1 & 2 with the same data points (x1 [i]=x2 [i] and y1 [i]= y2 [i] but if
  1725. sigma1 [i] < sigma2 [i] than chisq1 > chisq2.
  1726. This is not what we want.
  1727. We test therefore the variances of the parameters because if sigma1 [i] < sigma2 [i] than pvar1 < pvar2.
  1728. */
  1729. double minChiVar = 1e308;
  1730. integer index = 0;
  1731. for (integer iobject = 1; iobject <= numberOfFormantObjects; iobject ++) {
  1732. if (invalid [iobject] != 1) {
  1733. Formant fi = my at [iobject];
  1734. autoFormantModeler fs = Formant_to_FormantModeler (fi, tmin, tmax, numberOfFormantTracks, numberOfParametersPerTrack, useBandWidthsForTrackEstimation);
  1735. FormantModeler_setParameterValuesToZero (fs.get(), 1, numberOfFormantTracks, numberOfSigmas);
  1736. double cf = useConstraints ? FormantModeler_getFormantsConstraintsFactor (fs.get(), minF1, maxF1, minF2, maxF2, minF3) : 1;
  1737. double chiVar = FormantModeler_getSmoothnessValue (fs.get(), 1, numberOfFormantTracks, numberOfParametersPerTrack, power);
  1738. if (isdefined (chiVar) && cf * chiVar < minChiVar) {
  1739. minChiVar = cf * chiVar;
  1740. index = iobject;
  1741. }
  1742. }
  1743. }
  1744. return index;
  1745. } catch (MelderError) {
  1746. Melder_throw (U"No Formant object could be selected.");
  1747. }
  1748. }
  1749. autoFormant Formant_extractPart (Formant me, double tmin, double tmax) {
  1750. try {
  1751. if (tmin >= tmax) {
  1752. tmin = my xmin;
  1753. tmax = my xmax;
  1754. }
  1755. Melder_require (tmin < my xmax && tmax > my xmin,
  1756. U"Your start and end time should be between ", my xmin, U" and ", my xmax, U".");
  1757. integer thyindex = 1, ifmin, ifmax;
  1758. integer numberOfFrames = Sampled_getWindowSamples (me, tmin, tmax, & ifmin, & ifmax);
  1759. double t1 = Sampled_indexToX (me, ifmin);
  1760. autoFormant thee = Formant_create (tmin, tmax, numberOfFrames, my dx, t1, my maxnFormants);
  1761. for (integer iframe = ifmin; iframe <= ifmax; iframe++, thyindex++) {
  1762. Formant_Frame myFrame = & my d_frames [iframe];
  1763. Formant_Frame thyFrame = & thy d_frames [thyindex];
  1764. myFrame -> copy (thyFrame);
  1765. }
  1766. return thee;
  1767. } catch (MelderError) {
  1768. Melder_throw (U"Formant part could not be extracted.");
  1769. }
  1770. }
  1771. autoFormant Formants_extractSmoothestPart (CollectionOf<structFormant>* me, double tmin, double tmax,
  1772. integer numberOfFormantTracks, integer numberOfParametersPerTrack, int useBandWidthsForTrackEstimation, double numberOfSigmas, double power)
  1773. {
  1774. try {
  1775. integer index = Formants_getSmoothestInInterval (me, tmin, tmax, numberOfFormantTracks, numberOfParametersPerTrack,
  1776. useBandWidthsForTrackEstimation, 0, numberOfSigmas, power, 1.0, 1.0, 1.0, 1.0, 1.0); // last five are just fillers
  1777. Formant bestfit = my at [index];
  1778. autoFormant thee = Formant_extractPart (bestfit, tmin, tmax);
  1779. return thee;
  1780. } catch (MelderError) {
  1781. Melder_throw (U"Smoothest Formant part could not be extracted.");
  1782. }
  1783. }
  1784. autoFormant Formants_extractSmoothestPart_withFormantsConstraints (CollectionOf<structFormant>* me, double tmin, double tmax,
  1785. integer numberOfFormantTracks, integer numberOfParametersPerTrack, int useBandWidthsForTrackEstimation,
  1786. double numberOfSigmas, double power, double minF1, double maxF1, double minF2, double maxF2, double minF3)
  1787. {
  1788. try {
  1789. integer index = Formants_getSmoothestInInterval (me, tmin, tmax, numberOfFormantTracks, numberOfParametersPerTrack,
  1790. useBandWidthsForTrackEstimation, 1, numberOfSigmas, power, minF1, maxF1, minF2, maxF2, minF3);
  1791. Formant bestfit = my at [index];
  1792. autoFormant thee = Formant_extractPart (bestfit, tmin, tmax);
  1793. return thee;
  1794. } catch (MelderError) {
  1795. Melder_throw (U"Smoothest Formant part could not be extracted.");
  1796. }
  1797. }
  1798. Thing_implement (PitchModeler, DataModeler, 0);
  1799. autoPitchModeler Pitch_to_PitchModeler (Pitch me, double tmin, double tmax, integer numberOfParameters) {
  1800. try {
  1801. if (tmax <= tmin)
  1802. tmin = my xmin, tmax = my xmax;
  1803. integer ifmin, ifmax;
  1804. integer numberOfDataPoints = Sampled_getWindowSamples (me, tmin, tmax, & ifmin, & ifmax);
  1805. Melder_require (numberOfParameters <= numberOfDataPoints,
  1806. U"The number of parameters should not exceed the number of data points. Please, extend the selection.");
  1807. autoPitchModeler thee = Thing_new (PitchModeler);
  1808. DataModeler_init (thee.get(), tmin, tmax, numberOfDataPoints, numberOfParameters, DataModeler_TYPE_LEGENDRE);
  1809. integer idata = 0, validData = 0;
  1810. for (integer iframe = ifmin; iframe <= ifmax; iframe ++) {
  1811. thy x [++ idata] = Sampled_indexToX (me, iframe);
  1812. thy dataPointStatus [idata] = DataModeler_DATA_INVALID;
  1813. if (Pitch_isVoiced_i (me, iframe)) {
  1814. thy y [idata] = my frame [iframe]. candidate [1]. frequency;
  1815. thy dataPointStatus [idata] = DataModeler_DATA_VALID;
  1816. validData ++;
  1817. }
  1818. }
  1819. thy numberOfDataPoints = idata;
  1820. if (validData < numberOfParameters) // remove don't throw exception
  1821. Melder_throw (U"Not enough valid data in interval.");
  1822. DataModeler_fit (thee.get());
  1823. return thee;
  1824. } catch (MelderError) {
  1825. Melder_throw (U"No PitchModeler could be created.");
  1826. }
  1827. }
  1828. void PitchModeler_draw (PitchModeler me, Graphics g, double tmin, double tmax, double fmin, double fmax,
  1829. integer numberOfParameters, bool garnish)
  1830. {
  1831. Graphics_setInner (g);
  1832. DataModeler_drawTrack_inside (me, g, tmin, tmax, fmin, fmax, 1, numberOfParameters, 0);
  1833. Graphics_unsetInner (g);
  1834. if (garnish) {
  1835. Graphics_drawInnerBox (g);
  1836. Graphics_textBottom (g, true, U"Time (s)");
  1837. Graphics_textLeft (g, true, U"Frequency (Hz)");
  1838. Graphics_marksBottom (g, 2, true, true, false);
  1839. Graphics_marksLeftEvery (g, 1.0, 100.0, true, true, true);
  1840. }
  1841. }
  1842. double Sound_getOptimalFormantCeiling (Sound me, double startTime, double endTime,
  1843. double windowLength, double timeStep, double minFreq, double maxFreq, integer numberOfFrequencySteps,
  1844. double preemphasisFrequency, integer numberOfFormantTracks, integer numberOfParametersPerTrack, int weighData,
  1845. double numberOfSigmas, double power)
  1846. {
  1847. double optimalCeiling;
  1848. autoFormant thee = Sound_to_Formant_interval (me, startTime, endTime, windowLength, timeStep, minFreq, maxFreq, numberOfFrequencySteps, preemphasisFrequency, numberOfFormantTracks, numberOfParametersPerTrack, weighData, numberOfSigmas, power, false, 0.0, 5000.0, 0.0, 5000.0, 0.0, & optimalCeiling);
  1849. return optimalCeiling;
  1850. }
  1851. autoFormant Sound_to_Formant_interval (Sound me, double startTime, double endTime,
  1852. double windowLength, double timeStep, double minFreq, double maxFreq, integer numberOfFrequencySteps,
  1853. double preemphasisFrequency, integer numberOfFormantTracks, integer numberOfParametersPerTrack, int weighData,
  1854. double numberOfSigmas, double power, bool useConstraints, double minF1, double maxF1, double minF2, double maxF2, double minF3,
  1855. double *out_optimalCeiling)
  1856. {
  1857. try {
  1858. // parameter check
  1859. if (endTime <= startTime)
  1860. startTime = my xmin, endTime = my xmax;
  1861. double nyquistFrequency = 0.5 / my dx;
  1862. Melder_require (maxFreq <= nyquistFrequency, U"The upper value of the maximum frequency range should not exceed the Nyquist frequency of the sound.");
  1863. double df = 0, mincriterium = 1e28;
  1864. if (minFreq >= maxFreq) {
  1865. numberOfFrequencySteps = 1;
  1866. } else {
  1867. df = (maxFreq - minFreq) / (numberOfFrequencySteps - 1);
  1868. }
  1869. double optimalCeiling = minFreq;
  1870. integer i_best = 0;
  1871. // extract part +- windowLength because of Gaussian windowing in the formant analysis
  1872. // +timeStep/2 to have the analysis points maximally spread in the new domain.
  1873. autoSound part = Sound_extractPart (me, startTime - windowLength + timeStep / 2.0, endTime + windowLength + timeStep / 2.0, kSound_windowShape::RECTANGULAR, 1, 1);
  1874. // Resample to 2*maxFreq to reduce resampling load in Sound_to_Formant
  1875. autoSound resampled = Sound_resample (part.get(), 2.0 * maxFreq, 50);
  1876. OrderedOf<structFormant> formants;
  1877. Melder_progressOff ();
  1878. for (integer i = 1; i <= numberOfFrequencySteps; i ++) {
  1879. double currentCeiling = minFreq + (i - 1) * df;
  1880. autoFormant formant = Sound_to_Formant_burg (resampled.get(), timeStep, 5.0, currentCeiling, windowLength, preemphasisFrequency);
  1881. autoFormantModeler fm = Formant_to_FormantModeler (formant.get(), startTime, endTime, numberOfFormantTracks, numberOfParametersPerTrack, weighData);
  1882. FormantModeler_setParameterValuesToZero (fm.get(), 1, numberOfFormantTracks, numberOfSigmas);
  1883. formants. addItem_move (formant.move());
  1884. double cf = ( useConstraints ? FormantModeler_getFormantsConstraintsFactor (fm.get(), minF1, maxF1, minF2, maxF2, minF3) : 1 );
  1885. double chiVar = FormantModeler_getSmoothnessValue (fm.get(), 1, numberOfFormantTracks, numberOfParametersPerTrack, power);
  1886. double criterium = chiVar * cf;
  1887. if (isdefined (chiVar) && criterium < mincriterium) {
  1888. mincriterium = criterium;
  1889. optimalCeiling = currentCeiling;
  1890. i_best = i;
  1891. }
  1892. }
  1893. autoFormant thee = Formant_extractPart (formants.at [i_best], startTime, endTime);
  1894. Melder_progressOn ();
  1895. if (out_optimalCeiling)
  1896. *out_optimalCeiling = optimalCeiling;
  1897. return thee;
  1898. } catch (MelderError) {
  1899. Melder_throw (U"No Formant object created.");
  1900. }
  1901. }
  1902. autoFormant Sound_to_Formant_interval_robust (Sound me, double startTime, double endTime,
  1903. double windowLength, double timeStep, double minFreq, double maxFreq, integer numberOfFrequencySteps,
  1904. double preemphasisFrequency, integer numberOfFormantTracks, integer numberOfParametersPerTrack, int weighData,
  1905. double numberOfSigmas, double power, bool useConstraints, double minF1, double maxF1, double minF2, double maxF2, double minF3,
  1906. double *out_optimalCeiling)
  1907. {
  1908. try {
  1909. if (endTime <= startTime)
  1910. startTime = my xmin, endTime = my xmax;
  1911. double nyquistFrequency = 0.5 / my dx;
  1912. Melder_require (maxFreq <= nyquistFrequency, U"The upper value of the maximum frequency range should not exceed the Nyquist frequency of the sound.");
  1913. double df = 0, mincriterium = 1e28;
  1914. if (minFreq >= maxFreq) {
  1915. numberOfFrequencySteps = 1;
  1916. } else {
  1917. df = (maxFreq - minFreq) / (numberOfFrequencySteps - 1);
  1918. }
  1919. integer i_best = 0;
  1920. double optimalCeiling = minFreq;
  1921. // extract part +- windowLength because of Gaussian windowing in the formant analysis
  1922. // +timeStep/2 to have the analysis points maximally spread in the new domain.
  1923. autoSound part = Sound_extractPart (me, startTime - windowLength + timeStep / 2, endTime + windowLength + timeStep / 2, kSound_windowShape::RECTANGULAR, 1, 1);
  1924. // Resample to 2*maxFreq to reduce resampling load in Sound_to_Formant
  1925. autoSound resampled = Sound_resample (part.get(), 2.0 * maxFreq, 50);
  1926. OrderedOf<structFormant> formants;
  1927. Melder_progressOff ();
  1928. for (integer i = 1; i <= numberOfFrequencySteps; i ++) {
  1929. double currentCeiling = minFreq + (i - 1) * df;
  1930. autoFormant formant = Sound_to_Formant_robust (resampled.get(), timeStep, 5.0, currentCeiling, windowLength, preemphasisFrequency, 50.0, 1.5, 3, 0.0000001, 1);
  1931. autoFormantModeler fm = Formant_to_FormantModeler (formant.get(), startTime, endTime, numberOfFormantTracks, numberOfParametersPerTrack, weighData);
  1932. FormantModeler_setParameterValuesToZero (fm.get(), 1, numberOfFormantTracks, numberOfSigmas);
  1933. formants. addItem_move (formant.move());
  1934. double cf = ( useConstraints ? FormantModeler_getFormantsConstraintsFactor (fm.get(), minF1, maxF1, minF2, maxF2, minF3) : 1 );
  1935. double chiVar = FormantModeler_getSmoothnessValue (fm.get(), 1, numberOfFormantTracks, numberOfParametersPerTrack, power);
  1936. double criterium = chiVar * cf;
  1937. if (isdefined (chiVar) && criterium < mincriterium) {
  1938. mincriterium = criterium;
  1939. optimalCeiling = currentCeiling;
  1940. i_best = i;
  1941. }
  1942. }
  1943. autoFormant thee = Formant_extractPart (formants.at [i_best], startTime, endTime);
  1944. Melder_progressOn ();
  1945. if (out_optimalCeiling)
  1946. *out_optimalCeiling = optimalCeiling;
  1947. return thee;
  1948. } catch (MelderError) {
  1949. Melder_throw (U"No Formant object created.");
  1950. }
  1951. }
  1952. #if 0
  1953. // If e.g. first formant is obviously "missing" then assign F1 as
  1954. static void FormantModeler_correctFormantsProbablyIndexedFalsely (FormantModeler /* me */) {
  1955. /* if shift down F1 ("correct" F1 missed)
  1956. * elsif shift down F2 ("correct" F2 missed)
  1957. * else if spurious formant before F1
  1958. * else if spurious formant between F1 and F2
  1959. * endif
  1960. * */
  1961. }
  1962. #endif
  1963. autoOptimalCeilingTier Sound_to_OptimalCeilingTier (Sound me,
  1964. double windowLength, double timeStep, double minCeiling, double maxCeiling, integer numberOfFrequencySteps,
  1965. double preemphasisFrequency, double smoothingWindow, integer numberOfFormantTracks, integer numberOfParametersPerTrack, int weighData,
  1966. double numberOfSigmas, double power)
  1967. {
  1968. try {
  1969. OrderedOf<structFormant> formants;
  1970. double frequencyStep = numberOfFrequencySteps > 1 ? (maxCeiling - minCeiling) / (numberOfFrequencySteps - 1) : 0;
  1971. for (integer i = 1; i <= numberOfFrequencySteps; i ++) {
  1972. double ceiling = minCeiling + (i - 1) * frequencyStep;
  1973. autoFormant formant = Sound_to_Formant_burg (me, timeStep, 5, ceiling, windowLength, preemphasisFrequency);
  1974. formants. addItem_move (formant.move());
  1975. }
  1976. integer numberOfFrames;
  1977. double firstTime, modelingTimeStep = timeStep;
  1978. autoOptimalCeilingTier octier = OptimalCeilingTier_create (my xmin, my xmax);
  1979. Sampled_shortTermAnalysis (me, smoothingWindow, modelingTimeStep, & numberOfFrames, & firstTime);
  1980. for (integer iframe = 1; iframe <= numberOfFrames; iframe ++) {
  1981. double time = firstTime + (iframe - 1) * modelingTimeStep;
  1982. double tmin = time - smoothingWindow / 2.0;
  1983. double tmax = tmin + smoothingWindow;
  1984. integer index = Formants_getSmoothestInInterval (& formants, tmin, tmax, numberOfFormantTracks, numberOfParametersPerTrack, weighData,
  1985. 0, numberOfSigmas, power, 200.0, 1500.0, 300.0, 3000.0, 1000.0); // min/max values are not used
  1986. double ceiling = minCeiling + (index - 1) * frequencyStep;
  1987. RealTier_addPoint (octier.get(), time, ceiling);
  1988. }
  1989. return octier;
  1990. } catch (MelderError) {
  1991. Melder_throw (me, U" no OptimalCeilingTier calculated.");
  1992. }
  1993. }
  1994. /* End of file DataModeler.cpp */