#2704 Waterfall contrast for weak signals relative to the noise floor (#2712)

The waterfall mapped 256 palette indexes over the absolute dBFS level of the
mean bin value, about 2.5 steps per dB at a -100 dBFS floor, so weak signals
were only a few steps above the noise.  WaterfallScaler now tracks the noise
floor as a smoothed 25th percentile of the bins (robust to occupied spans) and
maps a fixed 45 dB span above it, so contrast no longer depends on sample size,
DFT size or tuner gain.  The classic palette's flat blue blocks are replaced
with a blue ramp over the bottom quarter, where the noise floor now sits.

Co-authored-by: denny <dennis.sheirer@gmail.com>
This commit is contained in:
Denny Sheirer
2026-11-20 02:47:57 -05:00
committed by GitHub
parent 838e7708c0
commit 8324233acb
8 changed files with 799 additions and 78 deletions

View File

@@ -65,7 +65,7 @@ public class ChannelSpectralDisplay extends DftSpectralDisplay
SpectralDisplayModel model = getSpectralDisplay().getModel();
getSpectralDisplay().messageProperty().set(MESSAGE_NO_CHANNEL);
//Noise floor slider along the right edge, below the configuration panel tab
//Noise floor slider for the spectrum
mNoiseFloorSlider = new EdgeSlider("Floor", "Noise floor: moves the bottom of the spectrum's dB range. " +
"Higher settings show a deeper range and move the noise floor up the display.",
MINIMUM_SAMPLE_SIZE, MAXIMUM_SAMPLE_SIZE, 1, this::formatPlotBottom);
@@ -82,9 +82,10 @@ public class ChannelSpectralDisplay extends DftSpectralDisplay
model.binCountProperty().addListener((ob, ov, nv) -> mNoiseFloorSlider.refreshLabel());
model.noiseBandwidthProperty().addListener((ob, ov, nv) -> mNoiseFloorSlider.refreshLabel());
mNoiseFloorSlider.refreshLabel();
//Along the right edge of the spectrum section, below the configuration panel tab
StackPane.setAlignment(mNoiseFloorSlider, Pos.CENTER_RIGHT);
StackPane.setMargin(mNoiseFloorSlider, new Insets(44, 0, 8, 0));
getChildren().add(mNoiseFloorSlider);
getSpectralDisplay().addSectionOverlay(DisplaySection.SPECTRUM, mNoiseFloorSlider);
installConfigPanel();
}

View File

@@ -55,6 +55,8 @@ public abstract class DftSpectralDisplay extends StackPane
private static final String PEAK_HOLD = "peak.hold";
private static final String SPECTRUM_ENABLED = "spectrum.enabled";
private static final String WATERFALL_ENABLED = "waterfall.enabled";
private static final String WATERFALL_RANGE = "waterfall.range";
private static final String WATERFALL_AVERAGING = "waterfall.averaging";
private static final String DIVIDER_POSITION = "divider.position";
private static final String DECIBEL_SCALE = "decibel.scale";
private static final String CONFIG_PINNED = "config.pinned";
@@ -121,6 +123,9 @@ public abstract class DftSpectralDisplay extends StackPane
});
mSpectralDisplay.averagingProperty().addListener((ob, ov, nv) -> save(AVERAGING, nv.intValue()));
mSpectralDisplay.peakHoldProperty().addListener((ob, ov, nv) -> save(PEAK_HOLD, nv));
mSpectralDisplay.waterfallRangeProperty().addListener((ob, ov, nv) -> save(WATERFALL_RANGE, nv.intValue()));
mSpectralDisplay.waterfallAveragingProperty().addListener((ob, ov, nv) ->
save(WATERFALL_AVERAGING, nv.intValue()));
mSpectralDisplay.dividerPositionProperty().addListener((ob, ov, nv) ->
save(DIVIDER_POSITION, String.valueOf(nv.doubleValue())));
mDecibelLayer.visibleProperty().addListener((ob, ov, nv) -> save(DECIBEL_SCALE, nv));
@@ -205,6 +210,12 @@ public abstract class DftSpectralDisplay extends StackPane
mSpectralDisplay.averagingProperty().set(Math.max(SpectralDisplay.AVERAGING_MINIMUM,
Math.min(SpectralDisplay.AVERAGING_MAXIMUM, averaging)));
mSpectralDisplay.peakHoldProperty().set(properties.get(key(PEAK_HOLD), false));
mSpectralDisplay.waterfallRangeProperty().set(Math.max(WaterfallScaler.RANGE_MINIMUM,
Math.min(WaterfallScaler.RANGE_MAXIMUM, properties.get(key(WATERFALL_RANGE),
WaterfallScaler.RANGE_DEFAULT))));
mSpectralDisplay.waterfallAveragingProperty().set(Math.max(WaterfallScaler.AVERAGING_MINIMUM,
Math.min(WaterfallScaler.AVERAGING_MAXIMUM, properties.get(key(WATERFALL_AVERAGING),
WaterfallScaler.AVERAGING_DEFAULT))));
mDecibelLayer.visibleProperty().set(properties.get(key(DECIBEL_SCALE), true));
try

View File

@@ -42,6 +42,8 @@ import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.Orientation;
import javafx.geometry.VPos;
import javafx.scene.Cursor;
@@ -104,6 +106,10 @@ public class SpectralDisplay extends StackPane
private final SplitPane mSplitPane = new SplitPane();
private final CanvasPane mSpectrumPane = new CanvasPane(DisplaySection.SPECTRUM);
private final CanvasPane mWaterfallPane = new CanvasPane(DisplaySection.WATERFALL);
//Each section's canvas with overlay controls stacked on top
private final StackPane mSpectrumSection = new StackPane(mSpectrumPane);
private final StackPane mWaterfallSection = new StackPane(mWaterfallPane);
private final EdgeSlider mWaterfallRangeSlider;
private final StackPane mDisabledPane = new StackPane();
private final BooleanProperty mSpectrumEnabled = new SimpleBooleanProperty(true);
@@ -112,6 +118,8 @@ public class SpectralDisplay extends StackPane
private final IntegerProperty mAveraging = new SimpleIntegerProperty(AVERAGING_DEFAULT);
private final BooleanProperty mPeakHold = new SimpleBooleanProperty(false);
private final DoubleProperty mDividerPosition = new SimpleDoubleProperty(0.5);
private final IntegerProperty mWaterfallRange = new SimpleIntegerProperty(WaterfallScaler.RANGE_DEFAULT);
private final IntegerProperty mWaterfallAveraging = new SimpleIntegerProperty(WaterfallScaler.AVERAGING_DEFAULT);
private final StringProperty mMessage = new SimpleStringProperty();
//Values read on the DFT results thread
@@ -119,6 +127,7 @@ public class SpectralDisplay extends StackPane
private volatile boolean mWaterfallActive = true;
private volatile boolean mWaterfallPausedFlag = false;
private final SpectrumAverager mAverager = new SpectrumAverager();
private final WaterfallScaler mWaterfallScaler = new WaterfallScaler();
//Hand off from the DFT results thread to the JavaFX thread
private final AtomicReference<SpectrumAverager.Frame> mLatestSpectrum = new AtomicReference<>();
@@ -133,8 +142,10 @@ public class SpectralDisplay extends StackPane
private WritableImage[] mWaterfallTiles;
private int mWaterfallWidth;
private int mWaterfallHeight;
private byte[] mWaterfallIndexes;
private byte[] mWaterfallLevels;
private int[] mWaterfallPixels;
//ARGB color for each waterfall level code, for the current theme and range. Null when it needs to be rebuilt.
private int[] mWaterfallColors;
private int mWaterfallWriteRow = 0;
private double[] mPolygonX = new double[0];
private double[] mPolygonY = new double[0];
@@ -167,6 +178,37 @@ public class SpectralDisplay extends StackPane
themeChanged(disabledLabel);
getChildren().addAll(mDisabledPane, mSplitPane);
setMinSize(0, 0);
mSpectrumSection.setMinSize(0, 0);
mWaterfallSection.setMinSize(0, 0);
//Waterfall gain slider along the right edge of the waterfall. The slider value is inverted so that moving it
//up narrows the dB range, which raises the contrast of weak signals.
int rangeSum = WaterfallScaler.RANGE_MINIMUM + WaterfallScaler.RANGE_MAXIMUM;
mWaterfallRangeSlider = new EdgeSlider("Gain", "Waterfall gain: dB range from the noise floor to the top " +
"of the color palette. Higher settings use a smaller range, which gives weak signals more contrast " +
"against the noise and saturates strong signals sooner.", WaterfallScaler.RANGE_MINIMUM,
WaterfallScaler.RANGE_MAXIMUM, 1, value -> (rangeSum - Math.round(value)) + " dB");
mWaterfallRangeSlider.valueProperty().set(rangeSum - mWaterfallRange.get());
mWaterfallRangeSlider.valueProperty().addListener((ob, ov, nv) ->
mWaterfallRange.set(rangeSum - (int)Math.round(nv.doubleValue())));
mWaterfallRange.addListener((ob, ov, nv) -> {
int range = Math.max(WaterfallScaler.RANGE_MINIMUM, Math.min(WaterfallScaler.RANGE_MAXIMUM,
nv.intValue()));
if(range != nv.intValue())
{
mWaterfallRange.set(range);
return;
}
mWaterfallRangeSlider.valueProperty().set(rangeSum - range);
mWaterfallColors = null;
repaintWaterfallImage();
});
StackPane.setAlignment(mWaterfallRangeSlider, Pos.CENTER_RIGHT);
mWaterfallSection.getChildren().add(mWaterfallRangeSlider);
mWaterfallAveraging.addListener((ob, ov, nv) -> mWaterfallScaler.setAveraging(nv.intValue()));
mSpectrumEnabled.addListener((ob, ov, nv) -> updateSections());
mWaterfallEnabled.addListener((ob, ov, nv) -> updateSections());
@@ -261,6 +303,7 @@ public class SpectralDisplay extends StackPane
SpectralDisplayTheme theme = getTheme();
mDisabledPane.setBackground(new Background(new BackgroundFill(theme.getBackground(), null, null)));
disabledLabel.setTextFill(theme.getLine());
mWaterfallColors = null;
repaintWaterfallImage();
requestRender();
}
@@ -357,6 +400,42 @@ public class SpectralDisplay extends StackPane
return mPeakHold;
}
/**
* Waterfall dB range from the noise floor to the top of the color palette, from WaterfallScaler.RANGE_MINIMUM to
* RANGE_MAXIMUM. Smaller ranges give weak signals more contrast. JavaFX thread only.
*/
public IntegerProperty waterfallRangeProperty()
{
return mWaterfallRange;
}
/**
* Number of DFT frames averaged into each waterfall row, from WaterfallScaler.AVERAGING_MINIMUM to
* AVERAGING_MAXIMUM. Averaging steadies the noise floor so weak signals stand out, but the waterfall scrolls
* more slowly.
*/
public IntegerProperty waterfallAveragingProperty()
{
return mWaterfallAveraging;
}
/**
* Waterfall gain slider along the right edge of the waterfall section.
*/
public EdgeSlider getWaterfallRangeSlider()
{
return mWaterfallRangeSlider;
}
/**
* Adds a control on top of the spectrum or the waterfall section, for example an EdgeSlider. Set its StackPane
* alignment and margin before adding it. The control is hidden with its section.
*/
public void addSectionOverlay(DisplaySection section, javafx.scene.Node node)
{
(section == DisplaySection.SPECTRUM ? mSpectrumSection : mWaterfallSection).getChildren().add(node);
}
/**
* Divider position (0 - 1.0) between the spectrum and the waterfall when both are showing.
*/
@@ -395,6 +474,7 @@ public class SpectralDisplay extends StackPane
public void clear()
{
mAverager.reset();
mWaterfallScaler.reset();
mLatestSpectrum.set(null);
mWaterfallRows.clear();
@@ -412,10 +492,11 @@ public class SpectralDisplay extends StackPane
return;
}
//The first DFT results on startup can be NaN or infinite
//The first DFT results on startup can be NaN or infinite - drop them. Replacing them with zeros would add
//full scale (0 dBFS) values to the spectrum averaging and the waterfall floor tracking.
if(Float.isNaN(bins[0]) || Float.isInfinite(bins[0]))
{
bins = new float[bins.length];
return;
}
if(mSpectrumActive)
@@ -425,11 +506,16 @@ public class SpectralDisplay extends StackPane
if(mWaterfallActive && !mWaterfallPausedFlag)
{
mWaterfallRows.offer(toWaterfallRow(bins));
byte[] row = mWaterfallScaler.process(bins);
while(mWaterfallRows.size() > WATERFALL_QUEUE_LIMIT)
if(row != null)
{
mWaterfallRows.poll();
mWaterfallRows.offer(row);
while(mWaterfallRows.size() > WATERFALL_QUEUE_LIMIT)
{
mWaterfallRows.poll();
}
}
}
}
@@ -452,33 +538,7 @@ public class SpectralDisplay extends StackPane
mLatestSpectrum.set(null);
mWaterfallRows.clear();
mWaterfallTiles = null;
mWaterfallIndexes = null;
}
/**
* Converts the DFT results to a row of waterfall intensity indexes (0 - 255). Each bin is scaled relative to the
* average bin value so the noise floor stays at the bottom of the palette regardless of tuner gain.
*/
private static byte[] toWaterfallRow(float[] bins)
{
double sum = 0.0;
for(float bin: bins)
{
sum += bin;
}
float average = (float)(sum / bins.length) - 1.0f;
float scale = 256.0f / average;
byte[] row = new byte[bins.length];
for(int x = 0; x < bins.length; x++)
{
int value = (int)((average - bins[x]) * scale);
row[x] = (byte)Math.max(0, Math.min(255, value));
}
return row;
mWaterfallLevels = null;
}
/**
@@ -491,11 +551,11 @@ public class SpectralDisplay extends StackPane
mSpectrumActive = spectrum;
mWaterfallActive = waterfall;
List<Pane> items = new ArrayList<>();
List<StackPane> items = new ArrayList<>();
if(spectrum)
{
items.add(mSpectrumPane);
items.add(mSpectrumSection);
}
else
{
@@ -505,13 +565,16 @@ public class SpectralDisplay extends StackPane
if(waterfall)
{
items.add(mWaterfallPane);
items.add(mWaterfallSection);
}
else
{
mWaterfallRows.clear();
}
//Keep the waterfall slider below the configuration panel tab when the waterfall is at the top
StackPane.setMargin(mWaterfallRangeSlider, new Insets(spectrum ? 8 : 44, 0, 8, 0));
mSplitPane.getItems().setAll(items);
mSplitPane.setVisible(!items.isEmpty());
@@ -543,7 +606,7 @@ public class SpectralDisplay extends StackPane
if(mWaterfallTiles != null)
{
mWaterfallTiles = null;
mWaterfallIndexes = null;
mWaterfallLevels = null;
mWaterfallWriteRow = 0;
}
@@ -581,8 +644,8 @@ public class SpectralDisplay extends StackPane
/**
* Writes the row into the waterfall ring buffer. The newest row is at mWaterfallWriteRow and older rows follow
* it, wrapping around the end of the image. The intensity indexes are kept alongside the image so the history
* can be recolored when the theme changes.
* it, wrapping around the end of the image. The level codes are kept alongside the image so the history
* can be recolored when the theme or the waterfall range changes.
*/
private void writeWaterfallRow(byte[] row)
{
@@ -590,7 +653,7 @@ public class SpectralDisplay extends StackPane
if(mWaterfallTiles == null || mWaterfallWidth != row.length)
{
mWaterfallIndexes = new byte[row.length * rows];
mWaterfallLevels = new byte[row.length * rows];
createWaterfallTiles(row.length, rows);
mWaterfallWriteRow = 0;
repaintWaterfallImage();
@@ -602,7 +665,7 @@ public class SpectralDisplay extends StackPane
int width = row.length;
mWaterfallWriteRow = (mWaterfallWriteRow - 1 + mWaterfallHeight) % mWaterfallHeight;
System.arraycopy(row, 0, mWaterfallIndexes, mWaterfallWriteRow * width, width);
System.arraycopy(row, 0, mWaterfallLevels, mWaterfallWriteRow * width, width);
writeWaterfallPixels(mWaterfallWriteRow);
}
@@ -625,7 +688,7 @@ public class SpectralDisplay extends StackPane
}
/**
* Colors one row of the waterfall image tiles from the stored intensity indexes.
* Colors one row of the waterfall image tiles from the stored level codes.
*/
private void writeWaterfallPixels(int imageRow)
{
@@ -636,12 +699,12 @@ public class SpectralDisplay extends StackPane
mWaterfallPixels = new int[width];
}
SpectralDisplayTheme theme = getTheme();
int[] colors = getWaterfallColors();
int offset = imageRow * width;
for(int x = 0; x < width; x++)
{
mWaterfallPixels[x] = theme.getWaterfallColor(mWaterfallIndexes[offset + x]);
mWaterfallPixels[x] = colors[mWaterfallLevels[offset + x] & 0xFF];
}
for(int tile = 0; tile < mWaterfallTiles.length; tile++)
@@ -653,11 +716,33 @@ public class SpectralDisplay extends StackPane
}
/**
* Recolors the whole waterfall image from the stored intensity indexes using the current theme.
* ARGB color for each waterfall level code, for the current theme and range.
*/
private int[] getWaterfallColors()
{
if(mWaterfallColors == null)
{
SpectralDisplayTheme theme = getTheme();
int range = mWaterfallRange.get();
int[] colors = new int[256];
for(int level = 0; level < colors.length; level++)
{
colors[level] = theme.getWaterfallColor(WaterfallScaler.paletteIndex(level, range));
}
mWaterfallColors = colors;
}
return mWaterfallColors;
}
/**
* Recolors the whole waterfall image from the stored level codes using the current theme and range.
*/
private void repaintWaterfallImage()
{
if(mWaterfallTiles != null && mWaterfallIndexes != null)
if(mWaterfallTiles != null && mWaterfallLevels != null)
{
for(int y = 0; y < mWaterfallHeight; y++)
{
@@ -689,10 +774,10 @@ public class SpectralDisplay extends StackPane
for(int y = 0; y < previousHeight; y++)
{
int source = (mWaterfallWriteRow + y) % previousHeight;
System.arraycopy(mWaterfallIndexes, source * width, indexes, y * width, width);
System.arraycopy(mWaterfallLevels, source * width, indexes, y * width, width);
}
mWaterfallIndexes = indexes;
mWaterfallLevels = indexes;
createWaterfallTiles(width, rows);
mWaterfallWriteRow = 0;
repaintWaterfallImage();

View File

@@ -353,6 +353,27 @@ public class SpectralDisplayConfigPanel extends VBox
"falling slowly"));
row = addRow(grid, row, peakHold.label(), peakHold.control());
//Waterfall
grid.add(sectionLabel("Waterfall"), 0, row++, 2, 1);
ComboBox<Integer> waterfallAveraging = new ComboBox<>();
for(int frames = WaterfallScaler.AVERAGING_MINIMUM; frames <= WaterfallScaler.AVERAGING_MAXIMUM; frames++)
{
waterfallAveraging.getItems().add(frames);
}
waterfallAveraging.setMaxWidth(Double.MAX_VALUE);
waterfallAveraging.setConverter(converter(frames -> frames == 1 ? "Off" : frames + " frames"));
waterfallAveraging.setValue(mSpectralDisplay.waterfallAveragingProperty().get());
waterfallAveraging.valueProperty().addListener((ob, ov, nv) -> {
if(nv != null)
{
mSpectralDisplay.waterfallAveragingProperty().set(nv);
}
});
waterfallAveraging.setTooltip(new Tooltip("FFT frames averaged into each waterfall row. Averaging steadies " +
"the noise floor so weak signals stand out, but the waterfall scrolls more slowly."));
trackPopup(waterfallAveraging.showingProperty());
row = addRow(grid, row, "Averaging", waterfallAveraging);
//Layer settings
for(SpectralDisplayLayer layer: mSpectralDisplay.getLayers())
{

View File

@@ -33,44 +33,31 @@ public class WaterfallPalette
}
/**
* Palette of the original Swing waterfall: dark blue noise floor, a blue to yellow ramp, yellow fading to orange,
* then red.
* Palette based on the original Swing waterfall colors: a dark to bright blue ramp, a blue to yellow ramp, yellow
* fading to orange, then red. The blue ramp covers the bottom quarter of the palette, where the waterfall places
* the noise floor, so the noise stays blue and weak signals stand out from it.
*/
public static int[] classic()
{
int[] palette = new int[SIZE];
for(int x = 0; x < 16; x++)
for(int x = 0; x < 64; x++)
{
palette[x] = argb(0, 0, 127);
palette[x] = argb(0, 0, blend(64, 191, x / 63.0));
}
for(int x = 16; x < 32; x++)
for(int x = 64; x < 100; x++)
{
palette[x] = argb(0, 0, 191);
double t = (x - 64) / 36.0;
palette[x] = argb(blend(0, 255, t), blend(0, 255, t), blend(191, 0, t));
}
int r = 0;
int g = 0;
int b = 191;
for(int x = 32; x < 60; x++)
for(int x = 100; x < 220; x++)
{
palette[x] = argb(r, g, b);
r += 9;
g += 9;
b -= 6;
palette[x] = argb(255, blend(255, 0, (x - 100) / 120.0), 0);
}
g = 255;
for(int x = 60; x < 188; x++)
{
palette[x] = argb(255, g, 0);
g -= 2;
}
for(int x = 188; x < SIZE; x++)
for(int x = 220; x < SIZE; x++)
{
palette[x] = argb(255, 0, 0);
}
@@ -78,6 +65,11 @@ public class WaterfallPalette
return palette;
}
private static int blend(int from, int to, double fraction)
{
return (int)Math.round(from + (to - from) * fraction);
}
/**
* Palette that blends linearly between evenly spaced colors.
* @param colors web color strings (for example #440154), lowest intensity first

View File

@@ -0,0 +1,289 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2026 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
* ****************************************************************************
*/
package io.github.dsheirer.gui.spectrum;
import java.util.Arrays;
/**
* Converts DFT results in dB to rows of waterfall level codes, and level codes to palette indexes.
*
* Optionally averages several DFT frames (in linear power) into each row. Averaging reduces the random variation of
* the noise bins (about 5.6 dB for a single frame), which is the only thing that makes weak signals statistically
* easier to see, at the cost of fewer rows per second.
*
* The noise floor reference is a low percentile of the bins, so signals that fill much of the span don't raise it,
* smoothed across rows so the waterfall background doesn't flicker.
*
* Each row stores a level code per bin: the bin level relative to the floor reference in LEVEL_STEP_DB steps. The
* level codes are mapped to palette indexes over a dB range above the floor (see paletteIndex()) when the row is
* colored, so the range can change and the waterfall history can be recolored without the original DFT results.
*
* Not thread safe: process() on the DFT results thread only. reset() and setAveraging() are safe from any thread.
*/
public class WaterfallScaler
{
public static final int RANGE_MINIMUM = 20;
public static final int RANGE_MAXIMUM = 80;
public static final int RANGE_DEFAULT = 45;
public static final int AVERAGING_MINIMUM = 1;
public static final int AVERAGING_MAXIMUM = 4;
public static final int AVERAGING_DEFAULT = 2;
//Percentile of the bins used as the noise floor reference. Robust to signals in up to 75% of the bins.
static final double FLOOR_PERCENTILE = 0.25;
//Smoothing applied to the floor reference each row: about 5 rows. The percentile barely varies from row to row,
//so this mostly limits jumps from bursts of signals while still following tuner gain changes quickly.
static final float FLOOR_SMOOTHING = 0.2f;
//Floor reference changes larger than this jump straight to the new level instead of smoothing, for example when
//the first samples arrive after DFT frames of an empty buffer (-196 dB), or after a tuner or gain change.
static final float FLOOR_JUMP_DB = 10.0f;
//Level code resolution and the level code of the floor reference: codes cover -8 dB to +94 dB from the floor,
//which covers the palette at the maximum range.
static final float LEVEL_STEP_DB = 0.4f;
static final int FLOOR_LEVEL = 20;
//Palette index for the floor reference. Noise bins spread above the 25th percentile, so this places the noise in
//the lower part of the palette as texture with only the weakest noise bins at the background color.
static final float FLOOR_INDEX = 16.0f;
private static final float DB_TO_LN_POWER = (float)(Math.log(10.0) / 10.0);
private float mFloor = Float.NaN;
private volatile boolean mResetRequested;
private volatile int mAveraging = AVERAGING_DEFAULT;
private float[] mScratch = new float[0];
private float[] mPowerSum = new float[0];
private float[] mAverage = new float[0];
private int mFrameCount;
/**
* Restarts floor tracking and frame averaging on the next frame, for example after the source changes. Safe to
* call from any thread.
*/
public void reset()
{
mResetRequested = true;
}
/**
* Sets the number of DFT frames averaged into each waterfall row. Safe to call from any thread.
* @param frames from AVERAGING_MINIMUM to AVERAGING_MAXIMUM
*/
public void setAveraging(int frames)
{
mAveraging = Math.max(AVERAGING_MINIMUM, Math.min(AVERAGING_MAXIMUM, frames));
}
/**
* Current noise floor reference in dB, or NaN before the first row.
*/
public float getFloor()
{
return mFloor;
}
/**
* Adds the DFT results to the current row.
* @param bins in dB
* @return the row of level codes (unsigned bytes) when the row is complete, otherwise null
*/
public byte[] process(float[] bins)
{
if(mResetRequested)
{
mResetRequested = false;
mFloor = Float.NaN;
mFrameCount = 0;
}
int frames = mAveraging;
if(frames <= 1 && mFrameCount == 0)
{
return levels(bins);
}
if(mPowerSum.length != bins.length)
{
mPowerSum = new float[bins.length];
mAverage = new float[bins.length];
mFrameCount = 0;
}
if(mFrameCount == 0)
{
Arrays.fill(mPowerSum, 0.0f);
}
for(int x = 0; x < bins.length; x++)
{
mPowerSum[x] += (float)Math.exp(bins[x] * DB_TO_LN_POWER);
}
mFrameCount++;
//The averaging can drop below the frames already summed - complete the row with what has been summed
if(mFrameCount < frames)
{
return null;
}
float scale = 1.0f / mFrameCount;
for(int x = 0; x < bins.length; x++)
{
mAverage[x] = (float)(10.0 * Math.log10(mPowerSum[x] * scale));
}
mFrameCount = 0;
return levels(mAverage);
}
/**
* Updates the floor reference and converts the bins to level codes.
*/
private byte[] levels(float[] bins)
{
float percentile = percentile(bins);
if(Float.isNaN(mFloor) || Float.isInfinite(mFloor) || Math.abs(percentile - mFloor) > FLOOR_JUMP_DB)
{
mFloor = percentile;
}
else
{
mFloor += (percentile - mFloor) * FLOOR_SMOOTHING;
}
byte[] row = new byte[bins.length];
float floor = mFloor;
for(int x = 0; x < bins.length; x++)
{
int level = Math.round((bins[x] - floor) / LEVEL_STEP_DB) + FLOOR_LEVEL;
row[x] = (byte)Math.max(0, Math.min(255, level));
}
return row;
}
/**
* Palette index for a level code.
* @param level code (0 - 255)
* @param range in dB from the noise floor reference to the top of the palette. Smaller ranges give weak signals
* more contrast against the noise and saturate strong signals sooner.
* @return palette index (0 - 255)
*/
public static int paletteIndex(int level, double range)
{
double db = (level - FLOOR_LEVEL) * LEVEL_STEP_DB;
long index = Math.round(db * (WaterfallPalette.SIZE - 1) / range + FLOOR_INDEX);
return (int)Math.max(0, Math.min(WaterfallPalette.SIZE - 1, index));
}
/**
* Floor reference percentile of the bins, using quickselect on a scratch copy.
*/
private float percentile(float[] bins)
{
if(mScratch.length != bins.length)
{
mScratch = new float[bins.length];
}
System.arraycopy(bins, 0, mScratch, 0, bins.length);
return select(mScratch, (int)(FLOOR_PERCENTILE * (bins.length - 1)));
}
/**
* Quickselect: the k-th smallest value, partially reordering the array.
*/
static float select(float[] values, int k)
{
int left = 0;
int right = values.length - 1;
while(left < right)
{
//Median of three pivot
int middle = (left + right) >>> 1;
if(values[middle] < values[left])
{
swap(values, middle, left);
}
if(values[right] < values[left])
{
swap(values, right, left);
}
if(values[right] < values[middle])
{
swap(values, right, middle);
}
float pivot = values[middle];
int i = left;
int j = right;
while(i <= j)
{
while(values[i] < pivot)
{
i++;
}
while(values[j] > pivot)
{
j--;
}
if(i <= j)
{
swap(values, i, j);
i++;
j--;
}
}
if(k <= j)
{
right = j;
}
else if(k >= i)
{
left = i;
}
else
{
return values[k];
}
}
return values[k];
}
private static void swap(float[] values, int a, int b)
{
float temp = values[a];
values[a] = values[b];
values[b] = temp;
}
}