blob: 707a89d44ac16fdb24abdf99e9c15897ad6461a7 [file] [log] [blame]
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_TRANSIENT_WPD_TREE_H_
12#define MODULES_AUDIO_PROCESSING_TRANSIENT_WPD_TREE_H_
pbos@webrtc.org788acd12014-12-15 09:41:24 +000013
kwiberg85d8bb02016-02-16 20:39:36 -080014#include <memory>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_processing/transient/wpd_node.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000017
18namespace webrtc {
19
20// Tree of a Wavelet Packet Decomposition (WPD).
21//
22// The root node contains all the data provided; for each node in the tree, the
23// left child contains the approximation coefficients extracted from the node,
24// and the right child contains the detail coefficients.
25// It preserves its state, so it can be multiple-called.
26//
27// The number of nodes in the tree will be 2 ^ levels - 1.
28//
29// Implementation details: Since the tree always will be a complete binary tree,
30// it is implemented using a single linear array instead of managing the
31// relationships in each node. For convience is better to use a array that
32// starts in 1 (instead of 0). Taking that into account, the following formulas
33// apply:
34// Root node index: 1.
35// Node(Level, Index in that level): 2 ^ Level + (Index in that level).
36// Left Child: Current node index * 2.
37// Right Child: Current node index * 2 + 1.
38// Parent: Current Node Index / 2 (Integer division).
39class WPDTree {
40 public:
41 // Creates a WPD tree using the data length and coefficients provided.
42 WPDTree(size_t data_length,
43 const float* high_pass_coefficients,
44 const float* low_pass_coefficients,
45 size_t coefficients_length,
46 int levels);
47 ~WPDTree();
48
49 // Returns the number of nodes at any given level.
Yves Gerey665174f2018-06-19 15:03:05 +020050 static int NumberOfNodesAtLevel(int level) { return 1 << level; }
pbos@webrtc.org788acd12014-12-15 09:41:24 +000051
52 // Returns a pointer to the node at the given level and index(of that level).
53 // Level goes from 0 to levels().
54 // Index goes from 0 to the number of NumberOfNodesAtLevel(level) - 1.
55 //
56 // You can use the following formulas to get any node within the tree:
57 // Notation: (Level, Index of node in that level).
58 // Root node: (0/0).
59 // Left Child: (Current node level + 1, Current node index * 2).
60 // Right Child: (Current node level + 1, Current node index * 2 + 1).
61 // Parent: (Current node level - 1, Current node index / 2) (Integer division)
62 //
63 // If level or index are out of bounds the function will return NULL.
64 WPDNode* NodeAt(int level, int index);
65
66 // Updates all the nodes of the tree with the new data. |data_length| must be
67 // teh same that was used for the creation of the tree.
68 // Returns 0 if correct, and -1 otherwise.
69 int Update(const float* data, size_t data_length);
70
71 // Returns the total number of levels below the root. Root is cosidered level
72 // 0.
73 int levels() const { return levels_; }
74
75 // Returns the total number of nodes.
76 int num_nodes() const { return num_nodes_; }
77
78 // Returns the total number of leaves.
79 int num_leaves() const { return 1 << levels_; }
80
81 private:
82 size_t data_length_;
83 int levels_;
84 int num_nodes_;
kwiberg85d8bb02016-02-16 20:39:36 -080085 std::unique_ptr<std::unique_ptr<WPDNode>[]> nodes_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000086};
87
88} // namespace webrtc
89
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020090#endif // MODULES_AUDIO_PROCESSING_TRANSIENT_WPD_TREE_H_