/tags/rel-0-4-0/FreeSpeech/audio_blocks/src/LPFilter.cc

# · C++ · 115 lines · 54 code · 16 blank · 45 comment · 12 complexity · a2f8515819a9d30dba5466be265cc1fe MD5 · raw file

  1. // Copyright (C) 1999 Jean-Marc Valin & Dominic Letourneau
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but
  9. // WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this file. If not, write to the Free Software Foundation,
  15. // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. #include "Node.h"
  17. #include "Vector.h"
  18. #include "ObjectParser.h"
  19. #include <strstream>
  20. #include <math.h>
  21. class LPFilter;
  22. DECLARE_NODE(LPFilter)
  23. /*Node
  24. * @name LPFilter
  25. * @category Signal:DSP
  26. * @description No description available
  27. * @output_name OUTPUT
  28. * @output_description No description available
  29. * @parameter_name LENGTH
  30. * @parameter_description No description available
  31. * @parameter_name THETA
  32. * @parameter_description No description available
  33. * @parameter_name HP
  34. * @parameter_description No description available
  35. END*/
  36. /** A constant node contains a value that will never changes. */
  37. class LPFilter : public Node
  38. {
  39. protected:
  40. /**The value of the constant*/
  41. ObjectRef value;
  42. /**The ID of the 'value' output*/
  43. int outputID;
  44. public:
  45. /**Constructor, takes the name of the node and a set of parameters*/
  46. LPFilter(string nodeName, ParameterSet params)
  47. : Node(nodeName, params)
  48. //, value (parameters.get("VALUE"))
  49. {
  50. int i;
  51. outputID = addOutput("OUTPUT");
  52. int length = dereference_cast <int> (parameters.get("LENGTH"));
  53. float theta = dereference_cast <float> (parameters.get("THETA"));
  54. value = ObjectRef(new Vector<float> (length));
  55. Vector<float> &val = object_cast<Vector<float> > (value);
  56. for (i=0;i<length;i++)
  57. {
  58. if (i-length/2 != 0)
  59. val[i]=sin(M_PI*(i-length/2)*theta)/(M_PI*(i-length/2)*theta);
  60. else val[i]=1.0;
  61. val[i]*=.54-.46*cos(2*M_PI*i/double(length-1));
  62. }
  63. float DC=0.0;
  64. for (i=0;i<length;i++)
  65. DC+=val[i];
  66. for (i=0;i<length;i++)
  67. val[i]/=DC;
  68. if (parameters.exist("NODC"))
  69. {
  70. for (i=0;i<length;i++)
  71. val[i] -= (.54-.46*cos(2*M_PI*i/double(length-1))) / (.54*(length-1));
  72. }
  73. if (parameters.exist("HP"))
  74. {
  75. for (i=0;i<length;i++)
  76. val[i]=-val[i];
  77. val[length/2]+=1;
  78. }
  79. /*for (i=0;i<length;i++)
  80. cout << val[i] << " ";
  81. cout << endl;*/
  82. }
  83. /**Ask for the node's output which ID (number) is output_id
  84. and for the 'count' iteration */
  85. virtual ObjectRef getOutput(int output_id, int count)
  86. {
  87. if (output_id==outputID) return value;
  88. else throw new NodeException (this, "LPFilter: Unknown output id", __FILE__, __LINE__);
  89. }
  90. protected:
  91. /**Default constructor, should not be used*/
  92. LPFilter() {throw new GeneralException("LPFilter copy constructor should not be called",__FILE__,__LINE__);}
  93. };