|
1 |
| -/******************************************************************************* |
2 |
| - * SDR Trunk |
3 |
| - * Copyright (C) 2014 Dennis Sheirer |
4 |
| - * |
5 |
| - * This program is free software: you can redistribute it and/or modify |
6 |
| - * it under the terms of the GNU General Public License as published by |
7 |
| - * the Free Software Foundation, either version 3 of the License, or |
8 |
| - * (at your option) any later version. |
9 |
| - * |
10 |
| - * This program is distributed in the hope that it will be useful, |
11 |
| - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
| - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
| - * GNU General Public License for more details. |
14 |
| - * |
15 |
| - * You should have received a copy of the GNU General Public License |
16 |
| - * along with this program. If not, see <http://www.gnu.org/licenses/> |
17 |
| - ******************************************************************************/ |
| 1 | +/* |
| 2 | + * ***************************************************************************** |
| 3 | + * Copyright (C) 2014-2025 Dennis Sheirer |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/> |
| 17 | + * **************************************************************************** |
| 18 | + */ |
18 | 19 | package io.github.dsheirer.buffer;
|
19 | 20 |
|
| 21 | +import java.util.Arrays; |
| 22 | + |
| 23 | +/** |
| 24 | + * Averaging buffer backed by a ring buffer implemented with a float array. |
| 25 | + */ |
20 | 26 | public class FloatAveragingBuffer
|
21 | 27 | {
|
22 |
| - private float[] mBuffer; |
23 |
| - private float mAverage = 0.0f; |
24 |
| - private int mBufferSize; |
25 |
| - private int mBufferPointer; |
26 |
| - |
27 |
| - public FloatAveragingBuffer( int size ) |
28 |
| - { |
29 |
| - mBufferSize = size; |
30 |
| - mBuffer = new float[ size ]; |
31 |
| - } |
32 |
| - |
33 |
| - public float get( float newValue ) |
34 |
| - { |
35 |
| - float oldValue = mBuffer[ mBufferPointer ]; |
36 |
| - |
37 |
| - if( Float.isInfinite( newValue ) || Float.isNaN( newValue ) ) |
38 |
| - { |
39 |
| - mAverage = mAverage - ( oldValue / mBufferSize ); |
40 |
| - |
41 |
| - mBuffer[ mBufferPointer++ ] = 0.0f; |
42 |
| - } |
43 |
| - else |
44 |
| - { |
45 |
| - mAverage = mAverage - ( oldValue / mBufferSize ) + ( newValue / mBufferSize ); |
46 |
| - |
47 |
| - mBuffer[ mBufferPointer++ ] = newValue; |
48 |
| - } |
49 |
| - |
50 |
| - if( mBufferPointer >= mBufferSize ) |
51 |
| - { |
52 |
| - mBufferPointer = 0; |
53 |
| - } |
54 |
| - |
55 |
| - return mAverage; |
56 |
| - } |
| 28 | + private final float[] mBuffer; |
| 29 | + private final int mBufferSize; |
| 30 | + private float mAverage = 0.0f; |
| 31 | + private int mBufferPointer; |
| 32 | + private boolean mRapidFill = false; |
| 33 | + private int mRapidFillCounter = 0; |
| 34 | + private int mRapidFillIncrement; |
| 35 | + |
| 36 | + /** |
| 37 | + * Constructs an instance that uses a rapid fill increment value where the initial values get counted multiple times |
| 38 | + * to more quickly get to the average value instead of walking up to the average from the default average value of 0. |
| 39 | + * @param size of buffer |
| 40 | + * @param rapidFillIncrement indicating how many value additions per individual add to execute until the averaging |
| 41 | + * buffer is filled. After the initial fill state is achieved, reverts to using a single value addition. |
| 42 | + */ |
| 43 | + public FloatAveragingBuffer(int size, int rapidFillIncrement) |
| 44 | + { |
| 45 | + this(size); |
| 46 | + mRapidFillIncrement = rapidFillIncrement; |
| 47 | + mRapidFill = true; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Constructs an instance that uses a ring buffer of the specified size. |
| 52 | + * @param size of the averaging buffer. |
| 53 | + */ |
| 54 | + public FloatAveragingBuffer(int size) |
| 55 | + { |
| 56 | + mBufferSize = size; |
| 57 | + mBuffer = new float[size]; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Resets this buffer to an empty state |
| 62 | + */ |
| 63 | + public void reset() |
| 64 | + { |
| 65 | + Arrays.fill(mBuffer, 0); |
| 66 | + mBufferPointer = 0; |
| 67 | + mAverage = 0.0f; |
| 68 | + |
| 69 | + if(mRapidFillIncrement > 0) |
| 70 | + { |
| 71 | + mRapidFill = true; |
| 72 | + mRapidFillCounter = 0; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Current average value. |
| 78 | + * @return average |
| 79 | + */ |
| 80 | + public float getAverage() |
| 81 | + { |
| 82 | + return mAverage; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Adds the value to the buffer and updates the average. |
| 87 | + * @param value to add |
| 88 | + */ |
| 89 | + public void add(float value) |
| 90 | + { |
| 91 | + if(mRapidFill) |
| 92 | + { |
| 93 | + for(int x = 0; x < mRapidFillIncrement; x++) |
| 94 | + { |
| 95 | + float oldValue = mBuffer[mBufferPointer]; |
| 96 | + |
| 97 | + if(Float.isInfinite(value) || Float.isNaN(value)) |
| 98 | + { |
| 99 | + mAverage = mAverage - (oldValue / mBufferSize); |
| 100 | + mBuffer[mBufferPointer++] = 0.0f; |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + mAverage = mAverage - (oldValue / mBufferSize) + (value / mBufferSize); |
| 105 | + mBuffer[mBufferPointer++] = value; |
| 106 | + } |
| 107 | + |
| 108 | + if(mBufferPointer >= mBufferSize) |
| 109 | + { |
| 110 | + mBufferPointer = 0; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + mRapidFillCounter += mRapidFillIncrement; |
| 115 | + |
| 116 | + if(mRapidFillCounter > mBufferSize) |
| 117 | + { |
| 118 | + mRapidFill = false; |
| 119 | + } |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + float oldValue = mBuffer[mBufferPointer]; |
| 124 | + |
| 125 | + if(Float.isInfinite(value) || Float.isNaN(value)) |
| 126 | + { |
| 127 | + mAverage = mAverage - (oldValue / mBufferSize); |
| 128 | + mBuffer[mBufferPointer++] = 0.0f; |
| 129 | + } |
| 130 | + else |
| 131 | + { |
| 132 | + mAverage = mAverage - (oldValue / mBufferSize) + (value / mBufferSize); |
| 133 | + mBuffer[mBufferPointer++] = value; |
| 134 | + } |
| 135 | + |
| 136 | + if(mBufferPointer >= mBufferSize) |
| 137 | + { |
| 138 | + mBufferPointer = 0; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Adds the value to the buffer and calculates a new average. |
| 145 | + * @param value to add |
| 146 | + * @return updated average |
| 147 | + */ |
| 148 | + public float get(float value) |
| 149 | + { |
| 150 | + add(value); |
| 151 | + return mAverage; |
| 152 | + } |
57 | 153 | }
|
0 commit comments