Advertisement
Guest User

Convert digital signal .iq into Morse code

a guest
Nov 28th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.17 KB | Source Code | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. def load_iq_file(filename):
  5.     """
  6.    Load IQ data from a file (complex values).
  7.    :param filename: Path to the IQ file
  8.    :return: Array of complex IQ data
  9.    """
  10.     try:
  11.         iq_data = np.fromfile(filename, dtype=np.complex64)  # 32-bit complex data
  12.         print(f"Loaded {len(iq_data)} IQ samples.")
  13.         return iq_data
  14.     except Exception as e:
  15.         print(f"Error loading file: {e}")
  16.         return None
  17.  
  18. def get_amplitude(iq_data):
  19.     """
  20.    Calculate the amplitude of the IQ data.
  21.    :param iq_data: Array of complex IQ samples
  22.    :return: Array of amplitude values
  23.    """
  24.     return np.abs(iq_data)
  25.  
  26. def detect_morse_pulses(amplitude, threshold=0.5):
  27.     """
  28.    Detect Morse pulses (ON/OFF states) based on a threshold.
  29.    :param amplitude: Array of amplitude values
  30.    :param threshold: Amplitude threshold for signal detection
  31.    :return: List of pulse durations (in samples)
  32.    """
  33.     is_signal_on = amplitude > threshold
  34.     pulse_durations = []
  35.     current_pulse = 0
  36.  
  37.     for state in is_signal_on:
  38.         if state:  # Signal is ON
  39.             current_pulse += 1
  40.         elif current_pulse > 0:  # Signal turns OFF
  41.             pulse_durations.append(current_pulse)
  42.             current_pulse = 0
  43.  
  44.     print(f"Detected {len(pulse_durations)} pulses.")
  45.     return pulse_durations
  46.  
  47. def classify_pulses(pulse_durations, dot_length):
  48.     """
  49.    Classify pulse durations as dots, dashes, or spaces.
  50.    :param pulse_durations: List of pulse durations (in samples)
  51.    :param dot_length: Duration of a dot in samples
  52.    :return: Morse code string
  53.    """
  54.     morse_code = []
  55.     for duration in pulse_durations:
  56.         if duration <= 1.5 * dot_length:  # Dot
  57.             morse_code.append("·")
  58.         elif duration <= 3.5 * dot_length:  # Dash
  59.             morse_code.append("−")
  60.         else:  # Long pause (space between words)
  61.             morse_code.append(" ")
  62.  
  63.     return "".join(morse_code)
  64.  
  65. def morse_to_text(morse_code):
  66.     """
  67.    Convert Morse code to plain text.
  68.    :param morse_code: String of Morse code
  69.    :return: Decoded text
  70.    """
  71.     MORSE_DICT = {
  72.         '·−': 'A', '−···': 'B', '−·−·': 'C', '−··': 'D', '·': 'E',
  73.         '··−·': 'F', '−−·': 'G', '····': 'H', '··': 'I', '·−−−': 'J',
  74.         '−·−': 'K', '·−··': 'L', '−−': 'M', '−·': 'N', '−−−': 'O',
  75.         '·−−·': 'P', '−−·−': 'Q', '·−·': 'R', '···': 'S', '−': 'T',
  76.         '··−': 'U', '···−': 'V', '·−−': 'W', '−··−': 'X', '−·−−': 'Y',
  77.         '−−··': 'Z', '·−−−−': '1', '··−−−': '2', '···−−': '3',
  78.         '····−': '4', '·····': '5', '−····': '6', '−−···': '7',
  79.         '−−−··': '8', '−−−−·': '9', '−−−−−': '0'
  80.     }
  81.  
  82.     words = morse_code.split(" ")
  83.     decoded_message = []
  84.  
  85.     for word in words:
  86.         letters = word.split(" ")
  87.         decoded_word = ''.join(MORSE_DICT.get(letter, '?') for letter in letters)
  88.         decoded_message.append(decoded_word)
  89.  
  90.     return ' '.join(decoded_message)
  91.  
  92. # Example usage
  93. if __name__ == "__main__":
  94.     # Path to your IQ file
  95.     filename = "signal.iq"  # Replace with your IQ data file path
  96.     iq_data = load_iq_file(filename)
  97.  
  98.     if iq_data is not None:
  99.         amplitude = get_amplitude(iq_data)
  100.  
  101.         # Plot the amplitude for visualization
  102.         plt.figure(figsize=(10, 6))
  103.         plt.plot(amplitude[:1000], label="Amplitude")
  104.         plt.title("Amplitude of IQ Signal")
  105.         plt.xlabel("Sample")
  106.         plt.ylabel("Amplitude")
  107.         plt.legend()
  108.         plt.grid()
  109.         plt.show()
  110.  
  111.         # Detect Morse pulses
  112.         pulse_durations = detect_morse_pulses(amplitude, threshold=0.5)
  113.        
  114.         # Classify pulses into Morse code
  115.         morse_code = classify_pulses(pulse_durations, dot_length=100)  # Adjust `dot_length` as needed
  116.         print(f"Decoded Morse Code: {morse_code}")
  117.  
  118.         # Convert Morse code to plain text
  119.         text = morse_to_text(morse_code)
  120.         print(f"Decoded Text: {text}")
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement