Advertisement
NullChips

Untitled

Apr 30th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. package ga.tomj.imdcalculator;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.LinkedHashSet;
  5.  
  6. public class IMDCalculator {
  7.  
  8.  
  9. //Initiate variables.
  10. static double rangeMin;
  11. static double rangeMax;
  12. static double resolution;
  13. static double spacing;
  14. static int freqsNeeded;
  15. static LinkedHashSet<Double> blockedFreqs = new LinkedHashSet<>();
  16. static LinkedHashSet<Double> imdProducts = new LinkedHashSet<>();
  17. static ArrayList<Double> chosenFreqs = new ArrayList<>();
  18.  
  19. public static void main(String[] args) {
  20. // Set variables.
  21. rangeMin = 606.000;
  22. rangeMax = 630.000;
  23. resolution = 0.010;
  24. spacing = 0.150;
  25. freqsNeeded = 10;
  26.  
  27. //Set blocked frequencies.
  28. blockedFreqs.add(608.500);
  29. blockedFreqs.add(611.000);
  30. blockedFreqs.add(622.150);
  31.  
  32. double currentTestFreq = rangeMin;
  33.  
  34. while (true) {
  35.  
  36. if (chosenFreqs.size() == freqsNeeded) {
  37. outputChosenFreqs();
  38. break;
  39. }
  40.  
  41. if (currentTestFreq > rangeMax || currentTestFreq < rangeMin) {
  42. System.out.println("#####################");
  43. System.out.println("Only " + chosenFreqs.size() + " frequencies found.");
  44. System.out.println("#####################");
  45. outputChosenFreqs();
  46. break;
  47. }
  48.  
  49. //Check if the frequency to be tested is a blocked frequncy.
  50. boolean useableFreq = !blockedFreqs.contains(currentTestFreq);
  51.  
  52. //Find if the frequency is allowed with the spacing.
  53. for (double d : chosenFreqs) {
  54. if (currentTestFreq >= d - spacing && currentTestFreq <= d + spacing) {
  55. useableFreq = false;
  56. break;
  57. }
  58. }
  59.  
  60. if(chosenFreqs.size() == 0) {
  61. chosenFreqs.add(currentTestFreq);
  62. }
  63.  
  64. //If the frequency is usable, calculate the IMD products with this frequency added.
  65. if (useableFreq) {
  66. System.out.println(String.format("%.3f", currentTestFreq) + " is a usable frequency");
  67. if (calculateImdFreqs(currentTestFreq)) {
  68. System.out.println("adding " + String.format("%.3f", currentTestFreq) + "to thing");
  69. chosenFreqs.add(currentTestFreq);
  70. }
  71. }
  72.  
  73. currentTestFreq += resolution;
  74. }
  75. }
  76.  
  77. static ArrayList<Double> chosenFreqsWithCurrent = new ArrayList<>();
  78.  
  79. public static boolean calculateImdFreqs(double current) {
  80. //Clear imdProducts.
  81. imdProducts.clear();
  82.  
  83. chosenFreqsWithCurrent.clear();
  84. chosenFreqsWithCurrent = chosenFreqs;
  85. chosenFreqsWithCurrent.add(current);
  86.  
  87. //Loop through all frequencies and calculate the IMD products for both.
  88. for (double d1 : chosenFreqsWithCurrent) {
  89. for (double d2 : chosenFreqsWithCurrent) {
  90. if (d1 != d2) {
  91. //Only the IMD frequencies that will pose a problem within the range that is being used are being
  92. //calculated.
  93.  
  94. //Third order IMD.
  95. imdProducts.add((2 * d1) - d2);
  96. imdProducts.add((2 * d2) - d1);
  97.  
  98. //Fifth order IMD.
  99. imdProducts.add((3 * d1) - (2 * d2));
  100. imdProducts.add((3 * d2) - (2 * d1));
  101. }
  102. }
  103. }
  104.  
  105. for (double d : chosenFreqsWithCurrent) {
  106. System.out.println(chosenFreqsWithCurrent.size());
  107. if (imdProducts.contains(d)) {
  108. return false;
  109. }
  110. }
  111. return true;
  112. }
  113.  
  114. private static void outputChosenFreqs() {
  115. int i = 1;
  116. for (double d : chosenFreqs) {
  117. System.out.println("Frequency " + i + ": " + String.format("%.3f", d) + "MHz");
  118. i++;
  119. }
  120. }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement