Lemma is an Electromagnetics API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WindowFilter.cpp 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* This file is part of Lemma, a geophysical modelling and inversion API */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /**
  6. @file
  7. @author Trevor Irons
  8. @date 07/20/2010
  9. @version $Id: windowfilter.cpp 87 2013-09-05 22:44:05Z tirons $
  10. **/
  11. #include "WindowFilter.h"
  12. namespace Lemma {
  13. // ==================== FRIEND METHODS =====================
  14. std::ostream &operator << (std::ostream &stream, const WindowFilter &ob) {
  15. stream << ob.Serialize() << "\n---\n"; // End of doc --- as a direct stream should encapulste thingy
  16. return stream;
  17. }
  18. // ==================== LIFECYCLE =======================
  19. //--------------------------------------------------------------------------------------
  20. // Class: WindowFilter
  21. // Method: WindowFilter
  22. // Description: constructor (locked with ctor_key)
  23. //--------------------------------------------------------------------------------------
  24. WindowFilter::WindowFilter( const ctor_key& ) : Filter( ),
  25. Width(0), SamplingRate(0), Bandwidth(0), Coefficients(), Type(RECTANGULAR)
  26. {
  27. } // ----- end of method WindowFilter::WindowFilter (constructor) -----
  28. //--------------------------------------------------------------------------------------
  29. // Class: WindowFilter
  30. // Method: WindowFilter
  31. // Description: DeSerializing constructor (locked with ctor_key)
  32. //--------------------------------------------------------------------------------------
  33. WindowFilter::WindowFilter( const YAML::Node& node, const ctor_key& ) : Filter( node ),
  34. Width(0), SamplingRate(0), Bandwidth(0), Coefficients(), Type(RECTANGULAR)
  35. {
  36. Width = node["Width"].as<int>( );
  37. Nt = node["Nt"].as<int>( );
  38. Nw = node["Nw"].as<int>( );
  39. SamplingRate = node["SamplingRate"].as<Real>();
  40. Bandwidth = node["Bandwidth"].as<Real>();
  41. Coefficients = node["Coefficients"].as<VectorXr>();
  42. Type = string2Enum<WINDOWTYPE>( node["WINDOWTYPE"].as<std::string>() );
  43. } // ----- end of method WindowFilter::WindowFilter (constructor) -----
  44. //--------------------------------------------------------------------------------------
  45. // Class: WindowFilter
  46. // Method: NewSP()
  47. // Description: public constructor
  48. //--------------------------------------------------------------------------------------
  49. std::shared_ptr< WindowFilter > WindowFilter::NewSP() {
  50. return std::make_shared< WindowFilter >( ctor_key() );
  51. }
  52. //--------------------------------------------------------------------------------------
  53. // Class: WindowFilter
  54. // Method: ~WindowFilter
  55. // Description: destructor (protected)
  56. //--------------------------------------------------------------------------------------
  57. WindowFilter::~WindowFilter() {
  58. }
  59. //--------------------------------------------------------------------------------------
  60. // Class: WindowFilter
  61. // Method: Serialize
  62. //--------------------------------------------------------------------------------------
  63. YAML::Node WindowFilter::Serialize ( ) const {
  64. YAML::Node node = Filter::Serialize();;
  65. node.SetTag( GetName() );
  66. // FILL IN CLASS SPECIFICS HERE
  67. node["Width"] = Width;
  68. node["Nt"] = Nt;
  69. node["Nw"] = Nw;
  70. node["SamplingRate"] = SamplingRate;
  71. node["Bandwidth"] = Bandwidth;
  72. node["Coefficients"] = Coefficients;
  73. node["WINDOWTYPE"] = enum2String(Type);
  74. return node;
  75. }
  76. //--------------------------------------------------------------------------------------
  77. // Class: WindowFilter
  78. // Method: DeSerialize
  79. //--------------------------------------------------------------------------------------
  80. std::shared_ptr<WindowFilter> WindowFilter::DeSerialize ( const YAML::Node& node ) {
  81. if (node.Tag() != "WindowFilter") {
  82. throw DeSerializeTypeMismatch( "WindowFilter", node.Tag());
  83. }
  84. return std::make_shared<WindowFilter>( node, ctor_key() );
  85. }
  86. //--------------------------------------------------------------------------------------
  87. // Class: WindowFilter
  88. // Method: GetName
  89. // Description: Class identifier
  90. //--------------------------------------------------------------------------------------
  91. inline std::string WindowFilter::GetName ( ) const {
  92. return CName;
  93. } // ----- end of method WindowFilter::GetName -----
  94. // ==================== OPERATIONS =======================
  95. void WindowFilter::SetWindowType(const WINDOWTYPE &type) {
  96. Type = type;
  97. }
  98. void WindowFilter::SetBandwidth(const Real& width) {
  99. Bandwidth = width;
  100. }
  101. void WindowFilter::SetSamplingInterval(const Real& rate) {
  102. SamplingRate = rate;
  103. }
  104. int WindowFilter::GetNumberOfFrequencyBins() {
  105. return this->Nw;
  106. }
  107. void WindowFilter::SetNumberOfSamples(const int& nt) {
  108. Nt = nt;
  109. Nw = Nt%2 ? (Nt-1)/2+1 : (Nt)/2+1; // odd , even
  110. }
  111. VectorXr WindowFilter::GetFilterCoefficients() {
  112. Real Nyquist = .5/SamplingRate;
  113. std::cout << "Window nyquist " << Nyquist << std::endl;
  114. std::cout << "Window Nw " << Nw << std::endl;
  115. Real df = (Nyquist)/((Real)(Nw)); // df Hz per bin
  116. Width = Bandwidth/df;
  117. Coefficients.resize(Width);
  118. switch (Type) {
  119. case HAMMING:
  120. for (int n=0; n<Width; ++n) {
  121. Coefficients(n) = 0.5 * (1.- std::cos( (2.*PI*(Real)(n)) /
  122. ((Real)(Width)-1.) ) );
  123. }
  124. break;
  125. case HANNING:
  126. for (int n=0; n<Width; ++n) {
  127. Coefficients(n) = 0.54 - 0.46 *
  128. std::cos( (2.*PI*(Real)(n)) /
  129. ((Real)(Width)-1.) ) ;
  130. }
  131. break;
  132. case RECTANGULAR:
  133. Coefficients.setOnes();
  134. break;
  135. }
  136. return Coefficients;
  137. }
  138. } // ----- end of Lemma name -----