WIP - updated tuners to use CIC decimation tuner channel sources instead

of polyphase channelizer.

Channel sample rate reduced to 30 kHz - seems to work better with P25
decoders than previous 25 kHz.
This commit is contained in:
Denny
2018-09-04 08:16:57 -04:00
parent f963d821a1
commit 8946d789ff
24 changed files with 1528 additions and 123 deletions

View File

@@ -1719,7 +1719,32 @@ public enum Filters
0.011873357051047719f, 0.0f, -0.007901319195893647f, 0.0f,
0.005055504379767936f, 0.0f, -0.003054430179754289f, 0.0f,
0.001695637278417295f, 0.0f, -0.000998606272947510f
} );
}),
/**
* FIRFilterSpecification specification = FIRFilterSpecification.lowPassBuilder()
* .sampleRate(100)
* .gridDensity(16)
* .oddLength(true)
* .passBandCutoff(21)
* .passBandAmplitude(1.0)
* .passBandRipple(0.01)
* .stopBandStart(29)
* .stopBandAmplitude(0.0)
* .stopBandRipple(0.01) //Approximately -85 dB suppression
* .build();
*/
HALF_BAND_FILTER_27T(new float[]
{
0.007495115f, 0.0f, -0.011045938f, 0.0f,
0.019129561f, 0.0f, -0.031747308f, 0.0f,
0.053220540f, 0.0f, -0.099569574f, 0.0f,
0.316083550f, 0.5f, 0.316083550f, 0.0f,
-0.099569574f, 0.0f, 0.053220540f, 0.0f,
-0.031747308f, 0.0f, 0.019129561f, 0.0f,
-0.011045938f, 0.0f, 0.007495115f
});
float[] mCoefficients;

View File

@@ -34,7 +34,6 @@ import io.github.dsheirer.source.tuner.channel.TunerChannelSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -172,36 +171,6 @@ public class PolyphaseChannelManager implements ISourceEventProcessor
return channelSource;
}
public TunerChannelSource getTestChannel(int index)
{
PolyphaseChannelSource channelSource = null;
try
{
List<Integer> polyphaseIndexes = new ArrayList<>();
polyphaseIndexes.add(index);
IPolyphaseChannelOutputProcessor outputProcessor = getOutputProcessor(polyphaseIndexes);
if(outputProcessor != null)
{
long centerFrequency = mChannelCalculator.getCenterFrequencyForIndexes(polyphaseIndexes);
TunerChannel tunerChannel = new TunerChannel(centerFrequency, 12500);
channelSource = new PolyphaseChannelSource(tunerChannel, outputProcessor, mChannelSourceEventListener,
mChannelCalculator.getChannelSampleRate(), centerFrequency);
}
}
catch(IllegalArgumentException iae)
{
mLog.error("Couldn't get test channel with index:" + index, iae);
}
return channelSource;
}
/**
* Creates a processor to process the channelizer channel indexes into a composite output stream providing
* channelized complex sample buffers to a registered source listener.

View File

@@ -201,7 +201,7 @@ public class PolyphaseChannelSource extends TunerChannelSource
* @param frequency in hertz
*/
@Override
protected void setFrequency(long frequency)
public void setFrequency(long frequency)
{
mIndexCenterFrequency = frequency;

View File

@@ -0,0 +1,493 @@
/*******************************************************************************
* sdr-trunk
* Copyright (C) 2014-2018 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.cic;
import io.github.dsheirer.dsp.filter.design.FilterDesignException;
import io.github.dsheirer.dsp.filter.fir.FIRFilterSpecification;
import io.github.dsheirer.dsp.filter.fir.complex.ComplexFIRFilter2;
import io.github.dsheirer.dsp.filter.fir.remez.RemezFIRFilterDesigner;
import io.github.dsheirer.sample.Listener;
import io.github.dsheirer.sample.buffer.ReusableComplexBuffer;
import io.github.dsheirer.sample.buffer.ReusableComplexBufferAssembler;
import io.github.dsheirer.sample.complex.ComplexSampleListener;
import io.github.dsheirer.sample.decimator.ComplexDecimator;
import org.apache.commons.lang3.Validate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Non-Recursive Prime-Factor CIC Filter.
*
* Implements the CIC filter described in Understanding Digital Signal Processing, 3e, Lyons, on page 769. This
* filter is comprised of multiple decimating stages each with a prime factor decimation rate. Multiple stages are
* cascaded to achieve the overall decimation rate.
*
* This filter currently supports a maximum decimation rate of 2801. Higher decimation rates can be added by
* adding additional prime factors to the PRIMES array.
*/
public class ComplexPrimeCICDecimate2 implements Listener<ReusableComplexBuffer>
{
// private final static Logger mLog = LoggerFactory.getLogger(ComplexPrimeCICDecimate2.class);
/**
* PRIME numbers to use for decimation stage sizing. This list contains values that should
* support input sample rates up to 70 MHz with an output channel rate of at least 25 kHz.
*/
public static int[] PRIMES = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,
59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251,
257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359,
367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593,
599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701,
709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827,
829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953,
967, 971, 977, 983, 991, 997, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051,
1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163,
1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279,
1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399,
1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489,
1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601,
1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709,
1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831,
1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951,
1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069,
2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179,
2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297,
2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399,
2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543,
2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671,
2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753,
2767, 2777, 2789, 2791, 2797, 2801
};
private static Map<Integer,float[]> sLowPassFilters = new HashMap();
private static Map<Integer,List<Integer>> sPrimeFactors = new HashMap();
private List<DecimatingStage> mDecimatingStages = new ArrayList<DecimatingStage>();
private DecimatingStage mFirstDecimatingStage;
private Output mOutput;
/**
* Constructs a new decimation filter.
*
* An example setup for a 2.4 MHz input sample rate and 25 kHz output channel rate with a desired 12.5 kHz channel
* pass band might be:
* sampleRate = 2,400,000.0
* decimation = 96
* passFrequency = 6250 (1/4 of the channel rate)
* stopFrequency = 7000
*
* Note: the transition bandwidth (stop - start) directly impacts the size of the low-pass cleanup filter,
* where smaller transition bandwidth creates a longer filter that requires more processing effort.
*
* @param sampleRate of the input sample stream
* @param decimation - overall decimation rate
* @param passFrequency for the final cleanup filter
* @param stopFrequency for the final cleanup filter
* @throws FilterDesignException if a final low-pass cleanup filter cannot be created for the output channel rate
* and specified pass/stop frequencies.
*/
public ComplexPrimeCICDecimate2(double sampleRate, int decimation, int passFrequency, int stopFrequency)
throws FilterDesignException
{
Validate.isTrue(decimation <= PRIMES[PRIMES.length - 1]);
List<Integer> stageSizes = getPrimeFactors(decimation);
for(int x = 0; x < stageSizes.size(); x++)
{
DecimatingStage stage = new DecimatingStage(stageSizes.get(x), 1);
mDecimatingStages.add(stage);
if(x == 0)
{
/* Reference to first stage -- will receive all samples */
mFirstDecimatingStage = stage;
}
else
{
/* Wire the current stage to the previous stage */
mDecimatingStages.get(x - 1).setListener(stage);
}
}
double channelRate = sampleRate / (double)decimation;
mOutput = new Output(channelRate, passFrequency, stopFrequency);
mDecimatingStages.get(mDecimatingStages.size() - 1).setListener(mOutput);
}
public void dispose()
{
for(DecimatingStage stage : mDecimatingStages)
{
stage.dispose();
}
mDecimatingStages.clear();
mDecimatingStages = null;
mFirstDecimatingStage = null;
mOutput.dispose();
mOutput = null;
}
/**
* Adds a listener to receive the output of this CIC decimation filter
*/
public void setListener(Listener<ReusableComplexBuffer> listener)
{
mOutput.setListener(listener);
}
/**
* Removes listener from output of this CIC decimation filter
*/
public void removeListener()
{
mOutput.removeListener();
}
/**
* Calculates the prime factors of the decimation rate. If you wish to have decimation rates higher than the
* highest rate listed in the PRIMES array, then add additional prime factors to the PRIMES array.
*
* @param decimation - integral decimation rate
* @return - ordered list (smallest to largest) of prime factors
*/
public static List<Integer> getPrimeFactors(int decimation)
{
if(decimation > PRIMES[PRIMES.length - 1])
{
throw new IllegalArgumentException("Decimation rates higher than [" + PRIMES[PRIMES.length - 1] +
"] are not supported. Update the PRIMES array to add support for higher decimation rates");
}
if(sPrimeFactors.containsKey(decimation))
{
return sPrimeFactors.get(decimation);
}
List<Integer> primeFactors = new ArrayList<Integer>();
int pointer = 0;
while(decimation > 0 && pointer < PRIMES.length)
{
int prime = PRIMES[pointer];
if(decimation % prime == 0)
{
primeFactors.add(prime);
decimation /= prime;
}
else
{
pointer++;
}
}
sPrimeFactors.put(decimation, primeFactors);
return primeFactors;
}
/**
* Primary input method for receiving sample arrays composed as I,Q,I,Q, etc.
*/
@Override
public void receive(ReusableComplexBuffer buffer)
{
if(mFirstDecimatingStage != null)
{
float[] samples = buffer.getSamples();
for(int x = 0; x < samples.length; x += 2)
{
mFirstDecimatingStage.receive(samples[x], samples[x + 1]);
}
}
buffer.decrementUserCount();
}
/**
* Decimating stage combines multiple CIC stages with a decimator. The
* number of stages is indicated by the order value and the size indicates
* the decimation rate of this stage.
*/
public class DecimatingStage implements ComplexSampleListener
{
private List<Stage> mStages = new ArrayList<Stage>();
private Stage mFirstStage;
private ComplexDecimator mDecimator;
public DecimatingStage(int size, int order)
{
for(int x = 0; x < order; x++)
{
Stage stage;
if(size == 2)
{
stage = new TwoStage();
}
else
{
stage = new Stage(size);
}
mStages.add(stage);
if(x == 0)
{
mFirstStage = stage;
}
else
{
mStages.get(x - 1).setListener(stage);
}
}
mDecimator = new ComplexDecimator(size);
mStages.get(mStages.size() - 1).setListener(mDecimator);
}
public void dispose()
{
for(Stage stage : mStages)
{
stage.dispose();
}
mStages.clear();
mDecimator.dispose();
mDecimator = null;
mFirstStage = null;
mStages = null;
}
@Override
public void receive(float i, float q)
{
mFirstStage.receive(i, q);
}
public void setListener(ComplexSampleListener listener)
{
mDecimator.setListener(listener);
}
}
/**
* Single non-decimating CIC stage component. Uses a circular buffer and a running average internally to implement
* the stage so that stage size has essentially no impact on the computational requirements of the stage
*/
public class Stage implements ComplexSampleListener
{
protected ComplexSampleListener mListener;
private float[] mISamples;
private float[] mQSamples;
protected float mISum;
protected float mQSum;
private int mSamplePointer = 0;
private int mSize;
protected float mGain;
protected Stage()
{
}
public Stage(int size)
{
mSize = size - 1;
mISamples = new float[mSize];
mQSamples = new float[mSize];
float baseGain = 1.0f / (float)size;
mGain = baseGain * (1.0f / (1.0f - baseGain));
}
public void dispose()
{
mListener = null;
}
public void receive(float i, float q)
{
/* Subtract the oldest sample and add in the newest sample */
mISum = mISum - mISamples[mSamplePointer] + i;
mQSum = mQSum - mQSamples[mSamplePointer] + q;
/* Overwrite the oldest sample with the newest */
mISamples[mSamplePointer] = i;
mQSamples[mSamplePointer] = q;
mSamplePointer++;
if(mSamplePointer >= mSize)
{
mSamplePointer = 0;
}
if(mListener != null)
{
mListener.receive((mISum * mGain), (mQSum * mGain));
}
}
public void setListener(ComplexSampleListener listener)
{
mListener = listener;
}
}
/**
* Size 2 stage that eliminates the unnecessary circular buffer management for a two-stage.
*/
public class TwoStage extends Stage
{
public TwoStage()
{
mGain = 0.5f;
}
public void receive(float i, float q)
{
if(mListener != null)
{
mListener.receive(((mISum + i) * mGain), ((mQSum + q) * mGain));
}
mISum = i;
mQSum = q;
}
}
/**
* Output adapter - applies gain correction and cleanup filter and broadcast
* to registered listener.
*/
public class Output implements ComplexSampleListener
{
/* Decimated output buffers will contain 1024 complex samples */
private ReusableComplexBufferAssembler mBufferAssembler;
private ComplexFIRFilter2 mLowPassFilter;
private Listener<ReusableComplexBuffer> mReusableComplexBufferListener;
public Output(double outputSampleRate, int passFrequency, int stopFrequency) throws FilterDesignException
{
mBufferAssembler = new ReusableComplexBufferAssembler(2400, outputSampleRate);
//This may throw an exception if we can't design a filter for the sample rate and pass/stop frequencies
float[] filterCoefficients = getLowPassFilter(outputSampleRate, passFrequency, stopFrequency);
mLowPassFilter = new ComplexFIRFilter2(filterCoefficients, 1.0f);
mBufferAssembler.setListener(new Listener<ReusableComplexBuffer>()
{
@Override
public void receive(ReusableComplexBuffer reusableComplexBuffer)
{
if(mReusableComplexBufferListener != null)
{
ReusableComplexBuffer filteredBuffer = mLowPassFilter.filter(reusableComplexBuffer);
mReusableComplexBufferListener.receive(filteredBuffer);
// mReusableComplexBufferListener.receive(reusableComplexBuffer);
}
else
{
reusableComplexBuffer.decrementUserCount();
}
}
});
}
public void dispose()
{
mBufferAssembler.dispose();
mLowPassFilter.dispose();
}
/**
* Interface for receiving CIC decimated output samples
*/
@Override
public void receive(float inphase, float quadrature)
{
mBufferAssembler.receive(inphase, quadrature);
}
public void setListener(Listener<ReusableComplexBuffer> listener)
{
mReusableComplexBufferListener = listener;
}
public void removeListener()
{
mReusableComplexBufferListener = null;
}
/**
* Creates a low-pass filter to use as the final cleanup filter for the decimated output stream
*
* @param sampleRate for the final output channel rate
* @param passFrequency for half of the desired channel rate
* @param stopFrequency for the attenuated band
* @return a newly designed filter or a previously designed (cached) filter
* @throws FilterDesignException
*/
private float[] getLowPassFilter(double sampleRate, int passFrequency, int stopFrequency) throws FilterDesignException
{
//Use existing filter if we've already designed one
if(sLowPassFilters.containsKey((int)sampleRate))
{
return sLowPassFilters.get((int)sampleRate);
}
FIRFilterSpecification specification = FIRFilterSpecification.lowPassBuilder()
.sampleRate(sampleRate)
.gridDensity(16)
.passBandCutoff(passFrequency)
.passBandAmplitude(1.0)
.passBandRipple(0.01)
.stopBandStart(stopFrequency)
.stopBandAmplitude(0.0)
.stopBandRipple(0.01)
.build();
RemezFIRFilterDesigner designer = new RemezFIRFilterDesigner(specification);
//This will throw an exception if the filter cannot be designed
float[] taps = designer.getImpulseResponse();
sLowPassFilters.put((int)sampleRate, taps);
return taps;
}
}
}

View File

@@ -8,6 +8,8 @@ import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
public class FilterViewer extends Application
{
private final static Logger mLog = LoggerFactory.getLogger(FilterViewer.class);
@@ -39,16 +41,27 @@ public class FilterViewer extends Application
*/
private float[] getFilter()
{
FIRFilterSpecification specification = FIRFilterSpecification.lowPassBuilder()
.sampleRate(25000)
.gridDensity(16)
.passBandCutoff(6000)
.passBandAmplitude(1.0)
.passBandRipple(0.01)
.stopBandStart(6700)
.stopBandAmplitude(0.0)
.stopBandRipple(0.03)
.build();
// FIRFilterSpecification specification = FIRFilterSpecification.lowPassBuilder()
// .sampleRate(8000)
// .sampleRate(100)
// .gridDensity(16)
// .oddLength(true)
// .passBandCutoff(300)
// .passBandCutoff(21)
// .passBandAmplitude(1.0)
// .passBandRipple(0.01)
// .stopBandStart(500)
// .stopBandStart(29)
// .stopBandAmplitude(0.0)
// .stopBandRipple(0.03) //Approximately 60 dB attenuation
// .stopBandRipple(0.01)
// .build();
//
// FIRFilterSpecification specification = FIRFilterSpecification.highPassBuilder()
@@ -61,15 +74,15 @@ public class FilterViewer extends Application
// .passBandRipple(0.01)
// .build();
FIRFilterSpecification specification = FIRFilterSpecification.bandPassBuilder()
.sampleRate(8000)
.stopFrequency1(1000)
.passFrequencyBegin(1100)
.passFrequencyEnd(1900)
.stopFrequency2(2000)
.stopRipple(0.000001)
.passRipple(0.00001)
.build();
// FIRFilterSpecification specification = FIRFilterSpecification.bandPassBuilder()
// .sampleRate(8000)
// .stopFrequency1(1000)
// .passFrequencyBegin(1100)
// .passFrequencyEnd(1900)
// .stopFrequency2(2000)
// .stopRipple(0.000001)
// .passRipple(0.00001)
// .build();
float[] taps = null;
@@ -102,7 +115,19 @@ public class FilterViewer extends Application
throw new IllegalStateException("Couldn't design filter");
}
// try
// {
// taps = FilterFactory.getSincM2Synthesizer(0.25, 2, 9);
// taps = FilterFactory.getSincM2Channelizer(12500.0, 2, 9, true);
// taps = FilterFactory.getSincM2Synthesizer( 25000.0, 12500.0, 2, 9);
//
// }
// catch(FilterDesignException e)
// {
// e.printStackTrace();
// }
mLog.debug(Arrays.toString(taps));
return taps;
}
}

