/
CenterFrequencyCalculator.java
171 lines (150 loc) · 7.47 KB
/
CenterFrequencyCalculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* *****************************************************************************
* 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.source.tuner.manager;
import io.github.dsheirer.source.tuner.TunerController;
import io.github.dsheirer.source.tuner.channel.TunerChannel;
import java.util.SortedSet;
import java.util.TreeSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Calculates tuner center frequency for a set of tuner channels.
*/
public class CenterFrequencyCalculator
{
private static final Logger mLog = LoggerFactory.getLogger(CenterFrequencyCalculator.class);
public static final long INVALID_FREQUENCY = -1;
/**
* Determines the optimal center frequency for a given tuner and set of tuner channels.
*
* @param tunerController that is providing tuner channels
* @param channels to calculate
* @return optimal center frequency or INVALID_FREQUENCY if a center frequency cannot be calculated.
*/
public static long getCenterFrequency(TunerController tunerController, SortedSet<TunerChannel> channels)
{
long candidateFrequency = INVALID_FREQUENCY;
boolean isValidCandidateFrequency = true;
//If there is only 1 channel set the center frequency so that the channel is positioned to the right of center
if(channels.size() == 1)
{
candidateFrequency = channels.first().getMinFrequency() - tunerController.getMiddleUnusableHalfBandwidth() + 1;
}
else
{
long minChannelFrequency = channels.first().getMinFrequency();
long maxChannelFrequency = channels.last().getMaxFrequency();
//Start by placing the highest frequency channel at the high end of the spectrum
candidateFrequency = maxChannelFrequency - (tunerController.getUsableBandwidth() / 2);
if(maxChannelFrequency - minChannelFrequency <= tunerController.getUsableHalfBandwidth())
{
candidateFrequency = minChannelFrequency - tunerController.getMiddleUnusableHalfBandwidth();
isValidCandidateFrequency = true;
}
else
{
//Iterate the channels and make sure that none of them overlap the center DC spike buffer, if one exists
if(tunerController.getMiddleUnusableHalfBandwidth() > 0)
{
boolean processingRequired = true;
while(isValidCandidateFrequency && processingRequired)
{
processingRequired = false;
long minAvoid = candidateFrequency - tunerController.getMiddleUnusableHalfBandwidth();
long maxAvoid = candidateFrequency + tunerController.getMiddleUnusableHalfBandwidth();
//If any of the center channel(s) overlap the central DC spike avoid area, we'll iteratively
//increase the tuned frequency causing the set of channels to move left in the tuned bandwidth until
//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 : channels)
{
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 the candidate frequency doesn't push the lowest channel out of bounds, make adjustment
if(candidateFrequency + adjustment - (tunerController.getUsableBandwidth() / 2) <= minChannelFrequency)
{
candidateFrequency += adjustment;
processingRequired = true;
}
//Otherwise, punt and indicate that we can't find a center frequency or add the channel
else
{
isValidCandidateFrequency = false;
}
//break out of the for/each loop, so that we can start over again with all of the channels
break;
}
}
}
}
}
}
if(isValidCandidateFrequency)
{
return candidateFrequency;
}
return INVALID_FREQUENCY;
}
/**
* Indicates if channel along with all of the other currently sourced
* channels can fit within the tunable bandwidth.
* @param channel to test
* @param tunerController that will provide the channel
* @param channels is a current set of channels being sourced by the tuner controller
* @return true if it can tune all of the channels.
*/
public static boolean canTune(TunerChannel channel, TunerController tunerController, SortedSet<TunerChannel> channels)
{
//Make sure we're within the tunable frequency range of this tuner
if(tunerController.getMinimumFrequency() < channel.getMinFrequency() && tunerController.getMaximumFrequency() > channel.getMaxFrequency())
{
//If this is the first lock, then we're good
if(channels.isEmpty())
{
return true;
}
else
{
SortedSet<TunerChannel> allChannels = new TreeSet<>();
for(TunerChannel tunerChannel: channels)
{
if(tunerChannel != null)
{
allChannels.add(tunerChannel);
}
else
{
mLog.error("Null tuner channel encountered - continuing");
}
}
allChannels.add(channel);
//If the bandwidth of the channel set is less than or equal to the tuner's usable bandwidth, then
//check to see if we can find a valid center frequency
if(allChannels.last().getMaxFrequency() - allChannels.first().getMinFrequency() <= tunerController.getUsableBandwidth())
{
return getCenterFrequency(tunerController, allChannels) != INVALID_FREQUENCY;
}
}
}
return false;
}
}