Guest User

Untitled

a guest
Nov 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. #include iostream>
  2. #include fstream>
  3. #include stdint.h>
  4. #include cstdlib>
  5. #include ctime>
  6. #include math.h>
  7.  
  8. using namespace std;
  9.  
  10. struct RIFFHeader
  11. {
  12. char chunkId[4]; // 4 byte character array
  13. uint32_t chunkSize; // 4 bytes
  14. char format[4]; // 4 byte array
  15. };
  16.  
  17. struct FormatSubChunk
  18. {
  19. char chunkId[4];
  20. uint32_t chunkSize;
  21. uint16_t audioFormat;
  22. uint16_t channels;
  23. uint32_t frequency; //
  24. uint32_t byteRate; //
  25. uint16_t blockAlign;
  26. uint16_t bitsPerSample;
  27. };
  28.  
  29. struct DataSubChunkHeader
  30. {
  31. char chunkId[4];
  32. uint32_t chunkSize; //
  33. };
  34.  
  35. struct Sample
  36. {
  37. uint16_t leftchannel;
  38. uint16_t rightchannel;
  39. };
  40.  
  41. double coeff_11025[17]={0.0011468,0.0051113,0.014364,0.030915,
  42. 0.054955,0.083795,0.11194,0.13262,0.14024,0.13262,0.11194,0.083795,
  43. 0.054955,0.030915,0.014364,0.0051113,0.0011468};
  44.  
  45. double coeff_22050[25] = {0.00093276,0.0033739,0.0062125,0.011378,0.018027,
  46. 0.026654,0.036717,0.047821,0.059142,0.069811,0.078867,0.085459,0.08893,
  47. 0.085459,0.078867,0.069811,0.059142,0.047821,0.036717,0.026654,0.018027,
  48. 0.011378,0.0062125,0.0033739,0.00093276};
  49.  
  50. void shiftArray22050(int16_t array[])
  51. {
  52. for(int i = 0; i < 24; i++)
  53. {
  54. array[i] = array[i+1];
  55. }
  56. }
  57.  
  58. void shiftArray11025(int16_t array[])
  59. {
  60. for(int i = 0; i < 16; i++)
  61. {
  62. array[i] = array[i+1];
  63. }
  64. }
  65.  
  66.  
  67. int main()
  68. {
  69.  
  70. clock_t start;
  71. double duration;
  72. start = clock();
  73.  
  74. string infile = "Frederick_N_mod.wav";
  75. string outfile = "Frederick_N_lp.wav";
  76. ifstream in(infile.c_str(), ios::in | ios::binary);
  77. ofstream output(outfile.c_str());
  78.  
  79. RIFFHeader RIFF;
  80. in.read((char*)&RIFF,sizeof(RIFF));
  81.  
  82.  
  83. FormatSubChunk Format;
  84. in.read((char*)&Format,sizeof(Format));
  85.  
  86.  
  87. DataSubChunkHeader Data;
  88. in.read((char*)&Data,sizeof(Data));
  89.  
  90.  
  91. cout << "Chunk ID: " << RIFF.chunkId[0] << RIFF.chunkId[1] << RIFF.chunkId[2] << RIFF.chunkId[3] << endl;
  92. cout << "Chunk Size: " << RIFF.chunkSize << endl;
  93. cout << "Format: " << RIFF.format[0] << RIFF.format[1] << RIFF.format[2] << RIFF.format[3] <<endl;
  94. cout << "Sub-chunk1 ID: " << Format.chunkId[0] << Format.chunkId[1] << Format.chunkId[2] << Format.chunkId[3] <<endl;
  95. cout << "Sub-chunk1 Size: " << Format.chunkSize << endl;
  96. cout << "Audio Format: " << Format.audioFormat << endl;
  97. cout << "Number of Channels: " << Format.channels << endl;
  98. cout << "Sample Rate: " << Format.frequency << endl;
  99. cout << "Byte Rate: " << Format.byteRate << endl;
  100. cout << "Block Align: " << Format.blockAlign << endl;
  101. cout << "Bits Per Sample: " << Format.bitsPerSample << endl;
  102. cout << "Sub-chunk2 ID: " << Data.chunkId[0] << Data.chunkId[1] << Data.chunkId[2] << Data.chunkId[3] << endl;
  103. cout << "Sub-chunk2 Size: " << Data.chunkSize << endl << endl;
  104.  
  105.  
  106. //uint16_t clear = 0;
  107.  
  108. output.write((char*)&RIFF, sizeof(RIFF));
  109. output.write((char*)&Format, sizeof(Format));
  110. output.write((char*)&Data, sizeof(Data));
  111.  
  112. int16_t leftSample;
  113. int16_t rightSample;
  114.  
  115. double leftDouble = 0;
  116. double rightDouble = 0;
  117.  
  118. int16_t leftFilter;
  119. int16_t rightFilter;
  120.  
  121. int32_t count = 0;
  122.  
  123. if(Format.frequency == 22050)
  124. {
  125. int16_t pastSamplesLeft[25] = {0};
  126. int16_t pastSamplesRight[25] = {0};
  127.  
  128.  
  129. while(!in.eof())
  130. {
  131. shiftArray22050(pastSamplesLeft);
  132. shiftArray22050(pastSamplesRight);
  133.  
  134. in.read((char*)&leftSample, 2);
  135. pastSamplesLeft[24] = leftSample;
  136. if(in.eof())
  137. break;
  138.  
  139. in.read((char*)&rightSample, 2);
  140. pastSamplesRight[24] = rightSample;
  141. if(in.eof())
  142. break;
  143.  
  144. for(int i = 0; i < 25; i++)
  145. {
  146. leftDouble +=((double)pastSamplesLeft[i] * coeff_22050[i]);
  147. rightDouble += ((double)pastSamplesRight[i] * coeff_22050[i]);
  148. }
  149.  
  150. leftFilter = (int16_t)leftDouble;
  151. rightFilter = (int16_t)rightDouble;
  152.  
  153.  
  154.  
  155.  
  156. output.write((char*)&leftFilter, sizeof(leftFilter));
  157. output.write((char*)&rightFilter, sizeof(rightFilter));
  158. leftDouble = 0;
  159. rightDouble = 0;
  160. leftFilter = 0;
  161. rightFilter = 0;
  162. count++;
  163. }
  164. }
  165. else if(Format.frequency == 11025)
  166. {
  167. int16_t pastSamplesLeft[17] = {0};
  168. int16_t pastSamplesRight[17] = {0};
  169.  
  170.  
  171. while(!in.eof())
  172. {
  173. shiftArray11025(pastSamplesLeft);
  174. shiftArray11025(pastSamplesRight);
  175.  
  176. in.read((char*)&leftSample, 2);
  177. pastSamplesLeft[16] = leftSample;
  178. if(in.eof())
  179. break;
  180.  
  181. in.read((char*)&rightSample, 2);
  182. pastSamplesRight[16] = rightSample;
  183. if(in.eof())
  184. break;
  185.  
  186. for(int i = 0; i < 17; i++)
  187. {
  188. leftDouble = leftDouble + ((double)pastSamplesLeft[i] * coeff_11025[i]);
  189. rightDouble = rightDouble + ((double)pastSamplesRight[i] * coeff_11025[i]);
  190. }
  191.  
  192. leftFilter = (int16_t)leftDouble;
  193. rightFilter = (int16_t)rightDouble;
  194.  
  195. leftDouble = 0;
  196. rightDouble = 0;
  197.  
  198.  
  199. output.write((char*)&leftFilter, sizeof(leftFilter));
  200. output.write((char*)&rightFilter, sizeof(rightFilter));
  201. count++;
  202. }
  203. }
  204. else
  205. {
  206. cout << "Frequency not supported, please use a wav file with frequency 11025 or 22050" << endl;
  207. return 0;
  208. }
  209.  
  210. in.close();
  211.  
  212. output.close();
  213.  
  214. string infile1 = "Frederick_N_lp.wav";
  215. ifstream in1(infile1.c_str(), ios::in | ios::binary);
  216.  
  217. RIFFHeader riff1;
  218. in1.read((char*)&riff1,sizeof(riff1));
  219.  
  220.  
  221. FormatSubChunk format1;
  222. in1.read((char*)&format1,sizeof(format1));
  223.  
  224. DataSubChunkHeader data1;
  225. in1.read((char*)&data1,sizeof(data1));
  226. in.close();
  227.  
  228. cout << "Chunk ID: " << riff1.chunkId[0] << riff1.chunkId[1] << riff1.chunkId[2] << riff1.chunkId[3] << endl;
  229. cout << "Chunk Size: " << riff1.chunkSize << endl;
  230. cout << "Format: " << riff1.format[0] << riff1.format[1] << riff1.format[2] << riff1.format[3] <<endl;
  231. cout << "Sub-chunk1 ID: " << format1.chunkId[0] << format1.chunkId[1] << format1.chunkId[2] << format1.chunkId[3] <<endl;
  232. cout << "Sub-chunk1 Size: " << format1.chunkSize << endl;
  233. cout << "Audio Format: " << format1.audioFormat << endl;
  234. cout << "Number of Channels: " << format1.channels << endl;
  235. cout << "Sample Rate: " << format1.frequency << endl;
  236. cout << "Byte Rate: " << format1.byteRate << endl;
  237. cout << "Block Align: " << format1.blockAlign << endl;
  238. cout << "Bits Per Sample: " << format1.bitsPerSample << endl;
  239. cout << "Sub-chunk2 ID: " << data1.chunkId[0] << data1.chunkId[1] << data1.chunkId[2] << data1.chunkId[3] << endl;
  240. cout << "Sub-chunk2 Size: " << data1.chunkSize << endl << endl;
  241. //---------------------------------------------------------------------------------------------------------------------------------
  242.  
  243. duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
  244.  
  245. string sumoutfile = "summary_filter.txt";
  246. ofstream sumoutput(sumoutfile.c_str());
  247. sumoutput << "sampling frequency = " << Format.frequency << 'n';
  248. long recordTime = count/Format.frequency;
  249. sumoutput << "record time = " << recordTime << " seconds" << 'n';
  250. sumoutput << "execution time = " << duration << " seconds" << 'n';
  251.  
  252.  
  253. sumoutput.close();
  254.  
  255. return 0;
  256. }
Add Comment
Please, Sign In to add comment