View File

@@ -0,0 +1,108 @@
/*******************************************************************************
* sdr-trunk
* Copyright (C) 2014-2018 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.halfband.complex;
import io.github.dsheirer.dsp.filter.Filters;
import io.github.dsheirer.dsp.filter.halfband.real.HalfBandFilter2;
import io.github.dsheirer.dsp.mixer.Oscillator;
import io.github.dsheirer.sample.buffer.ReusableComplexBuffer;
import io.github.dsheirer.sample.buffer.ReusableComplexBufferQueue;
public class ComplexHalfBandFilter2
{
private ReusableComplexBufferQueue mBufferQueue = new ReusableComplexBufferQueue("ComplexHalfBandFilter2");
private HalfBandFilter2 mIFilter;
private HalfBandFilter2 mQFilter;
private float mResidualISample;
private float mResidualQSample;
private boolean mHasResidual;
private int mOutputBufferLength;
private float[] mSamples;
private int mSamplesPointer;
private float[] mFilteredSamples;
private int mFilteredSamplesPointer;
public ComplexHalfBandFilter2(float[] coefficients, float gain)
{
mIFilter = new HalfBandFilter2(coefficients, gain);
mQFilter = new HalfBandFilter2(coefficients, gain);
}
public ReusableComplexBuffer filter(ReusableComplexBuffer originalBuffer)
{
mSamples = originalBuffer.getSamples();
mSamplesPointer = 0;
mOutputBufferLength = (mHasResidual ? (mSamples.length + 2) : mSamples.length) / 4 * 2;
ReusableComplexBuffer filteredBuffer = mBufferQueue.getBuffer(mOutputBufferLength);
mFilteredSamples = filteredBuffer.getSamples();
mFilteredSamplesPointer = 0;
if(mHasResidual)
{
mFilteredSamples[mFilteredSamplesPointer++] = mIFilter.filter(mResidualISample, mSamples[mSamplesPointer++]);
mFilteredSamples[mFilteredSamplesPointer++] = mQFilter.filter(mResidualQSample, mSamples[mSamplesPointer++]);
mHasResidual = false;
}
while(mSamplesPointer + 3 < mSamples.length)
{
mFilteredSamples[mFilteredSamplesPointer++] = mIFilter.filter(mSamples[mSamplesPointer], mSamples[mSamplesPointer + 2]);
mFilteredSamples[mFilteredSamplesPointer++] = mIFilter.filter(mSamples[mSamplesPointer + 1], mSamples[mSamplesPointer + 3]);
mSamplesPointer += 4;
}
if(mSamplesPointer < mSamples.length)
{
mHasResidual = true;
mResidualISample = mSamples[mSamplesPointer++];
mResidualQSample = mSamples[mSamplesPointer];
}
originalBuffer.decrementUserCount();
return filteredBuffer;
}
public static void main(String[] args)
{
Oscillator oscillator = new Oscillator(1, 16);
ComplexHalfBandFilter2 filter = new ComplexHalfBandFilter2(Filters.HALF_BAND_FILTER_27T.getCoefficients(), 1.0f);
ReusableComplexBufferQueue bufferQueue = new ReusableComplexBufferQueue("Test");
ReusableComplexBuffer complexBuffer = bufferQueue.getBuffer(8);
oscillator.generateComplex(complexBuffer);
ReusableComplexBuffer filteredBuffer = filter.filter(complexBuffer);
filteredBuffer.decrementUserCount();
ReusableComplexBuffer complexBuffer2 = bufferQueue.getBuffer(10);
oscillator.generateComplex(complexBuffer2);
ReusableComplexBuffer filteredBuffer2 = filter.filter(complexBuffer2);
filteredBuffer2.decrementUserCount();
ReusableComplexBuffer complexBuffer3 = bufferQueue.getBuffer(8);
oscillator.generateComplex(complexBuffer3);
ReusableComplexBuffer filteredBuffer3 = filter.filter(complexBuffer3);
filteredBuffer3.decrementUserCount();
}
}

View File

@@ -0,0 +1,89 @@
/*******************************************************************************
* sdr-trunk
* Copyright (C) 2014-2018 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.halfband.real;
/**
* Implements a half-band filter that produces one filtered output for every two input samples.
*
* This filter is structured for Java 8+ compiler JIT optimization for SIMD instructions when
* supported by the host CPU.
*/
public class HalfBandFilter2
{
private static final float CENTER_COEFFICIENT = 0.5f;
private float[] mCoefficients;
private float[] mEvenSamples;
private float[] mOddSamples;
private int mCenterSampleIndex;
private float mAccumulator;
private int mPointer;
private float mGain;
/**
* Creates a half band filter with inherent decimation by two.
*
* @param coefficients of the half-band filter that is odd-length where all odd index coefficients are
* zero valued except for the middle odd index coefficient which should be valued 0.5
* @param gain value to apply to the output
*/
public HalfBandFilter2(float[] coefficients, float gain)
{
if(coefficients.length % 2 == 0)
{
throw new IllegalArgumentException("Half-band filter coefficients must be odd-length");
}
mGain = gain;
int half = coefficients.length / 2 + 1;
mCoefficients = new float[half];
mEvenSamples = new float[half];
//Use only the even coefficients since the odd coefficients are all zero-valued
for(int x = 0; x < coefficients.length; x += 2)
{
mCoefficients[x / 2] = coefficients[x];
}
//Setup the odd samples array as a simple delay line, half the size of the even samples array
mOddSamples = new float[half / 2];
mCenterSampleIndex = mOddSamples.length - 1;
}
public float filter(float sample1, float sample2)
{
//Structured for SIMD array copy optimization when supported by host CPU
System.arraycopy(mEvenSamples, 0, mEvenSamples, 1, mEvenSamples.length - 1);
System.arraycopy(mOddSamples, 0, mOddSamples, 1, mOddSamples.length - 1);
mEvenSamples[0] = sample1;
mOddSamples[0] = sample2;
mAccumulator = 0.0f;
//Structured for SIMD dot.product optimization when supported by host CPU
for(mPointer = 0; mPointer < mEvenSamples.length; mPointer++)
{
mAccumulator += (mEvenSamples[mPointer] * mCoefficients[mPointer]);
}
mAccumulator += (mOddSamples[mCenterSampleIndex] * CENTER_COEFFICIENT);
mAccumulator *= mGain;
return mAccumulator;
}
}

View File

@@ -127,8 +127,8 @@ public abstract class AbstractOscillator implements IOscillator
for(int x = 0; x < sampleCount; x++)
{
samples[x] = inphase();
rotate();
samples[x] = quadrature();
}
return samples;

View File

@@ -18,6 +18,10 @@ package io.github.dsheirer.dsp.mixer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class LowPhaseNoiseOscillator extends AbstractOscillator
{
private final static Logger mLog = LoggerFactory.getLogger(LowPhaseNoiseOscillator.class);
@@ -82,4 +86,54 @@ public class LowPhaseNoiseOscillator extends AbstractOscillator
//Update the gain value for the next rotation
mGain = THREE_HALVES - ((mPreviousInphase * mPreviousInphase) + (mPreviousQuadrature * mPreviousQuadrature));
}
public static long process(AbstractOscillator oscillator, int iterations, int sampleCount)
{
long start = System.nanoTime();
for(int x = 0; x < iterations; x++)
{
float[] samples = oscillator.generateComplex(sampleCount);
}
long duration = System.nanoTime() - start;
return duration;
}
public static void main(String[] args)
{
mLog.debug("Starting ...");
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss.SSS");
AbstractOscillator lpno = new LowPhaseNoiseOscillator(100, 800);
AbstractOscillator regular = new Oscillator(100, 800);
int iterations = 10000;
int sampleCount = 1000000;
mLog.debug("Warm up - lpno");
process(lpno, iterations, sampleCount);
process(lpno, iterations, sampleCount);
process(lpno, iterations, sampleCount);
mLog.debug("Warm up - regular");
process(regular, iterations, sampleCount);
process(regular, iterations, sampleCount);
process(regular, iterations, sampleCount);
mLog.debug("Test - lpno");
process(lpno, iterations, sampleCount);
process(lpno, iterations, sampleCount);
long duration = process(lpno, iterations, sampleCount);
mLog.debug("Duration Low Phase Noise: " + sdf.format(new Date(TimeUnit.NANOSECONDS.toMillis(duration))));
mLog.debug("Test - regular");
process(regular, iterations, sampleCount);
process(regular, iterations, sampleCount);
duration = process(regular, iterations, sampleCount);
mLog.debug("Duration Regular: " + sdf.format(new Date(TimeUnit.NANOSECONDS.toMillis(duration))));
// mLog.debug(" Delta: " + sdf.format(new Date(TimeUnit.NANOSECONDS.toMillis(duration2 - duration))));
}
}