Resolves 1122. Adds decimation to NBFMDecoder so that channel bandwidth is reduced to min sample rate needed. This reduces the impact on channel power measurement from adjacent channels. (#1129)

Co-authored-by: Dennis Sheirer <dsheirer@github.com>
This commit is contained in:
Denny Sheirer
2022-01-02 08:35:19 -05:00
committed by GitHub
parent 2a0427c559
commit 8413343e76
9 changed files with 234 additions and 58 deletions

View File

@@ -1,7 +1,6 @@
/* /*
* ****************************************************************************** * *****************************************************************************
* sdrtrunk * Copyright (C) 2014-2022 Dennis Sheirer
* Copyright (C) 2014-2018 Dennis Sheirer
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -15,7 +14,7 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/> * along with this program. If not, see <http://www.gnu.org/licenses/>
* ***************************************************************************** * ****************************************************************************
*/ */
package io.github.dsheirer.dsp.filter.channelizer; package io.github.dsheirer.dsp.filter.channelizer;
@@ -40,6 +39,7 @@ public class PolyphaseChannelSource extends TunerChannelSource
{ {
// private final static Logger mLog = LoggerFactory.getLogger(PolyphaseChannelSource.class); // private final static Logger mLog = LoggerFactory.getLogger(PolyphaseChannelSource.class);
private static final int PROCESSED_BUFFER_SAMPLE_SIZE = 2048;
private ReusableComplexBufferAssembler mReusableComplexBufferAssembler; private ReusableComplexBufferAssembler mReusableComplexBufferAssembler;
private IPolyphaseChannelOutputProcessor mPolyphaseChannelOutputProcessor; private IPolyphaseChannelOutputProcessor mPolyphaseChannelOutputProcessor;
private IPolyphaseChannelOutputProcessor mReplacementPolyphaseChannelOutputProcessor; private IPolyphaseChannelOutputProcessor mReplacementPolyphaseChannelOutputProcessor;
@@ -71,7 +71,7 @@ public class PolyphaseChannelSource extends TunerChannelSource
super(producerSourceEventListener, tunerChannel); super(producerSourceEventListener, tunerChannel);
mPolyphaseChannelOutputProcessor = outputProcessor; mPolyphaseChannelOutputProcessor = outputProcessor;
mChannelSampleRate = channelSampleRate; mChannelSampleRate = channelSampleRate;
mReusableComplexBufferAssembler = new ReusableComplexBufferAssembler(2500, mChannelSampleRate); mReusableComplexBufferAssembler = new ReusableComplexBufferAssembler(PROCESSED_BUFFER_SAMPLE_SIZE, mChannelSampleRate);
float[] filterCoefficients = getLowPassFilter(channelSampleRate, channelSpecification.getPassFrequency(), float[] filterCoefficients = getLowPassFilter(channelSampleRate, channelSpecification.getPassFrequency(),
channelSpecification.getStopFrequency()); channelSpecification.getStopFrequency());

View File

@@ -0,0 +1,40 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2022 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.dsp.filter.decimate;
import io.github.dsheirer.sample.buffer.ReusableComplexBuffer;
/**
* No decimation filter.
*/
public class ComplexDecimateX0Filter implements IComplexDecimationFilter
{
@Override
public float[] decimateComplex(float[] samples)
{
return samples;
}
@Override
public ReusableComplexBuffer decimate(ReusableComplexBuffer buffer)
{
return buffer;
}
}

View File

@@ -1,6 +1,6 @@
/* /*
* ***************************************************************************** * *****************************************************************************
* Copyright (C) 2014-2021 Dennis Sheirer * Copyright (C) 2014-2022 Dennis Sheirer
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -24,7 +24,7 @@ package io.github.dsheirer.dsp.filter.decimate;
*/ */
public class DecimationFilterFactory public class DecimationFilterFactory
{ {
private static final int[] SUPPORTED_RATES = new int[]{2,4,8,16,32,64,128,256,512,1024}; private static final int[] SUPPORTED_RATES = new int[]{0,2,4,8,16,32,64,128,256,512,1024};
/** /**
* Creates a real-valued decimation filter for float array sample buffers providing greater than 100 dB of * Creates a real-valued decimation filter for float array sample buffers providing greater than 100 dB of
@@ -37,6 +37,8 @@ public class DecimationFilterFactory
{ {
switch(decimationRate) switch(decimationRate)
{ {
case 0:
return new RealDecimateX0Filter();
case 2: case 2:
return new RealDecimateX2Filter(); return new RealDecimateX2Filter();
case 4: case 4:
@@ -74,6 +76,8 @@ public class DecimationFilterFactory
{ {
switch(decimationRate) switch(decimationRate)
{ {
case 0:
return new ComplexDecimateX0Filter();
case 2: case 2:
return new ComplexDecimateX2Filter(); return new ComplexDecimateX2Filter();
case 4: case 4:

View File

@@ -1,6 +1,6 @@
/* /*
* ***************************************************************************** * *****************************************************************************
* Copyright (C) 2014-2021 Dennis Sheirer * Copyright (C) 2014-2022 Dennis Sheirer
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -19,10 +19,24 @@
package io.github.dsheirer.dsp.filter.decimate; package io.github.dsheirer.dsp.filter.decimate;
import io.github.dsheirer.sample.buffer.ReusableComplexBuffer;
/** /**
* Interface for a decimation filter for float array sample buffers * Interface for a decimation filter for float array sample buffers
*/ */
public interface IComplexDecimationFilter public interface IComplexDecimationFilter
{ {
/**
* Decimates an array of float complex samples
* @param samples to decimate
* @return decimated samples
*/
float[] decimateComplex(float[] samples); float[] decimateComplex(float[] samples);
/**
* Decimates an buffer of float complex samples
* @param buffer to decimate
* @return decimated samples
*/
ReusableComplexBuffer decimate(ReusableComplexBuffer buffer);
} }

View File

@@ -0,0 +1,32 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2022 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.dsp.filter.decimate;
/**
* No decimation filter.
*/
public class RealDecimateX0Filter implements IRealDecimationFilter
{
@Override
public float[] decimateReal(float[] samples)
{
return samples;
}
}

View File

@@ -1,6 +1,6 @@
/* /*
* ***************************************************************************** * *****************************************************************************
* Copyright (C) 2014-2021 Dennis Sheirer * Copyright (C) 2014-2022 Dennis Sheirer
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -20,6 +20,8 @@
package io.github.dsheirer.dsp.filter.halfband.complex; package io.github.dsheirer.dsp.filter.halfband.complex;
import io.github.dsheirer.dsp.filter.decimate.IComplexDecimationFilter; import io.github.dsheirer.dsp.filter.decimate.IComplexDecimationFilter;
import io.github.dsheirer.sample.buffer.ReusableComplexBuffer;
import io.github.dsheirer.sample.buffer.ReusableComplexBufferQueue;
/** /**
* Complex half-band filter that processes samples on a per-array basis, versus a per-sample basis. * Complex half-band filter that processes samples on a per-array basis, versus a per-sample basis.
@@ -35,6 +37,7 @@ public class ComplexHalfBandDecimationFilter implements IComplexDecimationFilter
private int mCoefficientsLengthMinus2; private int mCoefficientsLengthMinus2;
private int mBufferPointer; private int mBufferPointer;
private int mHalf; private int mHalf;
private ReusableComplexBufferQueue mReusableComplexBufferQueue = new ReusableComplexBufferQueue("half-band decimation filter");
/** /**
* Constructs a complex sample half-band decimate x2 filter using the specified filter coefficients. * Constructs a complex sample half-band decimate x2 filter using the specified filter coefficients.
@@ -118,4 +121,17 @@ public class ComplexHalfBandDecimationFilter implements IComplexDecimationFilter
return filtered; return filtered;
} }
/**
* Decimates the complex samples and returns a buffer of decimated samples.
* @param buffer to decimate
* @return decimated buffer.
*/
@Override
public ReusableComplexBuffer decimate(ReusableComplexBuffer buffer)
{
float[] decimated = decimateComplex(buffer.getSamples());
buffer.decrementUserCount();
return mReusableComplexBufferQueue.getBuffer(decimated, buffer.getTimestamp());
}
} }

View File

@@ -1,20 +1,21 @@
/******************************************************************************* /*
* SDR Trunk * *****************************************************************************
* Copyright (C) 2014,2015 Dennis Sheirer * Copyright (C) 2014-2022 Dennis Sheirer
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/> * along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/ * ****************************************************************************
*/
package io.github.dsheirer.module.decode.nbfm; package io.github.dsheirer.module.decode.nbfm;
import io.github.dsheirer.channel.state.DecoderStateEvent; import io.github.dsheirer.channel.state.DecoderStateEvent;
@@ -22,6 +23,8 @@ import io.github.dsheirer.channel.state.IDecoderStateEventProvider;
import io.github.dsheirer.channel.state.State; import io.github.dsheirer.channel.state.State;
import io.github.dsheirer.dsp.filter.FilterFactory; import io.github.dsheirer.dsp.filter.FilterFactory;
import io.github.dsheirer.dsp.filter.Window; import io.github.dsheirer.dsp.filter.Window;
import io.github.dsheirer.dsp.filter.decimate.DecimationFilterFactory;
import io.github.dsheirer.dsp.filter.decimate.IComplexDecimationFilter;
import io.github.dsheirer.dsp.filter.design.FilterDesignException; import io.github.dsheirer.dsp.filter.design.FilterDesignException;
import io.github.dsheirer.dsp.filter.fir.FIRFilterSpecification; import io.github.dsheirer.dsp.filter.fir.FIRFilterSpecification;
import io.github.dsheirer.dsp.filter.fir.complex.ComplexFIRFilter2; import io.github.dsheirer.dsp.filter.fir.complex.ComplexFIRFilter2;
@@ -49,11 +52,12 @@ public class NBFMDecoder extends PrimaryDecoder implements ISourceEventListener,
{ {
private final static Logger mLog = LoggerFactory.getLogger(NBFMDecoder.class); private final static Logger mLog = LoggerFactory.getLogger(NBFMDecoder.class);
private static final double DEMODULATED_AUDIO_SAMPLE_RATE = 8000.0; private static final double DEMODULATED_AUDIO_SAMPLE_RATE = 8000.0;
private static final double POWER_SQUELCH_ALPHA_DECAY = 0.0002; private static final double POWER_SQUELCH_ALPHA_DECAY = 0.0004;
private static final double POWER_SQUELCH_THRESHOLD_DB = -78.0; private static final double POWER_SQUELCH_THRESHOLD_DB = -78.0;
private static final int POWER_SQUELCH_RAMP = 4; private static final int POWER_SQUELCH_RAMP = 4;
private ComplexFIRFilter2 mIQFilter; private ComplexFIRFilter2 mIQFilter;
private IComplexDecimationFilter mDecimationFilter;
private SquelchingFMDemodulator mDemodulator = new SquelchingFMDemodulator(POWER_SQUELCH_ALPHA_DECAY, private SquelchingFMDemodulator mDemodulator = new SquelchingFMDemodulator(POWER_SQUELCH_ALPHA_DECAY,
POWER_SQUELCH_THRESHOLD_DB, POWER_SQUELCH_RAMP); POWER_SQUELCH_THRESHOLD_DB, POWER_SQUELCH_RAMP);
private RealResampler mResampler; private RealResampler mResampler;
@@ -131,7 +135,9 @@ public class NBFMDecoder extends PrimaryDecoder implements ISourceEventListener,
"event before it can process complex sample buffers"); "event before it can process complex sample buffers");
} }
ReusableComplexBuffer basebandFilteredBuffer = mIQFilter.filter(reusableComplexBuffer); ReusableComplexBuffer decimatedBuffer = mDecimationFilter.decimate(reusableComplexBuffer);
ReusableComplexBuffer basebandFilteredBuffer = mIQFilter.filter(decimatedBuffer);
ReusableFloatBuffer demodulatedBuffer = mDemodulator.demodulate(basebandFilteredBuffer); ReusableFloatBuffer demodulatedBuffer = mDemodulator.demodulate(basebandFilteredBuffer);
if(mResampler != null) if(mResampler != null)
@@ -268,11 +274,31 @@ public class NBFMDecoder extends PrimaryDecoder implements ISourceEventListener,
double sampleRate = sourceEvent.getValue().doubleValue(); double sampleRate = sourceEvent.getValue().doubleValue();
if((sampleRate < (2.0 * mChannelBandwidth))) int decimationRate = 0;
double decimatedSampleRate = sampleRate;
if(sampleRate / 2 > mChannelBandwidth)
{
decimationRate = 2;
while(sampleRate / decimationRate / 2 > mChannelBandwidth)
{
decimationRate *= 2;
}
}
if(decimationRate > 0)
{
decimatedSampleRate /= decimationRate;
}
mDecimationFilter = DecimationFilterFactory.getComplexDecimationFilter(decimationRate);
if((decimatedSampleRate < (2.0 * mChannelBandwidth)))
{ {
throw new IllegalStateException("FM Demodulator with channel bandwidth [" + mChannelBandwidth + throw new IllegalStateException("FM Demodulator with channel bandwidth [" + mChannelBandwidth +
"] requires a channel sample rate of [" + (2.0 * mChannelBandwidth + "] - sample rate of [" + "] requires a channel sample rate of [" + (2.0 * mChannelBandwidth + "] - sample rate of [" +
sampleRate + "] is not supported")); decimatedSampleRate + "] is not supported"));
} }
int passBandStop = (int)(mChannelBandwidth * .8); int passBandStop = (int)(mChannelBandwidth * .8);
@@ -281,7 +307,7 @@ public class NBFMDecoder extends PrimaryDecoder implements ISourceEventListener,
float[] filterTaps = null; float[] filterTaps = null;
FIRFilterSpecification specification = FIRFilterSpecification.lowPassBuilder() FIRFilterSpecification specification = FIRFilterSpecification.lowPassBuilder()
.sampleRate(sampleRate) .sampleRate(decimatedSampleRate * 2)
.gridDensity(16) .gridDensity(16)
.oddLength(true) .oddLength(true)
.passBandCutoff(passBandStop) .passBandCutoff(passBandStop)
@@ -300,18 +326,21 @@ public class NBFMDecoder extends PrimaryDecoder implements ISourceEventListener,
{ {
mLog.error("Couldn't design FM demodulator remez filter for sample rate [" + sampleRate + mLog.error("Couldn't design FM demodulator remez filter for sample rate [" + sampleRate +
"] pass frequency [" + passBandStop + "] and stop frequency [" + stopBandStart + "] pass frequency [" + passBandStop + "] and stop frequency [" + stopBandStart +
"] - using sinc filter"); "] - will proceed using sinc (low-pass) filter");
} }
if(filterTaps == null) if(filterTaps == null)
{ {
filterTaps = FilterFactory.getLowPass(sampleRate, passBandStop, stopBandStart, 60, 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");
filterTaps = FilterFactory.getLowPass(decimatedSampleRate, passBandStop, stopBandStart, 60,
Window.WindowType.HAMMING, true); Window.WindowType.HAMMING, true);
} }
mIQFilter = new ComplexFIRFilter2(filterTaps); mIQFilter = new ComplexFIRFilter2(filterTaps);
mResampler = new RealResampler(sampleRate, mOutputSampleRate, 2000, 1000); mResampler = new RealResampler(decimatedSampleRate, mOutputSampleRate, 2000, 1000);
mResampler.setListener(reusableFloatBuffer -> mResampler.setListener(reusableFloatBuffer ->
{ {

View File

@@ -1,18 +1,21 @@
/******************************************************************************* /*
* sdr-trunk * *****************************************************************************
* Copyright (C) 2014-2018 Dennis Sheirer * Copyright (C) 2014-2022 Dennis Sheirer
* *
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public * This program is free software: you can redistribute it and/or modify
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any * it under the terms of the GNU General Public License as published by
* later version. * 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 * This program is distributed in the hope that it will be useful,
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * 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. * You should have received a copy of the GNU General Public License
* If not, see <http://www.gnu.org/licenses/> * along with this program. If not, see <http://www.gnu.org/licenses/>
* * ****************************************************************************
******************************************************************************/ */
package io.github.dsheirer.sample.buffer; package io.github.dsheirer.sample.buffer;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -50,6 +53,14 @@ public abstract class AbstractReusableBufferQueue<T extends AbstractReusableBuff
{ {
} }
/**
* Turns on/off logging of buffer creation
*/
public void setDebugLogging(boolean enabled)
{
mBufferCreationLoggingEnabled = enabled;
}
/** /**
* Disposes of any reclaimed buffers to prepare this queue for disposal. * Disposes of any reclaimed buffers to prepare this queue for disposal.
*/ */

View File

@@ -1,18 +1,21 @@
/******************************************************************************* /*
* sdr-trunk * *****************************************************************************
* Copyright (C) 2014-2018 Dennis Sheirer * Copyright (C) 2014-2022 Dennis Sheirer
* *
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public * This program is free software: you can redistribute it and/or modify
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any * it under the terms of the GNU General Public License as published by
* later version. * 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 * This program is distributed in the hope that it will be useful,
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * 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. * You should have received a copy of the GNU General Public License
* If not, see <http://www.gnu.org/licenses/> * along with this program. If not, see <http://www.gnu.org/licenses/>
* * ****************************************************************************
******************************************************************************/ */
package io.github.dsheirer.sample.buffer; package io.github.dsheirer.sample.buffer;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -61,4 +64,31 @@ public class ReusableComplexBufferQueue extends AbstractReusableBufferQueue<Reus
return buffer; return buffer;
} }
/**
* Returns a buffer loaded with the samples.
* @param samples to load
* @param timestamp to apply
* @return buffer
*/
public ReusableComplexBuffer getBuffer(float[] samples, long timestamp)
{
ReusableComplexBuffer buffer = getRecycledBuffer();
if(buffer == null)
{
buffer = new ReusableComplexBuffer(this, samples);
buffer.setTimestamp(timestamp);
buffer.setDebugName("Owner:" + getDebugName());
incrementBufferCount();
}
else
{
buffer.reloadFrom(samples, timestamp);
}
buffer.incrementUserCount();
return buffer;
}
} }