Lemma is an Electromagnetics API
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

WindowFilter.cpp 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. **/
  10. #include "WindowFilter.h"
  11. namespace Lemma {
  12. // ==================== FRIEND METHODS =====================
  13. std::ostream &operator << (std::ostream &stream, const WindowFilter &ob) {
  14. stream << ob.Serialize() << "\n";
  15. return stream;
  16. }
  17. // ==================== LIFECYCLE =======================
  18. //--------------------------------------------------------------------------------------
  19. // Class: WindowFilter
  20. // Method: WindowFilter
  21. // Description: constructor (locked with ctor_key)
  22. //--------------------------------------------------------------------------------------
  23. WindowFilter::WindowFilter( const ctor_key& key ) : Filter( key ),
  24. Width(0), SamplingRate(0), Bandwidth(0), Coefficients(), Type(RECTANGULAR)
  25. {
  26. } // ----- end of method WindowFilter::WindowFilter (constructor) -----
  27. //--------------------------------------------------------------------------------------
  28. // Class: WindowFilter
  29. // Method: WindowFilter
  30. // Description: DeSerializing constructor (locked with ctor_key)
  31. //--------------------------------------------------------------------------------------
  32. WindowFilter::WindowFilter( const YAML::Node& node, const ctor_key& key) : Filter( node, key ),
  33. Width(0), SamplingRate(0), Bandwidth(0), Coefficients(), Type(RECTANGULAR)
  34. {
  35. Width = node["Width"].as<int>( );
  36. Nt = node["Nt"].as<int>( );
  37. Nw = node["Nw"].as<int>( );
  38. SamplingRate = node["SamplingRate"].as<Real>();
  39. Bandwidth = node["Bandwidth"].as<Real>();
  40. Coefficients = node["Coefficients"].as<VectorXr>();
  41. Type = string2Enum<WINDOWTYPE>( node["WINDOWTYPE"].as<std::string>() );
  42. } // ----- end of method WindowFilter::WindowFilter (constructor) -----
  43. //--------------------------------------------------------------------------------------
  44. // Class: WindowFilter
  45. // Method: NewSP()
  46. // Description: public constructor
  47. //--------------------------------------------------------------------------------------
  48. std::shared_ptr< WindowFilter > WindowFilter::NewSP() {
  49. return std::make_shared< WindowFilter >( ctor_key() );
  50. }
  51. //--------------------------------------------------------------------------------------
  52. // Class: WindowFilter
  53. // Method: ~WindowFilter
  54. // Description: destructor (protected)
  55. //--------------------------------------------------------------------------------------
  56. WindowFilter::~WindowFilter() {
  57. }
  58. //--------------------------------------------------------------------------------------
  59. // Class: WindowFilter
  60. // Method: Serialize
  61. //--------------------------------------------------------------------------------------
  62. YAML::Node WindowFilter::Serialize ( ) const {
  63. YAML::Node node = Filter::Serialize();;
  64. node.SetTag( GetName() );
  65. // FILL IN CLASS SPECIFICS HERE
  66. node["Width"] = Width;
  67. node["Nt"] = Nt;
  68. node["Nw"] = Nw;
  69. node["SamplingRate"] = SamplingRate;
  70. node["Bandwidth"] = Bandwidth;
  71. node["Coefficients"] = Coefficients;
  72. node["WINDOWTYPE"] = enum2String(Type);
  73. return node;
  74. }
  75. //--------------------------------------------------------------------------------------
  76. // Class: WindowFilter
  77. // Method: DeSerialize
  78. //--------------------------------------------------------------------------------------
  79. std::shared_ptr<WindowFilter> WindowFilter::DeSerialize ( const YAML::Node& node ) {
  80. if (node.Tag() != "WindowFilter") {
  81. throw DeSerializeTypeMismatch( "WindowFilter", node.Tag());
  82. }
  83. return std::make_shared<WindowFilter>( node, ctor_key() );
  84. }
  85. //--------------------------------------------------------------------------------------
  86. // Class: WindowFilter
  87. // Method: GetName
  88. // Description: Class identifier
  89. //--------------------------------------------------------------------------------------
  90. inline std::string WindowFilter::GetName ( ) const {
  91. return CName;
  92. } // ----- end of method WindowFilter::GetName -----
  93. // ==================== OPERATIONS =======================
  94. void WindowFilter::SetWindowType(const WINDOWTYPE &type) {
  95. Type = type;
  96. }
  97. void WindowFilter::SetBandwidth(const Real& width) {
  98. Bandwidth = width;
  99. }
  100. void WindowFilter::SetSamplingInterval(const Real& rate) {
  101. SamplingRate = rate;
  102. }
  103. int WindowFilter::GetNumberOfFrequencyBins() {
  104. return this->Nw;
  105. }
  106. void WindowFilter::SetNumberOfSamples(const int& nt) {
  107. Nt = nt;
  108. Nw = Nt%2 ? (Nt-1)/2+1 : (Nt)/2+1; // odd , even
  109. }
  110. VectorXr WindowFilter::GetFilterCoefficients() {
  111. Real Nyquist = .5/SamplingRate;
  112. std::cout << "Window nyquist " << Nyquist << std::endl;
  113. std::cout << "Window Nw " << Nw << std::endl;
  114. Real df = (Nyquist)/((Real)(Nw)); // df Hz per bin
  115. Width = (int)(std::round(Bandwidth/df));
  116. Coefficients.resize(Width);
  117. switch (Type) {
  118. case HAMMING:
  119. for (int n=0; n<Width; ++n) {
  120. Coefficients(n) = 0.5 * (1.- std::cos( (2.*PI*(Real)(n)) /
  121. ((Real)(Width)-1.) ) );
  122. }
  123. break;
  124. case HANNING:
  125. for (int n=0; n<Width; ++n) {
  126. Coefficients(n) = 0.54 - 0.46 *
  127. std::cos( (2.*PI*(Real)(n)) /
  128. ((Real)(Width)-1.) ) ;
  129. }
  130. break;
  131. case RECTANGULAR:
  132. Coefficients.setOnes();
  133. break;
  134. }
  135. return Coefficients;
  136. }
  137. } // ----- end of Lemma name -----