matlabfunctions.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. *
  3. * 音声分析合成法 WORLD by M. Morise
  4. *
  5. * matlabfunctions.cpp
  6. * (c) M. Morise 2010-
  7. * edit and comment by HAL, kbinani
  8. *
  9. * This file is a part of WORLD system.
  10. * WORLD needs FFTW. Please install FFTW to use these files.
  11. * FFTW is here; http://www.fftw.org/
  12. *
  13. * notice: this comment is added by HAL.
  14. * Original files are on the web page below;
  15. * http://www.aspl.is.ritsumei.ac.jp/morise/world/
  16. *
  17. * matlabfunctions.cpp is a set of functions
  18. * equivalent functions in matlab.
  19. *
  20. */
  21. // Matlabから移植した関数の寄せ集め
  22. #include <math.h>
  23. #include "world.h"
  24. // matlabに順ずる丸め
  25. // gccのmath.hにあるdouble round(double)と被るので名前が変わってますー
  26. int c_round( double x ){
  27. if( x > 0.0 ){
  28. return (int)(x + 0.5);
  29. }else{
  30. return (int)(x - 0.5);
  31. }
  32. }
  33. // histc based on Matlab
  34. // This function is hidden.
  35. // length of i (Index) and y is the same.
  36. void histc(double *x, int xLen, double *y, int yLen, int *index)
  37. {
  38. int i;
  39. int count = 1;
  40. for(i = 0;i < yLen;i++)
  41. {
  42. index[i] = 1;
  43. if(y[i] >= x[0])
  44. break;
  45. }
  46. for(;i < yLen;i++)
  47. {
  48. if(y[i] < x[count]) index[i] = count;
  49. else
  50. {
  51. index[i] = count++;
  52. i--;
  53. }
  54. if(count == xLen) break;
  55. }
  56. count--;
  57. for(i++;i < yLen;i++) index[i] = count;
  58. }
  59. // interp1 by using linear interpolation
  60. // This function is based on Matlab function that has the same name
  61. void interp1(double *t, double *y, int iLen, double *t1, int oLen, double *y1)
  62. {
  63. int i;
  64. double *h, *p, *s;
  65. int *k;
  66. h = (double *)malloc(sizeof(double) * (iLen-1));
  67. p = (double *)malloc(sizeof(double) * oLen);
  68. s = (double *)malloc(sizeof(double) * oLen);
  69. k = (int *)malloc(sizeof(int) * oLen);
  70. // 初期設定
  71. for(i = 0;i < iLen-1;i++) h[i] = t[i+1]-t[i];
  72. for(i = 0;i < oLen;i++) {p[i] = i; k[i] = 0;}
  73. histc(t, iLen, t1, oLen, k);
  74. for(i = 0;i < oLen;i++) s[i] = (t1[i] - t[k[i]-1]) / h[k[i]-1];
  75. for(i = 0;i < oLen;i++)
  76. y1[i] = y[k[i]-1] + s[i]*(y[k[i]] - y[k[i]-1]);
  77. free(k);
  78. free(s);
  79. free(p);
  80. free(h);
  81. }
  82. // decimate by using IIR and FIR filter coefficients
  83. // x: Input signal whose length is xLen [sample]
  84. // y: Output signal
  85. long decimateForF0(double *x, int xLen, double *y, int r)
  86. {
  87. // int r = 11;
  88. int nfact = 9; // 多分これは固定でOK
  89. double *tmp1, *tmp2;
  90. tmp1 = (double *)malloc(sizeof(double) * (xLen + nfact*2));
  91. tmp2 = (double *)malloc(sizeof(double) * (xLen + nfact*2));
  92. int i;
  93. for(i = 0;i < nfact;i++) tmp1[i] = 2*x[0] - x[nfact-i];
  94. for(i = nfact;i < nfact+xLen;i++) tmp1[i] = x[i-nfact];
  95. for(i = nfact+xLen;i < 2*nfact+xLen;i++) tmp1[i] = 2*x[xLen-1] - x[xLen-2 - (i-(nfact+xLen))];
  96. filterForDecimate(tmp1, 2*nfact+xLen, tmp2, r);
  97. for(i = 0;i < 2*nfact+xLen;i++) tmp1[i] = tmp2[2*nfact+xLen - i - 1];
  98. filterForDecimate(tmp1, 2*nfact+xLen, tmp2, r);
  99. for(i = 0;i < 2*nfact+xLen;i++) tmp1[i] = tmp2[2*nfact+xLen - i - 1];
  100. int nout = (int)(xLen/r) + 1;
  101. int nbeg = r - (r*nout - xLen);
  102. int count;
  103. for(i = nbeg, count = 0;i < xLen+nfact;i+=r, count++) y[count] = tmp1[i+nfact-1];
  104. free(tmp1); free(tmp2);
  105. return count;
  106. }
  107. // filter based on Matlab function
  108. // x: Input signal whose length is xLen [sample]
  109. // y: Output signal
  110. void filterForDecimate(double *x, int xLen, double *y, int r)
  111. {
  112. double w[3], wt;
  113. w[0] = w[1] = w[2] = 0.0;
  114. double a[3], b[2]; // フィルタ係数 (r依存)
  115. switch(r)
  116. {
  117. case 11: // fs : 44100
  118. a[0] = 2.450743295230728;
  119. a[1] = -2.06794904601978;
  120. a[2] = 0.59574774438332101;
  121. b[0] = 0.0026822508007163792;
  122. b[1] = 0.0080467524021491377;
  123. break;
  124. case 12: // fs : 48000
  125. a[0] = 2.4981398605924205;
  126. a[1] = -2.1368928194784025;
  127. a[2] = 0.62187513816221485;
  128. b[0] = 0.0021097275904709001;
  129. b[1] = 0.0063291827714127002;
  130. break;
  131. case 8: // fs : 32000
  132. a[0] = 2.2357462340187593;
  133. a[1] = -1.7780899984041358;
  134. a[2] = 0.49152555365968692;
  135. b[0] = 0.0063522763407111993;
  136. b[1] = 0.019056829022133598;
  137. break;
  138. case 6: // fs : 24000 and 22050
  139. a[0] = 1.9715352749512141;
  140. a[1] = -1.4686795689225347;
  141. a[2] = 0.3893908434965701;
  142. b[0] = 0.013469181309343825;
  143. b[1] = 0.040407543928031475;
  144. break;
  145. case 4: // fs : 16000
  146. a[0] = 1.4499664446880227;
  147. a[1] = -0.98943497080950582;
  148. a[2] = 0.24578252340690215;
  149. b[0] = 0.036710750339322612;
  150. b[1] = 0.11013225101796784;
  151. break;
  152. case 2: // fs : 8000
  153. a[0] = 0.041156734567757189;
  154. a[1] = -0.42599112459189636;
  155. a[2] = 0.041037215479961225;
  156. b[0] = 0.16797464681802227;
  157. b[1] = 0.50392394045406674;
  158. }
  159. for(int i = 0;i < xLen;i++)
  160. {
  161. wt = x[i] + a[0]*w[0]
  162. + a[1]*w[1]
  163. + a[2]*w[2];
  164. y[i] = b[0]*wt
  165. + b[1]*w[0]
  166. + b[1]*w[1]
  167. + b[0]*w[2];
  168. w[2] = w[1];
  169. w[1] = w[0];
  170. w[0] = wt;
  171. }
  172. }
  173. // 差分
  174. void diff(double *x, int xLength, double *ans)
  175. {
  176. for(int i = 0;i < xLength-1;i++)
  177. {
  178. ans[i] = x[i+1] - x[i];
  179. }
  180. return;
  181. }
  182. // サンプリング間隔が等間隔に限定し高速に動作するinterp1.
  183. // 基本的には同じだが,配列の要素数を明示的に指定する必要がある.
  184. void interp1Q(double x, double shift, double *y, int xLength, double *xi, int xiLength, double *ans)
  185. {
  186. double deltaX;
  187. double *xiFraction, *deltaY;
  188. int *xiBase;
  189. int i;
  190. xiFraction = (double *)malloc(xiLength*sizeof(double));
  191. deltaY = (double *)malloc(xLength*sizeof(double));
  192. xiBase = (int *)malloc(xiLength*sizeof(int));
  193. deltaX = shift;
  194. for(i = 0;i < xiLength;i++)
  195. {
  196. xiBase[i] = (int)floor((xi[i] - x) / deltaX);
  197. xiFraction[i] = (double)(xi[i]-x)/deltaX - (double)xiBase[i];
  198. }
  199. diff(y, xLength, deltaY);
  200. deltaY[xLength-1] = 0.0;
  201. for(i = 0;i < xiLength;i++)
  202. {
  203. ans[i] = y[xiBase[i]] + deltaY[xiBase[i]]*xiFraction[i];
  204. }
  205. free(xiFraction);
  206. free(xiBase);
  207. free(deltaY);
  208. }
  209. // xorshift法と中心極限定理との組み合わせ
  210. float randn(void)
  211. {
  212. static unsigned int x = 123456789;
  213. static unsigned int y = 362436069;
  214. static unsigned int z = 521288629;
  215. static unsigned int w = 88675123;
  216. unsigned int t;
  217. t = x ^ (x << 11);
  218. x = y; y = z; z = w;
  219. int i;
  220. unsigned int tmp;
  221. tmp = 0;
  222. for(i = 0;i < 12;i++)
  223. {
  224. t = x ^ (x << 11);
  225. x = y; y = z; z = w;
  226. w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  227. tmp += w>>4;
  228. }
  229. return (float)tmp / 268435456.0f - 6.0f;
  230. }
  231. // fftfilt関数の移植
  232. // yは,fftl分の長さを確保すること.
  233. void fftfilt(double *x, int xlen, double *h, int hlen, int fftl, double *y)
  234. {
  235. int i;
  236. fftw_plan forwardFFT1, forwardFFT2, inverseFFT;
  237. fftw_complex *specx, *spech;
  238. double *input1, *input2;
  239. input1 = (double *) malloc(sizeof(double) * fftl);
  240. input2 = (double *) malloc(sizeof(double) * fftl);
  241. specx = (fftw_complex *)malloc(sizeof(fftw_complex) * fftl);
  242. spech = (fftw_complex *)malloc(sizeof(fftw_complex) * fftl);
  243. forwardFFT1 = fftw_plan_dft_r2c_1d(fftl, input1, specx, FFTW_ESTIMATE);
  244. forwardFFT2 = fftw_plan_dft_r2c_1d(fftl, input2, spech, FFTW_ESTIMATE);
  245. inverseFFT = fftw_plan_dft_c2r_1d(fftl, specx, y, FFTW_ESTIMATE);
  246. for(i = 0;i < xlen;i++) input1[i] = x[i]/(double)fftl;
  247. for(; i < fftl;i++) input1[i] = 0;
  248. for(i = 0;i < hlen;i++) input2[i] = h[i];
  249. for(; i < fftl;i++) input2[i] = 0;
  250. fftw_execute(forwardFFT1);
  251. fftw_execute(forwardFFT2);
  252. double tmpR, tmpI;
  253. for(i = 0;i <= fftl/2;i++)
  254. {
  255. tmpR = specx[i][0]*spech[i][0] - specx[i][1]*spech[i][1];
  256. tmpI = specx[i][0]*spech[i][1] + specx[i][1]*spech[i][0];
  257. specx[i][0] = tmpR;
  258. specx[i][1] = tmpI;
  259. }
  260. fftw_execute(inverseFFT);
  261. free(input1); free(input2);
  262. free(specx); free(spech);
  263. fftw_destroy_plan(forwardFFT1);
  264. fftw_destroy_plan(forwardFFT2);
  265. fftw_destroy_plan(inverseFFT);
  266. }
  267. // 2次元配列 (n*n)の逆行列を計算.メモリは確保しておくこと
  268. void inv(double **r, int n, double **invr)
  269. {
  270. int i,j,k;
  271. double tmp;
  272. for(i = 0;i < n;i++)
  273. {
  274. for(j = 0;j < n;j++)
  275. {
  276. invr[i][j] = 0.0;
  277. }
  278. }
  279. for(i = 0;i < n;i++) invr[i][i] = 1.0;
  280. // 配列の初期化
  281. //
  282. for(i = 0;i < n;i++)
  283. {
  284. tmp = r[i][i]; r[i][i] = 1.0;
  285. for(j = 0;j <= i;j++) invr[i][j] /= tmp;
  286. for(;j < n;j++) r[i][j] /= tmp;
  287. for(j = i+1;j < n;j++)
  288. {
  289. tmp = r[j][i];
  290. for(k = 0;k <= i;k++) invr[j][k] -= invr[i][k]*tmp;
  291. for(k--;k < n;k++) r[j][k] -= r[i][k]*tmp;
  292. }
  293. }
  294. // これで半分完了
  295. for(i = n-1;i >= 0;i--)
  296. {
  297. for(j = 0;j < i;j++)
  298. {
  299. tmp = r[j][i];
  300. for(k = 0;k < n;k++) invr[j][k] -= invr[i][k]*tmp;
  301. }
  302. }
  303. }
  304. double std_mat(double *x, int xLen)
  305. {
  306. int i;
  307. double average, s;
  308. average = 0.0;
  309. for(i = 0;i < xLen;i++)
  310. average += x[i];
  311. average /= (double)xLen;
  312. s = 0.0;
  313. for(i = 0;i < xLen;i++)
  314. s += pow(x[i] - average, 2.0);
  315. s /= (double)(xLen-1);
  316. return sqrt(s);
  317. }