Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ga.tomj.imdcalculator;
- import java.util.ArrayList;
- import java.util.LinkedHashSet;
- public class IMDCalculator {
- //Initiate variables.
- static double rangeMin;
- static double rangeMax;
- static double resolution;
- static double spacing;
- static int freqsNeeded;
- static LinkedHashSet<Double> blockedFreqs = new LinkedHashSet<>();
- static LinkedHashSet<Double> imdProducts = new LinkedHashSet<>();
- static ArrayList<Double> chosenFreqs = new ArrayList<>();
- public static void main(String[] args) {
- // Set variables.
- rangeMin = 606.000;
- rangeMax = 630.000;
- resolution = 0.010;
- spacing = 0.150;
- freqsNeeded = 10;
- //Set blocked frequencies.
- blockedFreqs.add(608.500);
- blockedFreqs.add(611.000);
- blockedFreqs.add(622.150);
- double currentTestFreq = rangeMin;
- while (true) {
- if (chosenFreqs.size() == freqsNeeded) {
- outputChosenFreqs();
- break;
- }
- if (currentTestFreq > rangeMax || currentTestFreq < rangeMin) {
- System.out.println("#####################");
- System.out.println("Only " + chosenFreqs.size() + " frequencies found.");
- System.out.println("#####################");
- outputChosenFreqs();
- break;
- }
- //Check if the frequency to be tested is a blocked frequncy.
- boolean useableFreq = !blockedFreqs.contains(currentTestFreq);
- //Find if the frequency is allowed with the spacing.
- for (double d : chosenFreqs) {
- if (currentTestFreq >= d - spacing && currentTestFreq <= d + spacing) {
- useableFreq = false;
- break;
- }
- }
- if(chosenFreqs.size() == 0) {
- chosenFreqs.add(currentTestFreq);
- }
- //If the frequency is usable, calculate the IMD products with this frequency added.
- if (useableFreq) {
- System.out.println(String.format("%.3f", currentTestFreq) + " is a usable frequency");
- if (calculateImdFreqs(currentTestFreq)) {
- System.out.println("adding " + String.format("%.3f", currentTestFreq) + "to thing");
- chosenFreqs.add(currentTestFreq);
- }
- }
- currentTestFreq += resolution;
- }
- }
- static ArrayList<Double> chosenFreqsWithCurrent = new ArrayList<>();
- public static boolean calculateImdFreqs(double current) {
- //Clear imdProducts.
- imdProducts.clear();
- chosenFreqsWithCurrent.clear();
- chosenFreqsWithCurrent = chosenFreqs;
- chosenFreqsWithCurrent.add(current);
- //Loop through all frequencies and calculate the IMD products for both.
- for (double d1 : chosenFreqsWithCurrent) {
- for (double d2 : chosenFreqsWithCurrent) {
- if (d1 != d2) {
- //Only the IMD frequencies that will pose a problem within the range that is being used are being
- //calculated.
- //Third order IMD.
- imdProducts.add((2 * d1) - d2);
- imdProducts.add((2 * d2) - d1);
- //Fifth order IMD.
- imdProducts.add((3 * d1) - (2 * d2));
- imdProducts.add((3 * d2) - (2 * d1));
- }
- }
- }
- for (double d : chosenFreqsWithCurrent) {
- System.out.println(chosenFreqsWithCurrent.size());
- if (imdProducts.contains(d)) {
- return false;
- }
- }
- return true;
- }
- private static void outputChosenFreqs() {
- int i = 1;
- for (double d : chosenFreqs) {
- System.out.println("Frequency " + i + ": " + String.format("%.3f", d) + "MHz");
- i++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement