[WIP] Updating dsp modules to accept variable sample rate I/Q streams

and creating a new audio resampler.
This commit is contained in:
Denny
2018-02-09 05:29:07 -05:00
parent 96d9d6f750
commit 28cb9d9407
15 changed files with 369 additions and 171 deletions

View File

@@ -51,7 +51,7 @@ public class FilterFactory
* @param window - to apply against the coefficients
* @return
*/
public static float[] getSinc(int sampleRate, long frequency, int length, Window.WindowType window)
public static float[] getSinc(double sampleRate, long frequency, int length, Window.WindowType window)
{
int evenLength = length % 2 == 0 ? length : length + 1;
@@ -137,7 +137,7 @@ public class FilterFactory
* @param length
* @return
*/
public static float[] getUnityResponseArray(int sampleRate, long frequency, int length)
public static float[] getUnityResponseArray(double sampleRate, long frequency, int length)
{
float[] unityArray = new float[length * 2];
@@ -201,7 +201,7 @@ public class FilterFactory
* @param windowType - window to apply against the generated coefficients
* @return
*/
public static float[] getLowPass(int sampleRate, long cutoff, int filterLength, Window.WindowType windowType)
public static float[] getLowPass(double sampleRate, long cutoff, int filterLength, Window.WindowType windowType)
{
return getSinc(sampleRate, cutoff, filterLength, windowType);
}
@@ -220,7 +220,7 @@ public class FilterFactory
* - passFrequency < stopFrequency
* - stopFrequency <= sampleRate/2
*/
public static float[] getLowPass(int sampleRate, int passFrequency, int stopFrequency, int attenuation,
public static float[] getLowPass(double sampleRate, int passFrequency, int stopFrequency, int attenuation,
Window.WindowType windowType, boolean forceOddLength)
{
if(stopFrequency < passFrequency || stopFrequency > (sampleRate / 2))
@@ -323,9 +323,9 @@ public class FilterFactory
* @param attenuation in dB
* @return
*/
public static int getTapCount(int sampleRate, long pass, long stop, int attenuation)
public static int getTapCount(double sampleRate, long pass, long stop, int attenuation)
{
double frequency = ((double)stop - (double)pass) / (double)sampleRate;
double frequency = ((double)stop - (double)pass) / sampleRate;
return (int)(Math.round((double)attenuation / (22.0d * frequency)));
}

View File

@@ -292,6 +292,10 @@ public class PolyphaseChannelManager implements ISourceEventProcessor
//Defer channelizer configuration changes to be handled on the buffer processor thread
mBufferSourceEventMonitor.receive(sourceEvent);
break;
case NOTIFICATION_FREQUENCY_AND_SAMPLE_RATE_LOCKED:
case NOTIFICATION_FREQUENCY_AND_SAMPLE_RATE_UNLOCKED:
//no-op
break;
default:
mLog.info("Unrecognized source event: " + sourceEvent.toString());
break;

View File

@@ -1,8 +1,7 @@
package io.github.dsheirer.dsp.filter.design;
import io.github.dsheirer.dsp.filter.FilterFactory;
import io.github.dsheirer.dsp.filter.Window;
import io.github.dsheirer.dsp.filter.fir.FIRFilterSpecification;
import io.github.dsheirer.dsp.filter.fir.remez.RemezFIRFilterDesigner;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
@@ -41,14 +40,15 @@ public class FilterViewer extends Application
private float[] getFilter()
{
FIRFilterSpecification specification = FIRFilterSpecification.lowPassBuilder()
.sampleRate(48000)
.sampleRate(25000)
.gridDensity(16)
.passBandCutoff(2500)
.oddLength(true)
.passBandCutoff(6000)
.passBandAmplitude(1.0)
.passBandRipple(0.01)
.stopBandStart(4000)
.stopBandStart(6500)
.stopBandAmplitude(0.0)
.stopBandRipple(0.008)
.stopBandRipple(0.028) //Approximately 60 dB attenuation
.build();
// FIRFilterSpecification specification = FIRFilterSpecification.highPassBuilder()
@@ -73,29 +73,29 @@ public class FilterViewer extends Application
float[] taps = null;
try
{
taps = FilterFactory.getSincM2Channelizer(12500.0, 2, 19, Window.WindowType.BLACKMAN_HARRIS_7, true);
}
catch(FilterDesignException fde)
{
mLog.error("Error");
}
// try
// {
// RemezFIRFilterDesigner designer = new RemezFIRFilterDesigner(specification);
//
// if(designer.isValid())
// {
// taps = designer.getImpulseResponse();
// }
// taps = FilterFactory.getSincM2Channelizer(12500.0, 2, 19, Window.WindowType.BLACKMAN_HARRIS_7, true);
// }
// catch(FilterDesignException fde)
// {
// mLog.error("Filter design error", fde);
// mLog.error("Error");
// }
try
{
RemezFIRFilterDesigner designer = new RemezFIRFilterDesigner(specification);
if(designer.isValid())
{
taps = designer.getImpulseResponse();
}
}
catch(FilterDesignException fde)
{
mLog.error("Filter design error", fde);
}
return taps;
}
}

View File

@@ -295,8 +295,9 @@ public class FIRFilterSpecification
*/
public static class LowPassBuilder
{
private int mSampleRate;
private double mSampleRate;
private int mOrder;
private Boolean mOddLength;
private int mGridDensity = 16;
private int mPassBandEndFrequency;
private int mStopBandStartFrequency;
@@ -309,7 +310,7 @@ public class FIRFilterSpecification
{
}
public LowPassBuilder sampleRate(int sampleRateHz)
public LowPassBuilder sampleRate(double sampleRateHz)
{
mSampleRate = sampleRateHz;
return this;
@@ -325,6 +326,16 @@ public class FIRFilterSpecification
return this;
}
/**
* Indicates if the filters should be an odd-length (even order) filter. This will override any requested
* filter order if it is set, otherwise the filter length will be determined from the filter order.
*/
public LowPassBuilder oddLength(boolean oddLength)
{
mOddLength = oddLength;
return this;
}
public LowPassBuilder gridDensity(int density)
{
mGridDensity = density;
@@ -371,13 +382,32 @@ public class FIRFilterSpecification
{
if(mOrder < 6)
{
mOrder = estimateFilterOrder(mSampleRate, mPassBandEndFrequency,
mStopBandStartFrequency, mPassBandRipple, mStopBandRipple);
mOrder = estimateFilterOrder(mSampleRate, mPassBandEndFrequency, mStopBandStartFrequency,
mPassBandRipple, mStopBandRipple);
}
FIRLinearPhaseFilterType type = (mOrder % 2 == 0
? FIRLinearPhaseFilterType.TYPE_1_ODD_LENGTH_EVEN_ORDER_SYMMETRICAL
: FIRLinearPhaseFilterType.TYPE_2_EVEN_LENGTH_ODD_ORDER_SYMMETRICAL);
FIRLinearPhaseFilterType type = null;
if(mOddLength != null)
{
if(mOddLength)
{
type = FIRLinearPhaseFilterType.TYPE_1_ODD_LENGTH_EVEN_ORDER_SYMMETRICAL;
//Force order to even
mOrder += mOrder % 2;
}
else
{
type = FIRLinearPhaseFilterType.TYPE_2_EVEN_LENGTH_ODD_ORDER_SYMMETRICAL;
//Force order to odd
mOrder += (mOrder % 2 == 0 ? 1 : 0);
}
}
else
{
type = (mOrder % 2 == 0 ? FIRLinearPhaseFilterType.TYPE_1_ODD_LENGTH_EVEN_ORDER_SYMMETRICAL
: FIRLinearPhaseFilterType.TYPE_2_EVEN_LENGTH_ODD_ORDER_SYMMETRICAL);
}
FIRFilterSpecification spec = new FIRFilterSpecification(type, mOrder, mGridDensity);
@@ -386,7 +416,7 @@ public class FIRFilterSpecification
spec.addFrequencyBand(passBand);
FrequencyBand stopBand = new FrequencyBand(mSampleRate, mStopBandStartFrequency,
mSampleRate / 2, mStopBandAmplitude, mStopBandRipple);
(int)(mSampleRate / 2), mStopBandAmplitude, mStopBandRipple);
spec.addFrequencyBand(stopBand);
return spec;
@@ -398,7 +428,7 @@ public class FIRFilterSpecification
*/
public static class HighPassBuilder
{
private int mSampleRate;
private double mSampleRate;
private int mOrder = -1;
private int mGridDensity = 16;
private int mPassBandStartFrequency;
@@ -412,7 +442,7 @@ public class FIRFilterSpecification
{
}
public HighPassBuilder sampleRate(int sampleRateHz)
public HighPassBuilder sampleRate(double sampleRateHz)
{
mSampleRate = sampleRateHz;
return this;
@@ -474,8 +504,8 @@ public class FIRFilterSpecification
{
if(mOrder < 6)
{
mOrder = estimateFilterOrder(mSampleRate, mStopBandEndFrequency,
mPassBandStartFrequency, mPassBandRipple, mStopBandRipple);
mOrder = estimateFilterOrder(mSampleRate, mStopBandEndFrequency, mPassBandStartFrequency,
mPassBandRipple, mStopBandRipple);
}
// High pass is a Type 1 filter -- ensure order is even
@@ -493,7 +523,7 @@ public class FIRFilterSpecification
spec.addFrequencyBand(stopBand);
FrequencyBand passBand = new FrequencyBand(mSampleRate, mPassBandStartFrequency,
mSampleRate / 2, mPassBandAmplitude, mPassBandRipple);
(int)(mSampleRate / 2), mPassBandAmplitude, mPassBandRipple);
spec.addFrequencyBand(passBand);
return spec;
@@ -507,7 +537,7 @@ public class FIRFilterSpecification
{
private int mOrder;
private int mGridDensity = 16;
private int mSampleRate;
private double mSampleRate;
private int mStopFrequency1;
private int mPassFrequencyBegin;
private int mPassFrequencyEnd;
@@ -544,7 +574,7 @@ public class FIRFilterSpecification
/**
* Sets the filter sample rate in hertz
*/
public BandPassBuilder sampleRate(int sampleRate)
public BandPassBuilder sampleRate(double sampleRate)
{
mSampleRate = sampleRate;
return this;
@@ -644,15 +674,13 @@ public class FIRFilterSpecification
if(mPassFrequencyEnd >= mStopFrequency2)
{
throw new IllegalArgumentException("Pass band end frequency [" + mPassFrequencyEnd
+ "] must be less than stop band 2 beginning frequency [" + mStopFrequency2
+ "]");
+ "] must be less than stop band 2 beginning frequency [" + mStopFrequency2 + "]");
}
if(mStopFrequency2 >= (mSampleRate / 2))
{
throw new IllegalArgumentException("Stop band 2 beginning frequency ["
+ mStopFrequency2 + "] must be less than half of the sample rate ["
+ (mSampleRate / 2) + "]");
throw new IllegalArgumentException("Stop band 2 beginning frequency [" + mStopFrequency2 +
"] must be less than half of the sample rate [" + (mSampleRate / 2) + "]");
}
}
@@ -693,7 +721,7 @@ public class FIRFilterSpecification
public static class ChannelizerBuilder
{
private int mGridDensity = 16;
private int mSampleRate;
private double mSampleRate;
private int mChannelCount;
private int mChannelBandwidth;
private int mTapsPerChannel;
@@ -708,7 +736,7 @@ public class FIRFilterSpecification
/**
* Sets the filter sample rate in hertz
*/
public ChannelizerBuilder sampleRate(int sampleRate)
public ChannelizerBuilder sampleRate(double sampleRate)
{
mSampleRate = sampleRate;
return this;
@@ -805,7 +833,7 @@ public class FIRFilterSpecification
FIRFilterSpecification spec = new FIRFilterSpecification(type, order, mGridDensity);
double bandEdge = (double)mChannelBandwidth / (double)mSampleRate;
double bandEdge = (double)mChannelBandwidth / mSampleRate;
double passEnd = bandEdge * (1.0 - mAlpha);
double stopStart = bandEdge * (1.0 + mAlpha);
@@ -877,15 +905,13 @@ public class FIRFilterSpecification
* @param stopBandRipple stop band ripple in dB
* @return estimated filter length
*/
public static int estimateFilterOrder(int sampleRate, int frequency1, int frequency2,
public static int estimateFilterOrder(double sampleRate, int frequency1, int frequency2,
double passBandRipple, double stopBandRipple)
{
double df = Math.abs(frequency2 - frequency1) / (double)sampleRate;
double df = Math.abs(frequency2 - frequency1) / sampleRate;
double ddp = Math
.log10(stopBandRipple <= passBandRipple ? passBandRipple : stopBandRipple);
double dds = Math
.log10(stopBandRipple <= passBandRipple ? stopBandRipple : passBandRipple);
double ddp = Math.log10(stopBandRipple <= passBandRipple ? passBandRipple : stopBandRipple);
double dds = Math.log10(stopBandRipple <= passBandRipple ? stopBandRipple : passBandRipple);
double a1 = 5.309e-3;
double a2 = 7.114e-2;
@@ -917,12 +943,12 @@ public class FIRFilterSpecification
* @param passBandRippleDb
* @param stopBandRippleDb
*/
public static int estimateBandPassOrder(int sampleRate, int passBandStart, int passBandEnd,
public static int estimateBandPassOrder(double sampleRate, int passBandStart, int passBandEnd,
double passBandRippleDb, double stopBandRippleDb)
{
double df = (double)Math.abs(passBandEnd - passBandStart) / (double)sampleRate;
double ddp = (double)Math.log10(passBandRippleDb);
double dds = (double)Math.log10(stopBandRippleDb);
double df = (double)Math.abs(passBandEnd - passBandStart) / sampleRate;
double ddp = Math.log10(passBandRippleDb);
double dds = Math.log10(stopBandRippleDb);
double a1 = 0.01201;
double a2 = 0.09664;
@@ -987,13 +1013,13 @@ public class FIRFilterSpecification
* @param amplitude of the frequency band relative to unity (0.0 - 1.0)
* @param ripple of the frequency band specified as db ripple
*/
public FrequencyBand(int sampleRate, int start, int end, double amplitude, double ripple)
public FrequencyBand(double sampleRate, int start, int end, double amplitude, double ripple)
{
this((double)start / (double)sampleRate, (double)end / (double)sampleRate,
this((double)start / sampleRate, (double)end / sampleRate,
amplitude, ripple);
}
public FrequencyBand(int sampleRate, int start, int end, double amplitude, double ripple, double weight)
public FrequencyBand(double sampleRate, int start, int end, double amplitude, double ripple, double weight)
{
this(sampleRate, start, end, amplitude, ripple);
mWeight = weight;

View File

@@ -3,52 +3,65 @@ package io.github.dsheirer.dsp.filter.fir.complex;
import io.github.dsheirer.sample.Listener;
import io.github.dsheirer.sample.complex.ComplexBuffer;
public class ComplexFIRFilter_CB_CB extends ComplexFIRFilter
implements Listener<ComplexBuffer>
public class ComplexFIRFilter_CB_CB extends ComplexFIRFilter implements Listener<ComplexBuffer>
{
private Listener<ComplexBuffer> mListener;
/**
* Complex FIR Filter for processing complex buffers. Wraps two real FIR
* filters for processing each of the inphase and quadrature samples.
*
* @param coefficients - odd length symmetric filter taps
* @param gain - gain value to apply to each of the filtered samples
*/
public ComplexFIRFilter_CB_CB( float[] coefficients, float gain )
{
super( coefficients, gain );
}
public void dispose()
{
mListener = null;
}
private Listener<ComplexBuffer> mListener;
@Override
public void receive( ComplexBuffer buffer )
{
if( mListener != null )
{
float[] samples = buffer.getSamples();
for( int x = 0; x < samples.length; x += 2 )
{
samples[ x ] = filterInphase( samples[ x ] );
samples[ x + 1 ] = filterQuadrature( samples[ x + 1 ] );
}
mListener.receive( buffer );
}
}
public void setListener( Listener<ComplexBuffer> listener )
{
mListener = listener;
}
public void removeListener()
{
mListener = null;
}
/**
* Complex FIR Filter for processing complex buffers. Wraps two real FIR
* filters for processing each of the inphase and quadrature samples.
*
* @param coefficients - odd length symmetric filter taps
* @param gain - gain value to apply to each of the filtered samples
*/
public ComplexFIRFilter_CB_CB(float[] coefficients, float gain)
{
super(coefficients, gain);
}
public void dispose()
{
mListener = null;
}
@Override
public void receive(ComplexBuffer buffer)
{
if(mListener != null)
{
float[] samples = buffer.getSamples();
for(int x = 0; x < samples.length; x += 2)
{
samples[x] = filterInphase(samples[x]);
samples[x + 1] = filterQuadrature(samples[x + 1]);
}
mListener.receive(buffer);
}
}
/**
* Filters the samples and send the results to the registered listener
*/
public void receive(float[] samples)
{
if(mListener != null)
{
float[] filtered = filter(samples);
//TODO: this should be a reusable buffer here.
mListener.receive(new ComplexBuffer(filtered));
}
}
public void setListener(Listener<ComplexBuffer> listener)
{
mListener = listener;
}
public void removeListener()
{
mListener = null;
}
}

View File

@@ -0,0 +1,70 @@
/*******************************************************************************
* 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.resample;
import com.laszlosystems.libresample4j.Resampler;
import io.github.dsheirer.module.Module;
import io.github.dsheirer.sample.Listener;
import io.github.dsheirer.source.ISourceEventListener;
import io.github.dsheirer.source.SourceEvent;
public class AudioResampler extends Module implements ISourceEventListener
{
private Resampler mResampler;
public AudioResampler(double inputRate, double outputRate)
{
double resampleRate = outputRate / inputRate;
mResampler = new Resampler(true, resampleRate, resampleRate);
}
public float[] resample(float[] samples)
{
return null;
}
@Override
public void reset()
{
}
@Override
public void start()
{
}
@Override
public void stop()
{
}
@Override
public void dispose()
{
}
@Override
public Listener<SourceEvent> getSourceEventListener()
{
//TODO: we should get the channel sample rate and setup the resampler to output 8 kHz audio
return null;
}
}