#1310 NBFM decoder uses new Noise Squelch and includes new noise squelch view and control. (#2264)

Co-authored-by: denny <dennis.sheirer@gmail.com>
This commit is contained in:
Denny Sheirer
2025-08-02 08:34:32 -04:00
committed by GitHub
parent 912925ee7d
commit b2be38b239
32 changed files with 2999 additions and 858 deletions

View File

@@ -447,18 +447,19 @@ public class DecoderFactory
* @param aliasList for the channel
* @param decodeConfig for the channel
*/
private static void processAM(Channel channel, List<Module> modules, AliasList aliasList, DecodeConfiguration decodeConfig) {
if(!(decodeConfig instanceof DecodeConfigAM))
private static void processAM(Channel channel, List<Module> modules, AliasList aliasList, DecodeConfiguration decodeConfig)
{
if(decodeConfig instanceof DecodeConfigAM configAM)
{
modules.add(new AMDecoder(configAM));
modules.add(new AMDecoderState(channel.getName(), configAM));
modules.add(new AudioModule(aliasList, 0, 60000, AUDIO_FILTER_ENABLE));
}
else
{
throw new IllegalArgumentException("Can't create AM decoder - unrecognized decode config type: " +
(decodeConfig != null ? decodeConfig.getClass() : "null/empty"));
}
DecodeConfigAM decodeConfigAM = (DecodeConfigAM) decodeConfig;
modules.add(new AMDecoder(decodeConfigAM));
modules.add(new AMDecoderState(channel.getName(), decodeConfigAM));
modules.add(new AudioModule(aliasList, 0, 60000, AUDIO_FILTER_ENABLE));
}
/**
@@ -758,9 +759,12 @@ public class DecoderFactory
case NBFM:
DecodeConfigNBFM origNBFM = (DecodeConfigNBFM)config;
DecodeConfigNBFM copyNBFM = new DecodeConfigNBFM();
copyNBFM.setAudioFilter(origNBFM.isAudioFilter());
copyNBFM.setBandwidth(origNBFM.getBandwidth());
copyNBFM.setSquelchThreshold(origNBFM.getSquelchThreshold());
copyNBFM.setSquelchAutoTrack(origNBFM.isSquelchAutoTrack());
copyNBFM.setSquelchHysteresisCloseThreshold(origNBFM.getSquelchHysteresisCloseThreshold());
copyNBFM.setSquelchHysteresisOpenThreshold(origNBFM.getSquelchHysteresisOpenThreshold());
copyNBFM.setSquelchNoiseOpenThreshold(origNBFM.getSquelchNoiseOpenThreshold());
copyNBFM.setSquelchNoiseCloseThreshold(origNBFM.getSquelchNoiseCloseThreshold());
copyNBFM.setTalkgroup(origNBFM.getTalkgroup());
return copyNBFM;
case P25_PHASE1:

View File

@@ -1,6 +1,6 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2023 Dennis Sheirer
* Copyright (C) 2014-2025 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
@@ -16,34 +16,76 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
* ****************************************************************************
*/
package io.github.dsheirer.module.decode.am;
import io.github.dsheirer.channel.state.DecoderStateEvent;
import io.github.dsheirer.channel.state.IDecoderStateEventProvider;
import io.github.dsheirer.channel.state.State;
import io.github.dsheirer.dsp.am.SquelchingAMDemodulator;
import io.github.dsheirer.dsp.filter.FilterFactory;
import io.github.dsheirer.dsp.filter.decimate.DecimationFilterFactory;
import io.github.dsheirer.dsp.filter.decimate.IRealDecimationFilter;
import io.github.dsheirer.dsp.filter.design.FilterDesignException;
import io.github.dsheirer.dsp.filter.fir.FIRFilterSpecification;
import io.github.dsheirer.dsp.filter.fir.real.IRealFilter;
import io.github.dsheirer.dsp.filter.resample.RealResampler;
import io.github.dsheirer.dsp.fm.ISquelchingDemodulator;
import io.github.dsheirer.dsp.gain.AudioGainAndDcFilter;
import io.github.dsheirer.dsp.squelch.INoiseSquelchController;
import io.github.dsheirer.dsp.squelch.NoiseSquelch;
import io.github.dsheirer.dsp.squelch.NoiseSquelchState;
import io.github.dsheirer.dsp.window.WindowType;
import io.github.dsheirer.module.decode.DecoderType;
import io.github.dsheirer.module.decode.analog.SquelchingAnalogDecoder;
import io.github.dsheirer.module.decode.PrimaryDecoder;
import io.github.dsheirer.sample.Listener;
import io.github.dsheirer.sample.complex.ComplexSamples;
import io.github.dsheirer.sample.complex.IComplexSamplesListener;
import io.github.dsheirer.sample.real.IRealBufferProvider;
import io.github.dsheirer.source.ISourceEventListener;
import io.github.dsheirer.source.ISourceEventProvider;
import io.github.dsheirer.source.SourceEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Analog AM radio decoder module with integrated squelching control
* AM decoder module with integrated squelch control
*/
public class AMDecoder extends SquelchingAnalogDecoder
public class AMDecoder extends PrimaryDecoder implements ISourceEventListener, ISourceEventProvider,
IComplexSamplesListener, Listener<ComplexSamples>, IRealBufferProvider,
IDecoderStateEventProvider, INoiseSquelchController
{
private final static Logger mLog = LoggerFactory.getLogger(AMDecoder.class);
private static final float DEMODULATOR_GAIN = 150.0f;
private static final float SQUELCH_ALPHA_DECAY = 0.0004f;
private static final float MINIMUM_GAIN = 0.5f;
private static final float MAXIMUM_GAIN = 16.0f;
private static final float OBJECTIVE_AUDIO_AMPLITUDE = 0.75f;
private AudioGainAndDcFilter mAGC = new AudioGainAndDcFilter(MINIMUM_GAIN, MAXIMUM_GAIN, OBJECTIVE_AUDIO_AMPLITUDE);
private static final double DEMODULATED_AUDIO_SAMPLE_RATE = 8000.0;
private IRealFilter mIBasebandFilter;
private IRealFilter mQBasebandFilter;
private IRealDecimationFilter mIDecimationFilter;
private IRealDecimationFilter mQDecimationFilter;
private final ISquelchingDemodulator mDemodulator;
private RealResampler mResampler;
private final SourceEventProcessor mSourceEventProcessor = new SourceEventProcessor();
private Listener<float[]> mResampledBufferListener;
private Listener<DecoderStateEvent> mDecoderStateEventListener;
private final double mChannelBandwidth;
protected boolean mSquelch = true;
private final NoiseSquelch mNoiseSquelch = new NoiseSquelch(0.1f, 0.2f, 4, 6);
/**
* Constructs an instance
* @param config for the AM channel.
*
* @param config to setup the AM decoder
*/
public AMDecoder(DecodeConfigAM config)
{
super(config, new SquelchingAMDemodulator(DEMODULATOR_GAIN, SQUELCH_ALPHA_DECAY,
config.getSquelchThreshold(), config.isSquelchAutoTrack()));
super(config);
mDemodulator = new SquelchingAMDemodulator(DEMODULATOR_GAIN, SQUELCH_ALPHA_DECAY, config.getSquelchThreshold(),
config.isSquelchAutoTrack());
mChannelBandwidth = config.getBandwidth().getValue();
}
@Override
@@ -52,24 +94,365 @@ public class AMDecoder extends SquelchingAnalogDecoder
return DecoderType.AM;
}
/**
* Overrides the broadcast of the final demodulated audio samples so that we can apply AGC
* @param demodulatedSamples to broadcast
*/
@Override
protected void broadcast(float[] demodulatedSamples)
public void setSquelchOverride(boolean override)
{
//Apply audio gain and rebroadcast
super.broadcast(mAGC.process(demodulatedSamples));
mNoiseSquelch.setSquelchOverride(override);
}
@Override
public void setHysteresisThreshold(int open, int close)
{
mNoiseSquelch.setHysteresisThreshold(open, close);
}
@Override
public void setNoiseThreshold(float open, float close)
{
mNoiseSquelch.setNoiseThreshold(open, close);
}
@Override
public void setNoiseSquelchStateListener(Listener<NoiseSquelchState> listener)
{
mNoiseSquelch.setNoiseSquelchStateListener(listener);
}
/**
* Overrides the notify call start so that we can reset the AGC
* Implements the ISourceEventListener interface
*/
@Override
public Listener<SourceEvent> getSourceEventListener()
{
return mSourceEventProcessor;
}
/**
* Module interface methods - unused.
*/
@Override
public void reset()
{
}
@Override
public void start()
{
}
@Override
public void stop()
{
}
/**
* Broadcasts the demodulated audio samples to the registered listener.
* @param demodulatedSamples to broadcast
*/
protected void broadcast(float[] demodulatedSamples)
{
if(mResampledBufferListener != null)
{
mResampledBufferListener.receive(demodulatedSamples);
// //Apply audio gain and rebroadcast
// super.broadcast(mAGC.process(demodulatedSamples));
}
}
/**
* Implements the IRealBufferProvider interface to register a listener for demodulated audio samples.
*
* @param listener to receive demodulated audio sample buffers.
*/
@Override
public void setBufferListener(Listener<float[]> listener)
{
mResampledBufferListener = listener;
}
/**
* Implements the IRealBufferProvider interface to deregister a listener from receiving demodulated audio samples.
*/
@Override
public void removeBufferListener()
{
mResampledBufferListener = null;
}
/**
* Implements the IComplexSampleListener interface to receive a stream of complex sample buffers.
*/
@Override
public Listener<ComplexSamples> getComplexSamplesListener()
{
return this;
}
/**
* Implements the Listener<ComplexSample> interface to receive a stream of complex sample buffers
*/
@Override
public void receive(ComplexSamples samples)
{
if(mIDecimationFilter == null || mQDecimationFilter == null)
{
throw new IllegalStateException("NBFM demodulator module must receive a sample rate change source " +
"event before it can process complex sample buffers");
}
float[] decimatedI = mIDecimationFilter.decimateReal(samples.i());
float[] decimatedQ = mQDecimationFilter.decimateReal(samples.q());
float[] filteredI = mIBasebandFilter.filter(decimatedI);
float[] filteredQ = mQBasebandFilter.filter(decimatedQ);
float[] demodulated = mDemodulator.demodulate(filteredI, filteredQ);
mNoiseSquelch.process(demodulated);
if(mResampler != null)
{
//Squelch changed while processing this audio buffer
if(mDemodulator.isSquelchChanged())
{
if(mDemodulator.isMuted())
{
//Demodulator says to mute and we're already muted/squelched = Continue IDLE
if(mSquelch)
{
notifyIdle();
}
//Demodulator says to mute and we are unmuted = Squelch and End Call
else
{
mSquelch = true;
notifyCallEnd();
}
}
else
{
//Demodulator says to unmute and we're muted/squelched = Unmute and Start Call
if(mSquelch)
{
mSquelch = false;
notifyCallStart();
}
//Demodulator says to unmute and we're already unmuted = Continue Call
else
{
notifyCallContinuation();
}
mResampler.resample(demodulated);
}
}
else
{
//Demodulator says squelch state didn't change and we're muted/squelched = Continue IDLE
if(mSquelch)
{
notifyIdle();
}
//Demodulator says squelch state didn't change and we're unmuted/unsquelched = Continue CALL
else
{
notifyCallContinuation();
mResampler.resample(demodulated);
}
}
}
else
{
//This shouldn't happen
notifyIdle();
}
}
/**
* Broadcasts a call start state event
*/
protected void notifyCallStart()
{
mAGC.reset();
super.notifyCallStart();
broadcast(new DecoderStateEvent(this, DecoderStateEvent.Event.START, State.CALL, 0));
}
/**
* Broadcasts a call continuation state event
*/
private void notifyCallContinuation()
{
broadcast(new DecoderStateEvent(this, DecoderStateEvent.Event.CONTINUATION, State.CALL, 0));
}
/**
* Broadcasts a call end state event
*/
private void notifyCallEnd()
{
broadcast(new DecoderStateEvent(this, DecoderStateEvent.Event.END, State.CALL, 0));
}
/**
* Broadcasts an idle notification
*/
private void notifyIdle()
{
broadcast(new DecoderStateEvent(this, DecoderStateEvent.Event.CONTINUATION, State.IDLE, 0));
}
/**
* Broadcasts the decoder state event to an optional registered listener
*/
private void broadcast(DecoderStateEvent event)
{
if(mDecoderStateEventListener != null)
{
mDecoderStateEventListener.receive(event);
}
}
/**
* Sets the decoder state listener
*/
@Override
public void setDecoderStateListener(Listener<DecoderStateEvent> listener)
{
mDecoderStateEventListener = listener;
}
/**
* Removes the decoder state event listener
*/
@Override
public void removeDecoderStateListener()
{
mDecoderStateEventListener = null;
}
/**
* Registers the listener to receive squelch change events from the demodulator/squelch controller.
*/
@Override
public void setSourceEventListener(Listener<SourceEvent> listener)
{
mDemodulator.setSourceEventListener(listener);
}
/**
* De-registers the listener
*/
@Override
public void removeSourceEventListener()
{
mDemodulator.setSourceEventListener(null);
}
/**
* Monitors sample rate change source event(s) to setup the initial I/Q filter and passes squelch threshold
* change requests down to the demodulator.
*/
public class SourceEventProcessor implements Listener<SourceEvent>
{
@Override
public void receive(SourceEvent sourceEvent)
{
switch(sourceEvent.getEvent())
{
//Forward these requests to the sub-class implementation (AM or FM demod) to handle
case REQUEST_CURRENT_SQUELCH_THRESHOLD:
case REQUEST_CHANGE_SQUELCH_THRESHOLD:
case REQUEST_CURRENT_SQUELCH_AUTO_TRACK:
case REQUEST_CHANGE_SQUELCH_AUTO_TRACK:
mDemodulator.receive(sourceEvent);
break;
case NOTIFICATION_SAMPLE_RATE_CHANGE:
if(mIBasebandFilter != null)
{
mIBasebandFilter = null;
mQBasebandFilter = null;
}
double sampleRate = sourceEvent.getValue().doubleValue();
int decimationRate = 0;
double decimatedSampleRate = sampleRate;
if(sampleRate / 2 >= (mChannelBandwidth * 2))
{
decimationRate = 2;
while(sampleRate / decimationRate / 2 >= (mChannelBandwidth * 2))
{
decimationRate *= 2;
}
}
if(decimationRate > 0)
{
decimatedSampleRate /= decimationRate;
}
mIDecimationFilter = DecimationFilterFactory.getRealDecimationFilter(decimationRate);
mQDecimationFilter = DecimationFilterFactory.getRealDecimationFilter(decimationRate);
if((decimatedSampleRate < (2.0 * mChannelBandwidth)))
{
throw new IllegalStateException(getDecoderType().name() + " demodulator with channel bandwidth [" +
mChannelBandwidth + "] requires a channel sample rate of [" + (2.0 * mChannelBandwidth +
"] - sample rate of [" + decimatedSampleRate + "] is not supported"));
}
mDemodulator.setSampleRate((int) decimatedSampleRate);
mNoiseSquelch.setSampleRate(decimatedSampleRate);
int passBandStop = (int) (mChannelBandwidth * .8);
int stopBandStart = (int) mChannelBandwidth;
float[] coefficients = null;
FIRFilterSpecification specification = FIRFilterSpecification.lowPassBuilder()
.sampleRate(decimatedSampleRate * 2)
.gridDensity(16)
.oddLength(true)
.passBandCutoff(passBandStop)
.passBandAmplitude(1.0)
.passBandRipple(0.01)
.stopBandStart(stopBandStart)
.stopBandAmplitude(0.0)
.stopBandRipple(0.005) //Approximately 90 dB attenuation
.build();
try
{
coefficients = FilterFactory.getTaps(specification);
}
catch(FilterDesignException fde)
{
mLog.error("Couldn't design demodulator remez filter for sample rate [" + sampleRate +
"] pass frequency [" + passBandStop + "] and stop frequency [" + stopBandStart +
"] - will proceed using sinc (low-pass) filter");
}
if(coefficients == null)
{
mLog.info("Unable to use remez filter designer for sample rate [" + decimatedSampleRate +
"] pass band stop [" + passBandStop +
"] and stop band start [" + stopBandStart + "] - will proceed using simple low pass filter design");
coefficients = FilterFactory.getLowPass(decimatedSampleRate, passBandStop, stopBandStart, 60,
WindowType.HAMMING, true);
}
mIBasebandFilter = FilterFactory.getRealFilter(coefficients);
mQBasebandFilter = FilterFactory.getRealFilter(coefficients);
mResampler = new RealResampler(decimatedSampleRate, DEMODULATED_AUDIO_SAMPLE_RATE, 4192, 512);
mResampler.setListener(resampled -> broadcast(resampled));
break;
}
}
}
}

View File

@@ -1,6 +1,6 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2023 Dennis Sheirer
* Copyright (C) 2014-2025 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
@@ -20,6 +20,7 @@ package io.github.dsheirer.module.decode.am;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import io.github.dsheirer.dsp.squelch.ISquelchConfiguration;
import io.github.dsheirer.module.decode.DecoderType;
import io.github.dsheirer.module.decode.analog.DecodeConfigAnalog;
import io.github.dsheirer.source.tuner.channel.ChannelSpecification;
@@ -27,8 +28,11 @@ import io.github.dsheirer.source.tuner.channel.ChannelSpecification;
/**
* AM decoder configuration
*/
public class DecodeConfigAM extends DecodeConfigAnalog
public class DecodeConfigAM extends DecodeConfigAnalog implements ISquelchConfiguration
{
private int mSquelchThreshold = -78;
private boolean mSquelchAutoTrack = true;
public DecodeConfigAM()
{
}
@@ -68,4 +72,46 @@ public class DecodeConfigAM extends DecodeConfigAnalog
throw new IllegalArgumentException("Unrecognized AM bandwidth value: " + getBandwidth());
}
}
/**
* Sets squelch threshold
* @param threshold (dB)
*/
@JacksonXmlProperty(isAttribute = true, localName = "squelch")
@Override
public void setSquelchThreshold(int threshold)
{
mSquelchThreshold = threshold;
}
/**
* Squelch threshold
* @return threshold (dB)
*/
@Override
public int getSquelchThreshold()
{
return mSquelchThreshold;
}
/**
* Enable or disable the squelch noise floor auto-track feature.
* @param autoTrack true to enable.
*/
@JacksonXmlProperty(isAttribute = true, localName = "autoTrack")
@Override
public void setSquelchAutoTrack(boolean autoTrack)
{
mSquelchAutoTrack = autoTrack;
}
/**
* Indicates if the squelch noise floor auto-track feature is enabled.
* @return true if enabled.
*/
@Override
public boolean isSquelchAutoTrack()
{
return mSquelchAutoTrack;
}
}

View File

@@ -1,6 +1,6 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2023 Dennis Sheirer
* Copyright (C) 2014-2025 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
@@ -20,7 +20,6 @@ package io.github.dsheirer.module.decode.analog;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import io.github.dsheirer.dsp.squelch.ISquelchConfiguration;
import io.github.dsheirer.module.decode.config.DecodeConfiguration;
import io.github.dsheirer.source.tuner.channel.ChannelSpecification;
import java.util.EnumSet;
@@ -28,12 +27,10 @@ import java.util.EnumSet;
/**
* Decoder configuration for an squelching analog channel
*/
public abstract class DecodeConfigAnalog extends DecodeConfiguration implements ISquelchConfiguration
public abstract class DecodeConfigAnalog extends DecodeConfiguration
{
private Bandwidth mBandwidth;
private int mTalkgroup = 1;
private int mSquelchThreshold = -78;
private boolean mSquelchAutoTrack = true;
/**
* Constructs an instance
@@ -99,48 +96,6 @@ public abstract class DecodeConfigAnalog extends DecodeConfiguration implements
mTalkgroup = talkgroup;
}
/**
* Sets squelch threshold
* @param threshold (dB)
*/
@JacksonXmlProperty(isAttribute = true, localName = "squelch")
@Override
public void setSquelchThreshold(int threshold)
{
mSquelchThreshold = threshold;
}
/**
* Squelch threshold
* @return threshold (dB)
*/
@Override
public int getSquelchThreshold()
{
return mSquelchThreshold;
}
/**
* Enable or disable the squelch noise floor auto-track feature.
* @param autoTrack true to enable.
*/
@JacksonXmlProperty(isAttribute = true, localName = "autoTrack")
@Override
public void setSquelchAutoTrack(boolean autoTrack)
{
mSquelchAutoTrack = autoTrack;
}
/**
* Indicates if the squelch noise floor auto-track feature is enabled.
* @return true if enabled.
*/
@Override
public boolean isSquelchAutoTrack()
{
return mSquelchAutoTrack;
}
public enum Bandwidth
{
BW_3_0("3.0 kHz", 3000.0),

View File

@@ -1,410 +0,0 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2023 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.module.decode.analog;
import io.github.dsheirer.channel.state.DecoderStateEvent;
import io.github.dsheirer.channel.state.IDecoderStateEventProvider;
import io.github.dsheirer.channel.state.State;
import io.github.dsheirer.dsp.filter.FilterFactory;
import io.github.dsheirer.dsp.filter.decimate.DecimationFilterFactory;
import io.github.dsheirer.dsp.filter.decimate.IRealDecimationFilter;
import io.github.dsheirer.dsp.filter.design.FilterDesignException;
import io.github.dsheirer.dsp.filter.fir.FIRFilterSpecification;
import io.github.dsheirer.dsp.filter.fir.real.IRealFilter;
import io.github.dsheirer.dsp.filter.resample.RealResampler;
import io.github.dsheirer.dsp.fm.ISquelchingDemodulator;
import io.github.dsheirer.dsp.window.WindowType;
import io.github.dsheirer.module.decode.PrimaryDecoder;
import io.github.dsheirer.sample.Listener;
import io.github.dsheirer.sample.complex.ComplexSamples;
import io.github.dsheirer.sample.complex.IComplexSamplesListener;
import io.github.dsheirer.sample.real.IRealBufferProvider;
import io.github.dsheirer.source.ISourceEventListener;
import io.github.dsheirer.source.ISourceEventProvider;
import io.github.dsheirer.source.SourceEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Abstract analog decoder module with integrated squelch control
*/
public abstract class SquelchingAnalogDecoder extends PrimaryDecoder implements ISourceEventListener, ISourceEventProvider,
IComplexSamplesListener, Listener<ComplexSamples>, IRealBufferProvider,
IDecoderStateEventProvider
{
private final static Logger mLog = LoggerFactory.getLogger(SquelchingAnalogDecoder.class);
private static final double DEMODULATED_AUDIO_SAMPLE_RATE = 8000.0;
private IRealFilter mIBasebandFilter;
private IRealFilter mQBasebandFilter;
private IRealDecimationFilter mIDecimationFilter;
private IRealDecimationFilter mQDecimationFilter;
private ISquelchingDemodulator mDemodulator;
private RealResampler mResampler;
private SourceEventProcessor mSourceEventProcessor = new SourceEventProcessor();
private Listener<float[]> mResampledBufferListener;
private Listener<DecoderStateEvent> mDecoderStateEventListener;
private double mChannelBandwidth;
private double mOutputSampleRate = DEMODULATED_AUDIO_SAMPLE_RATE;
protected boolean mSquelch = true;
/**
* Constructs an instance
*
* @param config to setup the decoder
* @param squelchingDemodulator implementation (e.g. AM or FM).
*/
public SquelchingAnalogDecoder(DecodeConfigAnalog config, ISquelchingDemodulator squelchingDemodulator)
{
super(config);
mDemodulator = squelchingDemodulator;
mChannelBandwidth = config.getBandwidth().getValue();
mDemodulator.setSquelchThreshold(config.getSquelchThreshold());
}
/**
* Implements the ISourceEventListener interface
*/
@Override
public Listener<SourceEvent> getSourceEventListener()
{
return mSourceEventProcessor;
}
/**
* Module interface methods - unused.
*/
@Override
public void reset()
{
}
@Override
public void start()
{
}
@Override
public void stop()
{
}
/**
* Broadcasts the demodulated audio samples to the registered listener.
* @param demodulatedSamples to broadcast
*/
protected void broadcast(float[] demodulatedSamples)
{
if(mResampledBufferListener != null)
{
mResampledBufferListener.receive(demodulatedSamples);
}
}
/**
* Implements the IRealBufferProvider interface to register a listener for demodulated audio samples.
*
* @param listener to receive demodulated audio sample buffers.
*/
@Override
public void setBufferListener(Listener<float[]> listener)
{
mResampledBufferListener = listener;
}
/**
* Implements the IRealBufferProvider interface to deregister a listener from receiving demodulated audio samples.
*/
@Override
public void removeBufferListener()
{
mResampledBufferListener = null;
}
/**
* Implements the IComplexSampleListener interface to receive a stream of complex sample buffers.
*/
@Override
public Listener<ComplexSamples> getComplexSamplesListener()
{
return this;
}
/**
* Implements the Listener<ComplexSample> interface to receive a stream of complex sample buffers
*/
@Override
public void receive(ComplexSamples samples)
{
if(mIDecimationFilter == null || mQDecimationFilter == null)
{
throw new IllegalStateException("NBFM demodulator module must receive a sample rate change source " +
"event before it can process complex sample buffers");
}
float[] decimatedI = mIDecimationFilter.decimateReal(samples.i());
float[] decimatedQ = mQDecimationFilter.decimateReal(samples.q());
float[] filteredI = mIBasebandFilter.filter(decimatedI);
float[] filteredQ = mQBasebandFilter.filter(decimatedQ);
float[] demodulated = mDemodulator.demodulate(filteredI, filteredQ);
if(mResampler != null)
{
//Squelch changed while processing this audio buffer
if(mDemodulator.isSquelchChanged())
{
if(mDemodulator.isMuted())
{
//Demodulator says to mute and we're already muted/squelched = Continue IDLE
if(mSquelch)
{
notifyIdle();
}
//Demodulator says to mute and we are unmuted = Squelch and End Call
else
{
mSquelch = true;
notifyCallEnd();
}
}
else
{
//Demodulator says to unmute and we're muted/squelched = Unmute and Start Call
if(mSquelch)
{
mSquelch = false;
notifyCallStart();
}
//Demodulator says to unmute and we're already unmuted = Continue Call
else
{
notifyCallContinuation();
}
mResampler.resample(demodulated);
}
}
else
{
//Demodulator says squelch state didn't change and we're muted/squelched = Continue IDLE
if(mSquelch)
{
notifyIdle();
}
//Demodulator says squelch state didn't change and we're unmuted/unsquelched = Continue CALL
else
{
notifyCallContinuation();
mResampler.resample(demodulated);
}
}
}
else
{
//This shouldn't happen
notifyIdle();
}
}
/**
* Broadcasts a call start state event
*/
protected void notifyCallStart()
{
broadcast(new DecoderStateEvent(this, DecoderStateEvent.Event.START, State.CALL, 0));
}
/**
* Broadcasts a call continuation state event
*/
private void notifyCallContinuation()
{
broadcast(new DecoderStateEvent(this, DecoderStateEvent.Event.CONTINUATION, State.CALL, 0));
}
/**
* Broadcasts a call end state event
*/
private void notifyCallEnd()
{
broadcast(new DecoderStateEvent(this, DecoderStateEvent.Event.END, State.CALL, 0));
}
/**
* Broadcasts an idle notification
*/
private void notifyIdle()
{
broadcast(new DecoderStateEvent(this, DecoderStateEvent.Event.CONTINUATION, State.IDLE, 0));
}
/**
* Broadcasts the decoder state event to an optional registered listener
*/
private void broadcast(DecoderStateEvent event)
{
if(mDecoderStateEventListener != null)
{
mDecoderStateEventListener.receive(event);
}
}
/**
* Sets the decoder state listener
*/
@Override
public void setDecoderStateListener(Listener<DecoderStateEvent> listener)
{
mDecoderStateEventListener = listener;
}
/**
* Removes the decoder state event listener
*/
@Override
public void removeDecoderStateListener()
{
mDecoderStateEventListener = null;
}
/**
* Registers the listener to receive squelch change events from the demodulator/squelch controller.
*/
@Override
public void setSourceEventListener(Listener<SourceEvent> listener)
{
mDemodulator.setSourceEventListener(listener);
}
/**
* De-registers the listener
*/
@Override
public void removeSourceEventListener()
{
mDemodulator.setSourceEventListener(null);
}
/**
* Monitors sample rate change source event(s) to setup the initial I/Q filter and passes squelch threshold
* change requests down to the demodulator.
*/
public class SourceEventProcessor implements Listener<SourceEvent>
{
@Override
public void receive(SourceEvent sourceEvent)
{
switch(sourceEvent.getEvent())
{
//Forward these requests to the sub-class implementation (AM or FM demod) to handle
case REQUEST_CURRENT_SQUELCH_THRESHOLD:
case REQUEST_CHANGE_SQUELCH_THRESHOLD:
case REQUEST_CURRENT_SQUELCH_AUTO_TRACK:
case REQUEST_CHANGE_SQUELCH_AUTO_TRACK:
mDemodulator.receive(sourceEvent);
break;
case NOTIFICATION_SAMPLE_RATE_CHANGE:
if(mIBasebandFilter != null)
{
mIBasebandFilter = null;
mQBasebandFilter = null;
}
double sampleRate = sourceEvent.getValue().doubleValue();
int decimationRate = 0;
double decimatedSampleRate = sampleRate;
if(sampleRate / 2 >= (mChannelBandwidth * 2))
{
decimationRate = 2;
while(sampleRate / decimationRate / 2 >= (mChannelBandwidth * 2))
{
decimationRate *= 2;
}
}
if(decimationRate > 0)
{
decimatedSampleRate /= decimationRate;
}
mIDecimationFilter = DecimationFilterFactory.getRealDecimationFilter(decimationRate);
mQDecimationFilter = DecimationFilterFactory.getRealDecimationFilter(decimationRate);
if((decimatedSampleRate < (2.0 * mChannelBandwidth)))
{
throw new IllegalStateException(getDecoderType().name() + " demodulator with channel bandwidth [" +
mChannelBandwidth + "] requires a channel sample rate of [" + (2.0 * mChannelBandwidth +
"] - sample rate of [" + decimatedSampleRate + "] is not supported"));
}
mDemodulator.setSampleRate((int) decimatedSampleRate);
int passBandStop = (int) (mChannelBandwidth * .8);
int stopBandStart = (int) mChannelBandwidth;
float[] coefficients = null;
FIRFilterSpecification specification = FIRFilterSpecification.lowPassBuilder()
.sampleRate(decimatedSampleRate * 2)
.gridDensity(16)
.oddLength(true)
.passBandCutoff(passBandStop)
.passBandAmplitude(1.0)
.passBandRipple(0.01)
.stopBandStart(stopBandStart)
.stopBandAmplitude(0.0)
.stopBandRipple(0.005) //Approximately 90 dB attenuation
.build();
try
{
coefficients = FilterFactory.getTaps(specification);
}
catch(FilterDesignException fde)
{
mLog.error("Couldn't design demodulator remez filter for sample rate [" + sampleRate +
"] pass frequency [" + passBandStop + "] and stop frequency [" + stopBandStart +
"] - will proceed using sinc (low-pass) filter");
}
if(coefficients == null)
{
mLog.info("Unable to use remez filter designer for sample rate [" + decimatedSampleRate +
"] pass band stop [" + passBandStop +
"] and stop band start [" + stopBandStart + "] - will proceed using simple low pass filter design");
coefficients = FilterFactory.getLowPass(decimatedSampleRate, passBandStop, stopBandStart, 60,
WindowType.HAMMING, true);
}
mIBasebandFilter = FilterFactory.getRealFilter(coefficients);
mQBasebandFilter = FilterFactory.getRealFilter(coefficients);
mResampler = new RealResampler(decimatedSampleRate, mOutputSampleRate, 4192, 512);
mResampler.setListener(resampled -> broadcast(resampled));
break;
}
}
}
}

View File

@@ -1,6 +1,6 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2023 Dennis Sheirer
* Copyright (C) 2014-2025 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
@@ -20,6 +20,7 @@ package io.github.dsheirer.module.decode.nbfm;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import io.github.dsheirer.dsp.squelch.NoiseSquelch;
import io.github.dsheirer.module.decode.DecoderType;
import io.github.dsheirer.module.decode.analog.DecodeConfigAnalog;
import io.github.dsheirer.source.tuner.channel.ChannelSpecification;
@@ -30,6 +31,10 @@ import io.github.dsheirer.source.tuner.channel.ChannelSpecification;
public class DecodeConfigNBFM extends DecodeConfigAnalog
{
private boolean mAudioFilter = true;
private float mSquelchNoiseOpenThreshold = NoiseSquelch.DEFAULT_NOISE_OPEN_THRESHOLD;
private float mSquelchNoiseCloseThreshold = NoiseSquelch.DEFAULT_NOISE_CLOSE_THRESHOLD;
private int mSquelchHysteresisOpenThreshold = NoiseSquelch.DEFAULT_HYSTERESIS_OPEN_THRESHOLD;
private int mSquelchHysteresisCloseThreshold = NoiseSquelch.DEFAULT_HYSTERESIS_CLOSE_THRESHOLD;
/**
* Constructs an instance
@@ -88,4 +93,100 @@ public class DecodeConfigNBFM extends DecodeConfigAnalog
{
mAudioFilter = audioFilter;
}
/**
* Squelch noise open threshold in the range 0.0 to 1.0 with a default of 0.1
* @return noise open threshold
*/
@JacksonXmlProperty(isAttribute = true, localName = "squelchNoiseOpenThreshold")
public float getSquelchNoiseOpenThreshold()
{
return mSquelchNoiseOpenThreshold;
}
/**
* Squelch noise close threshold in the range 0.0 to 1.0, greater than or equal to open threshold, with a default of 0.2
* @return noise close threshold
*/
@JacksonXmlProperty(isAttribute = true, localName = "squelchNoiseCloseThreshold")
public float getSquelchNoiseCloseThreshold()
{
return mSquelchNoiseCloseThreshold;
}
/**
* Sets the squelch noise threshold.
* @param open in range 0.0 to 1.0 with a default of 0.1
*/
public void setSquelchNoiseOpenThreshold(float open)
{
if(open < NoiseSquelch.MINIMUM_NOISE_THRESHOLD || open > NoiseSquelch.MAXIMUM_NOISE_THRESHOLD)
{
throw new IllegalArgumentException("Squelch noise open threshold is out of range: " + open);
}
mSquelchNoiseOpenThreshold = open;
}
/**
* Sets the squelch noise close threshold.
* @param close in range 0.0 to 1.0 and greater than or equal to open, with a default of 0.1
*/
public void setSquelchNoiseCloseThreshold(float close)
{
if(close < NoiseSquelch.MINIMUM_NOISE_THRESHOLD || close > NoiseSquelch.MAXIMUM_NOISE_THRESHOLD)
{
throw new IllegalArgumentException("Squelch noise close threshold is out of range: " + close);
}
mSquelchNoiseCloseThreshold = close;
}
/**
* Squelch hysteresis open threshold in range 1-10 with a default of 4.
* @return hysteresis open threshold
*/
@JacksonXmlProperty(isAttribute = true, localName = "squelchHysteresisOpenThreshold")
public int getSquelchHysteresisOpenThreshold()
{
return mSquelchHysteresisOpenThreshold;
}
/**
* Sets the squelch time threshold in the range 1-10.
* @param open threshold
*/
public void setSquelchHysteresisOpenThreshold(int open)
{
if(open < NoiseSquelch.MINIMUM_HYSTERESIS_THRESHOLD || open > NoiseSquelch.MAXIMUM_HYSTERESIS_THRESHOLD)
{
throw new IllegalArgumentException("Squelch hysteresis open threshold is out of range: " + open);
}
mSquelchHysteresisOpenThreshold = open;
}
/**
* Squelch hysteresis close threshold in range 1-10 with a default of 4.
* @return hysteresis close threshold
*/
@JacksonXmlProperty(isAttribute = true, localName = "squelchHysteresisCloseThreshold")
public int getSquelchHysteresisCloseThreshold()
{
return mSquelchHysteresisCloseThreshold;
}
/**
* Sets the squelch close threshold in the range 1-10.
* @param close threshold
*/
public void setSquelchHysteresisCloseThreshold(int close)
{
if(close < NoiseSquelch.MINIMUM_HYSTERESIS_THRESHOLD || close > NoiseSquelch.MAXIMUM_HYSTERESIS_THRESHOLD)
{
throw new IllegalArgumentException("Squelch hysteresis close threshold is out of range: " + close);
}
mSquelchHysteresisCloseThreshold = close;
}
}

View File

@@ -1,6 +1,6 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2023 Dennis Sheirer
* Copyright (C) 2014-2025 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
@@ -18,34 +18,387 @@
*/
package io.github.dsheirer.module.decode.nbfm;
import io.github.dsheirer.dsp.fm.SquelchingFMDemodulator;
import io.github.dsheirer.audio.squelch.SquelchState;
import io.github.dsheirer.channel.state.DecoderStateEvent;
import io.github.dsheirer.channel.state.IDecoderStateEventProvider;
import io.github.dsheirer.channel.state.State;
import io.github.dsheirer.dsp.filter.FilterFactory;
import io.github.dsheirer.dsp.filter.decimate.DecimationFilterFactory;
import io.github.dsheirer.dsp.filter.decimate.IRealDecimationFilter;
import io.github.dsheirer.dsp.filter.design.FilterDesignException;
import io.github.dsheirer.dsp.filter.fir.FIRFilterSpecification;
import io.github.dsheirer.dsp.filter.fir.real.IRealFilter;
import io.github.dsheirer.dsp.filter.resample.RealResampler;
import io.github.dsheirer.dsp.fm.FmDemodulatorFactory;
import io.github.dsheirer.dsp.fm.IDemodulator;
import io.github.dsheirer.dsp.squelch.INoiseSquelchController;
import io.github.dsheirer.dsp.squelch.NoiseSquelch;
import io.github.dsheirer.dsp.squelch.NoiseSquelchState;
import io.github.dsheirer.dsp.window.WindowType;
import io.github.dsheirer.module.decode.DecoderType;
import io.github.dsheirer.module.decode.analog.SquelchingAnalogDecoder;
import io.github.dsheirer.module.decode.PrimaryDecoder;
import io.github.dsheirer.sample.Listener;
import io.github.dsheirer.sample.complex.ComplexSamples;
import io.github.dsheirer.sample.complex.IComplexSamplesListener;
import io.github.dsheirer.sample.real.IRealBufferProvider;
import io.github.dsheirer.source.ISourceEventListener;
import io.github.dsheirer.source.SourceEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Narrow Band FM Decoder module with integrated squelch control.
* NBFM decoder with integrated noise squelch. Demodulates complex sample buffers and feeds unfiltered, demodulated
* audio to Noise Squelch. Squelch operates on the noise level with open and close thresholds to pass low-noise audio
* and block high-noise audio. Audio is filtered and resampled to 8 kHz for downstream consumers.
*/
public class NBFMDecoder extends SquelchingAnalogDecoder
public class NBFMDecoder extends PrimaryDecoder implements ISourceEventListener, IComplexSamplesListener,
Listener<ComplexSamples>, IRealBufferProvider, IDecoderStateEventProvider, INoiseSquelchController
{
private final static Logger mLog = LoggerFactory.getLogger(NBFMDecoder.class);
private static final float SQUELCH_ALPHA_DECAY = 0.0004f;
private static final double DEMODULATED_AUDIO_SAMPLE_RATE = 8000.0;
private final IDemodulator mDemodulator = FmDemodulatorFactory.getFmDemodulator();
private final SourceEventProcessor mSourceEventProcessor = new SourceEventProcessor();
private final NoiseSquelch mNoiseSquelch;
private IRealFilter mIBasebandFilter;
private IRealFilter mQBasebandFilter;
private IRealDecimationFilter mIDecimationFilter;
private IRealDecimationFilter mQDecimationFilter;
private Listener<float[]> mResampledBufferListener;
private Listener<DecoderStateEvent> mDecoderStateEventListener;
private RealResampler mResampler;
private final double mChannelBandwidth;
/**
* Constructs an instance
*
* @param config to setup the decoder
* @param config to setup the NBFM decoder and noise squelch control.
*/
public NBFMDecoder(DecodeConfigNBFM config)
{
super(config, new SquelchingFMDemodulator(SQUELCH_ALPHA_DECAY, config.getSquelchThreshold(),
config.isSquelchAutoTrack()));
super(config);
//Save channel bandwidth to setup channel baseband filter.
mChannelBandwidth = config.getBandwidth().getValue();
mNoiseSquelch = new NoiseSquelch(config.getSquelchNoiseOpenThreshold(), config.getSquelchNoiseCloseThreshold(),
config.getSquelchHysteresisOpenThreshold(), config.getSquelchHysteresisCloseThreshold());
//Send squelch controlled audio to the resampler and notify the decoder state that the call continues.
mNoiseSquelch.setAudioListener(audio -> {
mResampler.resample(audio);
notifyCallContinuation();
});
//Notify the decoder state of call starts and ends
mNoiseSquelch.setSquelchStateListener(squelchState -> {
if(squelchState == SquelchState.SQUELCH)
{
notifyCallEnd();
}
else
{
notifyCallStart();
}
});
}
/**
* Decoder type.
* @return type
*/
@Override
public DecoderType getDecoderType()
{
return DecoderType.NBFM;
}
/**
* Decode configuration for this decoder.
* @return configuration
*/
@Override
public DecodeConfigNBFM getDecodeConfiguration()
{
return (DecodeConfigNBFM)super.getDecodeConfiguration();
}
/**
* Register the noise squelch state listener. This will normally be a GUI noise squelch state view/controller.
* @param listener to receive states or pass null to de-register a listener.
*/
@Override
public void setNoiseSquelchStateListener(Listener<NoiseSquelchState> listener)
{
mNoiseSquelch.setNoiseSquelchStateListener(listener);
}
/**
* Applies new open and close noise threshold values for the noise squelch.
* @param open for the open noise variance calculation in range 0.1 - 0.5 where open <= close value.
* @param close for the close noise variance calculation. in range 0.1 - 0.5 where close >= open.
*/
@Override
public void setNoiseThreshold(float open, float close)
{
mNoiseSquelch.setNoiseThreshold(open, close);
//Update the channel configuration and schedule a playlist save.
getDecodeConfiguration().setSquelchNoiseOpenThreshold(open);
getDecodeConfiguration().setSquelchNoiseCloseThreshold(close);
}
/**
* Sets the open and close hysteresis thresholds in units of 10 milliseconds.
* @param open in range 1-10, recommend: 4 where open <= close
* @param close in range 1-10, recommend: 6 where close >= open.
*/
@Override
public void setHysteresisThreshold(int open, int close)
{
mNoiseSquelch.setHysteresisThreshold(open, close);
getDecodeConfiguration().setSquelchHysteresisOpenThreshold(open);
getDecodeConfiguration().setSquelchHysteresisCloseThreshold(close);
}
/**
* Sets the squelch override state to temporarily bypass/override squelch control and pass all audio.
* @param override (true) or (false) to turn off squelch override.
*/
@Override
public void setSquelchOverride(boolean override)
{
mNoiseSquelch.setSquelchOverride(override);
}
/**
* Implements the ISourceEventListener interface
*/
@Override
public Listener<SourceEvent> getSourceEventListener()
{
return mSourceEventProcessor;
}
/**
* Module interface methods - unused.
*/
@Override
public void reset() {}
@Override
public void start() {}
@Override
public void stop() {}
/**
* Broadcasts the demodulated, resampled to 8 kHz audio samples to the registered listener.
*
* @param demodulatedSamples to broadcast
*/
protected void broadcast(float[] demodulatedSamples)
{
if(mResampledBufferListener != null)
{
mResampledBufferListener.receive(demodulatedSamples);
}
}
/**
* Implements the IRealBufferProvider interface to register a listener for demodulated audio samples.
*
* @param listener to receive demodulated, resampled audio sample buffers.
*/
@Override
public void setBufferListener(Listener<float[]> listener)
{
mResampledBufferListener = listener;
}
/**
* Implements the IRealBufferProvider interface to deregister a listener from receiving demodulated audio samples.
*/
@Override
public void removeBufferListener()
{
mResampledBufferListener = null;
}
/**
* Implements the IComplexSampleListener interface to receive a stream of complex sample buffers.
*/
@Override
public Listener<ComplexSamples> getComplexSamplesListener()
{
return this;
}
/**
* Implements the Listener<ComplexSample> interface to receive a stream of complex I/Q sample buffers
*/
@Override
public void receive(ComplexSamples samples)
{
if(mIDecimationFilter == null || mQDecimationFilter == null)
{
throw new IllegalStateException("NBFM demodulator module must receive a sample rate change source event " +
"before it can process complex sample buffers");
}
float[] decimatedI = mIDecimationFilter.decimateReal(samples.i());
float[] decimatedQ = mQDecimationFilter.decimateReal(samples.q());
float[] filteredI = mIBasebandFilter.filter(decimatedI);
float[] filteredQ = mQBasebandFilter.filter(decimatedQ);
float[] demodulated = mDemodulator.demodulate(filteredI, filteredQ);
mNoiseSquelch.process(demodulated);
//Once we process the sample buffer, if the ending state is squelch closed, update the decoder state that we
// are idle.
if(mNoiseSquelch.isSquelched())
{
notifyIdle();
}
}
/**
* Broadcasts a call start state event
*/
private void notifyCallStart()
{
broadcast(new DecoderStateEvent(this, DecoderStateEvent.Event.START, State.CALL, 0));
}
/**
* Broadcasts a call continuation state event
*/
private void notifyCallContinuation()
{
broadcast(new DecoderStateEvent(this, DecoderStateEvent.Event.CONTINUATION, State.CALL, 0));
}
/**
* Broadcasts a call end state event
*/
private void notifyCallEnd()
{
broadcast(new DecoderStateEvent(this, DecoderStateEvent.Event.END, State.CALL, 0));
}
/**
* Broadcasts an idle notification
*/
private void notifyIdle()
{
broadcast(new DecoderStateEvent(this, DecoderStateEvent.Event.CONTINUATION, State.IDLE, 0));
}
/**
* Broadcasts the decoder state event to an optional registered listener
*/
private void broadcast(DecoderStateEvent event)
{
if(mDecoderStateEventListener != null)
{
mDecoderStateEventListener.receive(event);
}
}
/**
* Sets the decoder state listener
*/
@Override
public void setDecoderStateListener(Listener<DecoderStateEvent> listener)
{
mDecoderStateEventListener = listener;
}
/**
* Removes the decoder state event listener
*/
@Override
public void removeDecoderStateListener()
{
mDecoderStateEventListener = null;
}
/**
* Updates the decoder to process complex sample buffers at the specified sample rate.
* @param sampleRate of the incoming complex sample buffer stream.
*/
private void setSampleRate(double sampleRate)
{
int decimationRate = 0;
double decimatedSampleRate = sampleRate;
if(sampleRate / 2 >= (mChannelBandwidth * 2))
{
decimationRate = 2;
while(sampleRate / decimationRate / 2 >= (mChannelBandwidth * 2))
{
decimationRate *= 2;
}
}
if(decimationRate > 0)
{
decimatedSampleRate /= decimationRate;
}
mIDecimationFilter = DecimationFilterFactory.getRealDecimationFilter(decimationRate);
mQDecimationFilter = DecimationFilterFactory.getRealDecimationFilter(decimationRate);
if((decimatedSampleRate < (2.0 * mChannelBandwidth)))
{
throw new IllegalStateException(getDecoderType().name() + " demodulator with channel bandwidth [" + mChannelBandwidth + "] requires a channel sample rate of [" + (2.0 * mChannelBandwidth + "] - sample rate of [" + decimatedSampleRate + "] is not supported"));
}
mNoiseSquelch.setSampleRate(decimatedSampleRate);
int passBandStop = (int) (mChannelBandwidth * .8);
int stopBandStart = (int) mChannelBandwidth;
float[] coefficients = null;
FIRFilterSpecification specification = FIRFilterSpecification.lowPassBuilder().sampleRate(decimatedSampleRate * 2).gridDensity(16).oddLength(true).passBandCutoff(passBandStop).passBandAmplitude(1.0).passBandRipple(0.01).stopBandStart(stopBandStart).stopBandAmplitude(0.0).stopBandRipple(0.005) //Approximately 90 dB attenuation
.build();
try
{
coefficients = FilterFactory.getTaps(specification);
}
catch(FilterDesignException fde)
{
mLog.error("Couldn't design demodulator remez filter for sample rate [" + sampleRate + "] pass frequency [" + passBandStop + "] and stop frequency [" + stopBandStart + "] - will proceed using sinc (low-pass) filter");
}
if(coefficients == null)
{
mLog.info("Unable to use remez filter designer for sample rate [" + decimatedSampleRate + "] pass band stop [" + passBandStop + "] and stop band start [" + stopBandStart + "] - will proceed using simple low pass filter design");
coefficients = FilterFactory.getLowPass(decimatedSampleRate, passBandStop, stopBandStart, 60, WindowType.HAMMING, true);
}
mIBasebandFilter = FilterFactory.getRealFilter(coefficients);
mQBasebandFilter = FilterFactory.getRealFilter(coefficients);
mResampler = new RealResampler(decimatedSampleRate, DEMODULATED_AUDIO_SAMPLE_RATE, 4192, 512);
mResampler.setListener(NBFMDecoder.this::broadcast);
}
/**
* Monitors sample rate change source event(s) to set up the filters, decimation, and demodulator.
*/
public class SourceEventProcessor implements Listener<SourceEvent>
{
@Override
public void receive(SourceEvent sourceEvent)
{
if(sourceEvent.getEvent() == SourceEvent.Event.NOTIFICATION_SAMPLE_RATE_CHANGE)
{
setSampleRate(sourceEvent.getValue().doubleValue());
}
}
}
}