wpd_tree.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_AUDIO_PROCESSING_TRANSIENT_WPD_TREE_H_
  11. #define MODULES_AUDIO_PROCESSING_TRANSIENT_WPD_TREE_H_
  12. #include <stddef.h>
  13. #include <memory>
  14. #include "modules/audio_processing/transient/wpd_node.h"
  15. namespace webrtc {
  16. // Tree of a Wavelet Packet Decomposition (WPD).
  17. //
  18. // The root node contains all the data provided; for each node in the tree, the
  19. // left child contains the approximation coefficients extracted from the node,
  20. // and the right child contains the detail coefficients.
  21. // It preserves its state, so it can be multiple-called.
  22. //
  23. // The number of nodes in the tree will be 2 ^ levels - 1.
  24. //
  25. // Implementation details: Since the tree always will be a complete binary tree,
  26. // it is implemented using a single linear array instead of managing the
  27. // relationships in each node. For convience is better to use a array that
  28. // starts in 1 (instead of 0). Taking that into account, the following formulas
  29. // apply:
  30. // Root node index: 1.
  31. // Node(Level, Index in that level): 2 ^ Level + (Index in that level).
  32. // Left Child: Current node index * 2.
  33. // Right Child: Current node index * 2 + 1.
  34. // Parent: Current Node Index / 2 (Integer division).
  35. class WPDTree {
  36. public:
  37. // Creates a WPD tree using the data length and coefficients provided.
  38. WPDTree(size_t data_length,
  39. const float* high_pass_coefficients,
  40. const float* low_pass_coefficients,
  41. size_t coefficients_length,
  42. int levels);
  43. ~WPDTree();
  44. // Returns the number of nodes at any given level.
  45. static int NumberOfNodesAtLevel(int level) { return 1 << level; }
  46. // Returns a pointer to the node at the given level and index(of that level).
  47. // Level goes from 0 to levels().
  48. // Index goes from 0 to the number of NumberOfNodesAtLevel(level) - 1.
  49. //
  50. // You can use the following formulas to get any node within the tree:
  51. // Notation: (Level, Index of node in that level).
  52. // Root node: (0/0).
  53. // Left Child: (Current node level + 1, Current node index * 2).
  54. // Right Child: (Current node level + 1, Current node index * 2 + 1).
  55. // Parent: (Current node level - 1, Current node index / 2) (Integer division)
  56. //
  57. // If level or index are out of bounds the function will return NULL.
  58. WPDNode* NodeAt(int level, int index);
  59. // Updates all the nodes of the tree with the new data. |data_length| must be
  60. // teh same that was used for the creation of the tree.
  61. // Returns 0 if correct, and -1 otherwise.
  62. int Update(const float* data, size_t data_length);
  63. // Returns the total number of levels below the root. Root is cosidered level
  64. // 0.
  65. int levels() const { return levels_; }
  66. // Returns the total number of nodes.
  67. int num_nodes() const { return num_nodes_; }
  68. // Returns the total number of leaves.
  69. int num_leaves() const { return 1 << levels_; }
  70. private:
  71. size_t data_length_;
  72. int levels_;
  73. int num_nodes_;
  74. std::unique_ptr<std::unique_ptr<WPDNode>[]> nodes_;
  75. };
  76. } // namespace webrtc
  77. #endif // MODULES_AUDIO_PROCESSING_TRANSIENT_WPD_TREE_H_