Sound_and_LPC.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /* Sound_and_LPC.cpp
  2. *
  3. * Copyright (C) 1994-2018 David Weenink
  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. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. djmw 20020625 GPL header
  20. djmw corrected a bug in Sound_into_LPC_Frame_marple that could crash praat when signal has only zero samples.
  21. djmw 20040303 Removed warning in Sound_to_LPC.
  22. djmw 20070103 Sound interface changes
  23. djmw 20080122 float -> double
  24. djmw 20101009 Filter and inverseFilter with one frame.
  25. */
  26. #include "Sound_and_LPC.h"
  27. #include "Sound_extensions.h"
  28. #include "Vector.h"
  29. #include "Spectrum.h"
  30. #include "NUM2.h"
  31. #define LPC_METHOD_AUTO 1
  32. #define LPC_METHOD_COVAR 2
  33. #define LPC_METHOD_BURG 3
  34. #define LPC_METHOD_MARPLE 4
  35. /* Markel&Gray, LP of S, page 219
  36. work [1..3*m+2]
  37. r = & work [1]; // r [1..m+1]
  38. a= & work [m+1+1]; // a [1..m+1]
  39. rc = & work [m+1+m+1+1]; // rc [1..m]
  40. for (i=1; i<= m+1+m+1+m;i ++) work [i] = 0;
  41. */
  42. #define LPC_METHOD_AUTO_WINDOW_CORRECTION 1
  43. static void LPC_Frame_Sound_filter (LPC_Frame me, Sound thee, integer channel) {
  44. double *y = thy z [channel], *a = my a.at;
  45. for (integer i = 1; i <= thy nx; i ++) {
  46. integer m = i > my nCoefficients ? my nCoefficients : i - 1;
  47. for (integer j = 1; j <= m; j ++) {
  48. y [i] -= a [j] * y [i - j];
  49. }
  50. }
  51. }
  52. void LPC_Frame_Sound_filterInverse (LPC_Frame me, Sound thee, integer channel) {
  53. double *x = thy z [channel];
  54. autoNUMvector <double> y ((integer) 0, my nCoefficients);
  55. for (integer i = 1; i <= thy nx; i ++) {
  56. y [0] = x [i];
  57. for (integer j = 1; j <= my nCoefficients; j ++) {
  58. x [i] += my a [j] * y [j];
  59. }
  60. for (integer j = my nCoefficients; j > 0; j--) {
  61. y [j] = y [j - 1];
  62. }
  63. }
  64. }
  65. static int Sound_into_LPC_Frame_auto (Sound me, LPC_Frame thee) {
  66. integer i = 1; // For error condition at end
  67. integer m = thy nCoefficients;
  68. autoNUMvector<double> r (1, m + 1);
  69. autoNUMvector<double> a (1, m + 1);
  70. autoNUMvector<double> rc (1, m);
  71. double *x = my z [1];
  72. for (i = 1; i <= m + 1; i ++) {
  73. for (integer j = 1; j <= my nx - i + 1; j ++) {
  74. r [i] += x [j] * x [j + i - 1];
  75. }
  76. }
  77. if (r [1] == 0.0) {
  78. i = 1; /* ! */ goto end;
  79. }
  80. a [1] = 1.0; a [2] = rc [1] = - r [2] / r [1];
  81. thy gain = r [1] + r [2] * rc [1];
  82. for (i = 2; i <= m; i ++) {
  83. double s = 0.0;
  84. for (integer j = 1; j <= i; j ++) {
  85. s += r [i - j + 2] * a [j];
  86. }
  87. rc [i] = - s / thy gain;
  88. for (integer j = 2; j <= i / 2 + 1; j ++) {
  89. double at = a [j] + rc [i] * a [i - j + 2];
  90. a [i - j + 2] += rc [i] * a [j];
  91. a [j] = at;
  92. }
  93. a [i + 1] = rc [i]; thy gain += rc [i] * s;
  94. if (thy gain <= 0) {
  95. goto end;
  96. }
  97. }
  98. end:
  99. i--;
  100. for (integer j = 1; j <= i; j ++) {
  101. thy a [j] = a [j + 1];
  102. }
  103. if (i == m) {
  104. return 1;
  105. }
  106. thy nCoefficients = i;
  107. for (integer j = i + 1; j <= m; j ++) {
  108. thy a [j] = 0.0;
  109. }
  110. return 0; // Melder_warning ("Less coefficienst than asked for.");
  111. }
  112. /* Markel&Gray, LP of S, page 221
  113. work [1..m(m+1)/2+m+m+1+m+m+1]
  114. b = & work [1]
  115. grc = & work [m*(m+1)/2+1];
  116. a = & work [m*(m+1)/2+m+1];
  117. beta = & work [m+1)/2+m+m+1+1];
  118. cc = & work [m+1)/2+m+m+1+m+1]
  119. for (i=1; i<=m(m+1)/2+m+m+1+m+m+1;i ++) work [i] = 0;
  120. */
  121. static int Sound_into_LPC_Frame_covar (Sound me, LPC_Frame thee) {
  122. integer i = 1, n = my nx, m = thy nCoefficients;
  123. double *x = my z [1];
  124. autoNUMvector<double> b (1, m * (m + 1) / 2);
  125. autoNUMvector<double> grc (1, m);
  126. autoNUMvector<double> a (1, m + 1);
  127. autoNUMvector<double> beta (1, m);
  128. autoNUMvector<double> cc (1, m + 1);
  129. thy gain = 0.0;
  130. for (i = m + 1; i <= n; i ++) {
  131. thy gain += x [i] * x [i];
  132. cc [1] += x [i] * x [i - 1];
  133. cc [2] += x [i - 1] * x [i - 1];
  134. }
  135. if (thy gain == 0.0) {
  136. i = 1; /* ! */ goto end;
  137. }
  138. b [1] = 1.0;
  139. beta [1] = cc [2];
  140. a [1] = 1.0;
  141. a [2] = grc [1] = -cc [1] / cc [2];
  142. thy gain += grc [1] * cc [1];
  143. for (i = 2; i <= m; i ++) { /*130*/
  144. double s = 0.0; /* 20 */
  145. for (integer j = 1; j <= i; j ++) {
  146. cc [i - j + 2] = cc [i - j + 1] + x [m - i + 1] * x [m - i + j] - x [n - i + 1] * x [n - i + j];
  147. }
  148. cc [1] = 0.0;
  149. for (integer j = m + 1; j <= n; j ++) {
  150. cc [1] += x [j - i] * x [j]; /* 30 */
  151. }
  152. b [i * (i + 1) / 2] = 1.0;
  153. for (integer j = 1; j <= i - 1; j ++) { /* 70 */
  154. double gam = 0.0;
  155. if (beta [j] < 0.0) {
  156. goto end;
  157. } else if (beta [j] == 0.0) {
  158. continue;
  159. }
  160. for (integer k = 1; k <= j; k ++) {
  161. gam += cc [k + 1] * b [j * (j - 1) / 2 + k]; /*50*/
  162. }
  163. gam /= beta [j];
  164. for (integer k = 1; k <= j; k ++) {
  165. b [i * (i - 1) / 2 + k] -= gam * b [j * (j - 1) / 2 + k]; /*60*/
  166. }
  167. }
  168. beta [i] = 0.0;
  169. for (integer j = 1; j <= i; j ++) {
  170. beta [i] += cc [j + 1] * b [i * (i - 1) / 2 + j]; /*80*/
  171. }
  172. if (beta [i] <= 0.0) {
  173. goto end;
  174. }
  175. for (integer j = 1; j <= i; j ++) {
  176. s += cc [j] * a [j]; /*100*/
  177. }
  178. grc [i] = -s / beta [i];
  179. for (integer j = 2; j <= i; j ++) {
  180. a [j] += grc [i] * b [i * (i - 1) / 2 + j - 1]; /*110*/
  181. }
  182. a [i + 1] = grc [i];
  183. s = grc [i] * grc [i] * beta [i];
  184. thy gain -= s;
  185. if (thy gain <= 0.0) {
  186. goto end;
  187. }
  188. }
  189. end:
  190. i--;
  191. for (integer j = 1; j <= i; j ++) {
  192. thy a [j] = a [j + 1];
  193. }
  194. if (i == m) {
  195. return 1;
  196. }
  197. thy nCoefficients = i;
  198. for (integer j = i + 1; j <= m; j ++) {
  199. thy a [j] = 0.0;
  200. }
  201. return 0; // Melder_warning ("Less coefficienst than asked for.");
  202. }
  203. static int Sound_into_LPC_Frame_burg (Sound me, LPC_Frame thee) {
  204. thy gain = NUMburg_preallocated (thy a.get(), my z.row(1));
  205. thy gain *= my nx;
  206. for (integer i = 1; i <= thy nCoefficients; i ++) {
  207. thy a [i] = -thy a [i];
  208. }
  209. return thy gain != 0.0;
  210. }
  211. static int Sound_into_LPC_Frame_marple (Sound me, LPC_Frame thee, double tol1, double tol2) {
  212. integer m = 1, n = my nx, mmax = thy nCoefficients;
  213. int status = 1;
  214. double *a = thy a.at, *x = my z [1];
  215. autoVEC c = VECzero (mmax + 1);
  216. autoVEC d = VECzero (mmax + 1);
  217. autoVEC r = VECzero (mmax + 1);
  218. double e0 = 0.0;
  219. for (integer k = 1; k <= n; k ++) {
  220. e0 += x [k] * x [k];
  221. }
  222. e0 *= 2.0;
  223. if (e0 == 0.0) {
  224. m = 0;
  225. thy gain *= 0.5; /* because e0 is twice the energy */
  226. thy nCoefficients = m;
  227. return 0; // warning no signal
  228. }
  229. double q1 = 1.0 / e0;
  230. double q2 = q1 * x [1], q = q1 * x [1] * x [1], w = q1 * x [n] * x [n];
  231. double v = q, u = w;
  232. double den = 1.0 - q - w;
  233. double q4 = 1.0 / den, q5 = 1.0 - q, q6 = 1.0 - w;
  234. double h = q2 * x [n], s = h;
  235. thy gain = e0 * den;
  236. q1 = 1.0 / thy gain;
  237. c [1] = q1 * x [1];
  238. d [1] = q1 * x [n];
  239. double s1 = 0.0;
  240. for (integer k = 1; k <= n - 1; k ++) {
  241. s1 += x [k + 1] * x [k];
  242. }
  243. r [1] = 2.0 * s1;
  244. a [1] = - q1 * r [1];
  245. thy gain *= (1.0 - a [1] * a [1]);
  246. while (m < mmax) {
  247. double eOld = thy gain, f = x [m + 1], b = x [n - m]; /*n-1 ->n-m*/
  248. for (integer k = 1; k <= m; k ++) {
  249. /* n-1 -> n-m */
  250. f += x [m + 1 - k] * a [k];
  251. b += x [n - m + k] * a [k];
  252. }
  253. q1 = 1.0 / thy gain;
  254. q2 = q1 * f;
  255. double q3 = q1 * b;
  256. for (integer k = m; k >= 1; k--) {
  257. c [k + 1] = c [k] + q2 * a [k];
  258. d [k + 1] = d [k] * q3 * a [k];
  259. }
  260. c [1] = q2; d [1] = q3;
  261. double q7 = s * s;
  262. double y1 = f * f;
  263. double y2 = v * v;
  264. double y3 = b * b;
  265. double y4 = u * u;
  266. double y5 = 2.0 * h * s;
  267. q += y1 * q1 + q4 * (y2 * q6 + q7 * q5 + v * y5);
  268. w += y3 * q1 + q4 * (y4 * q5 + q7 * q6 + u * y5);
  269. h = s = u = v = 0.0;
  270. for (integer k = 0; k <= m; k ++) {
  271. h += x [n - m + k] * c [k + 1];
  272. s += x [n - k] * c [k + 1];
  273. u += x [n - k] * d [k + 1];
  274. v += x [k + 1] * c [k + 1];
  275. }
  276. q5 = 1.0 - q;
  277. q6 = 1.0 - w;
  278. den = q5 * q6 - h * h;
  279. if (den <= 0.0) {
  280. status = 2; goto end; /* 2: ill-conditioning */
  281. }
  282. q4 = 1.0 / den;
  283. q1 *= q4;
  284. double alf = 1.0 / (1.0 + q1 * (y1 * q6 + y3 * q5 + 2.0 * h * f * b));
  285. thy gain *= alf;
  286. y5 = h * s;
  287. double c1 = q4 * (f * q6 + b * h);
  288. double c2 = q4 * (b * q5 + h * f);
  289. double c3 = q4 * (v * q6 + y5);
  290. double c4 = q4 * (s * q5 + v * h);
  291. double c5 = q4 * (s * q6 + h * u);
  292. double c6 = q4 * (u * q5 + y5);
  293. for (integer k = 1; k <= m; k ++) {
  294. a [k] = alf * (a [k] + c1 * c [k + 1] + c2 * d [k + 1]);
  295. }
  296. for (integer k = 1; k <= m / 2 + 1; k ++) {
  297. s1 = c [k];
  298. double s2 = d [k], s3 = c [m + 2 - k], s4 = d [m + 2 - k];
  299. c [k] += c3 * s3 + c4 * s4;
  300. d [k] += c5 * s3 + c6 * s4;
  301. if (m + 2 - k == k) {
  302. continue;
  303. }
  304. c [m + 2 - k] += c3 * s1 + c4 * s2;
  305. d [m + 2 - k] += c5 * s1 + c6 * s2;
  306. }
  307. m ++; c1 = x [n + 1 - m]; c2 = x [m];
  308. double delta = 0;
  309. for (integer k = m - 1; k >= 1; k--) {
  310. r [k + 1] = r [k] - x [n + 1 - k] * c1 - x [k] * c2;
  311. delta += r [k + 1] * a [k];
  312. }
  313. s1 = 0.0;
  314. for (integer k = 1; k <= n - m; k ++) {
  315. s1 += x [k + m] * x [k];
  316. }
  317. r [1] = 2.0 * s1;
  318. delta += r [1];
  319. q2 = - delta / thy gain;
  320. a [m] = q2;
  321. for (integer k = 1; k <= m / 2; k ++) {
  322. s1 = a [k];
  323. a [k] += q2 * a [m - k];
  324. if (k == m - k) {
  325. continue;
  326. }
  327. a [m - k] += q2 * s1;
  328. }
  329. y1 = q2 * q2;
  330. thy gain *= 1.0 - y1;
  331. if (y1 >= 1.0) {
  332. status = 3; goto end; /* |a [m]| > 1 */
  333. }
  334. if (thy gain < e0 * tol1) {
  335. status = 4; goto end;
  336. }
  337. if (eOld - thy gain < eOld * tol2) {
  338. status = 5; goto end;
  339. }
  340. }
  341. end:
  342. thy gain *= 0.5; /* because e0 is twice the energy */
  343. thy nCoefficients = m;
  344. return status == 1 || status == 4 || status == 5;
  345. }
  346. static autoLPC _Sound_to_LPC (Sound me, int predictionOrder, double analysisWidth, double dt, double preEmphasisFrequency, int method, double tol1, double tol2) {
  347. double t1, samplingFrequency = 1.0 / my dx;
  348. double windowDuration = 2.0 * analysisWidth; /* gaussian window */
  349. integer numberOfFrames, frameErrorCount = 0;
  350. Melder_require (Melder_roundDown (windowDuration / my dx) > predictionOrder,
  351. U"Analysis window duration too short.\n For a prediction order of ", predictionOrder,
  352. U" the analysis window duration should be greater than ", my dx * (predictionOrder + 1), U"Please increase the analysis window duration or lower the prediction order.");
  353. // Convenience: analyse the whole sound into one LPC_frame
  354. if (windowDuration > my dx * my nx) {
  355. windowDuration = my dx * my nx;
  356. }
  357. Sampled_shortTermAnalysis (me, windowDuration, dt, & numberOfFrames, & t1);
  358. autoSound sound = Data_copy (me);
  359. autoSound sframe = Sound_createSimple (1, windowDuration, samplingFrequency);
  360. autoSound window = Sound_createGaussian (windowDuration, samplingFrequency);
  361. autoLPC thee = LPC_create (my xmin, my xmax, numberOfFrames, dt, t1, predictionOrder, my dx);
  362. autoMelderProgress progress (U"LPC analysis");
  363. if (preEmphasisFrequency < samplingFrequency / 2.0) {
  364. Sound_preEmphasis (sound.get(), preEmphasisFrequency);
  365. }
  366. for (integer i = 1; i <= numberOfFrames; i ++) {
  367. LPC_Frame lpcframe = & thy d_frames [i];
  368. double t = Sampled_indexToX (thee.get(), i);
  369. LPC_Frame_init (lpcframe, predictionOrder);
  370. Sound_into_Sound (sound.get(), sframe.get(), t - windowDuration / 2);
  371. Vector_subtractMean (sframe.get());
  372. Sounds_multiply (sframe.get(), window.get());
  373. if (method == LPC_METHOD_AUTO) {
  374. if (! Sound_into_LPC_Frame_auto (sframe.get(), lpcframe)) {
  375. frameErrorCount ++;
  376. }
  377. } else if (method == LPC_METHOD_COVAR) {
  378. if (! Sound_into_LPC_Frame_covar (sframe.get(), lpcframe)) {
  379. frameErrorCount ++;
  380. }
  381. } else if (method == LPC_METHOD_BURG) {
  382. if (! Sound_into_LPC_Frame_burg (sframe.get(), lpcframe)) {
  383. frameErrorCount ++;
  384. }
  385. } else if (method == LPC_METHOD_MARPLE) {
  386. if (! Sound_into_LPC_Frame_marple (sframe.get(), lpcframe, tol1, tol2)) {
  387. frameErrorCount ++;
  388. }
  389. }
  390. if (i % 10 == 1)
  391. Melder_progress ( (double) i / numberOfFrames, U"LPC analysis of frame ", i, U" out of ", numberOfFrames, U".");
  392. }
  393. return thee;
  394. }
  395. autoLPC Sound_to_LPC_auto (Sound me, int predictionOrder, double analysisWidth, double dt, double preEmphasisFrequency) {
  396. try {
  397. autoLPC thee = _Sound_to_LPC (me, predictionOrder, analysisWidth, dt, preEmphasisFrequency, LPC_METHOD_AUTO, 0, 0);
  398. return thee;
  399. } catch (MelderError) {
  400. Melder_throw (me, U": no LPC (auto) created.");
  401. }
  402. }
  403. autoLPC Sound_to_LPC_covar (Sound me, int predictionOrder, double analysisWidth, double dt, double preEmphasisFrequency) {
  404. try {
  405. autoLPC thee = _Sound_to_LPC (me, predictionOrder, analysisWidth, dt, preEmphasisFrequency, LPC_METHOD_COVAR, 0, 0);
  406. return thee;
  407. } catch (MelderError) {
  408. Melder_throw (me, U": no LPC (covar) created.");
  409. }
  410. }
  411. autoLPC Sound_to_LPC_burg (Sound me, int predictionOrder, double analysisWidth, double dt, double preEmphasisFrequency) {
  412. try {
  413. autoLPC thee = _Sound_to_LPC (me, predictionOrder, analysisWidth, dt, preEmphasisFrequency, LPC_METHOD_BURG, 0, 0);
  414. return thee;
  415. } catch (MelderError) {
  416. Melder_throw (me, U": no LPC (burg) created.");
  417. }
  418. }
  419. autoLPC Sound_to_LPC_marple (Sound me, int predictionOrder, double analysisWidth, double dt, double preEmphasisFrequency, double tol1, double tol2) {
  420. try {
  421. autoLPC thee = _Sound_to_LPC (me, predictionOrder, analysisWidth, dt, preEmphasisFrequency, LPC_METHOD_MARPLE, tol1, tol2);
  422. return thee;
  423. } catch (MelderError) {
  424. Melder_throw (me, U": no LPC (marple) created.");
  425. }
  426. }
  427. autoSound LPC_Sound_filterInverse (LPC me, Sound thee) {
  428. try {
  429. Melder_require (my samplingPeriod == thy dx, U"Sampling frequencies should be equal.");
  430. Melder_require (my xmin == thy xmin && thy xmax == my xmax, U"Domains of LPC and Sound should be equal.");
  431. autoSound him = Data_copy (thee);
  432. double *e = his z [1], *x = thy z [1];
  433. for (integer i = 1; i <= his nx; i ++) {
  434. double t = his x1 + (i - 1) * his dx; // Sampled_indexToX (him, i)
  435. integer iFrame = Melder_iround ((t - my x1) / my dx + 1.0); // Sampled_xToNearestIndex (me, t)
  436. double *a = my d_frames [iFrame]. a.at;
  437. if (iFrame < 1 || iFrame > my nx) {
  438. e [i] = 0.0;
  439. continue;
  440. }
  441. integer m = i > my d_frames[iFrame].nCoefficients ? my d_frames [iFrame].nCoefficients : i - 1;
  442. for (integer j = 1; j <= m; j ++) {
  443. e [i] += a [j] * x [i - j];
  444. }
  445. }
  446. return him;
  447. } catch (MelderError) {
  448. Melder_throw (thee, U": not inverse filtered.");
  449. }
  450. }
  451. /*
  452. gain used as a constant amplitude multiplyer within a frame of duration my dx.
  453. future alternative: convolve gain with a smoother.
  454. */
  455. autoSound LPC_Sound_filter (LPC me, Sound thee, bool useGain) {
  456. try {
  457. double xmin = my xmin > thy xmin ? my xmin : thy xmin;
  458. double xmax = my xmax < thy xmax ? my xmax : thy xmax;
  459. Melder_require (xmin < xmax, U"Domains of Sound [", thy xmin, U",", thy xmax, U"] and LPC [",
  460. my xmin, U",", my xmax, U"] should overlap.");
  461. // resample sound if samplings don't match
  462. autoSound source;
  463. if (my samplingPeriod != thy dx) {
  464. source = Sound_resample (thee, 1.0 / my samplingPeriod, 50);
  465. thee = source.get(); // reference copy; remove at end
  466. }
  467. autoSound him = Data_copy (thee);
  468. double *x = his z [1];
  469. integer ifirst = Sampled_xToHighIndex (thee, xmin);
  470. integer ilast = Sampled_xToLowIndex (thee, xmax);
  471. for (integer i = ifirst; i <= ilast; i ++) {
  472. double t = his x1 + (i - 1) * his dx; // Sampled_indexToX (him, i)
  473. integer iFrame = Melder_iround ((t - my x1) / my dx + 1.0); // Sampled_xToNearestIndex (me, t)
  474. if (iFrame < 1) {
  475. continue;
  476. }
  477. if (iFrame > my nx) {
  478. break;
  479. }
  480. double *a = my d_frames [iFrame].a.at;
  481. integer m = i > my d_frames [iFrame].nCoefficients ? my d_frames [iFrame].nCoefficients : i - 1;
  482. for (integer j = 1; j <= m; j ++) {
  483. x [i] -= a [j] * x [i - j];
  484. }
  485. }
  486. // Make samples before first frame and after last frame zero.
  487. for (integer i = 1; i < ifirst; i ++) {
  488. x [i] = 0.0;
  489. }
  490. for (integer i = ilast + 1; i <= his nx; i ++) {
  491. x [i] = 0.0;
  492. }
  493. if (useGain) {
  494. for (integer i = ifirst; i <= ilast; i ++) {
  495. double t = his x1 + (i - 1) * his dx; /* Sampled_indexToX (him, i) */
  496. double riFrame = (t - my x1) / my dx + 1; /* Sampled_xToIndex (me, t); */
  497. integer iFrame = Melder_ifloor (riFrame);
  498. double phase = riFrame - iFrame;
  499. if (iFrame < 0 || iFrame > my nx) {
  500. x [i] = 0.0;
  501. } else if (iFrame == 0) {
  502. x [i] *= sqrt (my d_frames [1].gain) * phase;
  503. } else if (iFrame == my nx) {
  504. x [i] *= sqrt (my d_frames [my nx].gain) * (1.0 - phase);
  505. } else x [i] *=
  506. phase * sqrt (my d_frames [iFrame + 1].gain) + (1.0 - phase) * sqrt (my d_frames [iFrame].gain);
  507. }
  508. }
  509. return him;
  510. } catch (MelderError) {
  511. Melder_throw (thee, U": not filtered.");
  512. }
  513. }
  514. void LPC_Sound_filterWithFilterAtTime_inplace (LPC me, Sound thee, integer channel, double time) {
  515. integer frameIndex = Sampled_xToNearestIndex (me, time);
  516. if (frameIndex < 1) {
  517. frameIndex = 1;
  518. }
  519. if (frameIndex > my nx) {
  520. frameIndex = my nx;
  521. }
  522. if (channel > thy ny) {
  523. channel = 1;
  524. }
  525. Melder_require (frameIndex > 0 && frameIndex <= my nx, U"Frame should be in the range [1, ", my nx, U"].");
  526. if (channel > 0) {
  527. LPC_Frame_Sound_filter (& (my d_frames [frameIndex]), thee, channel);
  528. } else {
  529. for (integer ichan = 1; ichan <= thy ny; ichan ++) {
  530. LPC_Frame_Sound_filter (& (my d_frames [frameIndex]), thee, ichan);
  531. }
  532. }
  533. }
  534. autoSound LPC_Sound_filterWithFilterAtTime (LPC me, Sound thee, integer channel, double time) {
  535. try {
  536. autoSound him = Data_copy (thee);
  537. LPC_Sound_filterWithFilterAtTime_inplace (me, him.get(), channel, time);
  538. return him;
  539. } catch (MelderError) {
  540. Melder_throw (thee, U": not filtered.");
  541. }
  542. }
  543. void LPC_Sound_filterInverseWithFilterAtTime_inplace (LPC me, Sound thee, integer channel, double time) {
  544. try {
  545. integer frameIndex = Sampled_xToNearestIndex (me, time);
  546. if (frameIndex < 1) {
  547. frameIndex = 1;
  548. }
  549. if (frameIndex > my nx) {
  550. frameIndex = my nx;
  551. }
  552. if (channel > thy ny) {
  553. channel = 1;
  554. }
  555. if (channel > 0) {
  556. LPC_Frame_Sound_filterInverse (& (my d_frames [frameIndex]), thee, channel);
  557. } else {
  558. for (integer ichan = 1; ichan <= thy ny; ichan ++) {
  559. LPC_Frame_Sound_filterInverse (& (my d_frames [frameIndex]), thee, ichan);
  560. }
  561. }
  562. } catch (MelderError) {
  563. Melder_throw (thee, U": not inverse filtered.");
  564. }
  565. }
  566. autoSound LPC_Sound_filterInverseWithFilterAtTime (LPC me, Sound thee, integer channel, double time) {
  567. try {
  568. autoSound him = Data_copy (thee);
  569. LPC_Sound_filterInverseWithFilterAtTime_inplace (me, him.get(), channel, time);
  570. return him;
  571. } catch (MelderError) {
  572. Melder_throw (thee, U": not inverse filtered.");
  573. }
  574. }
  575. /* End of file Sound_and_LPC.cpp */