Merge branch 'master' into 39-polyphase-channelizer

# Conflicts:
#	src/source/tuner/TunerController.java
This commit is contained in:
Denny
2017-10-22 06:28:57 -04:00
2 changed files with 293 additions and 316 deletions

View File

@@ -7,7 +7,7 @@
<property name="major" value="0"/> <property name="major" value="0"/>
<property name="minor" value="3"/> <property name="minor" value="3"/>
<property name="patch" value="1"/> <property name="patch" value="1"/>
<property name="release" value="final"/> <property name="release" value=""/> <!-- Use dash (-) prefix (e.g. -beta1) -->
<property name="filename" value="sdrtrunk_${major}.${minor}.${patch}${release}"/> <property name="filename" value="sdrtrunk_${major}.${minor}.${patch}${release}"/>
<!-- Build properties --> <!-- Build properties -->
@@ -34,7 +34,7 @@
<mkdir dir="${output.classes}"/> <mkdir dir="${output.classes}"/>
<!-- Create version file --> <!-- Create version file -->
<echo message="${major}.${minor}.${patch}-${release}" file="${output.classes}/sdrtrunk-version"/> <echo message="${major}.${minor}.${patch}${release}" file="${output.classes}/sdrtrunk-version"/>
</target> </target>
<target name="compile" depends="clean,init,compile-java-8,compile-java-9" <target name="compile" depends="clean,init,compile-java-8,compile-java-9"

View File

@@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
* SDR Trunk * SDR Trunk
* Copyright (C) 2014-2016 Dennis Sheirer * Copyright (C) 2014-2017 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
@@ -17,13 +17,8 @@
******************************************************************************/ ******************************************************************************/
package source.tuner; package source.tuner;
import java.util.SortedSet;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.RejectedExecutionException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import source.SourceException; import source.SourceException;
import source.tuner.configuration.TunerConfiguration; import source.tuner.configuration.TunerConfiguration;
import source.SourceEvent; import source.SourceEvent;
@@ -33,51 +28,46 @@ import source.tuner.frequency.FrequencyController.Tunable;
import source.ISourceEventProcessor; import source.ISourceEventProcessor;
public abstract class TunerController implements Tunable, ISourceEventProcessor public abstract class TunerController implements Tunable, ISourceEventProcessor
import java.util.SortedSet;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.RejectedExecutionException;
public abstract class TunerController implements Tunable, IFrequencyChangeProcessor
{ {
private final static Logger mLog = private final static Logger mLog = LoggerFactory.getLogger(TunerController.class);
LoggerFactory.getLogger( TunerController.class );
/* List of currently tuned channels being served to demod channels */ /* List of currently tuned channels being served to demod channels */
private SortedSet<TunerChannel> mTunedChannels = new ConcurrentSkipListSet<>(); private SortedSet<TunerChannel> mTunedChannels = new ConcurrentSkipListSet<>();
protected FrequencyController mFrequencyController; protected FrequencyController mFrequencyController;
private int mMiddleUnusable; private int mMiddleUnusable;
private double mUsableBandwidthPercentage; private double mUsableBandwidthPercentage;
/** /**
* Abstract tuner controller class. The tuner controller manages frequency * Abstract tuner controller class. The tuner controller manages frequency bandwidth and currently tuned channels
* bandwidth and currently tuned channels that are being fed samples from * that are being fed samples from the tuner.
* the tuner. *
* * @param minimumFrequency minimum uncorrected tunable frequency
* @param minimumFrequency minimum uncorrected tunable frequency * @param maximumFrequency maximum uncorrected tunable frequency
* @param maximumFrequency maximum uncorrected tunable frequency * @param middleUnusable is the +/- value center DC spike to avoid for channels
* @param middleUnusable is the +/- value center DC spike to avoid for channels * @param usableBandwidth percentage of usable bandwidth relative to space at the extreme ends of the spectrum
* @param usableBandwidthPercentage is the unusable space at the extreme ends of the spectrum * @throws SourceException - for any issues related to constructing the
* * class, tuning a frequency, or setting the bandwidth
* @throws SourceException - for any issues related to constructing the */
* class, tuning a frequency, or setting the bandwidth public TunerController(long minimumFrequency, long maximumFrequency, int middleUnusable, double usableBandwidth)
*/ {
public TunerController( long minimumFrequency, mFrequencyController = new FrequencyController(this, minimumFrequency, maximumFrequency, 0.0d);
long maximumFrequency, mMiddleUnusable = middleUnusable;
int middleUnusable, mUsableBandwidthPercentage = usableBandwidth;
double usableBandwidthPercentage ) }
{
mFrequencyController = new FrequencyController( this,
minimumFrequency,
maximumFrequency,
0.0d );
mMiddleUnusable = middleUnusable;
mUsableBandwidthPercentage = usableBandwidthPercentage;
}
/** /**
* Applies the settings in the tuner configuration * Applies the settings in the tuner configuration
*/ */
public abstract void apply( TunerConfiguration config ) public abstract void apply(TunerConfiguration config) throws SourceException;
throws SourceException;
/** /**
* Responds to requests to set the frequency * Responds to requests to set the frequency
*/ */
@Override @Override
public void process(SourceEvent event ) throws SourceException public void process(SourceEvent event ) throws SourceException
{ {
@@ -87,9 +77,9 @@ public abstract class TunerController implements Tunable, ISourceEventProcessor
} }
} }
public int getBandwidth() public int getBandwidth()
{ {
return mFrequencyController.getBandwidth(); return mFrequencyController.getBandwidth();
} }
/** /**
@@ -98,281 +88,268 @@ public abstract class TunerController implements Tunable, ISourceEventProcessor
* @param frequency in hertz * @param frequency in hertz
* @throws SourceException - if the tuner has any issues * @throws SourceException - if the tuner has any issues
*/ */
public void setFrequency( long frequency ) throws SourceException public void setFrequency(long frequency) throws SourceException
{ {
mFrequencyController.setFrequency( frequency ); mFrequencyController.setFrequency(frequency);
} }
/** /**
* Gets the center frequency of the local oscillator * Gets the center frequency of the local oscillator
* *
* @return frequency in hertz * @return frequency in hertz
*/ */
public long getFrequency() public long getFrequency()
{ {
return mFrequencyController.getFrequency(); return mFrequencyController.getFrequency();
} }
public int getSampleRate() public int getSampleRate()
{ {
return mFrequencyController.getBandwidth(); return mFrequencyController.getBandwidth();
} }
public double getFrequencyCorrection() public double getFrequencyCorrection()
{ {
return mFrequencyController.getFrequencyCorrection(); return mFrequencyController.getFrequencyCorrection();
} }
public void setFrequencyCorrection( double correction ) throws SourceException public void setFrequencyCorrection(double correction) throws SourceException
{ {
mFrequencyController.setFrequencyCorrection( correction ); mFrequencyController.setFrequencyCorrection(correction);
} }
public long getMinFrequency() public long getMinFrequency()
{ {
return mFrequencyController.getMinimumFrequency(); return mFrequencyController.getMinimumFrequency();
} }
public long getMaxFrequency() public long getMaxFrequency()
{ {
return mFrequencyController.getMaximumFrequency(); return mFrequencyController.getMaximumFrequency();
} }
private long getMinTunedFrequency() throws SourceException private long getMinTunedFrequency() throws SourceException
{ {
return mFrequencyController.getFrequency() - ( getUsableBandwidth() / 2 ); return mFrequencyController.getFrequency() - (getUsableBandwidth() / 2);
} }
private long getMaxTunedFrequency() throws SourceException private long getMaxTunedFrequency() throws SourceException
{ {
return mFrequencyController.getFrequency() + ( getUsableBandwidth() / 2 ); return mFrequencyController.getFrequency() + (getUsableBandwidth() / 2);
} }
/** /**
* Indicates if channel along with all of the other currently sourced * Indicates if channel along with all of the other currently sourced
* channels can fit within the tunable bandwidth. * channels can fit within the tunable bandwidth.
*/ */
private boolean canTune( TunerChannel channel ) private boolean canTune(TunerChannel channel)
{ {
//Make sure we're within the tunable frequency range of this tuner //Make sure we're within the tunable frequency range of this tuner
if( getMinFrequency() < channel.getMinFrequency() && if(getMinFrequency() < channel.getMinFrequency() && getMaxFrequency() > channel.getMaxFrequency())
getMaxFrequency() > channel.getMaxFrequency() ) {
{ //If this is the first lock, then we're good
//If this is the first lock, then we're good if(mTunedChannels.isEmpty())
if( mTunedChannels.isEmpty() ) {
{ return true;
return true; }
} else
else {
{ int usableBandwidth = getUsableBandwidth();
int usableBandwidth = getUsableBandwidth(); long minLockedFrequency = mTunedChannels.first().getMinFrequency();
long minLockedFrequency = mTunedChannels.first().getMinFrequency(); long maxLockedFrequency = mTunedChannels.last().getMaxFrequency();
long maxLockedFrequency = mTunedChannels.last().getMaxFrequency();
//Requested channel is within current locked channel frequency range //Requested channel is within current locked channel frequency range
if( minLockedFrequency <= channel.getMinFrequency() && if(minLockedFrequency <= channel.getMinFrequency() && channel.getMaxFrequency() <= maxLockedFrequency)
channel.getMaxFrequency() <= maxLockedFrequency ) {
{ return true;
return true; }
}
//Requested channel is higher than min locked frequency //Requested channel is higher than min locked frequency
if( channel.getMaxFrequency() > minLockedFrequency && if(channel.getMaxFrequency() > minLockedFrequency &&
channel.getMaxFrequency() - minLockedFrequency <= usableBandwidth ) channel.getMaxFrequency() - minLockedFrequency <= usableBandwidth)
{ {
return true; return true;
} }
//Requested channel is lower than the max locked frequency
if( channel.getMinFrequency() <= maxLockedFrequency &&
maxLockedFrequency - channel.getMinFrequency() <= usableBandwidth )
{
return true;
}
}
}
return false; //Requested channel is lower than the max locked frequency
} if(channel.getMinFrequency() <= maxLockedFrequency &&
maxLockedFrequency - channel.getMinFrequency() <= usableBandwidth)
{
return true;
}
}
}
/** return false;
* Constructs a digital drop channel (DDC) as a tuner channel from from }
* the tuner, or returns null if the channel cannot be sourced from the
* tuner.
*
* This controller can't provide a channel if the channel, along with the
* current set of sourced channels, can't be accomodated within the current
* tuner bandwidth, or if a center frequency cannot be calculated that will
* ensure all of the channels can be accomodated and any defined central DC
* spike avoided.
*
* @param tuner to source the channel from
* @param channel with defined center frequency and bandwidth
*
* @return fully constructed tuner channel or null
*
* @throws RejectedExecutionException if the decimation processor has an error
*/
public TunerChannelSource getChannel( Tuner tuner, TunerChannel channel )
throws RejectedExecutionException
{
TunerChannelSource source = null;
if( canTune( channel ) ) /**
{ * Constructs a digital drop channel (DDC) as a tuner channel from from the tuner, or returns null if the channel
try * cannot be sourced from the tuner.
{ *
mTunedChannels.add( channel ); * This controller can't provide a channel if the channel, along with the current set of sourced channels, can't be
* accomodated within the current tuner bandwidth, or if a center frequency cannot be calculated that will ensure
* all of the channels can be accomodated and any defined central DC spike avoided.
*
* @param tuner to source the channel from
* @param channel with defined center frequency and bandwidth
* @return fully constructed tuner channel or null
* @throws RejectedExecutionException if the decimation processor has an error
*/
public TunerChannelSource getChannel(Tuner tuner, TunerChannel channel) throws RejectedExecutionException
{
TunerChannelSource source = null;
if( requiresLOUpdate( channel ) ) if(canTune(channel))
{ {
updateLOFrequency(); try
} {
mTunedChannels.add(channel);
source = new TunerChannelSource( tuner, channel ); if(requiresLOUpdate(channel))
} {
catch( SourceException se ) updateLOFrequency();
{ }
mTunedChannels.remove( channel );
source = null;
}
}
return source; source = new TunerChannelSource(tuner, channel);
} }
catch(SourceException se)
{
mTunedChannels.remove(channel);
source = null;
}
}
public int getChannelCount() return source;
{ }
return mTunedChannels.size();
}
/** public int getChannelCount()
* Indicates if the tuner's LO frequency must be updated in order to {
* accomodate the tuner channel return mTunedChannels.size();
*/ }
private boolean requiresLOUpdate( TunerChannel channel ) throws SourceException
{
return !( getMinTunedFrequency() <= channel.getMinFrequency() &&
channel.getMaxFrequency() <= getMaxTunedFrequency() &&
( ( mMiddleUnusable == 0 ) ||
( !channel.overlaps( getTunedFrequency() - mMiddleUnusable,
getTunedFrequency() + mMiddleUnusable ) ) ) );
}
/** /**
* Releases the currently sourced tuner channel from this tuner and shuts * Indicates if the tuner's LO frequency must be updated in order to accommodate the tuner channel
* down the tuner if no other sources exist. */
*/ private boolean requiresLOUpdate(TunerChannel channel) throws SourceException
public void releaseChannel( TunerChannelSource tunerChannelSource ) {
{ boolean isBelow = channel.getMinFrequency() < getMinTunedFrequency();
if( tunerChannelSource != null ) boolean isAbove = channel.getMaxFrequency() > getMaxTunedFrequency();
{ boolean overlapsCenter = mMiddleUnusable > 0 &&
mTunedChannels.remove( tunerChannelSource.getTunerChannel() ); channel.overlaps(getFrequency() - mMiddleUnusable, getFrequency() + mMiddleUnusable);
}
}
/** return isAbove || isBelow || overlapsCenter;
* Sets the Local Oscillator frequency accomodate the current set of tuned }
* channels.
*
* If there is only a single tuned channel, it is placed immediately to the
* right of the usable bandwidth right of the central DC spike.
*
* Otherwise, it places the highest channel frequency at the upper end of
* the tuner bandwidth, and then iteratively moves the center frequency
* higher until all channels fit within the bandwidth and none of the
* channels overlap any defined central DC spike unusable region. If a
* center tune frequency cannot be calculated, throw an exception so that
* the most recently added channel can be removed.
*
* Note: the tuned frequency is not changed until a legitimate new frequency
* can be calculated. If an exception is thrown, the current frequency is
* retained, so that the recently added channel can be removed and all other
* channels can continue as previously arranged.
*
* @throws SourceException if the set of tuner channels, including a recently
* added channel, cannot be tuned within the current tuner bandwidth and any
* central DC spike unusable region.
*/
private void updateLOFrequency() throws SourceException
{
long frequency = getFrequency();
boolean frequencyValid = true; /**
* Releases the currently sourced tuner channel from this tuner and shuts down the tuner if no other sources exist.
*/
public void releaseChannel(TunerChannelSource tunerChannelSource)
{
if(tunerChannelSource != null)
{
mTunedChannels.remove(tunerChannelSource.getTunerChannel());
}
}
//If there is only 1 channel, position it to the right of center /**
if( mTunedChannels.size() == 1 ) * Sets the Local Oscillator frequency to accomodate the current set of tuned channels.
{ *
frequency = mTunedChannels.first().getMinFrequency() - mMiddleUnusable; * If there is only a single tuned channel, it is placed immediately to the right of the usable bandwidth right
} * of the central DC spike.
else *
{ * Otherwise, it places the highest channel frequency at the upper end of the tuner bandwidth, and then iteratively
long minLockedFrequency = mTunedChannels.first().getMinFrequency(); * moves the center frequency higher until all channels fit within the bandwidth and none of the channels overlap
long maxLockedFrequency = mTunedChannels.last().getMaxFrequency(); * any defined central DC spike unusable region. If a center tune frequency cannot be calculated, throw an
* exception so that the most recently added channel can be removed.
*
* Note: the tuned frequency is not changed until a legitimate new frequency can be calculated. If an exception
* is thrown, the current frequency is retained, so that the recently added channel can be removed and all other
* channels can continue as previously arranged.
*
* @throws SourceException if the set of tuner channels, including a recently added channel, cannot be tuned
* within the current tuner bandwidth and any central DC spike unusable region.
*/
private void updateLOFrequency() throws SourceException
{
long candidateFrequency;
//Start by placing the highest frequency channel at the high end of boolean isValidCandidateFrequency = true;
//the spectrum
frequency = maxLockedFrequency - ( getUsableBandwidth() / 2 );
//Iterate the channels and make sure that none of them overlap the //If there is only 1 channel set the center frequency so that the channel is positioned to the right of center
//center DC spike buffer, if one exists if(mTunedChannels.size() == 1)
if( mMiddleUnusable > 0 ) {
{ candidateFrequency = mTunedChannels.first().getMinFrequency() - mMiddleUnusable + 1;
boolean processingRequired = true; }
else
{
long minLockedFrequency = mTunedChannels.first().getMinFrequency();
long maxLockedFrequency = mTunedChannels.last().getMaxFrequency();
while( frequencyValid && processingRequired ) //Start by placing the highest frequency channel at the high end of the spectrum
{ candidateFrequency = maxLockedFrequency - (getUsableBandwidth() / 2);
processingRequired = false;
long minAvoid = frequency - mMiddleUnusable; //Iterate the channels and make sure that none of them overlap the center DC spike buffer, if one exists
long maxAvoid = frequency + mMiddleUnusable; if(mMiddleUnusable > 0)
{
boolean processingRequired = true;
for( TunerChannel channel: mTunedChannels ) while(isValidCandidateFrequency && processingRequired)
{ {
if( channel.overlaps( minAvoid, maxAvoid ) ) processingRequired = false;
{
//Can we move the frequency lower so that the
//channel sits to the right of center?
long adjustment = channel.getMaxFrequency() - minAvoid;
if( frequency + adjustment - long minAvoid = candidateFrequency - mMiddleUnusable;
( getUsableBandwidth() / 2 ) <= minLockedFrequency ) long maxAvoid = candidateFrequency + mMiddleUnusable;
{
frequency += adjustment;
processingRequired = true;
}
else
{
frequencyValid = false;
}
//break out of the for/each loop, so that we can //If any of the center channel(s) overlap the central DC spike avoid area, we'll iteratively
//start over again with all of the channels //increase the tuned frequency causing the set of channels to move left in the tuned bandwidth until
break; //we either find a good center tune frequency, or we walk the lowest frequency channel out of the
} //minimum tuned range, in which case we'll throw an exception indicating we don't have a solution.
} for(TunerChannel channel : mTunedChannels)
} {
} if(channel.overlaps(minAvoid, maxAvoid))
} {
//Calculate a tuned frequency adjustment that places this overlapping channel just to the
//left of the central DC spike avoid zone
long adjustment = channel.getMaxFrequency() - minAvoid + 1;
if( frequencyValid ) //If the candidate frequency doesn't push the lowest channel out of bounds, make adjustment
{ if(candidateFrequency + adjustment - (getUsableBandwidth() / 2) <= minLockedFrequency)
mFrequencyController.setFrequency( frequency ); {
} candidateFrequency += adjustment;
else processingRequired = true;
{ }
throw new SourceException( "Couldn't calculate viable center " //Otherwise, punt and indicate that we can't find a center frequency or add the channel
+ "frequency from set of tuner channels" ); else
} {
} isValidCandidateFrequency = false;
}
/** //break out of the for/each loop, so that we can start over again with all of the channels
* Usable bandwidth - total bandwidth minus the unusable space at either end break;
* of the spectrum. }
*/ }
private int getUsableBandwidth() }
{ }
return (int)( getBandwidth() * mUsableBandwidthPercentage ); }
}
if(isValidCandidateFrequency)
{
mFrequencyController.setFrequency(candidateFrequency);
}
else
{
throw new SourceException("Couldn't calculate viable center frequency from set of tuner channels");
}
}
/**
* Usable bandwidth - total bandwidth minus the unusable space at either end of the spectrum.
*/
private int getUsableBandwidth()
{
return (int)(getBandwidth() * mUsableBandwidthPercentage);
}
/** /**
* Sets the listener to be notified any time that the tuner changes frequency * Sets the listener to be notified any time that the tuner changes frequency
@@ -383,7 +360,7 @@ public abstract class TunerController implements Tunable, ISourceEventProcessor
*/ */
public void addListener( ISourceEventProcessor processor ) public void addListener( ISourceEventProcessor processor )
{ {
mFrequencyController.addListener( processor ); mFrequencyController.addListener(processor);
} }
/** /**
@@ -391,6 +368,6 @@ public abstract class TunerController implements Tunable, ISourceEventProcessor
*/ */
public void removeListener( ISourceEventProcessor processor ) public void removeListener( ISourceEventProcessor processor )
{ {
mFrequencyController.removeFrequencyChangeProcessor( processor ); mFrequencyController.removeFrequencyChangeProcessor(processor);
} }
} }