Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <ctype.h>
  6. #include "wav.h"
  7.  
  8. struct header
  9. {
  10. int X, Y, Z;
  11. long frequency;
  12. };
  13.  
  14. struct partition_info
  15. {
  16. int back_part_num, front_part_num, size_x;
  17. int left_part_num, right_part_num, size_y;
  18. int down_part_num, up_part_num, size_z;
  19. };
  20.  
  21. typedef struct Plant
  22. {
  23. unsigned char node_type;
  24. double reflection_coeficient;
  25. double p; /* pressão acústica no nó */
  26. double pinFront; /* componente de pressão enviada para o vizinho a N */
  27. double poutFront; /* componente de pressão recebida do vizinho a N */
  28. double pinBack; /* componente de pressão enviada para o vizinho a S */
  29. double poutBack; /* etc.. */
  30. double pinLeft;
  31. double poutLeft;
  32. double pinRight;
  33. double poutRight;
  34. double pinUp;
  35. double poutUp;
  36. double pinDown;
  37. double poutDown;
  38. } pl;
  39.  
  40. void loadDataRoom();
  41. void createDataRoom();
  42. void insert_receiver_or_source(char node_type);
  43. void insert_cuboid();
  44. void insert_sphere();
  45. void room_division();
  46. void partitioning(int part_counter, int x_block, int y_block, int z_block, int start_x, int start_y, int start_z, int end_x, int end_y, int end_z, int x_blocks, int y_blocks, int z_blocks);
  47. void loadRoomPartition(int partitionId);
  48. void loadRoomPlant();
  49. void loadPartitionPlant(int part_counter);
  50. void plantMemoryAllocationAndDataReading(FILE *infile);
  51. void scattering();
  52. void delay();
  53. void run();
  54. double getCoeficient(unsigned char node_type);
  55.  
  56. //GLOBAL VARS
  57. unsigned char ***data;
  58. pl ***plant;
  59. float node_spacing;
  60. struct header input;
  61. struct partition_info partition;
  62. int number_samples;
  63. int16_t *samples = NULL;
  64. int16_t *result = NULL;
  65.  
  66. //Border buffers
  67. double **backInBuffer;
  68. double **backOutBuffer;
  69. double **frontInBuffer;
  70. double **frontOutBuffer;
  71. double **leftInBuffer;
  72. double **leftOutBuffer;
  73. double **rightInBuffer;
  74. double **rightOutBuffer;
  75. double **downInBuffer;
  76. double **downOutBuffer;
  77. double **upInBuffer;
  78. double **upOutBuffer;
  79.  
  80. //Sources and receivers - falta permitir múltiplos
  81. pl *source;
  82. pl *receiver;
  83.  
  84. int main()
  85. {
  86. number_samples = wavread("file.wav", &samples);
  87. int ch = 0, ch1 = 0, partitionId = 0;
  88. printf("1 - Create Room\n");
  89. printf("2 - Load data Room\n");
  90. printf("3 - Sound Simulation\n");
  91. printf("Option: ");
  92. scanf("%i", &ch);
  93. switch (ch)
  94. {
  95. case 1:
  96. createDataRoom();
  97. break;
  98. case 2:
  99. loadDataRoom();
  100. break;
  101. case 3:
  102. loadRoomPlant();
  103. run();
  104. break;
  105. default:
  106. printf("Invalid option\n");
  107. }
  108. printf("\n");
  109.  
  110. printf("1 - Room Division\n");
  111. printf("Option: ");
  112. scanf("%i", &ch1);
  113. switch (ch1)
  114. {
  115. case 1:
  116. room_division();
  117. break;
  118. case 2:
  119. printf("Merda\n");
  120. break;
  121. default:
  122. printf("Invalid option\n");
  123. }
  124. printf("\n");
  125.  
  126. return 0;
  127. }
  128.  
  129. void createDataRoom()
  130. {
  131. float width, height, depth;
  132. char ch;
  133. FILE *outfile;
  134.  
  135. int op = 0;
  136.  
  137. outfile = fopen("config.dwm", "w");
  138. if (outfile == NULL)
  139. {
  140. fprintf(stderr, "\nError opening config.dwm\n\n");
  141. return;
  142. }
  143. printf("Dimensions of Room\n");
  144. do
  145. {
  146. printf("Width : ");
  147. scanf("%f", &width);
  148. if (width < 0 && !isdigit(width))
  149. {
  150. printf("Invalid input\n");
  151. }
  152. } while (width < 0);
  153.  
  154. do
  155. {
  156. printf("Height : ");
  157. scanf("%f", &height);
  158. if (height < 0 && !isdigit(height))
  159. {
  160. printf("Invalid input\n");
  161. }
  162. } while (height < 0);
  163.  
  164. do
  165. {
  166. printf("Depth : ");
  167. scanf("%f", &depth);
  168. if (depth < 0 && !isdigit(depth))
  169. {
  170. printf("Invalid input\n");
  171. }
  172. } while (depth < 0);
  173.  
  174. do
  175. {
  176. printf("Frequency\n");
  177. printf("1-48000Hz\t2-44100Hz\t3-22050Hz\t4-16000Hz\t5-11025Hz\t6-8000Hz\n");
  178. printf("Frequency: ");
  179. scanf("%i", &op);
  180. switch (op)
  181. {
  182. case 1:
  183. input.frequency = 48000;
  184. break;
  185. case 2:
  186. input.frequency = 44100;
  187. break;
  188. case 3:
  189. input.frequency = 22050;
  190. break;
  191. case 4:
  192. input.frequency = 16000;
  193. break;
  194. case 5:
  195. input.frequency = 11025;
  196. break;
  197. case 6:
  198. input.frequency = 8000;
  199. break;
  200. default:
  201. printf("Invalid option\n");
  202. }
  203. } while (op < 1 || op > 6);
  204.  
  205. node_spacing = ((344 * sqrt(3)) / input.frequency);
  206.  
  207. input.X = width / node_spacing;
  208. input.Y = depth / node_spacing;
  209. input.Z = height / node_spacing;
  210.  
  211. float _w, _h, _d;
  212.  
  213. _w = input.X * node_spacing;
  214. _d = input.Y * node_spacing;
  215. _h = input.Z * node_spacing;
  216.  
  217. printf("Necessary Ajustments: ");
  218. printf("Width: %.2f Height: %.2f Depth: %.2f Frequency: %ld\n\n", _w, _h, _d, input.frequency);
  219. fflush(stdin);
  220. data = (unsigned char ***)malloc(input.X * sizeof(unsigned char **));
  221.  
  222. for (int i = 0; i < input.X; i++)
  223. {
  224. data[i] = (unsigned char **)malloc(input.Y * sizeof(unsigned char *));
  225. for (int j = 0; j < input.Y; j++)
  226. {
  227. data[i][j] = (unsigned char *)malloc(input.Z * sizeof(unsigned char));
  228. }
  229. }
  230.  
  231. unsigned char node_type;
  232. double r;
  233. do
  234. {
  235. printf("Node type:\n");
  236. scanf("%c", &node_type);
  237. r = getCoeficient(node_type);
  238. printf("Absorption coefficient (ρ) = %f\n", r);
  239. if (r == -1)
  240. {
  241. printf("Invalid input\n");
  242. }
  243. } while (r == -1);
  244.  
  245. for (int i = 0; i < input.X; i++)
  246. {
  247. for (int j = 0; j < input.Y; j++)
  248. {
  249. for (int k = 0; k < input.Z; k++)
  250. {
  251. if (i == 0 || j == 0 || k == 0 || i == (input.X - 1) || j == (input.Y - 1) || k == (input.Z - 1))
  252. {
  253. data[i][j][k] = node_type;
  254. }
  255. else
  256. {
  257. data[i][j][k] = ' ';
  258. }
  259. }
  260. }
  261. }
  262.  
  263. int has_receiver, has_source;
  264.  
  265. do
  266. {
  267. printf("Node type in Room\n");
  268. printf("Receiver - R || Source - S || C - Cuboid || E - Sphere\n");
  269. printf("0 - Exit: \n");
  270. scanf(" %c", &ch);
  271. switch (ch)
  272. {
  273. case 'R':
  274. has_receiver = 1;
  275. insert_receiver_or_source('R');
  276. break;
  277. case 'S':
  278. has_source = 1;
  279. insert_receiver_or_source('S');
  280. break;
  281. case 'C':
  282. insert_cuboid();
  283. break;
  284. case 'E':
  285. insert_sphere();
  286. break;
  287. case '0':
  288. printf("Room Created");
  289. break;
  290. default:
  291. printf("Invalid option\n");
  292. }
  293.  
  294. } while (ch != '0' || has_source != 1 || has_receiver != 1);
  295.  
  296. fprintf(outfile, "%d %d %d %ld\n", input.X, input.Y, input.Z, input.frequency);
  297.  
  298. for (int i = 0; i < input.X; i++)
  299. {
  300. for (int j = 0; j < input.Y; j++)
  301. {
  302. for (int k = 0; k < input.Z; k++)
  303. {
  304. fprintf(outfile, "%c", data[i][j][k]);
  305. }
  306. }
  307. }
  308. }
  309.  
  310. void loadDataRoom()
  311. {
  312. FILE *infile;
  313. infile = fopen("config.dwm", "r");
  314. if (infile == NULL)
  315. {
  316. fprintf(stderr, "\nError opening config.dwm\n\n");
  317. exit(1);
  318. }
  319. fscanf(infile, "%d %d %d %ld\n", &input.X, &input.Y, &input.Z, &input.frequency);
  320.  
  321. data = (unsigned char ***)malloc(input.X * sizeof(unsigned char **));
  322.  
  323. for (int i = 0; i < input.X; i++)
  324. {
  325. data[i] = (unsigned char **)malloc(input.Y * sizeof(unsigned char *));
  326. for (int j = 0; j < input.Y; j++)
  327. {
  328. data[i][j] = (unsigned char *)malloc(input.Z * sizeof(unsigned char));
  329. for (int k = 0; k < input.Z; k++)
  330. {
  331. fscanf(infile, "%c", &data[i][j][k]);
  332. }
  333. }
  334. }
  335. }
  336.  
  337. void insert_receiver_or_source(char node_type)
  338. {
  339. float _w, _h, _d;
  340. int x = 0, y = 0, z = 0;
  341. float i, j, k;
  342.  
  343. do
  344. {
  345. printf("I -> Position : ");
  346. scanf("%f", &i);
  347. if (i < 0 && !isdigit(i))
  348. {
  349. printf("Invalid input\n");
  350. }
  351. } while (i < 0);
  352.  
  353. x = i / node_spacing;
  354.  
  355. do
  356. {
  357. printf("J -> Position : ");
  358. scanf("%f", &j);
  359. if (j < 0 && !isdigit(j))
  360. {
  361. printf("Invalid input\n");
  362. }
  363. } while (j < 0);
  364.  
  365. y = j / node_spacing;
  366.  
  367. do
  368. {
  369. printf("K -> Position : ");
  370. scanf("%f", &k);
  371. if (k < 0 && !isdigit(k))
  372. {
  373. printf("Invalid input\n");
  374. }
  375. } while (k < 0);
  376.  
  377. z = k / node_spacing;
  378.  
  379. _w = x * node_spacing;
  380. _d = y * node_spacing;
  381. _h = z * node_spacing;
  382. printf("Necessarily Ajusts: Center: ");
  383. printf("Position X: %.2f Position Y: %.2f Position Z: %.2f\n\n", _w, _h, _d);
  384. data[x][y][z] = node_type;
  385. }
  386.  
  387. void insert_cuboid()
  388. {
  389. int x_min = 0, x_max = 0, y_min = 0, y_max = 0, z_min = 0, z_max = 0;
  390. float i_min = 0, i_max = 0, j_min = 0, j_max = 0, k_min = 0, k_max = 0;
  391. unsigned char node_type;
  392. double r;
  393. do
  394. {
  395. printf("Node type in Room\n");
  396. scanf("%c", &node_type);
  397. r = getCoeficient(node_type);
  398. printf("Absorption coefficient (ρ) = %f\n", r);
  399. if (r == -1)
  400. {
  401. printf("Invalid input\n");
  402. }
  403. } while (r == -1);
  404.  
  405. do
  406. {
  407. printf("X - MIN: ");
  408. scanf("%f", &i_min);
  409. if (i_min < 0 && !isdigit(i_min))
  410. {
  411. printf("Invalid input\n");
  412. }
  413. } while (i_min < 0);
  414.  
  415. x_min = i_min / node_spacing;
  416.  
  417. do
  418. {
  419. printf("X - MAX : ");
  420. scanf("%f", &i_max);
  421. if (i_max < 0 && !isdigit(i_max))
  422. {
  423. printf("Invalid input\n");
  424. }
  425. } while (i_max < 0);
  426.  
  427. x_max = i_max / node_spacing;
  428.  
  429. do
  430. {
  431. printf("Y - MIN: ");
  432. scanf("%f", &j_min);
  433. if (j_min < 0 && !isdigit(j_min))
  434. {
  435. printf("Invalid input\n");
  436. }
  437. } while (j_min < 0);
  438.  
  439. y_min = j_min / node_spacing;
  440.  
  441. do
  442. {
  443. printf("Y - MAX: ");
  444. scanf("%f", &j_max);
  445. if (j_max < 0 && !isdigit(j_max))
  446. {
  447. printf("Invalid input\n");
  448. }
  449. } while (j_max < 0);
  450.  
  451. y_max = j_max / node_spacing;
  452.  
  453. do
  454. {
  455. printf("Z - MIN : ");
  456. scanf("%f", &k_min);
  457. if (k_min < 0 && !isdigit(k_min))
  458. {
  459. printf("Invalid input\n");
  460. }
  461. } while (k_min < 0);
  462.  
  463. z_min = k_min / node_spacing;
  464.  
  465. do
  466. {
  467. printf("Z - MAX : ");
  468. scanf("%f", &k_max);
  469. if (k_max < 0 && !isdigit(k_max))
  470. {
  471. printf("Invalid input\n");
  472. }
  473. } while (k_max < 0);
  474.  
  475. z_max = k_max / node_spacing;
  476.  
  477. for (int x = x_min; x <= x_max; x++)
  478. {
  479. for (int y = y_min; y <= y_max; y++)
  480. {
  481. for (int z = z_min; z <= z_max; z++)
  482. {
  483. data[x][y][z] = node_type;
  484. }
  485. }
  486. }
  487. }
  488.  
  489. void insert_sphere()
  490. {/*
  491. int x = 0, y = 0, z = 0;
  492. float i, j, k, radius_, radius;
  493. unsigned char node_type;
  494. double r;
  495. do
  496. {
  497. printf("Node type in Room\n");
  498. scanf("%c", &node_type);
  499. r = getCoeficient(node_type);
  500. printf("Absorption coefficient (ρ) = %f\n", r);
  501. if (r == -1)
  502. {
  503. printf("Invalid input\n");
  504. }
  505. } while (r == -1);
  506.  
  507. do
  508. {
  509. printf("I : ");
  510. scanf("%f", &i);
  511. if (i < 0 && !isdigit(i))
  512. {
  513. printf("Invalid input\n");
  514. }
  515. } while (i < 0);
  516.  
  517. x = i / node_spacing;
  518.  
  519. do
  520. {
  521. printf("J : ");
  522. scanf("%f", &j);
  523. if (j < 0 && !isdigit(j))
  524. {
  525. printf("Invalid input\n");
  526. }
  527. } while (j < 0);
  528.  
  529. y = j / node_spacing;
  530.  
  531. do
  532. {
  533. printf("K : ");
  534. scanf("%f", &k);
  535. if (k < 0 && !isdigit(k))
  536. {
  537. printf("Invalid input\n");
  538. }
  539. } while (k < 0);
  540.  
  541. z = k / node_spacing;
  542.  
  543. do
  544. {
  545. printf("Radius : ");
  546. scanf("%f", &radius_);
  547. if (radius_ < 0 && !isdigit(radius_))
  548. {
  549. printf("Invalid input\n");
  550. }
  551. } while (radius_ < 0);
  552.  
  553. radius = radius_ / node_spacing;
  554.  
  555. for (int x_ = 0; x_ <= radius; x_++)
  556. {
  557. for (int y_ = 0; y_ <= radius; y_++)
  558. {
  559. for (int z_ = 0; z <= radius; z_++)
  560. {
  561. if (sqrt(pow(x_, 2) + pow(y_, 2) + pow(z_, 2)) <= radius)
  562. {
  563. data[x + x_][y + y_][z + z_] = node_type;
  564. data[x - x_][y - y_][z - z_] = node_type;
  565.  
  566. data[x - x_][y - y_][z + z_] = node_type;
  567. data[x + x_][y - y_][z - z_] = node_type;
  568. data[x - x_][y + y_][z - z_] = node_type;
  569.  
  570. data[x - x_][y + y_][z + z_] = node_type;
  571. data[x + x_][y - y_][z + z_] = node_type;
  572. data[x + x_][y + y_][z - z_] = node_type;
  573. }
  574. }
  575. }
  576. }*/
  577. }
  578.  
  579. void room_division()
  580. {
  581.  
  582. int x_blocks = 0, y_blocks = 0, z_blocks = 0;
  583. do
  584. {
  585. printf("Blocks on X axis : ");
  586. scanf("%d", &x_blocks);
  587. if (x_blocks < 0 && !isdigit(x_blocks))
  588. {
  589. printf("Invalid input\n");
  590. }
  591. } while (x_blocks < 0);
  592.  
  593. do
  594. {
  595. printf("Blocks on Y axis : ");
  596. scanf("%d", &y_blocks);
  597. if (y_blocks < 0 && !isdigit(y_blocks))
  598. {
  599. printf("Invalid input\n");
  600. }
  601. } while (y_blocks < 0);
  602.  
  603. do
  604. {
  605. printf("Blocks on Z axis : ");
  606. scanf("%d", &z_blocks);
  607. if (z_blocks < 0 && !isdigit(z_blocks))
  608. {
  609. printf("Invalid input\n");
  610. }
  611. } while (z_blocks < 0);
  612.  
  613. int x_block_size, y_block_size, z_block_size;
  614. int start_x, start_y, start_z;
  615. int end_x, end_y, end_z;
  616.  
  617. int part_counter = 0;
  618.  
  619. for (int x_block = 0; x_block < x_blocks; x_block++)
  620. {
  621. for (int y_block = 0; y_block < y_blocks; y_block++)
  622. {
  623. for (int z_block = 0; z_block < x_blocks; z_block++)
  624. {
  625. start_x = x_block * input.X / x_blocks;
  626. start_y = y_block * input.Y / y_blocks;
  627. start_z = z_block * input.Z / z_blocks;
  628. end_x = ((x_block+1) * input.X / x_blocks) - 1;
  629. end_y = ((y_block+1) * input.Y / y_blocks) - 1;
  630. end_z = ((z_block+1) * input.Z / z_blocks) - 1;
  631. partitioning(part_counter, x_block, y_block, z_block, start_x, start_y, start_z, end_x, end_y, end_z, x_blocks, y_blocks, z_blocks);
  632. part_counter++;
  633. }
  634. }
  635. }
  636. }
  637.  
  638. void partitioning(int part_counter, int x_block, int y_block, int z_block, int start_x, int start_y, int start_z, int end_x, int end_y, int end_z, int x_blocks, int y_blocks, int z_blocks)
  639. {
  640. char c_name[150];
  641. char c_index[50];
  642. char c_ext[50];
  643.  
  644. FILE *outfile;
  645.  
  646. strcpy(c_name, "config_");
  647. sprintf(c_index, "%d", part_counter);
  648. strcpy(c_ext, ".dwm");
  649. strcat(c_name, c_index);
  650. strcat(c_name, c_ext);
  651.  
  652. outfile = fopen(c_name, "w");
  653. if (outfile == NULL)
  654. {
  655. fprintf(stderr, "\nError opening %s\n\n", c_name);
  656. return;
  657. }
  658.  
  659.  
  660. int size_x = (end_x - start_x) + 1;
  661. int size_y = (end_y - start_y) + 1;
  662. int size_z = (end_z - start_z) + 1;
  663.  
  664. int back_part_num, front_part_num;
  665. int left_part_num, right_part_num;
  666. int down_part_num, up_part_num;
  667.  
  668. if (x_block == 0)
  669. {
  670. back_part_num = -1;
  671. front_part_num = part_counter + (y_blocks * z_blocks);
  672. }
  673. else if (x_block == x_blocks - 1)
  674. {
  675. back_part_num = part_counter - (y_blocks * z_blocks);
  676. front_part_num = -1;
  677. }
  678. else
  679. {
  680. back_part_num = part_counter - (y_blocks * z_blocks);
  681. front_part_num = part_counter + (y_blocks * z_blocks);
  682. }
  683.  
  684. if (y_block == 0)
  685. {
  686. left_part_num = -1;
  687. right_part_num = part_counter + z_blocks;
  688. }
  689. else if (y_block == y_blocks - 1)
  690. {
  691. left_part_num = part_counter - z_blocks;
  692. right_part_num = -1;
  693. }
  694. else
  695. {
  696. left_part_num = part_counter - z_blocks;
  697. right_part_num = part_counter + z_blocks;
  698. }
  699.  
  700. if (z_block == 0)
  701. {
  702. down_part_num = -1;
  703. up_part_num = part_counter + 1;
  704. }
  705. else if (z_block == z_blocks - 1)
  706. {
  707. up_part_num = -1;
  708. down_part_num = part_counter - 1;
  709. }
  710. else
  711. {
  712. down_part_num = part_counter -1;
  713. up_part_num = part_counter + 1;
  714. }
  715.  
  716. fprintf(outfile, "%d %d %d %d %d %d %d %d %d %ld\n", size_x, size_y, size_z, back_part_num, front_part_num, left_part_num, right_part_num, down_part_num, up_part_num, input.frequency);
  717.  
  718. int contador = 0;
  719.  
  720. for (int i = start_x; i <= end_x; i++)
  721. {
  722. for (int j = start_y; j <= end_y; j++)
  723. {
  724. for (int k = start_z; k <= end_z; k++)
  725. {
  726. fprintf(outfile, "%c", data[i][j][k]);
  727. contador++;
  728. }
  729. }
  730. }
  731. }
  732.  
  733. void loadRoomPartition(int partitionId)
  734. {
  735. char c_name[150];
  736. char c_index[50];
  737. char c_ext[50];
  738.  
  739. strcpy(c_name, "config_");
  740. sprintf(c_index, "%d", partitionId);
  741. strcpy(c_ext, ".dwm");
  742. strcat(c_name, c_index);
  743. strcat(c_name, c_ext);
  744.  
  745. FILE *infile;
  746. infile = fopen(c_name, "r");
  747. if (infile == NULL)
  748. {
  749. fprintf(stderr, "\nError opening %s\n\n", c_name);
  750. exit(1);
  751. }
  752.  
  753. fscanf(infile, "%d %d %d %d %d %d %d %d %d %ld\n", &partition.size_x, &partition.size_y, &partition.size_z, &partition.back_part_num, &partition.front_part_num, &partition.left_part_num, &partition.right_part_num, &partition.down_part_num, &partition.up_part_num, &input.frequency);
  754.  
  755. data = (unsigned char ***)malloc(partition.size_x * sizeof(unsigned char **));
  756.  
  757. for (int i = 0; i <= partition.size_x; i++)
  758. {
  759. data[i] = (unsigned char **)malloc(partition.size_y * sizeof(unsigned char *));
  760. for (int j = 0; j < partition.size_y; j++)
  761. {
  762. data[i][j] = (unsigned char *)malloc(partition.size_z * sizeof(unsigned char));
  763. for (int k = 0; k < partition.size_z; k++)
  764. {
  765. fscanf(infile, "%c", &data[i][j][k]);
  766. }
  767. }
  768. }
  769. }
  770.  
  771. void loadRoomPlant()
  772. {
  773. /*source = malloc(sizeof(pl));
  774. receiver = malloc(sizeof(pl));*/
  775.  
  776. FILE *infile;
  777. infile = fopen("config.dwm", "r");
  778. unsigned char *node_type;
  779. double r;
  780. if (infile == NULL)
  781. {
  782. fprintf(stderr, "\nError opening config.dwm\n\n");
  783. exit(1);
  784. }
  785. fscanf(infile, "%d %d %d %ld\n", &input.X, &input.Y, &input.Z, &input.frequency);
  786.  
  787. plantMemoryAllocationAndDataReading(infile);
  788. }
  789.  
  790. void loadPartitionPlant(int partitionId)
  791. {
  792. /*source = malloc(sizeof(pl));
  793. receiver = malloc(sizeof(pl));*/
  794.  
  795. char c_name[150];
  796. char c_index[50];
  797. char c_ext[50];
  798.  
  799. strcpy(c_name, "config_");
  800. sprintf(c_index, "%d", partitionId);
  801. strcpy(c_ext, ".dwm");
  802. strcat(c_name, c_index);
  803. strcat(c_name, c_ext);
  804.  
  805. FILE *infile;
  806. infile = fopen(c_name, "r");
  807. if (infile == NULL)
  808. {
  809. fprintf(stderr, "\nError opening %s\n\n", c_name);
  810. exit(1);
  811. }
  812.  
  813. fscanf(infile, "%d %d %d %d %d %d %d %d %d %ld\n", &partition.size_x, &partition.size_y, &partition.size_z, &partition.back_part_num, &partition.front_part_num, &partition.left_part_num, &partition.right_part_num, &partition.down_part_num, &partition.up_part_num, &input.frequency);
  814.  
  815. input.X = partition.size_x;
  816. input.Y = partition.size_y;
  817. input.Z = partition.size_z;
  818.  
  819. plantMemoryAllocationAndDataReading(infile);
  820. }
  821.  
  822. void plantMemoryAllocationAndDataReading(FILE *infile)
  823. {
  824.  
  825. //make buffers single dimensional
  826. /* X Axix Buffers memory alocations */
  827.  
  828. backInBuffer = (double **)malloc(input.Y * sizeof(double *));
  829. for (int i = 0; i < input.Y; i++)
  830. backInBuffer[i] = (double *)malloc(input.Z * sizeof(double));
  831.  
  832. backOutBuffer = (double **)malloc(input.Y * sizeof(double *));
  833. for (int i = 0; i < input.Y; i++)
  834. backOutBuffer[i] = (double *)malloc(input.Z * sizeof(double));
  835.  
  836. frontInBuffer = (double **)malloc(input.Y * sizeof(double *));
  837. for (int i = 0; i < input.Y; i++)
  838. frontInBuffer[i] = (double *)malloc(input.Z * sizeof(double));
  839.  
  840. frontOutBuffer = (double **)malloc(input.Y * sizeof(double *));
  841. for (int i = 0; i < input.Y; i++)
  842. frontOutBuffer[i] = (double *)malloc(input.Z * sizeof(double));
  843.  
  844. /* Y Axix Buffers memory alocations */
  845.  
  846. leftInBuffer = (double **)malloc(input.X * sizeof(double *));
  847. for (int i = 0; i < input.X; i++)
  848. leftInBuffer[i] = (double *)malloc(input.Z * sizeof(double));
  849.  
  850. leftOutBuffer = (double **)malloc(input.X * sizeof(double *));
  851. for (int i = 0; i < input.X; i++)
  852. leftOutBuffer[i] = (double *)malloc(input.Z * sizeof(double));
  853.  
  854. rightInBuffer = (double **)malloc(input.X * sizeof(double *));
  855. for (int i = 0; i < input.X; i++)
  856. rightInBuffer[i] = (double *)malloc(input.Z * sizeof(double));
  857.  
  858. rightOutBuffer = (double **)malloc(input.X * sizeof(double *));
  859. for (int i = 0; i < input.X; i++)
  860. rightOutBuffer[i] = (double *)malloc(input.Z * sizeof(double));
  861.  
  862. /* Z Axix Buffers memory alocations */
  863.  
  864. downInBuffer = (double **)malloc(input.X * sizeof(double *));
  865. for (int i = 0; i < input.Y; i++)
  866. downInBuffer[i] = (double *)malloc(input.Y * sizeof(double));
  867.  
  868. downOutBuffer = (double **)malloc(input.X * sizeof(double *));
  869. for (int i = 0; i < input.Y; i++)
  870. downOutBuffer[i] = (double *)malloc(input.Y * sizeof(double));
  871.  
  872. upInBuffer = (double **)malloc(input.X * sizeof(double *));
  873. for (int i = 0; i < input.Y; i++)
  874. upInBuffer[i] = (double *)malloc(input.Y * sizeof(double));
  875.  
  876. upOutBuffer = (double **)malloc(input.X * sizeof(double *));
  877. for (int i = 0; i < input.Y; i++)
  878. upOutBuffer[i] = (double *)malloc(input.Y * sizeof(double));
  879.  
  880. /* Room plant memory alocation*/
  881.  
  882. plant = (pl ***)malloc(input.X * sizeof(pl **));
  883.  
  884. for (int i = 0; i < input.X; i++)
  885. {
  886. plant[i] = (pl **)malloc(input.Y * sizeof(pl *));
  887. for (int j = 0; j < input.Y; j++)
  888. {
  889. plant[i][j] = (pl *)malloc(input.Z * sizeof(pl));
  890. for (int k = 0; k < input.Z; k++)
  891. {
  892. fscanf(infile, "%c", &plant[i][j][k].node_type);
  893. plant[i][j][k].reflection_coeficient = getCoeficient(plant[i][j][k].node_type);
  894. if (plant[i][j][k].node_type == 'S')
  895. {
  896. source = &plant[i][j][k];
  897. printf("source coordinate %d %d %d\n", i, j, k);
  898. }
  899. else if (plant[i][j][k].node_type == 'R')
  900. {
  901. receiver = &plant[i][j][k];
  902. printf("receiver coordinate %d %d %d\n", i, j, k);
  903. }
  904. }
  905. }
  906. }
  907. }
  908.  
  909. void scattering()
  910. {
  911. int i, j, k;
  912.  
  913. for (i = 0; i < input.X; i++)
  914. {
  915. for (j = 0; j < input.Y; j++)
  916. {
  917. for (k = 0; k < input.Z; k++)
  918. {
  919. if (plant[i][j][k].reflection_coeficient == -1)
  920. {
  921. plant[i][j][k].p = (plant[i][j][k].pinFront + plant[i][j][k].pinBack + plant[i][j][k].pinRight + plant[i][j][k].pinLeft + plant[i][j][k].pinDown + plant[i][j][k].pinUp) / 3;
  922.  
  923. plant[i][j][k].poutFront = plant[i][j][k].p - plant[i][j][k].pinFront;
  924. plant[i][j][k].poutBack = plant[i][j][k].p - plant[i][j][k].pinBack;
  925. plant[i][j][k].poutRight = plant[i][j][k].p - plant[i][j][k].pinRight;
  926. plant[i][j][k].poutLeft = plant[i][j][k].p - plant[i][j][k].pinLeft;
  927. plant[i][j][k].poutDown = plant[i][j][k].p - plant[i][j][k].pinDown;
  928. plant[i][j][k].poutUp = plant[i][j][k].p - plant[i][j][k].pinUp;
  929. }
  930. else
  931. {
  932. plant[i][j][k].poutFront = plant[i][j][k].reflection_coeficient * plant[i][j][k].poutFront;
  933. plant[i][j][k].poutBack = plant[i][j][k].reflection_coeficient * plant[i][j][k].poutBack;
  934. plant[i][j][k].poutRight = plant[i][j][k].reflection_coeficient * plant[i][j][k].poutRight;
  935. plant[i][j][k].poutLeft = plant[i][j][k].reflection_coeficient * plant[i][j][k].poutLeft;
  936. plant[i][j][k].poutDown = plant[i][j][k].reflection_coeficient * plant[i][j][k].poutDown;
  937. plant[i][j][k].poutUp = plant[i][j][k].reflection_coeficient * plant[i][j][k].poutUp;
  938. }
  939. }
  940. }
  941. }
  942. }
  943.  
  944. void delay()
  945. {
  946.  
  947. int i, j, k;
  948. //-----------------Buffers e pontos fronteira----------------------
  949.  
  950. /* X Axix Buffers */
  951. for (j = 0; j < input.Y; j++)
  952. {
  953. for (k = 0; k < input.Z; k++)
  954. {
  955. plant[0][j][k].pinBack = backInBuffer[j][k];
  956. plant[input.X - 1][j][k].pinFront = frontInBuffer[j][k];
  957. backOutBuffer[j][k] = plant[0][j][k].poutBack;
  958. frontOutBuffer[j][k] = plant[input.X - 1][j][k].poutFront;
  959. }
  960. }
  961.  
  962. /* Y Axix Buffers */
  963. for (i = 0; i < input.X; i++)
  964. {
  965. for (k = 0; k < input.Z; k++)
  966. {
  967. plant[i][0][k].pinLeft = leftInBuffer[i][k];
  968. plant[i][input.Y - 1][k].pinRight = rightInBuffer[i][k];
  969. leftOutBuffer[i][k] = plant[i][0][k].poutLeft;
  970. rightOutBuffer[i][k] = plant[i][input.Y - 1][k].poutRight;
  971. }
  972. }
  973.  
  974. /* Z Axix Buffers */
  975. for (i = 0; i < input.X; i++)
  976. {
  977. for (j = 0; j < input.Y; j++)
  978. {
  979. plant[i][j][0].pinDown = downInBuffer[i][j];
  980. plant[i][j][input.Z - 1].pinUp = upInBuffer[i][j];
  981. downOutBuffer[i][j] = plant[i][j][0].poutDown;
  982. upOutBuffer[i][j] = plant[i][j][input.Z - 1].poutUp;
  983. }
  984. }
  985.  
  986. //---------------Interior Nodes---------------------------
  987. for (i = 1; i < input.X - 1; i++)
  988. {
  989. for (j = 1; j < input.Y - 1; j++)
  990. {
  991. for (k = 1; k < input.Z - 1; k++)
  992. {
  993. plant[i][j][k].pinBack = plant[i - 1][j][k].poutFront;
  994. plant[i][j][k].pinLeft = plant[i][j - 1][k].poutRight;
  995. plant[i][j][k].pinDown = plant[i][j][k - 1].poutUp;
  996. plant[i][j][k].pinFront = plant[i + 1][j][k].poutBack;
  997. plant[i][j][k].pinRight = plant[i][j + 1][k].poutLeft;
  998. plant[i][j][k].pinUp = plant[i][j][k + 1].poutDown;
  999. }
  1000. }
  1001. }
  1002. }
  1003.  
  1004. void run()
  1005. {
  1006. int iterations = number_samples + 2 * (input.frequency);
  1007. result = (int16_t *)malloc((size_t)(iterations * sizeof(int16_t)));
  1008. for (int iter = 0; iter < iterations; iter++)
  1009. {
  1010. if (iter < number_samples){
  1011. source->pinFront = (double)samples[iter]/2;
  1012. source->pinBack = (double)samples[iter]/2;
  1013. source->pinUp = (double)samples[iter]/2;
  1014. source->pinDown = (double)samples[iter]/2;
  1015. source->pinRight = (double)samples[iter]/2;
  1016. source->pinLeft = (double)samples[iter]/2;
  1017. }
  1018.  
  1019. scattering();
  1020.  
  1021. result[iter] = (int16_t)receiver->p;
  1022.  
  1023. delay();
  1024.  
  1025. }
  1026. wavwrite("result.wav", result);
  1027. }
  1028.  
  1029. double getCoeficient(unsigned char c)
  1030. {
  1031. double reflex_coeficient;
  1032. switch (c)
  1033. {
  1034. case ('A'):
  1035. reflex_coeficient = 0;
  1036. break;
  1037. case ('B'):
  1038. reflex_coeficient = 0.1;
  1039. break;
  1040. case ('C'):
  1041. reflex_coeficient = 0.2;
  1042. break;
  1043. case ('D'):
  1044. reflex_coeficient = 0.3;
  1045. break;
  1046. case ('E'):
  1047. reflex_coeficient = 0.4;
  1048. break;
  1049. case ('F'):
  1050. reflex_coeficient = 0.5;
  1051. break;
  1052. case ('G'):
  1053. reflex_coeficient = 0.6;
  1054. break;
  1055. case ('H'):
  1056. reflex_coeficient = 0.7;
  1057. break;
  1058. case ('I'):
  1059. reflex_coeficient = 0.8;
  1060. break;
  1061. case ('J'):
  1062. reflex_coeficient = 0.9;
  1063. break;
  1064. case ('Z'):
  1065. reflex_coeficient = 1;
  1066. break;
  1067. case ('1'):
  1068. reflex_coeficient = 0.91;
  1069. break;
  1070. case ('2'):
  1071. reflex_coeficient = 0.92;
  1072. break;
  1073. case ('3'):
  1074. reflex_coeficient = 0.93;
  1075. break;
  1076. case ('4'):
  1077. reflex_coeficient = 0.94;
  1078. break;
  1079. case ('5'):
  1080. reflex_coeficient = 0.95;
  1081. break;
  1082. case ('6'):
  1083. reflex_coeficient = 0.96;
  1084. break;
  1085. case ('7'):
  1086. reflex_coeficient = 0.97;
  1087. break;
  1088. case ('8'):
  1089. reflex_coeficient = 0.98;
  1090. break;
  1091. case ('9'):
  1092. reflex_coeficient = 0.99;
  1093. break;
  1094. default:
  1095. reflex_coeficient = -1;
  1096. break;
  1097. }
  1098. return reflex_coeficient;
  1099. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement