Advertisement
Mark2020H

How to use python for woking out wavelength acc frequency [ Amatuer radio clubs ]

Oct 7th, 2024 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.02 KB | Source Code | 0 0
  1. #!/usr/bin/env python3
  2. # MD Harrington  London Kent  DA6 8NP
  3. # Witten for Course 172 Amatuer  Radio  
  4.  
  5. # Please see video at https://www.facebook.com/mark.harrington.14289/videos/2904843346320174/
  6.  
  7.  
  8. #  Also located on Instagram  @  https://www.instagram.com/p/DA1SgydpG2h
  9.  
  10.  
  11. ## Purpose  to create an easy and quick tool displaying graphs that will
  12. ## Present you with  answers for wavelength and frequency
  13.  
  14. ## You will need to if using linux make adjustments to  your configuration
  15. ## file in /etc/ImageMagick-6/policy.xml
  16.  
  17. """  Note *************************************************************
  18.      Change line  of config file using command
  19.         sudo nano /etc/ImageMagick-6/policy.xml
  20.         from this line towards the end of the file
  21.      <policy domain="coder" rights="none  pattern="PS" />
  22.        
  23.        to
  24.        
  25.      <policy domain="coder" rights="read|write" pattern="PS" />
  26.      
  27.      
  28.      Otherwise the program will hang !!
  29.      ******************************************************************
  30. """
  31.  
  32.  
  33.  
  34.  
  35.  
  36. import matplotlib.pyplot as plt
  37. import numpy as np
  38. import time
  39. import sys
  40. from colorama import Fore, Style, init
  41. import mplcursors  # Importing mplcursors for interactive tooltips
  42. import os
  43. import platform
  44.  
  45. # Initialize colorama
  46. init()
  47.  
  48. # Constants
  49. C = 3 * 10**8  # Speed of light in meters per second
  50.  
  51. def clear_screen():
  52.     # Detect the OS and clear the screen accordingly
  53.     if platform.system() == "Windows":
  54.         os.system("cls")
  55.     else:
  56.         os.system("clear")
  57.  
  58.  
  59. def typewriter_text(text, color):
  60.     for char in text:
  61.         sys.stdout.write(color + char + Style.RESET_ALL)
  62.         sys.stdout.flush()
  63.         time.sleep(0.05)  # Pause to create typewriter effect
  64.     print()
  65.  
  66. def display_menu():
  67.     typewriter_text("1: Frequencies covering the HF bands (3 MHz to 30 MHz)", Fore.GREEN)
  68.     typewriter_text("2: Frequencies covering the VHF bands (30 MHz to 300 MHz)", Fore.YELLOW)
  69.     typewriter_text("3: Frequencies covering the UHF bands (300 MHz to 3000 MHz)", Fore.BLUE)
  70.     typewriter_text("4: Exit the program", Fore.WHITE)
  71.  
  72. def calculate_wavelength(frequencies):
  73.     return C / frequencies
  74.  
  75. def plot_graph(frequencies, wavelengths, title):
  76.     frequencies_mhz = frequencies / 1e6  # Convert frequencies from Hz to MHz
  77.    
  78.     # Set the figure size to 800x600 pixels (8x6 inches at 100 DPI)
  79.     fig, ax = plt.subplots(figsize=(8, 6))
  80.    
  81.     scatter = ax.scatter(frequencies_mhz, wavelengths, s=1)  # Smallest visible points
  82.     plt.title(title)
  83.     plt.xlabel('Frequency (MHz)')
  84.     plt.ylabel('Wavelength (m)')
  85.    
  86.     # Add header
  87.     plt.figtext(0.5, 0.01, 'Created by MD Harrington London UK DA6 8NP', ha='center')
  88.     plt.grid(True)
  89.    
  90.     # Adding hover functionality using mplcursors, formatted for MHz and meters
  91.     cursor = mplcursors.cursor(scatter, hover=True)
  92.     cursor.connect("add", lambda sel: sel.annotation.set_text(
  93.         f'f={frequencies_mhz[sel.index]:.2f} MHz\nλ={wavelengths[sel.index]:.2f} m'))
  94.    
  95.     plt.show()
  96.  
  97. def main():
  98.     while True:
  99.         clear_screen()  # Clear the screen before displaying the menu
  100.        
  101.         print("\n" + "-"*40)
  102.         display_menu()
  103.         print("\n" + "-"*40)
  104.        
  105.         choice = input("Enter your choice: ")
  106.        
  107.         if choice == '1':
  108.             frequencies = np.linspace(3e6, 30e6, 100)  # HF: 3 MHz to 30 MHz
  109.         elif choice == '2':
  110.             frequencies = np.linspace(30e6, 300e6, 100)  # VHF: 30 MHz to 300 MHz
  111.         elif choice == '3':
  112.             frequencies = np.linspace(300e6, 3000e6, 100)  # UHF: 300 MHz to 3000 MHz
  113.         elif choice == '4':
  114.             print("Exiting program...")
  115.             break
  116.         else:
  117.             print("Invalid choice, please try again.")
  118.             continue
  119.        
  120.         # Calculate wavelengths
  121.         wavelengths = calculate_wavelength(frequencies)
  122.        
  123.         # Plot the graph with hover functionality
  124.         plot_graph(frequencies, wavelengths, "Wavelength vs Frequency")
  125.  
  126. if __name__ == "__main__":
  127.     main()
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement