fork(2) download
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. // THE FOLLOWING HEADER CONTAINS CODE FOR DIGITAL FILTERS. TWO CLASSES ARE
  7. // PRESENTED, INPUT SIGNAL SEQUENCE AND THE FILTERED OUTPUT SIGNAL SEQUENCE (FIR).
  8.  
  9. // ALL COPY RIGHTS RESERVED.
  10.  
  11. class sequence /* compiler executes line by line */ /* REMEMBER! NO implementation in headers!!! */
  12. {
  13. public:
  14. //friend class FIR; /* Make sequence class a friend of FIR class */
  15. sequence(int _NUMBER_OF_SAMPLES = 0, float* _SAMPLES = NULL); /* constructor */
  16. ~sequence(); /* Destructor */
  17. sequence(const sequence& s); /* constructor copy, s is just dummy variable and used repeatitevly */
  18.  
  19. const sequence& operator=(const sequence& s); /* Assignment operator */
  20. sequence operator+(const sequence& s); /* operator + */
  21. sequence& operator+=(const sequence &s); /* Be careful, this operator returns a pointer! */
  22.  
  23. friend istream& operator>>(istream&, sequence&);
  24. /* input stream operator to read data from a file to sample array, both operators come in pairs */
  25.  
  26. friend ostream& operator<<(ostream&, sequence&); /* output stream operator */
  27.  
  28. int get_number_of_samples() const; /* Get number of samples member function */
  29. float* get_samples() const; /* Get the samples from a file, return type could be void (create another array) or float */
  30. /* const is typed after argument list above to promise compiler that sequence won't be modified in apply_filter function */
  31.  
  32. float* all_samples_zero(); /* a function to zero all sample array values */
  33.  
  34. private:
  35. int number_of_samples;
  36. float* samples;
  37. };
  38.  
  39. class FIR
  40. {
  41. public:
  42. //friend class sequence;
  43. FIR(int _NUMBER_OF_COEFFICIENTS = 0, float* _COEFFICIENTS = NULL); /* constructor */
  44. ~FIR(); /* Destructor */
  45. FIR(const FIR& f); /* constructor copy */
  46.  
  47. const FIR& operator=(const FIR& f); /* Assignment operator */
  48. friend istream& operator>>(istream&, FIR&); /* stream operator */
  49. friend ostream& operator<<(ostream&, FIR&);
  50.  
  51. int get_number_of_coefficients(); /* Get number of coefficients */
  52. float* get_coefficients(); /* Get the samples from a file, return type could be void (create another array) or float */
  53.  
  54. sequence* apply_filter(const sequence& f); /* Write the argument inside (in this decleration!!), because it is another argument besides FIR elements */
  55. /* filter function || HOW TO ACCESS Vin FROM SEQUENCE CLASS? use
  56.   (const sequence& s) as argument || output sequence is read to a file */
  57.  
  58. private:
  59. int number_of_coefficients;
  60. float* coefficients;
  61. };
  62.  
  63. //=====================================================================================================================
  64.  
  65. sequence::sequence(int _NUMBER_OF_SAMPLES, float* _SAMPLES) /* CLASS: SEQUENCE (CONSTRUCTOR) */
  66. {
  67. number_of_samples=_NUMBER_OF_SAMPLES;
  68. samples=new float [number_of_samples];
  69. }
  70.  
  71. sequence::~sequence() /* CLASS: SEQUENCE (DESTRUCTOR) */
  72. {
  73. delete [] samples;
  74. }
  75.  
  76. sequence::sequence(const sequence& s) /* CLASS: SEQUENCE (COPY CONSTRUCTOR) */
  77. :number_of_samples(s.number_of_samples)
  78. {
  79. samples=new float [number_of_samples]; /* Necessary to allocate a new array */
  80. for(int i=0; i<number_of_samples; i++) samples[i]=s.samples[i];
  81. }
  82.  
  83. istream& operator>>(istream& in, sequence &s) /* CLASS: SEQUENCE (INPUT STREAM OPERATOR) */
  84. {
  85. in>>s.number_of_samples;//>>s.samples(in);
  86. //return(in); NO NEED, IF SO OPERATOR WILL EXIT HERE!
  87. s.samples=new float [s.number_of_samples];
  88. for(int i=0; i<s.number_of_samples; i++) in>>s.samples[i];
  89. return(in);
  90. }
  91.  
  92. ostream& operator<<(ostream& out, sequence &s) /* CLASS: SEQUENCE (OUTPUT STREAM OPERATOR) */
  93. {
  94. out<<s.number_of_samples;//<<s.samples; // STREAM OPERATOR NOT MEMBER FUNC ANY MORE! INSTANCES WITH DOTS REQUIRED
  95. s.samples=new float [s.number_of_samples];
  96. for(int i=0; i<s.number_of_samples; i++) out<<s.samples[i];
  97. return(out);
  98. }
  99.  
  100. const sequence& sequence::operator=(const sequence& s) /* CLASS: SEQUENCE (ASSIGNMENT OPERATOR) */
  101. {
  102. if(this==&s) return(*this);
  103. if(number_of_samples!=s.number_of_samples)
  104. {
  105. number_of_samples=s.number_of_samples;
  106. delete[] samples;
  107. samples = new float[number_of_samples];
  108. }
  109. for(int i=0; i<number_of_samples; i++) samples[i]=s.samples[i];
  110. return(*this);
  111. }
  112.  
  113. sequence sequence::operator+(const sequence &s) /* CLASS: SEQUENCE (OVERLOAD ADDITION OPERATOR) */
  114. {
  115. sequence result(s);
  116. //result.number_of_samples+=s.number_of_samples; /* should I add number of samples ? */
  117. for(int i=0; i<number_of_samples; i++) result.samples[i]+=samples[i];
  118. return(result);
  119. }
  120.  
  121. sequence& sequence::operator+=(const sequence& s) /* CLASS: SEQUENCE (OVERLOAD ADDITION OPERATOR, change instance value to the total summation) */
  122. {
  123. //sequence result(*this);
  124. //result.number_of_samples+=s.number_of_samples; /* should I add number of samples ? */
  125. for(int i=0; i<number_of_samples; i++) this->samples[i]+=s.samples[i];
  126. return(*this);
  127. }
  128.  
  129. FIR::FIR(int _NUMBER_OF_COEFFICIENTS, float* _COEFFICIENTS) /* CLASS: FIR (CONSTRUCTOR) */
  130. {
  131. number_of_coefficients=_NUMBER_OF_COEFFICIENTS;
  132. coefficients=new float [number_of_coefficients];
  133. }
  134.  
  135. FIR::~FIR() /* CLASS: FIR (DESTRUCTOR) */
  136. {
  137. delete [] coefficients;
  138. }
  139.  
  140. FIR::FIR(const FIR& f) /* CLASS: FIR (COPY CONSTRUCTOR) */
  141. :number_of_coefficients(f.number_of_coefficients)
  142. {
  143. coefficients=new float [number_of_coefficients];
  144. for(int i=0; i<number_of_coefficients; i++) coefficients[i]=f.coefficients[i];
  145. }
  146.  
  147. istream& operator>>(istream& in, FIR &f) /* CLASS: FIR (INPUT STREAM OPERATOR) */
  148. {
  149. in>>f.number_of_coefficients;//>>f.coefficients;
  150. f.coefficients=new float [f.number_of_coefficients];
  151. for(int i=0; i<f.number_of_coefficients; i++) in>>f.coefficients[i];
  152. return(in);
  153. }
  154.  
  155. ostream& operator<<(ostream& out, FIR &f) /* CLASS: FIR (OUTPUT STREAM OPERATOR) */
  156. {
  157. out<<f.number_of_coefficients;//<<f.coefficients;
  158. f.coefficients=new float [f.number_of_coefficients]; /* Allocating a new array is required for every stream operator */
  159. for(int i=0; i<f.number_of_coefficients; i++) out<<f.coefficients[i];
  160. return(out);
  161. }
  162.  
  163. const FIR& FIR::operator=(const FIR& f) /* CLASS: FIR (ASSIGNMENT OPERATOR) */
  164. {
  165. if(this==&f) return(*this);
  166. if(number_of_coefficients!=f.number_of_coefficients)
  167. {
  168. number_of_coefficients=f.number_of_coefficients;
  169. delete[] coefficients;
  170. coefficients = new float[number_of_coefficients];
  171. }
  172. for(int i=0; i<number_of_coefficients; i++) coefficients[i]=f.coefficients[i];
  173. return(*this);
  174. }
  175.  
  176. int sequence::get_number_of_samples() const
  177. {
  178. return(number_of_samples); /* Change number of samples to public instead of private using member function */
  179. }
  180.  
  181. float* sequence::get_samples() const
  182. {
  183. /* Change samples array pointer to public instead of private using member function */
  184. //for(int i=0;i<number_of_samples;i++) samples_file>>samples[i];
  185. return(samples);
  186. }
  187.  
  188. float* sequence::all_samples_zero()
  189. {
  190. for(int i=0; i<number_of_samples; i++) samples[i]=0.0;
  191. return(samples); /* is it correct to return a pointer? */
  192. }
  193.  
  194. int FIR::get_number_of_coefficients()
  195. {
  196. /* Change number of coefficients to public instead of private using member function */
  197. return(number_of_coefficients);
  198. }
  199.  
  200. float* FIR::get_coefficients()
  201. {
  202. /* Change coefficients array pointer to public instead of private using member function */
  203. //for(int i=0;i<number_of_coefficients;i++) coefficients_file>>coefficients[i];
  204. return(coefficients);
  205. }
  206.  
  207. sequence* FIR::apply_filter(const sequence& f)
  208. {
  209. sequence* filtered_samples = new sequence; /* Create a pointer to a new sequence */
  210. int counter=f.get_number_of_samples();
  211. for(int i=0; i<counter; i++)
  212. {
  213. if (0==i) filtered_samples->get_samples()[i]=coefficients[0]*f.get_samples()[i];
  214. else filtered_samples->get_samples()[i]=coefficients[0] * f.get_samples()[i]+ coefficients[1] * f.get_samples()[i-1];
  215. }
  216. return(filtered_samples);
  217. } // SOMETIMES PROGRAMME CRASHES BECAUSE IT TRIES TO ACCESS SMTHUNG WHICH DOESN'T EXIST
  218.  
  219. int main()
  220. {
  221. ifstream samples_file("input.txt");
  222. ifstream coefficients_file("filter.txt");
  223. ofstream output_filtered_samples_file("output.txt");
  224. FIR test_FIR; /* CREATE NEW FIR INSTANCE */
  225. sequence test_sequence; /* CREATE NEW SEQUENCE INSTANCE */
  226. //sequence* filtered_sequence = new sequence;coefficients_file>>test_FIR;
  227. samples_file>>test_sequence;
  228. coefficients_file>>test_FIR;
  229. sequence* filtered_sequence = test_FIR.apply_filter(test_sequence);
  230. output_filtered_samples_file<<filtered_sequence;
  231. for(int i=0;i<filtered_sequence->get_number_of_samples();i++) cout<<filtered_sequence->get_samples()[i]<<endl; /* INPUT OPERATORS ARE WRONG!!! FIX THEM, ARE THEY? */
  232.  
  233. // cin>>1; /* DEMONSTRATOR SAID SOMETHING ABOUT GETFUNCTIONS RETUREN VALUE NOT VARIABLE! WHAT? CHECK LATER */
  234. // for (int i=0; i<12; i++) cout<<test_sequence.get_samples()[i]<<endl;
  235. // for (int i=0; i<a; i++) coefficients_file>>test_FIR.get_coefficients()[i]; NO NEED FOR THESE, STREAM OPERATORS DO THAT ALREADY!!!
  236. // for (int i=0; i<b; i++) samples_file>>test_sequence.get_samples()[i];
  237. // for (int i=0; i<a; i++) output_filtered_samples_file<<filtered_sequence->get_samples()[i];
  238.  
  239. return 0;
  240. }
  241.  
  242. // WHY CODE WORKS IF CPP FILE RUN ALONE?
  243.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Standard output is empty